On 6/5/20 6:42 AM, Christoph Dreis wrote:
Hi,

forgive me if this was discussed already, but I found that the implementation 
of hidden classes causes a change in behavior for Class.getCanonicalName().

A hidden class has no canonical name [1] and it can't be nominally referenced (like an anonymous class which has no canonical name).

Lambda proxy classes are now hidden classes in JDK 15 but that's implementation details.   For diagnosiability, Class::getName or Class::toString can be used.

Mandy
[1] https://download.java.net/java/early_access/jdk15/docs/api/java.base/java/lang/Class.html#getCanonicalName()


When using the following example code:

interface Function {
        void doSomething();
}

public class Example {

        public static void main(String[] args) {
                Class<? extends Function> lambdaClass = ((Function) () -> 
{}).getClass();
                String canonicalName = lambdaClass.getCanonicalName();
                System.out.println(canonicalName);
                String name = lambdaClass.getName();
                System.out.println(name);
        }

}

I get the following results:

JDK 14
com.example.demo.Example$$Lambda$14/0x0000000800b66840
com.example.demo.Example$$Lambda$14/0x0000000800b66840

JDK 15
null
com.example.demo.Example$$Lambda$14/0x0000000800b89448

As far as I can tell from the code Lambdas are considered hidden classes now. 
Is that correct?

I guess Class.getName() should be used instead?

Cheers,
Christoph



Reply via email to