test/tools/javac/tree/TypeAnnotationsPretty.java

Wed, 21 Aug 2013 16:13:50 -0700

author
jjg
date
Wed, 21 Aug 2013 16:13:50 -0700
changeset 1969
7de231613e4a
child 1986
b0b25c1f6cbd
permissions
-rw-r--r--

8023515: import type-annotations updates
Reviewed-by: jjg
Contributed-by: wdietl@gmail.com

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@1969 26 * @bug 1234567
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@1969 90 System.err.println(mm + "\n");
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@1969 110
jjg@1969 111 private void runField(String code) throws IOException {
jjg@1969 112 String src = prefix +
jjg@1969 113 code + "; }" +
jjg@1969 114 postfix;
jjg@1969 115
jjg@1969 116 JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
jjg@1969 117 null, Arrays.asList(new MyFileObject(src)));
jjg@1969 118
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@1969 123
jjg@1969 124 if (!code.equals(var.toString())) {
jjg@1969 125 mismatches.add("Expected: " + code +
jjg@1969 126 "\nObtained: " + var.toString());
jjg@1969 127 } else {
jjg@1969 128 matches.add("Passed: " + code);
jjg@1969 129 }
jjg@1969 130 }
jjg@1969 131 }
jjg@1969 132
jjg@1969 133 private void runMethod(String code) throws IOException {
jjg@1969 134 String src = prefix +
jjg@1969 135 code + "}" +
jjg@1969 136 postfix;
jjg@1969 137
jjg@1969 138 JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null,
jjg@1969 139 null, Arrays.asList(new MyFileObject(src)));
jjg@1969 140
jjg@1969 141
jjg@1969 142 for (CompilationUnitTree cut : ct.parse()) {
jjg@1969 143 JCTree.JCMethodDecl var =
jjg@1969 144 (JCTree.JCMethodDecl) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0);
jjg@1969 145
jjg@1969 146 if (!code.equals(var.toString())) {
jjg@1969 147 mismatches.add("Expected: " + code +
jjg@1969 148 "\nObtained: " + var.toString());
jjg@1969 149 } else {
jjg@1969 150 matches.add("Passed: " + code);
jjg@1969 151 }
jjg@1969 152 }
jjg@1969 153 }
jjg@1969 154 }
jjg@1969 155
jjg@1969 156
jjg@1969 157 class MyFileObject extends SimpleJavaFileObject {
jjg@1969 158
jjg@1969 159 private String text;
jjg@1969 160
jjg@1969 161 public MyFileObject(String text) {
jjg@1969 162 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
jjg@1969 163 this.text = text;
jjg@1969 164 }
jjg@1969 165
jjg@1969 166 @Override
jjg@1969 167 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
jjg@1969 168 return text;
jjg@1969 169 }
jjg@1969 170 }

mercurial