eclipse-platform / .github

Common contribution content for eclipse-platform repositories
https://www.eclipse.org/eclipse/
5 stars 10 forks source link

Generics and Multiple Bounds - When you have Abstract Class and an Interface. #85

Closed mvetom closed 1 year ago

mvetom commented 1 year ago

Here's an an example that will reproduce the issue.

Create a Simple Java project in Eclipse.

Create two packages: package1 and package 2

In package2 create the following abstract class and interface:

package package2;

public abstract class MyAbstract {

    public MyAbstract() 
    {

    }

    abstract void someAbstractMethod();

}
package package2;

public interface MyInterface 
{
     void myMethod();
}

In package1, create this Java Class:

package package1;

import package2.*;
public class MyGenericClass<T extends MyAbstract> {

    public MyGenericClass() {

    }

}

Save the project. Note that it compiles with no issues.

Now change the line with the class declaration of MyGenericClass like this: public class MyGenericClass<T extends MyAbstract & MyInterface> {

You will get an error in the Eclipse IDE. Note that the error does not talk about MyInterface, which you just added. Instead it talks about MyAbstract, which it had no issue with a minute ago, when you had not added MyInterface as the lower bound. This is the error:

This class must implement the inherited abstract method MyAbstract.someAbstractMethod(), but cannot override it since it is not visible from MyGenericClass. Either make the type abstract or make the inherited method visible

For comparison, you should be able to run compile this code on the command line with no issues.

Just wanted to let the Eclipse team know about this.

Thanks!

mickaelistria commented 1 year ago

Hi, and thanks for the report. This eclipse-platform GitHub org is for general issues of Eclipse Platform (workspace, UI, text editor frameworks, SCM frameworks...); the Java Development Tools are in a separate org. So please report this issue to https://github.com/eclipse-jdt/eclipse.jdt.core/issues to reach the right audience.

iloveeclipse commented 1 year ago

Hmm, I believe Eclipse is right, javac is not here. Package protected methods can't be accessed from other packages. Just try with reduced example:

package package1;
import package2.MyAbstract;
public class MyGenericClass extends MyAbstract {
    public MyGenericClass() {
    }
}

package package2;
public abstract class MyAbstract {
    abstract void someAbstractMethod();
}
iloveeclipse commented 1 year ago

Hmm, I believe Eclipse is right, javac is not here. Package protected methods can't be accessed from other packages.

Sorry, overlooked the fact that there was multiple bounds and it was working with a single one. Indeed, sounds like a bug. => https://github.com/eclipse-jdt/eclipse.jdt.core/issues/656

mvetom commented 1 year ago

mickael and iloveeclipse,

Thanks for looking into this, and for pointing out and moving the bug report to the correct spot!