test/tools/javac/generics/diamond/6996914/T6996914a.java

changeset 914
ca32f2986301
parent 740
bce19889597e
child 2525
2eb010b6cb22
equal deleted inserted replaced
913:74f0c05c51eb 914:ca32f2986301
1 /* 1 /*
2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2010, 2011, 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 26 * @bug 6996914 7020044
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;
64 } 53 }
65 } 54 }
66 55
67 enum ConstructorKind { 56 enum ConstructorKind {
68 PACKAGE(""), 57 PACKAGE(""),
102 static class ClientClass extends SimpleJavaFileObject { 91 static class ClientClass extends SimpleJavaFileObject {
103 92
104 final static String sourceStub = 93 final static String sourceStub =
105 "#I\n" + 94 "#I\n" +
106 "class Test {\n" + 95 "class Test {\n" +
107 " Foo<String> fs = #D\n" + 96 " Foo<String> fs = new Foo<>();\n" +
108 "}\n"; 97 "}\n";
109 98
110 String source; 99 String source;
111 100
112 public ClientClass(PackageKind pk, DiamondKind dk) { 101 public ClientClass(PackageKind pk) {
113 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 102 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
114 source = sourceStub.replace("#I", pk.importDecl).replace("#D", dk.expr); 103 source = sourceStub.replace("#I", pk.importDecl);
115 } 104 }
116 105
117 @Override 106 @Override
118 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 107 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
119 return source; 108 return source;
121 } 110 }
122 111
123 public static void main(String... args) throws Exception { 112 public static void main(String... args) throws Exception {
124 for (PackageKind pk : PackageKind.values()) { 113 for (PackageKind pk : PackageKind.values()) {
125 for (ConstructorKind ck : ConstructorKind.values()) { 114 for (ConstructorKind ck : ConstructorKind.values()) {
126 for (DiamondKind dk : DiamondKind.values()) { 115 compileAndCheck(pk, ck);
127 compileAndCheck(pk, ck, dk);
128 }
129 } 116 }
130 } 117 }
131 } 118 }
132 119
133 static void compileAndCheck(PackageKind pk, ConstructorKind ck, DiamondKind dk) throws Exception { 120 static void compileAndCheck(PackageKind pk, ConstructorKind ck) throws Exception {
134 FooClass foo = new FooClass(pk, ck); 121 FooClass foo = new FooClass(pk, ck);
135 ClientClass client = new ClientClass(pk, dk); 122 ClientClass client = new ClientClass(pk);
136 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 123 final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
137 ErrorListener el = new ErrorListener(); 124 ErrorListener el = new ErrorListener();
138 JavacTask ct = (JavacTask)tool.getTask(null, null, el, 125 JavacTask ct = (JavacTask)tool.getTask(null, null, el,
139 null, null, Arrays.asList(foo, client)); 126 null, null, Arrays.asList(foo, client));
140 ct.analyze(); 127 ct.analyze();
141 if (el.errors > 0 == check(pk, ck, dk)) { 128 if (el.errors > 0 == check(pk, ck)) {
142 String msg = el.errors > 0 ? 129 String msg = el.errors > 0 ?
143 "Error compiling files" : 130 "Error compiling files" :
144 "No error when compiling files"; 131 "No error when compiling files";
145 throw new AssertionError(msg + ": \n" + foo.source + "\n" + client.source); 132 throw new AssertionError(msg + ": \n" + foo.source + "\n" + client.source);
146 } 133 }
147 } 134 }
148 135
149 static boolean check(PackageKind pk, ConstructorKind ck, DiamondKind dk) { 136 static boolean check(PackageKind pk, ConstructorKind ck) {
150 switch (pk) { 137 switch (pk) {
151 case A: return ck == ConstructorKind.PUBLIC || 138 case A: return ck == ConstructorKind.PUBLIC;
152 (ck == ConstructorKind.PROTECTED && dk == DiamondKind.ANON);
153 case DEFAULT: return ck != ConstructorKind.PRIVATE; 139 case DEFAULT: return ck != ConstructorKind.PRIVATE;
154 default: throw new AssertionError("Unknown package kind"); 140 default: throw new AssertionError("Unknown package kind");
155 } 141 }
156 } 142 }
157 143

mercurial