Mon, 19 Jun 2017 14:51:25 -0700
Added tag jdk8u151-b02 for changeset 732e1bab2660
jjg@1969 | 1 | /* |
jjg@1969 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
jjg@1969 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@1969 | 4 | * |
jjg@1969 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@1969 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@1969 | 7 | * published by the Free Software Foundation. |
jjg@1969 | 8 | * |
jjg@1969 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@1969 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@1969 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@1969 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@1969 | 13 | * accompanied this code). |
jjg@1969 | 14 | * |
jjg@1969 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@1969 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@1969 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@1969 | 18 | * |
jjg@1969 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@1969 | 20 | * or visit www.oracle.com if you need additional information or have any |
jjg@1969 | 21 | * questions. |
jjg@1969 | 22 | */ |
jjg@1969 | 23 | |
jjg@1969 | 24 | /* |
jjg@1969 | 25 | * @test |
jjg@1986 | 26 | * @bug 8023522 |
jjg@1969 | 27 | * @summary test Pretty print of type annotations |
jjg@1969 | 28 | * @author wmdietl |
jjg@1969 | 29 | */ |
jjg@1969 | 30 | |
jjg@1969 | 31 | import com.sun.source.tree.ClassTree; |
jjg@1969 | 32 | import com.sun.source.tree.CompilationUnitTree; |
jjg@1969 | 33 | import com.sun.tools.javac.api.JavacTaskImpl; |
jjg@1969 | 34 | import com.sun.tools.javac.tree.JCTree; |
jjg@1969 | 35 | |
jjg@1969 | 36 | import java.io.IOException; |
jjg@1969 | 37 | import java.net.URI; |
jjg@1969 | 38 | import java.util.Arrays; |
jjg@1969 | 39 | import java.util.List; |
jjg@1969 | 40 | import java.util.LinkedList; |
jjg@1969 | 41 | |
jjg@1969 | 42 | import javax.tools.JavaCompiler; |
jjg@1969 | 43 | import javax.tools.JavaFileObject; |
jjg@1969 | 44 | import javax.tools.SimpleJavaFileObject; |
jjg@1969 | 45 | import javax.tools.ToolProvider; |
jjg@1969 | 46 | |
jjg@1969 | 47 | public class TypeAnnotationsPretty { |
jjg@1969 | 48 | private final JavaCompiler tool; |
jjg@1969 | 49 | |
jjg@1969 | 50 | TypeAnnotationsPretty() { |
jjg@1969 | 51 | tool = ToolProvider.getSystemJavaCompiler(); |
jjg@1969 | 52 | } |
jjg@1969 | 53 | |
jjg@1969 | 54 | private List<String> matches = new LinkedList<String>(); |
jjg@1969 | 55 | private List<String> mismatches = new LinkedList<String>(); |
jjg@1969 | 56 | |
jjg@1969 | 57 | public static void main(String... args) throws Exception { |
jjg@1969 | 58 | TypeAnnotationsPretty tap = new TypeAnnotationsPretty(); |
jjg@1969 | 59 | |
jjg@1969 | 60 | tap.runField("@TA()\nObject cls = null"); |
jjg@1969 | 61 | tap.runField("@TA()\nObject cls = new @TA() Object()"); |
jjg@1969 | 62 | |
jjg@1969 | 63 | tap.runField("@TA()\nList<@TB() Object> cls = null"); |
jjg@1969 | 64 | tap.runField("@TA()\nList<@TB() Object> cls = new @TA() LinkedList<@TB() Object>()"); |
jjg@1969 | 65 | |
jjg@1969 | 66 | tap.runField("Class[] cls = null"); |
jjg@1969 | 67 | tap.runField("@TA()\nClass[] cls = null"); |
jjg@1969 | 68 | tap.runField("Class @TA() [] cls = null"); |
jjg@1969 | 69 | tap.runField("@TA()\nClass @TB() [] cls = null"); |
jjg@1969 | 70 | |
jjg@1969 | 71 | tap.runField("Class[] cls = new Class[]{Object.class}"); |
jjg@1969 | 72 | tap.runField("@TA()\nClass[] cls = new @TA() Class[]{Object.class}"); |
jjg@1969 | 73 | tap.runField("Class @TB() [] cls = new Class @TB() []{Object.class}"); |
jjg@1969 | 74 | tap.runField("@TA()\nClass @TB() [] cls = new @TA() Class @TB() []{Object.class}"); |
jjg@1969 | 75 | tap.runField("@TA()\nClass @TB() [] @TC() [] cls = new @TA() Class @TB() [10] @TC() []"); |
jjg@1969 | 76 | tap.runField("Class @TB() [] @TC() [] cls = new Class @TB() [10] @TC() []"); |
jjg@1969 | 77 | tap.runField("@TA()\nClass @TB() [] @TC() [] @TD() [] cls = new @TA() Class @TB() [10] @TC() [] @TD() []"); |
jjg@1969 | 78 | |
jjg@1969 | 79 | tap.runMethod("\n@TA()\nObject test(@TB()\nList<@TC() String> p) {\n" + |
jjg@1969 | 80 | " return null;\n" + |
jjg@1969 | 81 | "}"); |
jjg@1969 | 82 | |
jjg@1969 | 83 | |
jjg@1969 | 84 | if (!tap.matches.isEmpty()) { |
jjg@1969 | 85 | for (String m : tap.matches) |
jjg@1969 | 86 | System.out.println(m); |
jjg@1969 | 87 | } |
jjg@1969 | 88 | if (!tap.mismatches.isEmpty()) { |
jjg@1969 | 89 | for (String mm : tap.mismatches) |
jjg@1986 | 90 | System.err.println(mm + NL); |
jjg@1969 | 91 | throw new RuntimeException("Tests failed!"); |
jjg@1969 | 92 | } |
jjg@1969 | 93 | } |
jjg@1969 | 94 | |
jjg@1969 | 95 | private static final String prefix = |
jjg@1969 | 96 | "import java.lang.annotation.*;" + |
jjg@1969 | 97 | "import java.util.*;" + |
jjg@1969 | 98 | "public class Test {"; |
jjg@1969 | 99 | |
jjg@1969 | 100 | private static final String postfix = |
jjg@1969 | 101 | "@Target(ElementType.TYPE_USE)" + |
jjg@1969 | 102 | "@interface TA {}" + |
jjg@1969 | 103 | "@Target(ElementType.TYPE_USE)" + |
jjg@1969 | 104 | "@interface TB {}" + |
jjg@1969 | 105 | "@Target(ElementType.TYPE_USE)" + |
jjg@1969 | 106 | "@interface TC {}" + |
jjg@1969 | 107 | "@Target(ElementType.TYPE_USE)" + |
jjg@1969 | 108 | "@interface TD {}"; |
jjg@1969 | 109 | |
jjg@1986 | 110 | private static final String NL = System.getProperty("line.separator"); |
jjg@1969 | 111 | |
jjg@1969 | 112 | private void runField(String code) throws IOException { |
jjg@1969 | 113 | String src = prefix + |
jjg@1969 | 114 | code + "; }" + |
jjg@1969 | 115 | postfix; |
jjg@1969 | 116 | |
jjg@1969 | 117 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, |
jjg@1969 | 118 | null, Arrays.asList(new MyFileObject(src))); |
jjg@1969 | 119 | |
jjg@1969 | 120 | for (CompilationUnitTree cut : ct.parse()) { |
jjg@1969 | 121 | JCTree.JCVariableDecl var = |
jjg@1969 | 122 | (JCTree.JCVariableDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0); |
jjg@1986 | 123 | checkMatch(code, var); |
jjg@1969 | 124 | } |
jjg@1969 | 125 | } |
jjg@1969 | 126 | |
jjg@1969 | 127 | private void runMethod(String code) throws IOException { |
jjg@1969 | 128 | String src = prefix + |
jjg@1969 | 129 | code + "}" + |
jjg@1969 | 130 | postfix; |
jjg@1969 | 131 | |
jjg@1969 | 132 | JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, |
jjg@1969 | 133 | null, Arrays.asList(new MyFileObject(src))); |
jjg@1969 | 134 | |
jjg@1969 | 135 | |
jjg@1969 | 136 | for (CompilationUnitTree cut : ct.parse()) { |
jjg@1986 | 137 | JCTree.JCMethodDecl meth = |
jjg@1969 | 138 | (JCTree.JCMethodDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0); |
jjg@1986 | 139 | checkMatch(code, meth); |
jjg@1986 | 140 | } |
jjg@1986 | 141 | } |
jjg@1969 | 142 | |
jjg@1986 | 143 | void checkMatch(String code, JCTree tree) { |
jjg@1986 | 144 | String expect = code.replace("\n", NL); |
jjg@1986 | 145 | String found = tree.toString(); |
jjg@1986 | 146 | if (!expect.equals(found)) { |
jjg@1986 | 147 | mismatches.add("Expected: " + expect + NL + |
jjg@1986 | 148 | "Obtained: " + found); |
jjg@1986 | 149 | } else { |
jjg@1986 | 150 | matches.add("Passed: " + expect); |
jjg@1969 | 151 | } |
jjg@1969 | 152 | } |
jjg@1969 | 153 | } |
jjg@1969 | 154 | |
jjg@1969 | 155 | |
jjg@1969 | 156 | class MyFileObject extends SimpleJavaFileObject { |
jjg@1969 | 157 | |
jjg@1969 | 158 | private String text; |
jjg@1969 | 159 | |
jjg@1969 | 160 | public MyFileObject(String text) { |
jjg@1969 | 161 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
jjg@1969 | 162 | this.text = text; |
jjg@1969 | 163 | } |
jjg@1969 | 164 | |
jjg@1969 | 165 | @Override |
jjg@1969 | 166 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
jjg@1969 | 167 | return text; |
jjg@1969 | 168 | } |
jjg@1969 | 169 | } |