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

Problem with @Self return value and extending classes #642

Open
EotT123 opened this issue Nov 20, 2024 · 0 comments
Open

Problem with @Self return value and extending classes #642

EotT123 opened this issue Nov 20, 2024 · 0 comments

Comments

@EotT123
Copy link
Contributor

EotT123 commented Nov 20, 2024

Describe the bug
There appears to be a bug with the @Self return type when used with extended classes. Consider the following example:

public interface MyIntf {
    @Self MyIntf doSomethingAndReturnYourself();
}

public class MyClass1 implements MyIntf{
    @Override
    public MyClass1 doSomethingAndReturnYourself() {
        // do something ...
        return this;
    }
}

public class MyClass2 extends MyClass1 {
    @Override
    public MyClass2 doSomethingAndReturnYourself() {
        super.doSomethingAndReturnYourself(); // :(
        return this;
    }
}

This approach works, but I don't want to duplicate the doSomethingAndReturnYourself method in class MyClass2. When I remove the method and add the @Self annotation to MyClass1, I encounter the following error: @Self is not allowed here, use it on method and field declarations.

Additionally, the error message is not very helpful as it lacks context. It took me a long time to figure out the root cause of the issue, and I had to add extra debug output in the Manifold project and compile it myself to track it down.

public interface MyIntf {
    @Self MyIntf doSomethingAndReturnYourself();
}

public class MyClass1 implements MyIntf {
    @Override
    public @Self MyClass1 doSomethingAndReturnYourself(){
        // do something ...
        return this;
    }
}

public class MyClass2 extends MyClass1 {
     // also inherits doSomethingAndReturnYourself method, but should now return MyClass2

     // --> ERROR
}

The same error occurs both when building in IntelliJ or using Maven.

When disabling the isSelfInMethodDeclOrFieldDecl check in the visitAnnotation( JCTree.JCAnnotation tree ) method in class ExtensionTransformer.java, it seems to compile fine.

Desktop (please complete the following information):

  • OS Type & Version: Windows 10 22H2
  • Java/JDK version: openjdk 23.0.1
  • IDE version (IntelliJ IDEA or Android Studio): IntelliJ IDEA 2024.3
  • Manifold version: 2024.1.42 (latest version)
  • Manifold IntelliJ plugin version: 2024.1.13 (latest version)
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

1 participant