author | jjg |
Wed, 05 Nov 2014 19:12:45 -0800 | |
changeset 27388 | d694da45bd7a |
parent 19668 | 6b6009d00484 |
child 30730 | d3ce7619db2c |
permissions | -rw-r--r-- |
19651 | 1 |
/* |
27388
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
2 |
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. |
19651 | 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; |
|
27388
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
43 |
import javax.tools.JavaFileManager; |
19651 | 44 |
import javax.tools.JavaFileObject; |
45 |
import javax.tools.SimpleJavaFileObject; |
|
46 |
import javax.tools.ToolProvider; |
|
47 |
||
48 |
public class TypeAnnotationsPretty { |
|
49 |
private final JavaCompiler tool; |
|
50 |
||
51 |
TypeAnnotationsPretty() { |
|
52 |
tool = ToolProvider.getSystemJavaCompiler(); |
|
53 |
} |
|
54 |
||
55 |
private List<String> matches = new LinkedList<String>(); |
|
56 |
private List<String> mismatches = new LinkedList<String>(); |
|
57 |
||
58 |
public static void main(String... args) throws Exception { |
|
59 |
TypeAnnotationsPretty tap = new TypeAnnotationsPretty(); |
|
60 |
||
61 |
tap.runField("@TA()\nObject cls = null"); |
|
62 |
tap.runField("@TA()\nObject cls = new @TA() Object()"); |
|
63 |
||
64 |
tap.runField("@TA()\nList<@TB() Object> cls = null"); |
|
65 |
tap.runField("@TA()\nList<@TB() Object> cls = new @TA() LinkedList<@TB() Object>()"); |
|
66 |
||
67 |
tap.runField("Class[] cls = null"); |
|
68 |
tap.runField("@TA()\nClass[] cls = null"); |
|
69 |
tap.runField("Class @TA() [] cls = null"); |
|
70 |
tap.runField("@TA()\nClass @TB() [] cls = null"); |
|
71 |
||
72 |
tap.runField("Class[] cls = new Class[]{Object.class}"); |
|
73 |
tap.runField("@TA()\nClass[] cls = new @TA() Class[]{Object.class}"); |
|
74 |
tap.runField("Class @TB() [] cls = new Class @TB() []{Object.class}"); |
|
75 |
tap.runField("@TA()\nClass @TB() [] cls = new @TA() Class @TB() []{Object.class}"); |
|
76 |
tap.runField("@TA()\nClass @TB() [] @TC() [] cls = new @TA() Class @TB() [10] @TC() []"); |
|
77 |
tap.runField("Class @TB() [] @TC() [] cls = new Class @TB() [10] @TC() []"); |
|
78 |
tap.runField("@TA()\nClass @TB() [] @TC() [] @TD() [] cls = new @TA() Class @TB() [10] @TC() [] @TD() []"); |
|
79 |
||
80 |
tap.runMethod("\n@TA()\nObject test(@TB()\nList<@TC() String> p) {\n" + |
|
81 |
" return null;\n" + |
|
82 |
"}"); |
|
83 |
||
84 |
||
85 |
if (!tap.matches.isEmpty()) { |
|
86 |
for (String m : tap.matches) |
|
87 |
System.out.println(m); |
|
88 |
} |
|
89 |
if (!tap.mismatches.isEmpty()) { |
|
90 |
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
|
91 |
System.err.println(mm + NL); |
19651 | 92 |
throw new RuntimeException("Tests failed!"); |
93 |
} |
|
94 |
} |
|
95 |
||
96 |
private static final String prefix = |
|
97 |
"import java.lang.annotation.*;" + |
|
98 |
"import java.util.*;" + |
|
99 |
"public class Test {"; |
|
100 |
||
101 |
private static final String postfix = |
|
102 |
"@Target(ElementType.TYPE_USE)" + |
|
103 |
"@interface TA {}" + |
|
104 |
"@Target(ElementType.TYPE_USE)" + |
|
105 |
"@interface TB {}" + |
|
106 |
"@Target(ElementType.TYPE_USE)" + |
|
107 |
"@interface TC {}" + |
|
108 |
"@Target(ElementType.TYPE_USE)" + |
|
109 |
"@interface TD {}"; |
|
110 |
||
19668
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
111 |
private static final String NL = System.getProperty("line.separator"); |
19651 | 112 |
|
113 |
private void runField(String code) throws IOException { |
|
114 |
String src = prefix + |
|
115 |
code + "; }" + |
|
116 |
postfix; |
|
117 |
||
27388
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
118 |
try (JavaFileManager fm = tool.getStandardFileManager(null, null, null)) { |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
119 |
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null, |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
120 |
null, Arrays.asList(new MyFileObject(src))); |
19651 | 121 |
|
27388
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
122 |
for (CompilationUnitTree cut : ct.parse()) { |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
123 |
JCTree.JCVariableDecl var = |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
124 |
(JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0); |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
125 |
checkMatch(code, var); |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
126 |
} |
19651 | 127 |
} |
128 |
} |
|
129 |
||
130 |
private void runMethod(String code) throws IOException { |
|
131 |
String src = prefix + |
|
132 |
code + "}" + |
|
133 |
postfix; |
|
134 |
||
27388
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
135 |
try (JavaFileManager fm = tool.getStandardFileManager(null, null, null)) { |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
136 |
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null, |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
137 |
null, Arrays.asList(new MyFileObject(src))); |
19651 | 138 |
|
139 |
||
27388
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
140 |
for (CompilationUnitTree cut : ct.parse()) { |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
141 |
JCTree.JCMethodDecl meth = |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
142 |
(JCTree.JCMethodDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0); |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
143 |
checkMatch(code, meth); |
d694da45bd7a
8062676: Tests which leak lots of file managers should be fixed (group 2)
jjg
parents:
19668
diff
changeset
|
144 |
} |
19668
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
145 |
} |
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
146 |
} |
19651 | 147 |
|
19668
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
148 |
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
|
149 |
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
|
150 |
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
|
151 |
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
|
152 |
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
|
153 |
"Obtained: " + found); |
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
154 |
} else { |
6b6009d00484
8023522: tools/javac/tree/TypeAnnotationsPretty.java test cases with @TA newline fail on windows only
jjg
parents:
19651
diff
changeset
|
155 |
matches.add("Passed: " + expect); |
19651 | 156 |
} |
157 |
} |
|
158 |
} |
|
159 |
||
160 |
||
161 |
class MyFileObject extends SimpleJavaFileObject { |
|
162 |
||
163 |
private String text; |
|
164 |
||
165 |
public MyFileObject(String text) { |
|
166 |
super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
|
167 |
this.text = text; |
|
168 |
} |
|
169 |
||
170 |
@Override |
|
171 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
172 |
return text; |
|
173 |
} |
|
174 |
} |