99import japicmp .util .SignatureParser ;
1010import javassist .ClassPool ;
1111import javassist .CtClass ;
12+ import javassist .CtMethod ;
1213import javassist .NotFoundException ;
1314
1415import 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 ) {
0 commit comments