test/tools/javac/lambda/BadLambdaExpr.java

changeset 0
959103a6100f
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
1 /*
2 * Copyright (c) 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 */
23
24 /*
25 * @test
26 * @bug 8003280
27 * @summary Add lambda tests
28 * compile crashes on partial lambda expressions
29 */
30
31 import com.sun.source.util.JavacTask;
32 import java.net.URI;
33 import java.util.Arrays;
34 import javax.tools.Diagnostic;
35 import javax.tools.JavaCompiler;
36 import javax.tools.JavaFileObject;
37 import javax.tools.SimpleJavaFileObject;
38 import javax.tools.StandardJavaFileManager;
39 import javax.tools.ToolProvider;
40
41
42 public class BadLambdaExpr {
43
44 static int checkCount = 0;
45
46 enum ParameterListKind {
47 ZERO_ARY("()"),
48 UNARY("(#P)"),
49 TWO_ARY("(#P, #P)"),
50 THREE_ARY("(#P, #P, #P)");
51
52 String parametersTemplateStr;
53
54 ParameterListKind(String parametersTemplateStr) {
55 this.parametersTemplateStr = parametersTemplateStr;
56 }
57
58 String getParameterString(ParameterKind pk) {
59 return parametersTemplateStr.replaceAll("#P", pk.parameterStr);
60 }
61 }
62
63 enum ParameterKind {
64 IMPLICIT("a"),
65 EXPLIICT("A a");
66
67 String parameterStr;
68
69 ParameterKind(String parameterStr) {
70 this.parameterStr = parameterStr;
71 }
72 }
73
74 enum ArrowKind {
75 NONE(""),
76 SEMI("-"),
77 FULL("->");
78
79 String arrowStr;
80
81 ArrowKind(String arrowStr) {
82 this.arrowStr = arrowStr;
83 }
84 }
85
86 enum ExprKind {
87 NONE("#P#A"),
88 METHOD_CALL("m(#P#A)"),
89 CONSTR_CALL("new Foo(#P#A)");
90
91 String expressionTemplate;
92
93 ExprKind(String expressionTemplate) {
94 this.expressionTemplate = expressionTemplate;
95 }
96
97 String expressionString(ParameterListKind plk, ParameterKind pk,
98 ArrowKind ak) {
99 return expressionTemplate.replaceAll("#P", plk.getParameterString(pk))
100 .replaceAll("#A", ak.arrowStr);
101 }
102 }
103
104 public static void main(String... args) throws Exception {
105
106 //create default shared JavaCompiler - reused across multiple compilations
107 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
108 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
109
110 for (ParameterListKind plk : ParameterListKind.values()) {
111 for (ParameterKind pk : ParameterKind.values()) {
112 for (ArrowKind ak : ArrowKind.values()) {
113 for (ExprKind ek : ExprKind.values()) {
114 new BadLambdaExpr(plk, pk, ak, ek).run(comp, fm);
115 }
116 }
117 }
118 }
119 System.out.println("Total check executed: " + checkCount);
120 }
121
122 ParameterListKind plk;
123 ParameterKind pk;
124 ArrowKind ak;
125 ExprKind ek;
126 JavaSource source;
127 DiagnosticChecker diagChecker;
128
129 BadLambdaExpr(ParameterListKind plk, ParameterKind pk, ArrowKind ak, ExprKind ek) {
130 this.plk = plk;
131 this.pk = pk;
132 this.ak = ak;
133 this.ek = ek;
134 this.source = new JavaSource();
135 this.diagChecker = new DiagnosticChecker();
136 }
137
138 class JavaSource extends SimpleJavaFileObject {
139
140 String template = "class Test {\n" +
141 " SAM s = #E;\n" +
142 "}";
143
144 String source;
145
146 public JavaSource() {
147 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
148 source = template.replaceAll("#E", ek.expressionString(plk, pk, ak));
149 }
150
151 @Override
152 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
153 return source;
154 }
155 }
156
157 void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
158 JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
159 null, null, Arrays.asList(source));
160 try {
161 ct.parse();
162 } catch (Throwable ex) {
163 throw new AssertionError("Error thron when parsing the following source:\n" + source.getCharContent(true));
164 }
165 check();
166 }
167
168 void check() {
169 boolean errorExpected =
170 ak != ArrowKind.NONE ||
171 plk != ParameterListKind.UNARY ||
172 pk != ParameterKind.IMPLICIT;
173 if (errorExpected != diagChecker.errorFound) {
174 throw new Error("bad diag for source:\n" +
175 source.getCharContent(true));
176 }
177 checkCount++;
178 }
179
180 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
181
182 boolean errorFound;
183
184 @Override
185 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
186 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
187 errorFound = true;
188 }
189 }
190 }
191 }

mercurial