langtools/test/tools/javac/generics/diamond/6996914/T6996914a.java
changeset 29776 984a79b71cfe
parent 8635 383a416a2bdf
child 30730 d3ce7619db2c
equal deleted inserted replaced
29775:dc7df633fea1 29776:984a79b71cfe
     1 /*
     1 /*
     2  * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 6996914 7020044
    26  * @bug 6996914 7020044 8062373
    27  * @summary  Diamond inference: problem when accessing protected constructor
    27  * @summary  Diamond inference: problem when accessing protected constructor
    28  * @run main T6996914a
    28  * @run main T6996914a
    29  */
    29  */
    30 
    30 
    31 import com.sun.source.util.JavacTask;
    31 import com.sun.source.util.JavacTask;
    48         String importDecl;
    48         String importDecl;
    49 
    49 
    50         PackageKind(String pkgDecl, String importDecl) {
    50         PackageKind(String pkgDecl, String importDecl) {
    51             this.pkgDecl = pkgDecl;
    51             this.pkgDecl = pkgDecl;
    52             this.importDecl = importDecl;
    52             this.importDecl = importDecl;
       
    53         }
       
    54     }
       
    55 
       
    56     enum DiamondKind {
       
    57         STANDARD("new Foo<>();"),
       
    58         ANON("new Foo<>() {};");
       
    59 
       
    60         String expr;
       
    61 
       
    62         DiamondKind(String expr) {
       
    63             this.expr = expr;
    53         }
    64         }
    54     }
    65     }
    55 
    66 
    56     enum ConstructorKind {
    67     enum ConstructorKind {
    57         PACKAGE(""),
    68         PACKAGE(""),
    91     static class ClientClass extends SimpleJavaFileObject {
   102     static class ClientClass extends SimpleJavaFileObject {
    92 
   103 
    93         final static String sourceStub =
   104         final static String sourceStub =
    94                         "#I\n" +
   105                         "#I\n" +
    95                         "class Test {\n" +
   106                         "class Test {\n" +
    96                         "  Foo<String> fs = new Foo<>();\n" +
   107                         "  Foo<String> fs = #D\n" +
    97                         "}\n";
   108                         "}\n";
    98 
   109 
    99         String source;
   110         String source;
   100 
   111 
   101         public ClientClass(PackageKind pk) {
   112         public ClientClass(PackageKind pk, DiamondKind dk) {
   102             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   113             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
   103             source = sourceStub.replace("#I", pk.importDecl);
   114             source = sourceStub.replace("#I", pk.importDecl).replace("#D", dk.expr);
   104         }
   115         }
   105 
   116 
   106         @Override
   117         @Override
   107         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   118         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
   108             return source;
   119             return source;
   110     }
   121     }
   111 
   122 
   112     public static void main(String... args) throws Exception {
   123     public static void main(String... args) throws Exception {
   113         for (PackageKind pk : PackageKind.values()) {
   124         for (PackageKind pk : PackageKind.values()) {
   114             for (ConstructorKind ck : ConstructorKind.values()) {
   125             for (ConstructorKind ck : ConstructorKind.values()) {
   115                     compileAndCheck(pk, ck);
   126                 for (DiamondKind dk : DiamondKind.values()) {
       
   127                     compileAndCheck(pk, ck, dk);
       
   128                 }
   116             }
   129             }
   117         }
   130         }
   118     }
   131     }
   119 
   132 
   120     static void compileAndCheck(PackageKind pk, ConstructorKind ck) throws Exception {
   133     static void compileAndCheck(PackageKind pk, ConstructorKind ck, DiamondKind dk) throws Exception {
   121         FooClass foo = new FooClass(pk, ck);
   134         FooClass foo = new FooClass(pk, ck);
   122         ClientClass client = new ClientClass(pk);
   135         ClientClass client = new ClientClass(pk, dk);
   123         final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
   136         final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
   124         ErrorListener el = new ErrorListener();
   137         ErrorListener el = new ErrorListener();
   125         JavacTask ct = (JavacTask)tool.getTask(null, null, el,
   138         JavacTask ct = (JavacTask)tool.getTask(null, null, el,
   126                 null, null, Arrays.asList(foo, client));
   139                 null, null, Arrays.asList(foo, client));
   127         ct.analyze();
   140         ct.analyze();
   128         if (el.errors > 0 == check(pk, ck)) {
   141         if (el.errors > 0 == check(pk, ck, dk)) {
   129             String msg = el.errors > 0 ?
   142             String msg = el.errors > 0 ?
   130                 "Error compiling files" :
   143                 "Error compiling files" :
   131                 "No error when compiling files";
   144                 "No error when compiling files";
   132             throw new AssertionError(msg + ": \n" + foo.source + "\n" + client.source);
   145             throw new AssertionError(msg + ": \n" + foo.source + "\n" + client.source);
   133         }
   146         }
   134     }
   147     }
   135 
   148 
   136     static boolean check(PackageKind pk, ConstructorKind ck) {
   149     static boolean check(PackageKind pk, ConstructorKind ck, DiamondKind dk) {
   137         switch (pk) {
   150         switch (pk) {
   138             case A: return ck == ConstructorKind.PUBLIC;
   151             case A: return ck == ConstructorKind.PUBLIC ||
       
   152                     (ck  == ConstructorKind.PROTECTED && dk == DiamondKind.ANON);
   139             case DEFAULT: return ck != ConstructorKind.PRIVATE;
   153             case DEFAULT: return ck != ConstructorKind.PRIVATE;
   140             default: throw new AssertionError("Unknown package kind");
   154             default: throw new AssertionError("Unknown package kind");
   141         }
   155         }
   142     }
   156     }
   143 
   157