Tue, 18 Dec 2012 22:16:45 +0000
8005193: New regression test test/tools/javac/lambda/BadMethodCall2.java fails
Summary: Bad golden file in negative test
Reviewed-by: jjh
1 /*
2 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
24 /*
25 * @test
26 * @bug 7115052
27 * @bug 8003280
28 * @summary Add lambda tests
29 * Add parser support for method references
30 */
32 import com.sun.source.util.JavacTask;
33 import java.net.URI;
34 import java.util.Arrays;
35 import javax.tools.Diagnostic;
36 import javax.tools.JavaCompiler;
37 import javax.tools.JavaFileObject;
38 import javax.tools.SimpleJavaFileObject;
39 import javax.tools.StandardJavaFileManager;
40 import javax.tools.ToolProvider;
42 public class MethodReferenceParserTest {
44 static int checkCount = 0;
46 enum ReferenceKind {
47 METHOD_REF("#Q::#Gm"),
48 CONSTRUCTOR_REF("#Q::#Gnew"),
49 FALSE_REF("min < max"),
50 ERR_SUPER("#Q::#Gsuper"),
51 ERR_METH0("#Q::#Gm()"),
52 ERR_METH1("#Q::#Gm(X)"),
53 ERR_CONSTR0("#Q::#Gnew()"),
54 ERR_CONSTR1("#Q::#Gnew(X)");
56 String referenceTemplate;
58 ReferenceKind(String referenceTemplate) {
59 this.referenceTemplate = referenceTemplate;
60 }
62 String getReferenceString(QualifierKind qk, GenericKind gk) {
63 return referenceTemplate
64 .replaceAll("#Q", qk.qualifier)
65 .replaceAll("#G", gk.typeParameters);
66 }
68 boolean erroneous() {
69 switch (this) {
70 case ERR_SUPER:
71 case ERR_METH0:
72 case ERR_METH1:
73 case ERR_CONSTR0:
74 case ERR_CONSTR1:
75 return true;
76 default: return false;
77 }
78 }
79 }
81 enum ContextKind {
82 ASSIGN("SAM s = #E;"),
83 METHOD("m(#E, i);");
85 String contextTemplate;
87 ContextKind(String contextTemplate) {
88 this.contextTemplate = contextTemplate;
89 }
91 String contextString(ExprKind ek, ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk) {
92 return contextTemplate.replaceAll("#E", ek.expressionString(rk, qk, gk, sk));
93 }
94 }
96 enum GenericKind {
97 NONE(""),
98 ONE("<X>"),
99 TWO("<X,Y>");
101 String typeParameters;
103 GenericKind(String typeParameters) {
104 this.typeParameters = typeParameters;
105 }
106 }
108 enum QualifierKind {
109 THIS("this"),
110 SUPER("super"),
111 NEW("new Foo()"),
112 METHOD("m()"),
113 FIELD("a.f"),
114 UBOUND_SIMPLE("A"),
115 UNBOUND_ARRAY1("int[]"),
116 UNBOUND_ARRAY2("A<G>[][]"),
117 UNBOUND_GENERIC1("A<X>"),
118 UNBOUND_GENERIC2("A<X, Y>"),
119 UNBOUND_GENERIC3("A<? extends X, ? super Y>"),
120 UNBOUND_GENERIC4("A<int[], short[][]>"),
121 NESTED_GENERIC1("A<A<X,Y>, A<X,Y>>"),
122 NESTED_GENERIC2("A<A<A<X,Y>,A<X,Y>>, A<A<X,Y>,A<X,Y>>>");
124 String qualifier;
126 QualifierKind(String qualifier) {
127 this.qualifier = qualifier;
128 }
129 }
131 enum ExprKind {
132 NONE("#R::S"),
133 SINGLE_PAREN1("(#R#S)"),
134 SINGLE_PAREN2("(#R)#S"),
135 DOUBLE_PAREN1("((#R#S))"),
136 DOUBLE_PAREN2("((#R)#S)"),
137 DOUBLE_PAREN3("((#R))#S");
139 String expressionTemplate;
141 ExprKind(String expressionTemplate) {
142 this.expressionTemplate = expressionTemplate;
143 }
145 String expressionString(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk) {
146 return expressionTemplate
147 .replaceAll("#R", rk.getReferenceString(qk, gk))
148 .replaceAll("#S", sk.subExpression);
149 }
150 }
152 enum SubExprKind {
153 NONE(""),
154 SELECT_FIELD(".f"),
155 SELECT_METHOD(".f()"),
156 SELECT_NEW(".new Foo()"),
157 POSTINC("++"),
158 POSTDEC("--");
160 String subExpression;
162 SubExprKind(String subExpression) {
163 this.subExpression = subExpression;
164 }
165 }
167 public static void main(String... args) throws Exception {
169 //create default shared JavaCompiler - reused across multiple compilations
170 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
171 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
173 for (ReferenceKind rk : ReferenceKind.values()) {
174 for (QualifierKind qk : QualifierKind.values()) {
175 for (GenericKind gk : GenericKind.values()) {
176 for (SubExprKind sk : SubExprKind.values()) {
177 for (ExprKind ek : ExprKind.values()) {
178 for (ContextKind ck : ContextKind.values()) {
179 new MethodReferenceParserTest(rk, qk, gk, sk, ek, ck).run(comp, fm);
180 }
181 }
182 }
183 }
184 }
185 }
186 System.out.println("Total check executed: " + checkCount);
187 }
189 ReferenceKind rk;
190 QualifierKind qk;
191 GenericKind gk;
192 SubExprKind sk;
193 ExprKind ek;
194 ContextKind ck;
195 JavaSource source;
196 DiagnosticChecker diagChecker;
198 MethodReferenceParserTest(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk, ExprKind ek, ContextKind ck) {
199 this.rk = rk;
200 this.qk = qk;
201 this.gk = gk;
202 this.sk = sk;
203 this.ek = ek;
204 this.ck = ck;
205 this.source = new JavaSource();
206 this.diagChecker = new DiagnosticChecker();
207 }
209 class JavaSource extends SimpleJavaFileObject {
211 String template = "class Test {\n" +
212 " void test() {\n" +
213 " #C\n" +
214 " }" +
215 "}";
217 String source;
219 public JavaSource() {
220 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
221 source = template.replaceAll("#C", ck.contextString(ek, rk, qk, gk, sk));
222 }
224 @Override
225 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
226 return source;
227 }
228 }
230 void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
231 JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
232 null, null, Arrays.asList(source));
233 try {
234 ct.parse();
235 } catch (Throwable ex) {
236 throw new AssertionError("Error thrown when parsing the following source:\n" + source.getCharContent(true));
237 }
238 check();
239 }
241 void check() {
242 checkCount++;
244 if (diagChecker.errorFound != rk.erroneous()) {
245 throw new Error("invalid diagnostics for source:\n" +
246 source.getCharContent(true) +
247 "\nFound error: " + diagChecker.errorFound +
248 "\nExpected error: " + rk.erroneous());
249 }
250 }
252 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
254 boolean errorFound;
256 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
257 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
258 errorFound = true;
259 }
260 }
261 }
262 }