java 17: anonymous classes with diamonds

Srikanth srikanth.adayapalam at oracle.com
Tue Jan 19 08:21:06 UTC 2021



On 18/01/21 7:26 pm, Anna Kozlova wrote:
> Thanks!
>
> Not sure if it's exactly the same issue, could you please check as 
> well? At least the error message is not very helpful: "cannot find symbol
>   symbol:   class <captured wildcard>
>   location: class foo.Boo<T>" (no line number)

Thanks for the report, I have raised JDK-8259960 to investigate this.

Srikanth

>
> ```
> import java.util.List;
> import java.util.function.Predicate;
>
> public class Boo<T> {
>      public Boo(List<?extends T> l) {
>      }
>      void m(List<?extends String> list){
>          Boo<String> boo3 =new Boo<>(findAll(list, s -> s.length() >0)){};
>      }
>
>      static <T> List<T> findAll(List<?extends T> collection,
>                                 Predicate<?super T> condition) {
>          return null;
>      }
> }
> ```
>
> If I remove anonymous class braces, the code starts to compile. 
> Checked with with java 11 & java 17
>
> Thanks,
> Anna
>
> P.S. I've migrated IntelliJ sources to java 11 diamonds, thus the 
> issues. This one is the last issue I could reproduce with java 17. A 
> lot of issues have been fixed since java 11!
>
> On Mon, Jan 18, 2021 at 11:18 AM Srikanth 
> <srikanth.adayapalam at oracle.com 
> <mailto:srikanth.adayapalam at oracle.com>> wrote:
>
>     Thanks for the problem report Anna.
>
>     It does look suspect and I have raised JDK-8259910 to follow up.
>
>     This problem dates back to jdk9 and is reproducible in all the
>     major releases from then on.
>
>     Srikanth
>
>     On 18/01/21 3:08 pm, Anna Kozlova wrote:
>>     Hi all,
>>
>>     The following code doesn't compile with java 17 eap (java:
>>     foo.Boo.Nested is abstract; cannot be instantiated)
>>     ```
>>     public class Boo {
>>          static abstract class Nested<T> {
>>              protected Nested(Class<T> aAcceptedClass) { }
>>
>>          }
>>          static class Outer<T>  {
>>              protected Outer(final Nested<T> condition) { }
>>          }
>>
>>          static final Outer<Boo> PATTERN =new Outer<>(new Nested<>(Boo.class) {}) {};
>>     }
>>     ```
>>     though the anonymous class is used.
>>
>>     At the same time, if I remove the anonymous class from outer
>>     call, like
>>     `static final Outer<Boo> PATTERN = new Outer<>(new
>>     Nested<>(Boo.class) {});` the code would compile.
>>
>>     If explicit types instead of diamonds are used, everything
>>     compiles fine as well.
>>
>>     To me this looks like a bug in the compiler, could someone check
>>     this please?
>>
>>     Thanks,
>>     Anna
>>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20210119/7aaa121c/attachment.htm>


More information about the compiler-dev mailing list