Skip to content

Commit

Permalink
fix: check changes in return types
Browse files Browse the repository at this point in the history
  • Loading branch information
Billlynch committed Apr 26, 2024
1 parent 68727ed commit 8a10b71
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions japicmp-testbase/japicmp-test-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,12 @@
<sourceCompatible>true</sourceCompatible>
<semanticVersionLevel>PATCH</semanticVersionLevel>
</overrideCompatibilityChangeParameter>
<overrideCompatibilityChangeParameter>
<compatibilityChange>METHOD_RETURN_TYPE_GENERICS_CHANGED</compatibilityChange>
<binaryCompatible>true</binaryCompatible>
<sourceCompatible>true</sourceCompatible>
<semanticVersionLevel>PATCH</semanticVersionLevel>
</overrideCompatibilityChangeParameter>
</overrideCompatibilityChangeParameters>
<breakBuildOnBinaryIncompatibleModifications>true</breakBuildOnBinaryIncompatibleModifications>
<breakBuildOnSourceIncompatibleModifications>true</breakBuildOnSourceIncompatibleModifications>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,23 @@ public void visit(Iterator<JApiMethod> iterator, JApiMethod jApiMethod) {
.append(")").append(":").append(change.getType().name());
}
}

for (JApiCompatibilityChange change : jApiMethod.getReturnType().getCompatibilityChanges()) {
if (!change.isBinaryCompatible() || !change.isSourceCompatible()) {
if (!change.isBinaryCompatible() && options.isErrorOnBinaryIncompatibility()) {
breakBuildResult.binaryIncompatibleChanges = true;
}
if (!change.isSourceCompatible() && options.isErrorOnSourceIncompatibility()) {
breakBuildResult.sourceIncompatibleChanges = true;
}
if (sb.length() > 1) {
sb.append(',');
}
sb.append(jApiMethod.getjApiClass().getFullyQualifiedName()).append(".")
.append(jApiMethod.getName()).append("(").append(methodParameterToList(jApiMethod))
.append(")").append(":").append(change.getType().name());
}
}
}

private boolean breakBuildIfCausedByExclusion(JApiMethod jApiMethod) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import japicmp.util.Optional;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import org.junit.Test;

import java.util.Arrays;
Expand Down Expand Up @@ -242,4 +243,38 @@ public List<CtClass> createNewClasses(ClassPool classPool) throws Exception {
options.setNewArchives(Collections.singletonList(newFile));
new IncompatibleErrorOutput(options, result.getjApiClasses(), result.getJarArchiveComparator()).generate();
}

@Test(expected = JApiCmpException.class)
public void testBreakBuildIfGenericReturnTypeModifiedTrue() throws Exception {
testBreakBuildIfGenericReturnTypeModified(true);
}

@Test
public void testBreakBuildIfGenericReturnTypeModifiedFalse() throws Exception {
testBreakBuildIfGenericReturnTypeModified(false);
}

private void testBreakBuildIfGenericReturnTypeModified(boolean errorOnSourceIncompatibility) throws Exception {
Options options = Options.newDefault();
JarArchiveComparatorOptions jarArchiveComparatorOptions = JarArchiveComparatorOptions.of(options);
ClassesHelper.CompareClassesResult result = ClassesHelper.compareClassesWithResult(jarArchiveComparatorOptions, new ClassesHelper.ClassesGenerator() {
@Override
public List<CtClass> createOldClasses(ClassPool classPool) throws Exception {
CtClass ctClass = CtClassBuilder.create().name("japicmp.Test").addToClassPool(classPool);
CtMethod method = CtMethodBuilder.create().publicAccess().returnType(classPool.get("java.util.List")).name("method").body("return new java.util.ArrayList();").addToClass(ctClass);
method.setGenericSignature("()Ljava/util/List<Ljava/lang/Object;>;");
return Collections.singletonList(ctClass);
}

@Override
public List<CtClass> createNewClasses(ClassPool classPool) throws Exception {
CtClass ctClass = CtClassBuilder.create().name("japicmp.Test").addToClassPool(classPool);
CtMethod method = CtMethodBuilder.create().publicAccess().returnType(classPool.get("java.util.List")).name("method").body("return new java.util.ArrayList();").addToClass(ctClass);
method.setGenericSignature("()Ljava/util/List<Ljava/lang/Integer;>;");
return Collections.singletonList(ctClass);
}
});
options.setErrorOnSourceIncompatibility(errorOnSourceIncompatibility);
new IncompatibleErrorOutput(options, result.getjApiClasses(), result.getJarArchiveComparator()).generate();
}
}

0 comments on commit 8a10b71

Please sign in to comment.