Mon, 07 Mar 2011 14:31:50 +0000
7020044: Project Coin: diamond erroneous allowed on some anonymous inner classes
Summary: Disallow diamond on anonymous innner class creation expression (as per JSR 334's EDR)
Reviewed-by: jjg
mcimadamore@740 | 1 | /* |
mcimadamore@914 | 2 | * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@740 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@740 | 4 | * |
mcimadamore@740 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@740 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@740 | 7 | * published by the Free Software Foundation. |
mcimadamore@740 | 8 | * |
mcimadamore@740 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@740 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@740 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@740 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@740 | 13 | * accompanied this code). |
mcimadamore@740 | 14 | * |
mcimadamore@740 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@740 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@740 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@740 | 18 | * |
mcimadamore@740 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@740 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@740 | 21 | * questions. |
mcimadamore@740 | 22 | */ |
mcimadamore@740 | 23 | |
mcimadamore@740 | 24 | /* |
mcimadamore@740 | 25 | * @test |
mcimadamore@914 | 26 | * @bug 6996914 7020044 |
mcimadamore@740 | 27 | * @summary Diamond inference: problem when accessing protected constructor |
mcimadamore@740 | 28 | * @run main T6996914a |
mcimadamore@740 | 29 | */ |
mcimadamore@740 | 30 | |
mcimadamore@740 | 31 | import com.sun.source.util.JavacTask; |
mcimadamore@740 | 32 | import java.net.URI; |
mcimadamore@740 | 33 | import java.util.Arrays; |
mcimadamore@740 | 34 | import javax.tools.Diagnostic; |
mcimadamore@740 | 35 | import javax.tools.DiagnosticListener; |
mcimadamore@740 | 36 | import javax.tools.JavaCompiler; |
mcimadamore@740 | 37 | import javax.tools.JavaFileObject; |
mcimadamore@740 | 38 | import javax.tools.SimpleJavaFileObject; |
mcimadamore@740 | 39 | import javax.tools.ToolProvider; |
mcimadamore@740 | 40 | |
mcimadamore@740 | 41 | public class T6996914a { |
mcimadamore@740 | 42 | |
mcimadamore@740 | 43 | enum PackageKind { |
mcimadamore@740 | 44 | DEFAULT("", ""), |
mcimadamore@740 | 45 | A("package a;", "import a.*;"); |
mcimadamore@740 | 46 | |
mcimadamore@740 | 47 | String pkgDecl; |
mcimadamore@740 | 48 | String importDecl; |
mcimadamore@740 | 49 | |
mcimadamore@740 | 50 | PackageKind(String pkgDecl, String importDecl) { |
mcimadamore@740 | 51 | this.pkgDecl = pkgDecl; |
mcimadamore@740 | 52 | this.importDecl = importDecl; |
mcimadamore@740 | 53 | } |
mcimadamore@740 | 54 | } |
mcimadamore@740 | 55 | |
mcimadamore@740 | 56 | enum ConstructorKind { |
mcimadamore@740 | 57 | PACKAGE(""), |
mcimadamore@740 | 58 | PROTECTED("protected"), |
mcimadamore@740 | 59 | PRIVATE("private"), |
mcimadamore@740 | 60 | PUBLIC("public"); |
mcimadamore@740 | 61 | |
mcimadamore@740 | 62 | String mod; |
mcimadamore@740 | 63 | |
mcimadamore@740 | 64 | ConstructorKind(String mod) { |
mcimadamore@740 | 65 | this.mod = mod; |
mcimadamore@740 | 66 | } |
mcimadamore@740 | 67 | } |
mcimadamore@740 | 68 | |
mcimadamore@740 | 69 | static class FooClass extends SimpleJavaFileObject { |
mcimadamore@740 | 70 | |
mcimadamore@740 | 71 | final static String sourceStub = |
mcimadamore@740 | 72 | "#P\n" + |
mcimadamore@740 | 73 | "public class Foo<X> {\n" + |
mcimadamore@740 | 74 | " #M Foo() {}\n" + |
mcimadamore@740 | 75 | "}\n"; |
mcimadamore@740 | 76 | |
mcimadamore@740 | 77 | String source; |
mcimadamore@740 | 78 | |
mcimadamore@740 | 79 | public FooClass(PackageKind pk, ConstructorKind ck) { |
mcimadamore@740 | 80 | super(URI.create("myfo:/" + (pk != PackageKind.DEFAULT ? "a/Foo.java" : "Foo.java")), |
mcimadamore@740 | 81 | JavaFileObject.Kind.SOURCE); |
mcimadamore@740 | 82 | source = sourceStub.replace("#P", pk.pkgDecl).replace("#M", ck.mod); |
mcimadamore@740 | 83 | } |
mcimadamore@740 | 84 | |
mcimadamore@740 | 85 | @Override |
mcimadamore@740 | 86 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
mcimadamore@740 | 87 | return source; |
mcimadamore@740 | 88 | } |
mcimadamore@740 | 89 | } |
mcimadamore@740 | 90 | |
mcimadamore@740 | 91 | static class ClientClass extends SimpleJavaFileObject { |
mcimadamore@740 | 92 | |
mcimadamore@740 | 93 | final static String sourceStub = |
mcimadamore@740 | 94 | "#I\n" + |
mcimadamore@740 | 95 | "class Test {\n" + |
mcimadamore@914 | 96 | " Foo<String> fs = new Foo<>();\n" + |
mcimadamore@740 | 97 | "}\n"; |
mcimadamore@740 | 98 | |
mcimadamore@740 | 99 | String source; |
mcimadamore@740 | 100 | |
mcimadamore@914 | 101 | public ClientClass(PackageKind pk) { |
mcimadamore@740 | 102 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
mcimadamore@914 | 103 | source = sourceStub.replace("#I", pk.importDecl); |
mcimadamore@740 | 104 | } |
mcimadamore@740 | 105 | |
mcimadamore@740 | 106 | @Override |
mcimadamore@740 | 107 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
mcimadamore@740 | 108 | return source; |
mcimadamore@740 | 109 | } |
mcimadamore@740 | 110 | } |
mcimadamore@740 | 111 | |
mcimadamore@740 | 112 | public static void main(String... args) throws Exception { |
mcimadamore@740 | 113 | for (PackageKind pk : PackageKind.values()) { |
mcimadamore@740 | 114 | for (ConstructorKind ck : ConstructorKind.values()) { |
mcimadamore@914 | 115 | compileAndCheck(pk, ck); |
mcimadamore@740 | 116 | } |
mcimadamore@740 | 117 | } |
mcimadamore@740 | 118 | } |
mcimadamore@740 | 119 | |
mcimadamore@914 | 120 | static void compileAndCheck(PackageKind pk, ConstructorKind ck) throws Exception { |
mcimadamore@740 | 121 | FooClass foo = new FooClass(pk, ck); |
mcimadamore@914 | 122 | ClientClass client = new ClientClass(pk); |
mcimadamore@740 | 123 | final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
mcimadamore@740 | 124 | ErrorListener el = new ErrorListener(); |
mcimadamore@740 | 125 | JavacTask ct = (JavacTask)tool.getTask(null, null, el, |
mcimadamore@740 | 126 | null, null, Arrays.asList(foo, client)); |
mcimadamore@740 | 127 | ct.analyze(); |
mcimadamore@914 | 128 | if (el.errors > 0 == check(pk, ck)) { |
mcimadamore@740 | 129 | String msg = el.errors > 0 ? |
mcimadamore@740 | 130 | "Error compiling files" : |
mcimadamore@740 | 131 | "No error when compiling files"; |
mcimadamore@740 | 132 | throw new AssertionError(msg + ": \n" + foo.source + "\n" + client.source); |
mcimadamore@740 | 133 | } |
mcimadamore@740 | 134 | } |
mcimadamore@740 | 135 | |
mcimadamore@914 | 136 | static boolean check(PackageKind pk, ConstructorKind ck) { |
mcimadamore@740 | 137 | switch (pk) { |
mcimadamore@914 | 138 | case A: return ck == ConstructorKind.PUBLIC; |
mcimadamore@740 | 139 | case DEFAULT: return ck != ConstructorKind.PRIVATE; |
mcimadamore@740 | 140 | default: throw new AssertionError("Unknown package kind"); |
mcimadamore@740 | 141 | } |
mcimadamore@740 | 142 | } |
mcimadamore@740 | 143 | |
mcimadamore@740 | 144 | /** |
mcimadamore@740 | 145 | * DiagnosticListener to count any errors that occur |
mcimadamore@740 | 146 | */ |
mcimadamore@740 | 147 | private static class ErrorListener implements DiagnosticListener<JavaFileObject> { |
mcimadamore@740 | 148 | |
mcimadamore@740 | 149 | public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
mcimadamore@740 | 150 | switch (diagnostic.getKind()) { |
mcimadamore@740 | 151 | case ERROR: |
mcimadamore@740 | 152 | errors++; |
mcimadamore@740 | 153 | } |
mcimadamore@740 | 154 | } |
mcimadamore@740 | 155 | int errors; |
mcimadamore@740 | 156 | } |
mcimadamore@740 | 157 | } |