19651
|
1 |
/*
|
|
2 |
* Copyright (c) 2013, 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 1234567
|
|
27 |
* @summary test Pretty print of type annotations
|
|
28 |
* @author wmdietl
|
|
29 |
*/
|
|
30 |
|
|
31 |
import com.sun.source.tree.ClassTree;
|
|
32 |
import com.sun.source.tree.CompilationUnitTree;
|
|
33 |
import com.sun.tools.javac.api.JavacTaskImpl;
|
|
34 |
import com.sun.tools.javac.tree.JCTree;
|
|
35 |
|
|
36 |
import java.io.IOException;
|
|
37 |
import java.net.URI;
|
|
38 |
import java.util.Arrays;
|
|
39 |
import java.util.List;
|
|
40 |
import java.util.LinkedList;
|
|
41 |
|
|
42 |
import javax.tools.JavaCompiler;
|
|
43 |
import javax.tools.JavaFileObject;
|
|
44 |
import javax.tools.SimpleJavaFileObject;
|
|
45 |
import javax.tools.ToolProvider;
|
|
46 |
|
|
47 |
public class TypeAnnotationsPretty {
|
|
48 |
private final JavaCompiler tool;
|
|
49 |
|
|
50 |
TypeAnnotationsPretty() {
|
|
51 |
tool = ToolProvider.getSystemJavaCompiler();
|
|
52 |
}
|
|
53 |
|
|
54 |
private List<String> matches = new LinkedList<String>();
|
|
55 |
private List<String> mismatches = new LinkedList<String>();
|
|
56 |
|
|
57 |
public static void main(String... args) throws Exception {
|
|
58 |
TypeAnnotationsPretty tap = new TypeAnnotationsPretty();
|
|
59 |
|
|
60 |
tap.runField("@TA()\nObject cls = null");
|
|
61 |
tap.runField("@TA()\nObject cls = new @TA() Object()");
|
|
62 |
|
|
63 |
tap.runField("@TA()\nList<@TB() Object> cls = null");
|
|
64 |
tap.runField("@TA()\nList<@TB() Object> cls = new @TA() LinkedList<@TB() Object>()");
|
|
65 |
|
|
66 |
tap.runField("Class[] cls = null");
|
|
67 |
tap.runField("@TA()\nClass[] cls = null");
|
|
68 |
tap.runField("Class @TA() [] cls = null");
|
|
69 |
tap.runField("@TA()\nClass @TB() [] cls = null");
|
|
70 |
|
|
71 |
tap.runField("Class[] cls = new Class[]{Object.class}");
|
|
72 |
tap.runField("@TA()\nClass[] cls = new @TA() Class[]{Object.class}");
|
|
73 |
tap.runField("Class @TB() [] cls = new Class @TB() []{Object.class}");
|
|
74 |
tap.runField("@TA()\nClass @TB() [] cls = new @TA() Class @TB() []{Object.class}");
|
|
75 |
tap.runField("@TA()\nClass @TB() [] @TC() [] cls = new @TA() Class @TB() [10] @TC() []");
|
|
76 |
tap.runField("Class @TB() [] @TC() [] cls = new Class @TB() [10] @TC() []");
|
|
77 |
tap.runField("@TA()\nClass @TB() [] @TC() [] @TD() [] cls = new @TA() Class @TB() [10] @TC() [] @TD() []");
|
|
78 |
|
|
79 |
tap.runMethod("\n@TA()\nObject test(@TB()\nList<@TC() String> p) {\n" +
|
|
80 |
" return null;\n" +
|
|
81 |
"}");
|
|
82 |
|
|
83 |
|
|
84 |
if (!tap.matches.isEmpty()) {
|
|
85 |
for (String m : tap.matches)
|
|
86 |
System.out.println(m);
|
|
87 |
}
|
|
88 |
if (!tap.mismatches.isEmpty()) {
|
|
89 |
for (String mm : tap.mismatches)
|
|
90 |
System.err.println(mm + "\n");
|
|
91 |
throw new RuntimeException("Tests failed!");
|
|
92 |
}
|
|
93 |
}
|
|
94 |
|
|
95 |
private static final String prefix =
|
|
96 |
"import java.lang.annotation.*;" +
|
|
97 |
"import java.util.*;" +
|
|
98 |
"public class Test {";
|
|
99 |
|
|
100 |
private static final String postfix =
|
|
101 |
"@Target(ElementType.TYPE_USE)" +
|
|
102 |
"@interface TA {}" +
|
|
103 |
"@Target(ElementType.TYPE_USE)" +
|
|
104 |
"@interface TB {}" +
|
|
105 |
"@Target(ElementType.TYPE_USE)" +
|
|
106 |
"@interface TC {}" +
|
|
107 |
"@Target(ElementType.TYPE_USE)" +
|
|
108 |
"@interface TD {}";
|
|
109 |
|
|
110 |
|
|
111 |
private void runField(String code) throws IOException {
|
|
112 |
String src = prefix +
|
|
113 |
code + "; }" +
|
|
114 |
postfix;
|
|
115 |
|
|
116 |
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
|
|
117 |
null, Arrays.asList(new MyFileObject(src)));
|
|
118 |
|
|
119 |
|
|
120 |
for (CompilationUnitTree cut : ct.parse()) {
|
|
121 |
JCTree.JCVariableDecl var =
|
|
122 |
(JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
|
|
123 |
|
|
124 |
if (!code.equals(var.toString())) {
|
|
125 |
mismatches.add("Expected: " + code +
|
|
126 |
"\nObtained: " + var.toString());
|
|
127 |
} else {
|
|
128 |
matches.add("Passed: " + code);
|
|
129 |
}
|
|
130 |
}
|
|
131 |
}
|
|
132 |
|
|
133 |
private void runMethod(String code) throws IOException {
|
|
134 |
String src = prefix +
|
|
135 |
code + "}" +
|
|
136 |
postfix;
|
|
137 |
|
|
138 |
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
|
|
139 |
null, Arrays.asList(new MyFileObject(src)));
|
|
140 |
|
|
141 |
|
|
142 |
for (CompilationUnitTree cut : ct.parse()) {
|
|
143 |
JCTree.JCMethodDecl var =
|
|
144 |
(JCTree.JCMethodDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
|
|
145 |
|
|
146 |
if (!code.equals(var.toString())) {
|
|
147 |
mismatches.add("Expected: " + code +
|
|
148 |
"\nObtained: " + var.toString());
|
|
149 |
} else {
|
|
150 |
matches.add("Passed: " + code);
|
|
151 |
}
|
|
152 |
}
|
|
153 |
}
|
|
154 |
}
|
|
155 |
|
|
156 |
|
|
157 |
class MyFileObject extends SimpleJavaFileObject {
|
|
158 |
|
|
159 |
private String text;
|
|
160 |
|
|
161 |
public MyFileObject(String text) {
|
|
162 |
super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
|
|
163 |
this.text = text;
|
|
164 |
}
|
|
165 |
|
|
166 |
@Override
|
|
167 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
|
|
168 |
return text;
|
|
169 |
}
|
|
170 |
}
|