Skip to content

Commit

Permalink
Merge pull request #397 from Billlynch/master
Browse files Browse the repository at this point in the history
fix: check changes in return types
  • Loading branch information
siom79 authored May 3, 2024
2 parents 68727ed + 7a23595 commit 317f263
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
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 @@ -38,6 +38,8 @@
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class IncompatibleErrorOutput extends OutputGenerator<Void> {

Expand Down Expand Up @@ -213,7 +215,13 @@ public void visit(Iterator<JApiClass> iterator, JApiClass jApiClass) {

@Override
public void visit(Iterator<JApiMethod> iterator, JApiMethod jApiMethod) {
for (JApiCompatibilityChange change : jApiMethod.getCompatibilityChanges()) {
final List<JApiCompatibilityChange> changes = Stream.concat(
jApiMethod.getCompatibilityChanges().stream(),
jApiMethod.getReturnType().getCompatibilityChanges().stream()
)
.collect(Collectors.toList());

for (JApiCompatibilityChange change : changes) {
if (!change.isBinaryCompatible() || !change.isSourceCompatible()) {
if (!change.isBinaryCompatible() && breakBuildIfCausedByExclusion(jApiMethod)) {
breakBuildResult.binaryIncompatibleChanges = true;
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 317f263

Please sign in to comment.