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