Diamond with anonymous classes: invoking non-private methods through reflection

Georgiy Rakov georgiy.rakov at oracle.com
Thu Jun 4 14:04:32 UTC 2015


Hello,

new feature about diamond with anonymous classes demands every 
non-private method declared in the anonymous class to override some 
method from its super type, otherwise compiler error occurs. I 
understand the intention of it which is specified in this comment 
<https://bugs.openjdk.java.net/browse/JDK-8073593?focusedCommentId=13622512&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-13622512>:

    When the diamond form is used, the inferred type arguments may not
    be as anticipated by the programmer. Consequently, the supertype of
    the anonymous class may not be as anticipated, and methods declared
    in the anonymous class may not override supertype methods as
    intended. Treating such methods as if annotated with @Override (if
    they are not actually annotated with @Override) helps avoid silently
    incorrect programs.

And this looks reasonable because when some method is declared in the 
anonymous class the only purpose of declaring it as non-private seems to 
be overriding since the method cannot be invoked from outside of the 
anonymous class, at least by the language means. But another purpose 
still exists, namely, invoking such method through reflection by some 
framework. For that purpose it actually becomes impossible to declare 
non-private method in the anonymous class instance created with diamond 
unless such method is declared in its super type.

Could you please tell if you have considered such use case and see it as 
a shortcoming or maybe not that serious one?

Thank you,
Georgiy.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20150604/cebccd6f/attachment.html>


More information about the compiler-dev mailing list