Thu, 10 Oct 2013 14:58:04 +0200
8001931: The new build system whitespace cleanup
Reviewed-by: tbell, simonis, erikj
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 */
24 /*
25 * @test
26 * @bug 8023522
27 * @summary test Pretty print of type annotations
28 * @author wmdietl
29 */
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;
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;
42 import javax.tools.JavaCompiler;
43 import javax.tools.JavaFileObject;
44 import javax.tools.SimpleJavaFileObject;
45 import javax.tools.ToolProvider;
47 public class TypeAnnotationsPretty {
48 private final JavaCompiler tool;
50 TypeAnnotationsPretty() {
51 tool = ToolProvider.getSystemJavaCompiler();
52 }
54 private List<String> matches = new LinkedList<String>();
55 private List<String> mismatches = new LinkedList<String>();
57 public static void main(String... args) throws Exception {
58 TypeAnnotationsPretty tap = new TypeAnnotationsPretty();
60 tap.runField("@TA()\nObject cls = null");
61 tap.runField("@TA()\nObject cls = new @TA() Object()");
63 tap.runField("@TA()\nList<@TB() Object> cls = null");
64 tap.runField("@TA()\nList<@TB() Object> cls = new @TA() LinkedList<@TB() Object>()");
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");
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() []");
79 tap.runMethod("\n@TA()\nObject test(@TB()\nList<@TC() String> p) {\n" +
80 " return null;\n" +
81 "}");
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 + NL);
91 throw new RuntimeException("Tests failed!");
92 }
93 }
95 private static final String prefix =
96 "import java.lang.annotation.*;" +
97 "import java.util.*;" +
98 "public class Test {";
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 {}";
110 private static final String NL = System.getProperty("line.separator");
112 private void runField(String code) throws IOException {
113 String src = prefix +
114 code + "; }" +
115 postfix;
117 JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
118 null, Arrays.asList(new MyFileObject(src)));
120 for (CompilationUnitTree cut : ct.parse()) {
121 JCTree.JCVariableDecl var =
122 (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
123 checkMatch(code, var);
124 }
125 }
127 private void runMethod(String code) throws IOException {
128 String src = prefix +
129 code + "}" +
130 postfix;
132 JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
133 null, Arrays.asList(new MyFileObject(src)));
136 for (CompilationUnitTree cut : ct.parse()) {
137 JCTree.JCMethodDecl meth =
138 (JCTree.JCMethodDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
139 checkMatch(code, meth);
140 }
141 }
143 void checkMatch(String code, JCTree tree) {
144 String expect = code.replace("\n", NL);
145 String found = tree.toString();
146 if (!expect.equals(found)) {
147 mismatches.add("Expected: " + expect + NL +
148 "Obtained: " + found);
149 } else {
150 matches.add("Passed: " + expect);
151 }
152 }
153 }
156 class MyFileObject extends SimpleJavaFileObject {
158 private String text;
160 public MyFileObject(String text) {
161 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
162 this.text = text;
163 }
165 @Override
166 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
167 return text;
168 }
169 }