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