Sun, 11 Dec 2011 17:48:25 +0000
7120266: javac fails to compile hotspot code
Summary: Parser changes for method references cause bad intercation with method call syntax
Reviewed-by: jjg
1 /*
2 * Copyright (c) 2011, 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 * @ignore 7120266
28 * @summary Add parser support for method references
29 */
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;
41 public class MethodReferenceParserTest {
43 static int checkCount = 0;
45 enum ReferenceKind {
46 METHOD_REF("#Q##Gm"),
47 CONSTRUCTOR_REF("#Q##Gnew"),
48 ERR_SUPER("#Q##Gsuper"),
49 ERR_METH0("#Q##Gm()"),
50 ERR_METH1("#Q##Gm(X)"),
51 ERR_CONSTR0("#Q##Gnew()"),
52 ERR_CONSTR1("#Q##Gnew(X)");
54 String referenceTemplate;
56 ReferenceKind(String referenceTemplate) {
57 this.referenceTemplate = referenceTemplate;
58 }
60 String getReferenceString(QualifierKind qk, GenericKind gk) {
61 return referenceTemplate
62 .replaceAll("#Q", qk.qualifier)
63 .replaceAll("#G", gk.typeParameters);
64 }
66 boolean erroneous() {
67 switch (this) {
68 case ERR_SUPER:
69 case ERR_METH0:
70 case ERR_METH1:
71 case ERR_CONSTR0:
72 case ERR_CONSTR1:
73 return true;
74 default: return false;
75 }
76 }
77 }
79 enum GenericKind {
80 NONE(""),
81 ONE("<X>"),
82 TWO("<X,Y>");
84 String typeParameters;
86 GenericKind(String typeParameters) {
87 this.typeParameters = typeParameters;
88 }
89 }
91 enum QualifierKind {
92 THIS("this"),
93 SUPER("super"),
94 NEW("new Foo()"),
95 METHOD("m()"),
96 FIELD("a.f"),
97 UBOUND_SIMPLE("A"),
98 UNBOUND_GENERIC1("A<X>"),
99 UNBOUND_GENERIC2("A<X, Y>"),
100 UNBOUND_GENERIC3("A<? extends X, ? super Y>");
102 String qualifier;
104 QualifierKind(String qualifier) {
105 this.qualifier = qualifier;
106 }
107 }
109 enum ExprKind {
110 NONE("#R#S"),
111 SINGLE_PAREN1("(#R#S)"),
112 SINGLE_PAREN2("(#R)#S"),
113 DOUBLE_PAREN1("((#R#S))"),
114 DOUBLE_PAREN2("((#R)#S)"),
115 DOUBLE_PAREN3("((#R))#S");
117 String expressionTemplate;
119 ExprKind(String expressionTemplate) {
120 this.expressionTemplate = expressionTemplate;
121 }
123 String expressionString(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk) {
124 return expressionTemplate
125 .replaceAll("#R", rk.getReferenceString(qk, gk))
126 .replaceAll("#S", sk.subExpression);
127 }
128 }
130 enum SubExprKind {
131 NONE(""),
132 SELECT_FIELD(".f"),
133 SELECT_METHOD(".f()"),
134 SELECT_NEW(".new Foo()"),
135 POSTINC("++"),
136 POSTDEC("--");
138 String subExpression;
140 SubExprKind(String subExpression) {
141 this.subExpression = subExpression;
142 }
143 }
145 public static void main(String... args) throws Exception {
147 //create default shared JavaCompiler - reused across multiple compilations
148 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
149 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
151 for (ReferenceKind rk : ReferenceKind.values()) {
152 for (QualifierKind qk : QualifierKind.values()) {
153 for (GenericKind gk : GenericKind.values()) {
154 for (SubExprKind sk : SubExprKind.values()) {
155 for (ExprKind ek : ExprKind.values()) {
156 new MethodReferenceParserTest(rk, qk, gk, sk, ek).run(comp, fm);
157 }
158 }
159 }
160 }
161 }
162 System.out.println("Total check executed: " + checkCount);
163 }
165 ReferenceKind rk;
166 QualifierKind qk;
167 GenericKind gk;
168 SubExprKind sk;
169 ExprKind ek;
170 JavaSource source;
171 DiagnosticChecker diagChecker;
173 MethodReferenceParserTest(ReferenceKind rk, QualifierKind qk, GenericKind gk, SubExprKind sk, ExprKind ek) {
174 this.rk = rk;
175 this.qk = qk;
176 this.gk = gk;
177 this.sk = sk;
178 this.ek = ek;
179 this.source = new JavaSource();
180 this.diagChecker = new DiagnosticChecker();
181 }
183 class JavaSource extends SimpleJavaFileObject {
185 String template = "class Test {\n" +
186 " SAM s = #E;\n" +
187 "}";
189 String source;
191 public JavaSource() {
192 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
193 source = template.replaceAll("#E", ek.expressionString(rk, qk, gk, sk));
194 }
196 @Override
197 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
198 return source;
199 }
200 }
202 void run(JavaCompiler tool, StandardJavaFileManager fm) throws Exception {
203 JavacTask ct = (JavacTask)tool.getTask(null, fm, diagChecker,
204 Arrays.asList("-XDallowMethodReferences"), null, Arrays.asList(source));
205 try {
206 ct.parse();
207 } catch (Throwable ex) {
208 throw new AssertionError("Error thrown when parsing the following source:\n" + source.getCharContent(true));
209 }
210 check();
211 }
213 void check() {
214 checkCount++;
216 if (diagChecker.errorFound != rk.erroneous()) {
217 throw new Error("invalid diagnostics for source:\n" +
218 source.getCharContent(true) +
219 "\nFound error: " + diagChecker.errorFound +
220 "\nExpected error: " + rk.erroneous());
221 }
222 }
224 static class DiagnosticChecker implements javax.tools.DiagnosticListener<JavaFileObject> {
226 boolean errorFound;
228 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
229 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
230 errorFound = true;
231 }
232 }
233 }
234 }