The javadoc confusion on `Class.getDeclaredMethod`

Class.getDeclaredMethod has the following javadoc.

If this class object represents a type that has several declared methods with the same parameter names and types, but different inverse types , then the returned array has a Method object for each such Method.

I know that we cannot overload a method with the same name and parameters. So what does this document mean?

+4
source share
1 answer

As EJP said in a comment :

This can happen with synthesized compiler models for Generics.

Actually, this is not only for generics.

, , .

:

public class Super {
    public static void main(String... args) throws Exception {
        for (Method method : Sub.class.getDeclaredMethods())
            System.out.println(method + (method.isSynthetic() ? " **SYNTHETIC**" : ""));
    }
    public Number get() {
        return Double.NaN;
    }
}
class Sub extends Super {
    @Override
    public Integer get() {
        return 42;
    }
}

public java.lang.Integer Sub.get()
public java.lang.Number Sub.get() **SYNTHETIC**
+3

Source: https://habr.com/ru/post/1695190/


All Articles