Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move of method to superclass with generics METHOD_REMOVED #360

Open
mederel opened this issue Aug 16, 2023 · 1 comment
Open

Move of method to superclass with generics METHOD_REMOVED #360

mederel opened this issue Aug 16, 2023 · 1 comment

Comments

@mederel
Copy link

mederel commented Aug 16, 2023

Found a narrow usecase of moving a method to a superclass adding generic return type for Builder pattern.

Before:

  • MyClass.java:
public class MyClass {
  int blabla;

  public MyClass withBlabla(int blibli) {
    this.blabla = blibli;
    return this;
  }
}

After:

  • MyClass.java:
public class MyClass extends ParentClass<MyClass> {
}
  • ParentClass.java:
public class ParentClass<T extends ParentClass<T>> {
  int blabla;

  public T withBlabla(int blibli) {
    this.blabla = blibli;
    return (T) this;
  }
}

When using the japicmp-maven-plugin against this change we get:

There is at least one incompatibility: blabla.MyClass.withBlabla(int):METHOD_REMOVED

Plugin config is:

          <parameter>
            <overrideCompatibilityChangeParameters>
              <overrideCompatibilityChangeParameter>
                <!-- Allow the adding of new method to interfaces -->
                <compatibilityChange>METHOD_ADDED_TO_INTERFACE</compatibilityChange>
                <binaryCompatible>true</binaryCompatible>
                <sourceCompatible>true</sourceCompatible>
              </overrideCompatibilityChangeParameter>
            </overrideCompatibilityChangeParameters>
            <breakBuildOnSourceIncompatibleModifications>true</breakBuildOnSourceIncompatibleModifications>
            <breakBuildOnBinaryIncompatibleModifications>true</breakBuildOnBinaryIncompatibleModifications>
            <!-- Allow new Maven API modules -->
            <ignoreMissingOldVersion>true</ignoreMissingOldVersion>
            <!-- Report only the failures in HTML report -->
            <onlyModified>true</onlyModified>
            <skipXmlReport>true</skipXmlReport>
            <skipDiffReport>true</skipDiffReport>
          </parameter>

All that is put together in this reproducer project:

reproducer-japicmp-error.zip

@siom79
Copy link
Owner

siom79 commented Aug 25, 2023

The point here is that a method with a generic return type (like public T withBlabla(int blibli)) is translated by the compiler to public Object withBlabla(int blibli). Hence; the return type has changed, despite the fact that you have restricted T to MyClass in the super class. The restriction of MyClass is only evaluated by the compiler, at runtime it is possible to invoke the method and assign the return value to any Object reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants