Skip to content

Commit

Permalink
Refactor some usages of Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
guillermocalvo committed Oct 4, 2024
1 parent bfd09f2 commit 4f5a8c4
Show file tree
Hide file tree
Showing 25 changed files with 221 additions and 530 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ private List<JApiCmpArchive> resolveDependencyToFile(String parameterName, Depen
private String guessVersion(File file) {
String name = file.getName();
Optional<SemanticVersion> semanticVersion = japicmp.versioning.Version.getSemanticVersion(name);
String version = semanticVersion.isPresent() ? semanticVersion.get().toString() : "n.a.";
String version = semanticVersion.map(Object::toString).orElse("n.a.");
if (name.contains("SNAPSHOT")) {
version += "-SNAPSHOT";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -103,10 +103,8 @@ private static void toJarArchiveComparatorClassPathMode(Options options, JarArch
}

private static void toJarArchiveComparatorClassPath(Optional<String> classPathOptional, List<String> comparatorClassPath) {
if (classPathOptional.isPresent()) {
String classPathAsString = classPathOptional.get();
Collections.addAll(comparatorClassPath, classPathAsString.split(File.pathSeparator));
}
classPathOptional.map(classPathAsString -> Arrays.asList(classPathAsString.split(File.pathSeparator)))
.ifPresent(comparatorClassPath::addAll);
}

public Filters getFilters() {
Expand Down
122 changes: 43 additions & 79 deletions japicmp/src/main/java/japicmp/compat/CompatibilityChanges.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,24 +209,21 @@ private <T> void forAllSuperclasses(JApiClass jApiClass, Map<String, JApiClass>
JApiSuperclass superclass = jApiClass.getSuperclass();
if (superclass.getNewSuperclassName().isPresent()) {
String newSuperclassName = superclass.getNewSuperclassName().get();
JApiClass foundClass = classMap.get(newSuperclassName);
if (foundClass == null) {
Optional<JApiClass> superclassJApiClassOptional = superclass.getJApiClass();
if (superclassJApiClassOptional.isPresent()) {
foundClass = superclassJApiClassOptional.get();
} else {
foundClass = loadClass(newSuperclassName, EnumSet.of(Classpath.NEW_CLASSPATH));
evaluate(Collections.singletonList(foundClass));
}
classMap.put(foundClass.getFullyQualifiedName(), foundClass);
}
superclass.getJApiClass().ifPresent(x -> classMap.putIfAbsent(newSuperclassName, x));
JApiClass foundClass = classMap.computeIfAbsent(newSuperclassName, this::loadAndEvaluate);
superclass.setJApiClass(foundClass);
T returnValue = onSuperclassCallback.callback(foundClass, classMap, superclass.getChangeStatus());
returnValues.add(returnValue);
forAllSuperclasses(foundClass, classMap, returnValues, onSuperclassCallback);
}
}

private JApiClass loadAndEvaluate(String className) {
JApiClass loaded = loadClass(className, EnumSet.of(Classpath.NEW_CLASSPATH));
evaluate(Collections.singletonList(loaded));
return loaded;
}

private enum Classpath {
OLD_CLASSPATH,
NEW_CLASSPATH
Expand Down Expand Up @@ -284,15 +281,12 @@ private JApiClass loadClass(String newSuperclassName, EnumSet<Classpath> classpa
}

private boolean fieldTypeMatches(JApiField field1, JApiField field2) {
boolean matches = true;
JApiType type1 = field1.getType();
JApiType type2 = field2.getType();
if (type1.getNewTypeOptional().isPresent() && type2.getNewTypeOptional().isPresent()) {
if (!type1.getNewTypeOptional().get().equals(type2.getNewTypeOptional().get())) {
matches = false;
}
Optional<String> newType1 = field1.getType().getNewTypeOptional();
Optional<String> newType2 = field2.getType().getNewTypeOptional();
if (newType1.isPresent() && newType2.isPresent()) {
return newType1.equals(newType2);
}
return matches;
return true;
}

private void checkIfConstructorsHaveChangedIncompatible(JApiClass jApiClass, Map<String, JApiClass> classMap) {
Expand All @@ -318,7 +312,7 @@ private void checkIfConstructorsHaveChangedIncompatible(JApiClass jApiClass, Map
}

private static boolean isNotFinal(JApiClass jApiClass) {
return !(jApiClass.getFinalModifier().getNewModifier().isPresent() && jApiClass.getFinalModifier().getNewModifier().get() == FinalModifier.FINAL);
return !jApiClass.getFinalModifier().getNewModifier().map(FinalModifier.FINAL::equals).orElse(false);
}

private void checkIfMethodsHaveChangedIncompatible(JApiClass jApiClass, Map<String, JApiClass> classMap) {
Expand Down Expand Up @@ -366,11 +360,9 @@ private void checkIfMethodsHaveChangedIncompatible(JApiClass jApiClass, Map<Stri
return 1;
}
}
if (superMethod.getStaticModifier().getNewModifier().isPresent() && method.getStaticModifier().getNewModifier().isPresent()) {
if (superMethod.getStaticModifier().getNewModifier().get() == StaticModifier.NON_STATIC
&& method.getStaticModifier().getNewModifier().get() == StaticModifier.STATIC) {
return 2;
}
if (superMethod.getStaticModifier().getNewModifier().map(StaticModifier.NON_STATIC::equals).orElse(false) &&
method.getStaticModifier().getNewModifier().map(StaticModifier.STATIC::equals).orElse(false)) {
return 2;
}
}
}
Expand All @@ -391,8 +383,8 @@ private void checkIfMethodsHaveChangedIncompatible(JApiClass jApiClass, Map<Stri
}
// section 13.4.17 of "Java Language Specification" SE7
if (isNotPrivate(method) && method.getFinalModifier().hasChangedFromTo(FinalModifier.NON_FINAL, FinalModifier.FINAL)) {
if ((jApiClass.getFinalModifier().getOldModifier().isPresent() && jApiClass.getFinalModifier().getOldModifier().get() != FinalModifier.FINAL) &&
!(method.getStaticModifier().getOldModifier().isPresent() && method.getStaticModifier().getOldModifier().get() == StaticModifier.STATIC)) {
if (jApiClass.getFinalModifier().getOldModifier().map(x -> x != FinalModifier.FINAL).orElse(false) &&
!method.getStaticModifier().getOldModifier().map(StaticModifier.STATIC::equals).orElse(false)) {
addCompatibilityChange(method, JApiCompatibilityChangeType.METHOD_NOW_FINAL);
}
}
Expand All @@ -401,10 +393,8 @@ private void checkIfMethodsHaveChangedIncompatible(JApiClass jApiClass, Map<Stri
forAllSuperclasses(jApiClass, classMap, returnValues, (superclass, classMap1, changeStatusOfSuperclass) -> {
for (JApiMethod superMethod : superclass.getMethods()) {
if (areMatching(superMethod, method)) {
if (method.getFinalModifier().getOldModifier().isPresent()
&& method.getFinalModifier().getOldModifier().get() == FinalModifier.NON_FINAL
&& superMethod.getFinalModifier().getNewModifier().isPresent()
&& superMethod.getFinalModifier().getNewModifier().get() == FinalModifier.FINAL) {
if (method.getFinalModifier().getOldModifier().map(FinalModifier.NON_FINAL::equals).orElse(false)
&& superMethod.getFinalModifier().getNewModifier().map(FinalModifier.FINAL::equals).orElse(false)) {
addCompatibilityChange(superMethod, JApiCompatibilityChangeType.METHOD_NOW_FINAL);
return 1;
}
Expand Down Expand Up @@ -542,8 +532,8 @@ private void checkAbstractMethod(JApiClass jApiClass, Map<String, JApiClass> cla
}
} else if (method.getChangeStatus() == JApiChangeStatus.MODIFIED || method.getChangeStatus() == JApiChangeStatus.UNCHANGED) {
JApiModifier<AbstractModifier> abstractModifier = method.getAbstractModifier();
if (abstractModifier.getOldModifier().isPresent() && abstractModifier.getOldModifier().get() == AbstractModifier.ABSTRACT &&
abstractModifier.getNewModifier().isPresent() && abstractModifier.getNewModifier().get() == AbstractModifier.NON_ABSTRACT) {
if (abstractModifier.getOldModifier().map(AbstractModifier.ABSTRACT::equals).orElse(false) &&
abstractModifier.getNewModifier().map(AbstractModifier.NON_ABSTRACT::equals).orElse(false)) {
// method changed from abstract to default
addCompatibilityChange(method, JApiCompatibilityChangeType.METHOD_ABSTRACT_NOW_DEFAULT);
}
Expand Down Expand Up @@ -618,11 +608,7 @@ private List<JApiMethod> getMethodsInImplementedInterfacesWithSameSignature(fina
}

private boolean isAbstract(JApiHasAbstractModifier jApiHasAbstractModifier) {
if (jApiHasAbstractModifier.getAbstractModifier().getNewModifier().isPresent()) {
AbstractModifier abstractModifier = jApiHasAbstractModifier.getAbstractModifier().getNewModifier().get();
return abstractModifier == AbstractModifier.ABSTRACT;
}
return false;
return jApiHasAbstractModifier.getAbstractModifier().getNewModifier().map(AbstractModifier.ABSTRACT::equals).orElse(false);
}

private void checkIfExceptionIsNowChecked(JApiBehavior behavior) {
Expand All @@ -637,19 +623,19 @@ private void checkIfExceptionIsNowChecked(JApiBehavior behavior) {
}

private boolean isClass(final JApiClass jApiClass) {
return jApiClass.getClassType().getNewTypeOptional().isPresent() && jApiClass.getClassType().getNewTypeOptional().get() == JApiClassType.ClassType.CLASS;
return jApiClass.getClassType().getNewTypeOptional().map(JApiClassType.ClassType.CLASS::equals).orElse(false);
}

private boolean isInterface(final JApiClass jApiClass) {
return jApiClass.getClassType().getNewTypeOptional().isPresent() && jApiClass.getClassType().getNewTypeOptional().get() == JApiClassType.ClassType.INTERFACE;
return jApiClass.getClassType().getNewTypeOptional().map(JApiClassType.ClassType.INTERFACE::equals).orElse(false);
}

private boolean isAnnotation(final JApiClass jApiClass) {
return jApiClass.getClassType().getNewTypeOptional().isPresent() && jApiClass.getClassType().getNewTypeOptional().get() == JApiClassType.ClassType.ANNOTATION;
return jApiClass.getClassType().getNewTypeOptional().map(JApiClassType.ClassType.ANNOTATION::equals).orElse(false);
}

private boolean isEnum(final JApiClass jApiClass) {
return jApiClass.getClassType().getNewTypeOptional().isPresent() && jApiClass.getClassType().getNewTypeOptional().get() == JApiClassType.ClassType.ENUM;
return jApiClass.getClassType().getNewTypeOptional().map(JApiClassType.ClassType.ENUM::equals).orElse(false);
}

private void checkIfMethodHasBeenPulledUp(JApiClass jApiClass, Map<String, JApiClass> classMap, final JApiMethod method, List<Integer> returnValues) {
Expand Down Expand Up @@ -693,11 +679,7 @@ private boolean isImplemented(JApiMethod jApiMethod, JApiClass aClass) {
}
}

if (aClass.getSuperclass() != null && aClass.getSuperclass().getJApiClass().isPresent()) {
aClass = aClass.getSuperclass().getJApiClass().get();
} else {
aClass = null;
}
aClass = Optional.ofNullable(aClass.getSuperclass()).flatMap(JApiSuperclass::getJApiClass).orElse(null);
}
return false;
}
Expand Down Expand Up @@ -777,15 +759,11 @@ private void checkIfSuperclassesOrInterfacesChangedIncompatible(final JApiClass
addCompatibilityChange(jApiClass, JApiCompatibilityChangeType.FIELD_REMOVED_IN_SUPERCLASS);
}
if (superclass.getOldSuperclassName().isPresent() && superclass.getNewSuperclassName().isPresent()) {
if (!superclass.getOldSuperclassName().get().equals(superclass.getNewSuperclassName().get())) {
boolean superClassChangedToObject = false;
boolean superClassChangedFromObject = false;
if (!superclass.getOldSuperclassName().get().equals("java.lang.Object") && superclass.getNewSuperclassName().get().equals("java.lang.Object")) {
superClassChangedToObject = true;
}
if (superclass.getOldSuperclassName().get().equals("java.lang.Object") && !superclass.getNewSuperclassName().get().equals("java.lang.Object")) {
superClassChangedFromObject = true;
}
if (!superclass.getOldSuperclassName().equals(superclass.getNewSuperclassName())) {
String oldSuperclassName = superclass.getOldSuperclassName().get();
String newSuperclassName = superclass.getNewSuperclassName().get();
boolean superClassChangedToObject = !oldSuperclassName.equals("java.lang.Object") && newSuperclassName.equals("java.lang.Object");
boolean superClassChangedFromObject = oldSuperclassName.equals("java.lang.Object") && !newSuperclassName.equals("java.lang.Object");
if (superClassChangedToObject) {
addCompatibilityChange(superclass, JApiCompatibilityChangeType.SUPERCLASS_REMOVED);
} else if (superClassChangedFromObject) {
Expand All @@ -796,7 +774,7 @@ private void checkIfSuperclassesOrInterfacesChangedIncompatible(final JApiClass
final List<JApiSuperclass> matchingAncestors = new ArrayList<>();
forAllSuperclasses(jApiClass, classMap, ancestors, (clazz, classMap12, changeStatusOfSuperclass) -> {
JApiSuperclass ancestor = clazz.getSuperclass();
if (ancestor.getNewSuperclassName().isPresent() && ancestor.getNewSuperclassName().get().equals(superclass.getOldSuperclassName().get())) {
if (ancestor.getNewSuperclassName().filter(oldSuperclassName::equals).isPresent()) {
matchingAncestors.add(ancestor);
}
return ancestor;
Expand Down Expand Up @@ -912,14 +890,9 @@ private void checkIfAbstractMethodAddedInSuperclass(final JApiClass jApiClass, M
}
if (!isImplemented) {
if (jApiMethod.getChangeStatus() == JApiChangeStatus.NEW || changeStatusOfSuperclass == JApiChangeStatus.NEW || changeStatusOfSuperclass == JApiChangeStatus.MODIFIED) {
if (jApiMethod.getAbstractModifier().getNewModifier().isPresent()) {
AbstractModifier abstractModifier = jApiMethod.getAbstractModifier().getNewModifier().get();
if (abstractModifier == AbstractModifier.ABSTRACT) {
abstractMethods.add(jApiMethod);
} else {
defaultMethods.add(jApiMethod);
}
}
jApiMethod.getAbstractModifier().getNewModifier()
.map(x -> x == AbstractModifier.ABSTRACT ? abstractMethods : defaultMethods)
.ifPresent(methods -> methods.add(jApiMethod));
}
}
}
Expand All @@ -944,14 +917,9 @@ private void checkIfAbstractMethodAddedInSuperclass(final JApiClass jApiClass, M
}
if (!isImplemented) {
if (interfaceMethod.getChangeStatus() == JApiChangeStatus.NEW || jApiImplementedInterface.getChangeStatus() == JApiChangeStatus.NEW) {
if (interfaceMethod.getAbstractModifier().getNewModifier().isPresent()) {
AbstractModifier abstractModifier = interfaceMethod.getAbstractModifier().getNewModifier().get();
if (abstractModifier == AbstractModifier.ABSTRACT) {
abstractMethods.add(interfaceMethod);
} else {
defaultMethods.add(interfaceMethod);
}
}
interfaceMethod.getAbstractModifier().getNewModifier()
.map(x -> x == AbstractModifier.ABSTRACT ? abstractMethods : defaultMethods)
.ifPresent(methods -> methods.add(interfaceMethod));
}
}
}
Expand Down Expand Up @@ -1004,12 +972,8 @@ private JApiClass getJApiClass(JApiImplementedInterface implementedInterface, Ma
private void checkIfClassNowCheckedException(JApiClass jApiClass) {
JApiSuperclass jApiClassSuperclass = jApiClass.getSuperclass();
if (jApiClassSuperclass.getChangeStatus() == JApiChangeStatus.MODIFIED) {
if (jApiClassSuperclass.getNewSuperclassName().isPresent()) {
String fqn = jApiClassSuperclass.getNewSuperclassName().get();
if ("java.lang.Exception".equals(fqn)) {
addCompatibilityChange(jApiClass, JApiCompatibilityChangeType.CLASS_NOW_CHECKED_EXCEPTION);
}
}
jApiClassSuperclass.getNewSuperclassName().filter("java.lang.Exception"::equals)
.ifPresent(fqn -> addCompatibilityChange(jApiClass, JApiCompatibilityChangeType.CLASS_NOW_CHECKED_EXCEPTION));
}
}

Expand Down
5 changes: 2 additions & 3 deletions japicmp/src/main/java/japicmp/model/AccessModifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ public static String listOfAccessModifier() {
}

public static Optional<AccessModifier> toModifier(String accessModifierArg) {
Optional<String> stringOptional = Optional.ofNullable(accessModifierArg);
if (stringOptional.isPresent()) {
if (accessModifierArg != null) {
try {
return Optional.of(valueOf(stringOptional.get().toUpperCase()));
return Optional.of(valueOf(accessModifierArg.toUpperCase()));
} catch (IllegalArgumentException e) {
throw new JApiCmpException(JApiCmpException.Reason.CliError, String.format("Invalid value for option accessModifier: %s. Possible values are: %s.",
accessModifierArg, listOfAccessModifier()), e);
Expand Down
6 changes: 1 addition & 5 deletions japicmp/src/main/java/japicmp/model/JApiAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,7 @@ private Map<String, Optional<MemberValue>> buildMemberValueMap(Annotation annota
if (memberNames != null) {
for (String memberName : memberNames) {
MemberValue memberValue = annotation.getMemberValue(memberName);
if (memberValue == null) {
map.put(memberName, Optional.<MemberValue>empty());
} else {
map.put(memberName, Optional.of(memberValue));
}
map.put(memberName, Optional.ofNullable(memberValue));
}
}
return map;
Expand Down
30 changes: 10 additions & 20 deletions japicmp/src/main/java/japicmp/model/JApiAnnotationElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ static JApiAnnotationElementValue getMemberValue(MemberValue memberValue) {
}
}

private static List<JApiAnnotationElementValue> getElementValues(MemberValue memberValue) {
JApiAnnotationElementValue elementValue = getMemberValue(memberValue);
if(elementValue.getType() == JApiAnnotationElementValue.Type.Array) {
return new ArrayList<>(elementValue.getValues());
}
return new ArrayList<>(Collections.singleton(elementValue));
}

@XmlAttribute(name = "name")
public String getName() {
return name;
Expand All @@ -127,31 +135,13 @@ public JApiChangeStatus getChangeStatus() {
@XmlElementWrapper(name = "oldElementValues")
@XmlElement(name = "oldElementValue")
public List<JApiAnnotationElementValue> getOldElementValues() {
List<JApiAnnotationElementValue> values = new ArrayList<>();
if (this.oldValue.isPresent()) {
JApiAnnotationElementValue memberValue = getMemberValue(this.oldValue.get());
if (memberValue.getType() == JApiAnnotationElementValue.Type.Array) {
values.addAll(memberValue.getValues());
} else {
values.add(memberValue);
}
}
return values;
return this.oldValue.map(JApiAnnotationElement::getElementValues).orElseGet(ArrayList::new);
}

@XmlElementWrapper(name = "newElementValues")
@XmlElement(name = "newElementValue")
public List<JApiAnnotationElementValue> getNewElementValues() {
List<JApiAnnotationElementValue> values = new ArrayList<>();
if (this.newValue.isPresent()) {
JApiAnnotationElementValue memberValue = getMemberValue(this.newValue.get());
if (memberValue.getType() == JApiAnnotationElementValue.Type.Array) {
values.addAll(memberValue.getValues());
} else {
values.add(memberValue);
}
}
return values;
return this.newValue.map(JApiAnnotationElement::getElementValues).orElseGet(ArrayList::new);
}

@XmlAttribute
Expand Down
Loading

0 comments on commit 4f5a8c4

Please sign in to comment.