author | akulyakh |
Thu, 21 May 2015 11:41:04 -0700 | |
changeset 30730 | d3ce7619db2c |
parent 19932 | 1386f12ad4b9 |
child 34752 | 9c262a013456 |
permissions | -rw-r--r-- |
8625 | 1 |
/* |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
19932
diff
changeset
|
2 |
* Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. |
8625 | 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 |
/* |
|
26 |
* @test |
|
10192
378321489bea
7025809: Provided new utility visitors supporting SourceVersion.RELEASE_8
darcy
parents:
8842
diff
changeset
|
27 |
* @bug 6639645 7026414 7025809 |
8625 | 28 |
* @summary Modeling type implementing missing interfaces |
14963
974d4423c999
8005282: Use @library tag with non-relative path for javac tests
darcy
parents:
10192
diff
changeset
|
29 |
* @library /tools/javac/lib |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
19932
diff
changeset
|
30 |
* @modules jdk.compiler/com.sun.tools.javac.processing |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
19932
diff
changeset
|
31 |
* jdk.compiler/com.sun.tools.javac.util |
8625 | 32 |
* @build JavacTestingAbstractProcessor TestMissingElement |
15385 | 33 |
* @compile/fail/ref=TestMissingElement.ref -proc:only -XprintRounds -XDrawDiagnostics -processor TestMissingElement InvalidSource.java |
8625 | 34 |
*/ |
35 |
||
19932
1386f12ad4b9
8006972: jtreg test fails: test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java
jjg
parents:
15385
diff
changeset
|
36 |
import java.io.PrintWriter; |
8625 | 37 |
import java.util.*; |
38 |
import javax.annotation.processing.*; |
|
39 |
import javax.lang.model.element.*; |
|
40 |
import javax.lang.model.type.*; |
|
41 |
import javax.lang.model.util.*; |
|
42 |
import static javax.tools.Diagnostic.Kind.*; |
|
43 |
||
19932
1386f12ad4b9
8006972: jtreg test fails: test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java
jjg
parents:
15385
diff
changeset
|
44 |
import com.sun.tools.javac.processing.JavacProcessingEnvironment; |
1386f12ad4b9
8006972: jtreg test fails: test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java
jjg
parents:
15385
diff
changeset
|
45 |
import com.sun.tools.javac.util.Log; |
1386f12ad4b9
8006972: jtreg test fails: test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java
jjg
parents:
15385
diff
changeset
|
46 |
|
8625 | 47 |
public class TestMissingElement extends JavacTestingAbstractProcessor { |
19932
1386f12ad4b9
8006972: jtreg test fails: test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java
jjg
parents:
15385
diff
changeset
|
48 |
private PrintWriter out; |
1386f12ad4b9
8006972: jtreg test fails: test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java
jjg
parents:
15385
diff
changeset
|
49 |
|
1386f12ad4b9
8006972: jtreg test fails: test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java
jjg
parents:
15385
diff
changeset
|
50 |
@Override |
1386f12ad4b9
8006972: jtreg test fails: test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java
jjg
parents:
15385
diff
changeset
|
51 |
public void init(ProcessingEnvironment env) { |
1386f12ad4b9
8006972: jtreg test fails: test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java
jjg
parents:
15385
diff
changeset
|
52 |
super.init(env); |
1386f12ad4b9
8006972: jtreg test fails: test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java
jjg
parents:
15385
diff
changeset
|
53 |
out = ((JavacProcessingEnvironment) env).getContext().get(Log.outKey); |
1386f12ad4b9
8006972: jtreg test fails: test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java
jjg
parents:
15385
diff
changeset
|
54 |
} |
1386f12ad4b9
8006972: jtreg test fails: test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java
jjg
parents:
15385
diff
changeset
|
55 |
|
8625 | 56 |
@Override |
57 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
|
58 |
for (TypeElement te: ElementFilter.typesIn(roundEnv.getRootElements())) { |
|
59 |
if (isSimpleName(te, "InvalidSource")) { |
|
60 |
for (Element c: te.getEnclosedElements()) { |
|
61 |
for (AnnotationMirror am: c.getAnnotationMirrors()) { |
|
62 |
Element ate = am.getAnnotationType().asElement(); |
|
63 |
if (isSimpleName(ate, "ExpectInterfaces")) { |
|
64 |
checkInterfaces((TypeElement) c, getValue(am)); |
|
65 |
} else if (isSimpleName(ate, "ExpectSupertype")) { |
|
66 |
checkSupertype((TypeElement) c, getValue(am)); |
|
67 |
} |
|
68 |
} |
|
69 |
} |
|
70 |
} |
|
71 |
} |
|
72 |
return true; |
|
73 |
} |
|
74 |
||
75 |
private boolean isSimpleName(Element e, String name) { |
|
76 |
return e.getSimpleName().contentEquals(name); |
|
77 |
} |
|
78 |
||
79 |
private String getValue(AnnotationMirror am) { |
|
80 |
Map<? extends ExecutableElement, ? extends AnnotationValue> map = am.getElementValues(); |
|
81 |
if (map.size() != 1) throw new IllegalArgumentException(); |
|
82 |
AnnotationValue v = map.values().iterator().next(); |
|
83 |
return (String) v.getValue(); |
|
84 |
} |
|
85 |
||
86 |
private void checkInterfaces(TypeElement te, String expect) { |
|
19932
1386f12ad4b9
8006972: jtreg test fails: test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java
jjg
parents:
15385
diff
changeset
|
87 |
out.println("check interfaces: " + te + " -- " + expect); |
8625 | 88 |
String found = asString(te.getInterfaces(), ", "); |
89 |
checkEqual("interfaces", te, found, expect); |
|
90 |
} |
|
91 |
||
92 |
private void checkSupertype(TypeElement te, String expect) { |
|
19932
1386f12ad4b9
8006972: jtreg test fails: test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java
jjg
parents:
15385
diff
changeset
|
93 |
out.println("check supertype: " + te + " -- " + expect); |
8625 | 94 |
String found = asString(te.getSuperclass()); |
95 |
checkEqual("supertype", te, found, expect); |
|
96 |
} |
|
97 |
||
98 |
private void checkEqual(String label, TypeElement te, String found, String expect) { |
|
99 |
if (found.equals(expect)) { |
|
100 |
// messager.printMessage(NOTE, "expected " + label + " found: " + expect, te); |
|
101 |
} else { |
|
19932
1386f12ad4b9
8006972: jtreg test fails: test/tools/javac/processing/model/element/TestMissingElement/TestMissingElement.java
jjg
parents:
15385
diff
changeset
|
102 |
out.println("unexpected " + label + ": " + te + "\n" |
8625 | 103 |
+ " found: " + found + "\n" |
104 |
+ "expect: " + expect); |
|
105 |
messager.printMessage(ERROR, "unexpected " + label + " found: " + found + "; expected: " + expect, te); |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
private String asString(List<? extends TypeMirror> ts, String sep) { |
|
110 |
StringBuilder sb = new StringBuilder(); |
|
111 |
for (TypeMirror t: ts) { |
|
112 |
if (sb.length() != 0) sb.append(sep); |
|
113 |
sb.append(asString(t)); |
|
114 |
} |
|
115 |
return sb.toString(); |
|
116 |
} |
|
117 |
||
118 |
private String asString(TypeMirror t) { |
|
119 |
if (t == null) |
|
120 |
return "[typ:null]"; |
|
10192
378321489bea
7025809: Provided new utility visitors supporting SourceVersion.RELEASE_8
darcy
parents:
8842
diff
changeset
|
121 |
return t.accept(new SimpleTypeVisitor<String, Void>() { |
8625 | 122 |
@Override |
123 |
public String defaultAction(TypeMirror t, Void ignore) { |
|
124 |
return "[typ:" + t.toString() + "]"; |
|
125 |
} |
|
126 |
||
127 |
@Override |
|
128 |
public String visitDeclared(DeclaredType t, Void ignore) { |
|
8842 | 129 |
checkEqual(t.asElement(), types.asElement(t)); |
8625 | 130 |
String s = asString(t.asElement()); |
131 |
List<? extends TypeMirror> args = t.getTypeArguments(); |
|
132 |
if (!args.isEmpty()) |
|
133 |
s += "<" + asString(args, ",") + ">"; |
|
134 |
return s; |
|
135 |
} |
|
136 |
||
137 |
@Override |
|
138 |
public String visitTypeVariable(TypeVariable t, Void ignore) { |
|
139 |
return "tvar " + t; |
|
140 |
} |
|
141 |
||
142 |
@Override |
|
143 |
public String visitError(ErrorType t, Void ignore) { |
|
144 |
return "!:" + visitDeclared(t, ignore); |
|
145 |
} |
|
146 |
}, null); |
|
147 |
} |
|
148 |
||
149 |
private String asString(Element e) { |
|
150 |
if (e == null) |
|
151 |
return "[elt:null]"; |
|
10192
378321489bea
7025809: Provided new utility visitors supporting SourceVersion.RELEASE_8
darcy
parents:
8842
diff
changeset
|
152 |
return e.accept(new SimpleElementVisitor<String, Void>() { |
8625 | 153 |
@Override |
154 |
public String defaultAction(Element e, Void ignore) { |
|
155 |
return "[elt:" + e.getKind() + " " + e.toString() + "]"; |
|
156 |
} |
|
157 |
@Override |
|
158 |
public String visitPackage(PackageElement e, Void ignore) { |
|
159 |
return "pkg " + e.getQualifiedName(); |
|
160 |
} |
|
161 |
@Override |
|
162 |
public String visitType(TypeElement e, Void ignore) { |
|
163 |
StringBuilder sb = new StringBuilder(); |
|
164 |
if (e.getEnclosedElements().isEmpty()) |
|
165 |
sb.append("empty "); |
|
166 |
ElementKind ek = e.getKind(); |
|
167 |
switch (ek) { |
|
168 |
case CLASS: |
|
169 |
sb.append("clss"); |
|
170 |
break; |
|
171 |
case INTERFACE: |
|
172 |
sb.append("intf"); |
|
173 |
break; |
|
174 |
default: |
|
175 |
sb.append(ek); |
|
176 |
break; |
|
177 |
} |
|
178 |
sb.append(" "); |
|
179 |
Element encl = e.getEnclosingElement(); |
|
180 |
if (!isUnnamedPackage(encl) && encl.asType().getKind() != TypeKind.NONE) { |
|
181 |
sb.append("("); |
|
182 |
sb.append(asString(encl)); |
|
183 |
sb.append(")"); |
|
184 |
sb.append("."); |
|
185 |
} |
|
186 |
sb.append(e.getSimpleName()); |
|
187 |
if (e.asType().getKind() == TypeKind.ERROR) sb.append("!"); |
|
188 |
return sb.toString(); |
|
189 |
} |
|
190 |
}, null); |
|
191 |
} |
|
192 |
||
193 |
boolean isUnnamedPackage(Element e) { |
|
194 |
return (e != null && e.getKind() == ElementKind.PACKAGE |
|
195 |
&& ((PackageElement) e).isUnnamed()); |
|
196 |
} |
|
8842 | 197 |
|
198 |
void checkEqual(Element e1, Element e2) { |
|
199 |
if (e1 != e2) { |
|
200 |
throw new AssertionError("elements not equal as expected: " |
|
201 |
+ e1 + ", " + e2); |
|
202 |
} |
|
203 |
} |
|
8625 | 204 |
} |
205 |
||
206 |
||
207 |