Skip to content

Commit 2d6b2c8

Browse files
authored
Merge pull request #523 from siom79/claude/japicmp-issue-522-1DtKr
Fix false positive METHOD_RETURN_TYPE_CHANGED for covariant return type overrides (#522)
2 parents 6b7dbb6 + a476601 commit 2d6b2c8

5 files changed

Lines changed: 96 additions & 4 deletions

File tree

‎japicmp/src/main/java/japicmp/compat/CompatibilityChanges.java‎

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import japicmp.util.SignatureParser;
1010
import javassist.ClassPool;
1111
import javassist.CtClass;
12+
import javassist.CtMethod;
1213
import javassist.NotFoundException;
1314

1415
import java.util.*;
@@ -448,10 +449,14 @@ private void checkIfMethodsHaveChangedIncompatible(JApiClass jApiClass, Map<Stri
448449
private void checkIfReturnTypeChanged(JApiMethod method) {
449450
// section 13.4.15 of "Java Language Specification" SE7 (Method Result Type)
450451
if (method.getReturnType().getChangeStatus() == JApiChangeStatus.MODIFIED) {
451-
JApiCompatibilityChange change = addCompatibilityChange(method, JApiCompatibilityChangeType.METHOD_RETURN_TYPE_CHANGED);
452-
if (method.getAccessModifier().hasChangedToMoreVisible()) {
453-
change.setSourceCompatible(true);
454-
change.setBinaryCompatible(true);
452+
if (isCovariantReturnTypeChange(method)) {
453+
addCompatibilityChange(method, JApiCompatibilityChangeType.METHOD_RETURN_TYPE_COVARIANT_CHANGED);
454+
} else {
455+
JApiCompatibilityChange change = addCompatibilityChange(method, JApiCompatibilityChangeType.METHOD_RETURN_TYPE_CHANGED);
456+
if (method.getAccessModifier().hasChangedToMoreVisible()) {
457+
change.setSourceCompatible(true);
458+
change.setBinaryCompatible(true);
459+
}
455460
}
456461
}
457462
if (method.getChangeStatus() == JApiChangeStatus.MODIFIED ||
@@ -462,6 +467,47 @@ private void checkIfReturnTypeChanged(JApiMethod method) {
462467
}
463468
}
464469

470+
private boolean isCovariantReturnTypeChange(JApiMethod method) {
471+
if (!method.getOldMethod().isPresent() || !method.getNewMethod().isPresent()) {
472+
return false;
473+
}
474+
// Check for a compiler-generated bridge method with the original return type in the new class
475+
String methodName = method.getName();
476+
String oldReturnType = method.getReturnType().getOldReturnType();
477+
JApiClass jApiClass = method.getjApiClass();
478+
boolean bridgeMethodFound = false;
479+
for (JApiMethod candidate : jApiClass.getMethods()) {
480+
if (candidate == method) {
481+
continue;
482+
}
483+
if (!candidate.getName().equals(methodName)) {
484+
continue;
485+
}
486+
Optional<BridgeModifier> newBridgeModifier = candidate.getBridgeModifier().getNewModifier();
487+
if (!newBridgeModifier.isPresent() || newBridgeModifier.get() != BridgeModifier.BRIDGE) {
488+
continue;
489+
}
490+
if (!candidate.getReturnType().getNewReturnType().equals(oldReturnType)) {
491+
continue;
492+
}
493+
if (candidate.hasSameParameter(method)) {
494+
bridgeMethodFound = true;
495+
break;
496+
}
497+
}
498+
if (!bridgeMethodFound) {
499+
return false;
500+
}
501+
// Verify the new return type is a subtype of the old return type
502+
try {
503+
CtClass oldReturnCtClass = method.getOldMethod().get().getReturnType();
504+
CtClass newReturnCtClass = method.getNewMethod().get().getReturnType();
505+
return newReturnCtClass.subtypeOf(oldReturnCtClass);
506+
} catch (NotFoundException e) {
507+
return true; // trust the bridge method as the primary indicator
508+
}
509+
}
510+
465511
private void checkIfParametersGenericsChanged(JApiBehavior jApiBehavior) {
466512
if (jApiBehavior.getChangeStatus() == JApiChangeStatus.MODIFIED ||
467513
jApiBehavior.getChangeStatus() == JApiChangeStatus.UNCHANGED) {

‎japicmp/src/main/java/japicmp/model/JApiCompatibilityChangeType.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public enum JApiCompatibilityChangeType {
3131
METHOD_LESS_ACCESSIBLE_THAN_IN_SUPERCLASS(false, false, JApiSemanticVersionLevel.MAJOR),
3232
METHOD_IS_STATIC_AND_OVERRIDES_NOT_STATIC(false, false, JApiSemanticVersionLevel.MAJOR),
3333
METHOD_RETURN_TYPE_CHANGED(false, false, JApiSemanticVersionLevel.MAJOR),
34+
METHOD_RETURN_TYPE_COVARIANT_CHANGED(true, true, JApiSemanticVersionLevel.MINOR),
3435
METHOD_RETURN_TYPE_GENERICS_CHANGED(true, false, JApiSemanticVersionLevel.MINOR),
3536
METHOD_PARAMETER_GENERICS_CHANGED(true, false, JApiSemanticVersionLevel.MINOR),
3637
METHOD_NOW_ABSTRACT(false, false, JApiSemanticVersionLevel.MAJOR),

‎japicmp/src/test/java/japicmp/compat/MethodCompatibilityTest.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,45 @@ public List<CtClass> createNewClasses(ClassPool classPool) throws Exception {
221221
assertThat(jApiMethod.isSourceCompatible(), is(true));
222222
}
223223

224+
@Test
225+
void testMethodReturnTypeCovariantChange() throws Exception {
226+
JarArchiveComparatorOptions options = new JarArchiveComparatorOptions();
227+
options.setIncludeSynthetic(true);
228+
List<JApiClass> jApiClasses = ClassesHelper.compareClasses(options, new ClassesHelper.ClassesGenerator() {
229+
@Override
230+
public List<CtClass> createOldClasses(ClassPool classPool) throws Exception {
231+
CtClass ctClass = CtClassBuilder.create().name("japicmp.Test").addToClassPool(classPool);
232+
CtMethodBuilder.create().publicAccess().returnType(classPool.get("java.lang.Object")).name("getValue").body("return null;").addToClass(ctClass);
233+
return Collections.singletonList(ctClass);
234+
}
235+
236+
@Override
237+
public List<CtClass> createNewClasses(ClassPool classPool) throws Exception {
238+
CtClass ctClass = CtClassBuilder.create().name("japicmp.Test").addToClassPool(classPool);
239+
// Covariant override — Integer is a subtype of Object
240+
CtMethodBuilder.create().publicAccess().returnType(classPool.get("java.lang.Integer")).name("getValue").body("return Integer.valueOf(0);").addToClass(ctClass);
241+
// Compiler-generated bridge method with original return type
242+
CtMethodBuilder.create().publicAccess().bridgeModifier().syntheticModifier().returnType(classPool.get("java.lang.Object")).name("getValue").body("return null;").addToClass(ctClass);
243+
return Collections.singletonList(ctClass);
244+
}
245+
});
246+
JApiClass jApiClass = getJApiClass(jApiClasses, "japicmp.Test");
247+
assertThat(jApiClass.getChangeStatus(), is(JApiChangeStatus.MODIFIED));
248+
JApiMethod nonBridgeMethod = null;
249+
for (JApiMethod m : jApiClass.getMethods()) {
250+
if (m.getName().equals("getValue") &&
251+
m.getBridgeModifier().getNewModifier().map(bm -> bm == BridgeModifier.NON_BRIDGE).orElse(false)) {
252+
nonBridgeMethod = m;
253+
break;
254+
}
255+
}
256+
assertThat(nonBridgeMethod, is(notNullValue()));
257+
assertThat(nonBridgeMethod.getCompatibilityChanges(), hasItem(new JApiCompatibilityChange(JApiCompatibilityChangeType.METHOD_RETURN_TYPE_COVARIANT_CHANGED)));
258+
assertThat(nonBridgeMethod.getCompatibilityChanges(), not(hasItem(new JApiCompatibilityChange(JApiCompatibilityChangeType.METHOD_RETURN_TYPE_CHANGED))));
259+
assertThat(nonBridgeMethod.isBinaryCompatible(), is(true));
260+
assertThat(jApiClass.isBinaryCompatible(), is(true));
261+
}
262+
224263
@Test
225264
void testMethodNowAbstract() throws Exception {
226265
JarArchiveComparatorOptions options = new JarArchiveComparatorOptions();

‎japicmp/src/test/java/japicmp/util/CtMethodBuilder.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public CtMethodBuilder syntheticModifier() {
3939
return this;
4040
}
4141

42+
public CtMethodBuilder bridgeModifier() {
43+
this.modifier = this.modifier | ModifierHelper.ACC_BRIDGE;
44+
return this;
45+
}
46+
4247
public CtMethodBuilder parameters(CtClass[] parameters) {
4348
return (CtMethodBuilder) super.parameters(parameters);
4449
}

‎src/site/markdown/MavenPlugin.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ for each check. This allows you to customize the following verifications:
325325
| METHOD_REMOVED | false | false | MAJOR |
326326
| METHOD_REMOVED_IN_SUPERCLASS | false | false | MAJOR |
327327
| METHOD_RETURN_TYPE_CHANGED | false | false | MAJOR |
328+
| METHOD_RETURN_TYPE_COVARIANT_CHANGED | true | true | MINOR |
328329
| METHOD_RETURN_TYPE_GENERICS_CHANGED | true | false | MINOR |
329330
| METHOD_STATIC_IN_INTERFACE_NO_LONGER_STATIC | false | false | MAJOR |
330331
| SUPERCLASS_ADDED | true | true | MINOR |

0 commit comments

Comments
 (0)