author | jjg |
Thu, 29 Aug 2013 11:57:52 -0700 | |
changeset 19668 | 6b6009d00484 |
parent 19651 | b1aa46cc2198 |
child 27388 | d694da45bd7a |
permissions | -rw-r--r-- |
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 |
|
19668
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
26 |
* @bug 8023522 |
19651 | 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) |
|
19668
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
90 |
System.err.println(mm + NL); |
19651 | 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 |
||
19668
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
110 |
private static final String NL = System.getProperty("line.separator"); |
19651 | 111 |
|
112 |
private void runField(String code) throws IOException { |
|
113 |
String src = prefix + |
|
114 |
code + "; }" + |
|
115 |
postfix; |
|
116 |
||
117 |
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, |
|
118 |
null, Arrays.asList(new MyFileObject(src))); |
|
119 |
||
120 |
for (CompilationUnitTree cut : ct.parse()) { |
|
121 |
JCTree.JCVariableDecl var = |
|
122 |
(JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0); |
|
19668
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
123 |
checkMatch(code, var); |
19651 | 124 |
} |
125 |
} |
|
126 |
||
127 |
private void runMethod(String code) throws IOException { |
|
128 |
String src = prefix + |
|
129 |
code + "}" + |
|
130 |
postfix; |
|
131 |
||
132 |
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, |
|
133 |
null, Arrays.asList(new MyFileObject(src))); |
|
134 |
||
135 |
||
136 |
for (CompilationUnitTree cut : ct.parse()) { |
|
19668
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
137 |
JCTree.JCMethodDecl meth = |
19651 | 138 |
(JCTree.JCMethodDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0); |
19668
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
139 |
checkMatch(code, meth); |
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
140 |
} |
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
141 |
} |
19651 | 142 |
|
19668
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
143 |
void checkMatch(String code, JCTree tree) { |
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
144 |
String expect = code.replace("\n", NL); |
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
145 |
String found = tree.toString(); |
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
146 |
if (!expect.equals(found)) { |
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
147 |
mismatches.add("Expected: " + expect + NL + |
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
148 |
"Obtained: " + found); |
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
149 |
} else { |
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
150 |
matches.add("Passed: " + expect); |
19651 | 151 |
} |
152 |
} |
|
153 |
} |
|
154 |
||
155 |
||
156 |
class MyFileObject extends SimpleJavaFileObject { |
|
157 |
||
158 |
private String text; |
|
159 |
||
160 |
public MyFileObject(String text) { |
|
161 |
super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
|
162 |
this.text = text; |
|
163 |
} |
|
164 |
||
165 |
@Override |
|
166 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
167 |
return text; |
|
168 |
} |
|
169 |
} |