eclipse-jdt / eclipse.jdt.ui

Eclipse Public License 2.0
36 stars 86 forks source link

Quickfix for return type that's a wildcard doesn't work post 1.4 #1558

Closed datho7561 closed 1 month ago

datho7561 commented 1 month ago

The quickfix to fix the return type in the following code doesn't work.

import java.lang.reflect.AccessibleObject;

public class WeirdAnonymousThingy {

    @Override
    public void m() {
        new AccessibleObject() {
            public void getAnnotation(Class annotationClass) {

            }
        };
    }
}

It's a particular nasty edge case: this anonymous class inherits a method that has a method-level type parameter with a bound set, and the return type is that type variable. (see the javadoc for AccessibleObject.getAnnotation)

Pre-1.5, the erasure of the type variable is calculated as the value that void gets replaced with. Post 1.5, the type variable is inserted in place of void, which results in invalid code, since the type variable is declared in the override implementation.

I think that ideally this quickfix should provide two options:

  1. Returning a type variable (and properly declaring it)
  2. Returning the erasure of the type variable
datho7561 commented 1 month ago

I started working on this; doing 1. is pretty easy, doing 2. is a bit more involved.