Anonymous classes are not final according to reflection API

Georgiy Rakov georgiy.rakov at oracle.com
Mon Jun 22 15:17:01 UTC 2015


Hello,

if I understand correctly according to following assertion from JLS 
15.9.5 anonymous classes are always final:

    An anonymous class is always implicitly final (§8.1.1.2).

But reflection API reports that the class is not final. Namely let's 
consider following code:

    import java.lang.reflect.Modifier;

    public class Test12 {
         static class Foo<T> {}

         public static void main(String argv[]) {
             Foo<Integer> foo = new Foo<>() {};
             if ( (foo.getClass().getModifiers() & Modifier.FINAL) != 0 ) {
                 System.out.println("final, modifiers: " +
    foo.getClass().getModifiers());
             } else {
                 System.out.println("not final, modifiers: " +
    foo.getClass().getModifiers());
             }
         }
    }

On JDK9b69 it reports:

    not final, modifiers: 0

Could you please tell if you consider this as a discrepancy between spec 
and javac (VM?) which should be fixed.

Thank you,
Georgiy.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20150622/92c1f7a9/attachment.html>
-------------- next part --------------
import java.lang.reflect.Modifier;

public class Test12 {
    static class Foo<T> {}

    public static void main(String argv[]) {
        Foo<Integer> foo = new Foo<>() {};
        if ( (foo.getClass().getModifiers() & Modifier.FINAL) != 0 ) {
            System.out.println("final, modifiers: " + foo.getClass().getModifiers());
        } else {
            System.out.println("not final, modifiers: " + foo.getClass().getModifiers());
        }
    }
}


More information about the compiler-dev mailing list