test/tools/javac/tree/DocCommentToplevelTest.java

Tue, 08 Nov 2011 11:51:05 -0800

author
jjg
date
Tue, 08 Nov 2011 11:51:05 -0800
changeset 1127
ca49d50318dc
parent 1113
d346ab55031b
child 1280
5c0b3faeb0b0
permissions
-rw-r--r--

6921494: provide way to print javac tree tag values
Reviewed-by: jjg, mcimadamore
Contributed-by: vicenterz@yahoo.es

mcimadamore@1113 1 /*
mcimadamore@1113 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
mcimadamore@1113 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@1113 4 *
mcimadamore@1113 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@1113 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@1113 7 * published by the Free Software Foundation.
mcimadamore@1113 8 *
mcimadamore@1113 9 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@1113 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@1113 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@1113 12 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@1113 13 * accompanied this code).
mcimadamore@1113 14 *
mcimadamore@1113 15 * You should have received a copy of the GNU General Public License version
mcimadamore@1113 16 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@1113 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@1113 18 *
mcimadamore@1113 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mcimadamore@1113 20 * or visit www.oracle.com if you need additional information or have any
mcimadamore@1113 21 * questions.
mcimadamore@1113 22 */
mcimadamore@1113 23
mcimadamore@1113 24 /*
mcimadamore@1113 25 * @test
mcimadamore@1113 26 * @bug 7096014
mcimadamore@1113 27 * @summary Javac tokens should retain state
mcimadamore@1113 28 */
mcimadamore@1113 29
mcimadamore@1113 30 import com.sun.source.tree.*;
mcimadamore@1113 31 import com.sun.source.util.*;
mcimadamore@1113 32 import com.sun.tools.javac.tree.JCTree;
mcimadamore@1113 33
mcimadamore@1113 34 import java.net.URI;
mcimadamore@1113 35 import java.util.*;
mcimadamore@1113 36 import javax.tools.*;
mcimadamore@1113 37
mcimadamore@1113 38
mcimadamore@1113 39 public class DocCommentToplevelTest {
mcimadamore@1113 40
mcimadamore@1113 41 enum PackageKind {
mcimadamore@1113 42 HAS_PKG("package pkg;"),
mcimadamore@1113 43 NO_PKG("");
mcimadamore@1113 44
mcimadamore@1113 45 String pkgStr;
mcimadamore@1113 46
mcimadamore@1113 47 PackageKind(String pkgStr) {
mcimadamore@1113 48 this.pkgStr = pkgStr;
mcimadamore@1113 49 }
mcimadamore@1113 50 }
mcimadamore@1113 51
mcimadamore@1113 52 enum ImportKind {
mcimadamore@1113 53 ZERO(""),
mcimadamore@1113 54 ONE("import java.lang.*;"),
mcimadamore@1113 55 TWO("import java.lang.*; import java.util.*;");
mcimadamore@1113 56
mcimadamore@1113 57 String importStr;
mcimadamore@1113 58
mcimadamore@1113 59 ImportKind(String importStr) {
mcimadamore@1113 60 this.importStr = importStr;
mcimadamore@1113 61 }
mcimadamore@1113 62 }
mcimadamore@1113 63
mcimadamore@1113 64 enum ModifierKind {
mcimadamore@1113 65 DEFAULT(""),
mcimadamore@1113 66 PUBLIC("public");
mcimadamore@1113 67
mcimadamore@1113 68 String modStr;
mcimadamore@1113 69
mcimadamore@1113 70 ModifierKind(String modStr) {
mcimadamore@1113 71 this.modStr = modStr;
mcimadamore@1113 72 }
mcimadamore@1113 73 }
mcimadamore@1113 74
mcimadamore@1113 75 enum ToplevelDocKind {
mcimadamore@1113 76 HAS_DOC("/** Toplevel! */"),
mcimadamore@1113 77 NO_DOC("");
mcimadamore@1113 78
mcimadamore@1113 79 String docStr;
mcimadamore@1113 80
mcimadamore@1113 81 ToplevelDocKind(String docStr) {
mcimadamore@1113 82 this.docStr = docStr;
mcimadamore@1113 83 }
mcimadamore@1113 84 }
mcimadamore@1113 85
mcimadamore@1113 86 static int errors;
mcimadamore@1113 87 static int checks;
mcimadamore@1113 88
mcimadamore@1113 89 public static void main(String... args) throws Exception {
mcimadamore@1113 90 //create default shared JavaCompiler - reused across multiple compilations
mcimadamore@1113 91 JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
mcimadamore@1113 92 StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
mcimadamore@1113 93
mcimadamore@1113 94 for (PackageKind pk : PackageKind.values()) {
mcimadamore@1113 95 for (ImportKind ik : ImportKind.values()) {
mcimadamore@1113 96 for (ModifierKind mk1 : ModifierKind.values()) {
mcimadamore@1113 97 for (ModifierKind mk2 : ModifierKind.values()) {
mcimadamore@1113 98 for (ToplevelDocKind tdk : ToplevelDocKind.values()) {
mcimadamore@1113 99 new DocCommentToplevelTest(pk, ik, mk1, mk2, tdk).run(comp, fm);
mcimadamore@1113 100 }
mcimadamore@1113 101 }
mcimadamore@1113 102 }
mcimadamore@1113 103 }
mcimadamore@1113 104 }
mcimadamore@1113 105
mcimadamore@1113 106 if (errors > 0)
mcimadamore@1113 107 throw new AssertionError(errors + " errors found");
mcimadamore@1113 108
mcimadamore@1113 109 System.out.println(checks + " checks were made");
mcimadamore@1113 110 }
mcimadamore@1113 111
mcimadamore@1113 112 PackageKind pk;
mcimadamore@1113 113 ImportKind ik;
mcimadamore@1113 114 ModifierKind mk1;
mcimadamore@1113 115 ModifierKind mk2;
mcimadamore@1113 116 ToplevelDocKind tdk;
mcimadamore@1113 117 JavaSource source;
mcimadamore@1113 118
mcimadamore@1113 119 DocCommentToplevelTest(PackageKind pk, ImportKind ik, ModifierKind mk1, ModifierKind mk2, ToplevelDocKind tdk) {
mcimadamore@1113 120 this.pk = pk;
mcimadamore@1113 121 this.ik = ik;
mcimadamore@1113 122 this.mk1 = mk1;
mcimadamore@1113 123 this.mk2 = mk2;
mcimadamore@1113 124 this.tdk = tdk;
mcimadamore@1113 125 source = new JavaSource();
mcimadamore@1113 126 }
mcimadamore@1113 127
mcimadamore@1113 128 void run(JavaCompiler comp, JavaFileManager fm) throws Exception {
mcimadamore@1113 129 JavacTask task = (JavacTask)comp.getTask(null, fm, null, Arrays.asList("-printsource"), null, Arrays.asList(source));
mcimadamore@1113 130 for (CompilationUnitTree cu: task.parse()) {
mcimadamore@1113 131 check(cu);
mcimadamore@1113 132 }
mcimadamore@1113 133 }
mcimadamore@1113 134
mcimadamore@1113 135 void check(CompilationUnitTree cu) {
mcimadamore@1113 136 checks++;
mcimadamore@1113 137
mcimadamore@1113 138 new TreeScanner<ClassTree,Void>() {
mcimadamore@1113 139
mcimadamore@1113 140 Map<JCTree, String> docComments;
mcimadamore@1113 141
mcimadamore@1113 142 @Override
mcimadamore@1113 143 public ClassTree visitCompilationUnit(CompilationUnitTree node, Void unused) {
mcimadamore@1113 144 docComments = ((JCTree.JCCompilationUnit)node).docComments;
mcimadamore@1113 145 boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC &&
mcimadamore@1113 146 (pk != PackageKind.NO_PKG || ik != ImportKind.ZERO);
mcimadamore@1113 147 boolean foundComment = docComments.get(node) != null;
mcimadamore@1113 148 if (expectedComment != foundComment) {
mcimadamore@1113 149 error("Unexpected comment " + docComments.get(node) + " on toplevel");
mcimadamore@1113 150 }
mcimadamore@1113 151 return super.visitCompilationUnit(node, null);
mcimadamore@1113 152 }
mcimadamore@1113 153
mcimadamore@1113 154 @Override
mcimadamore@1113 155 public ClassTree visitClass(ClassTree node, Void unused) {
mcimadamore@1113 156 boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC &&
mcimadamore@1113 157 pk == PackageKind.NO_PKG && ik == ImportKind.ZERO &&
mcimadamore@1113 158 node.getSimpleName().toString().equals("First");
mcimadamore@1113 159 boolean foundComment = docComments.get(node) != null;
mcimadamore@1113 160 if (expectedComment != foundComment) {
mcimadamore@1113 161 error("Unexpected comment " + docComments.get(node) + " on class " + node.getSimpleName());
mcimadamore@1113 162 }
mcimadamore@1113 163 return super.visitClass(node, unused);
mcimadamore@1113 164 }
mcimadamore@1113 165 }.scan(cu, null);
mcimadamore@1113 166 }
mcimadamore@1113 167
mcimadamore@1113 168 void error(String msg) {
mcimadamore@1113 169 System.err.println("Error: " + msg);
mcimadamore@1113 170 System.err.println("Source: " + source.source);
mcimadamore@1113 171 errors++;
mcimadamore@1113 172 }
mcimadamore@1113 173
mcimadamore@1113 174 class JavaSource extends SimpleJavaFileObject {
mcimadamore@1113 175
mcimadamore@1113 176 String template = "#D\n#P\n#I\n" +
mcimadamore@1113 177 "#M1 class First { }\n" +
mcimadamore@1113 178 "#M2 class Second { }\n";
mcimadamore@1113 179
mcimadamore@1113 180 String source;
mcimadamore@1113 181
mcimadamore@1113 182 public JavaSource() {
mcimadamore@1113 183 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
mcimadamore@1113 184 source = template.replace("#P", pk.pkgStr)
mcimadamore@1113 185 .replace("#I", ik.importStr)
mcimadamore@1113 186 .replace("#M1", mk1.modStr)
mcimadamore@1113 187 .replace("#M2", mk2.modStr)
mcimadamore@1113 188 .replace("#D", tdk.docStr);
mcimadamore@1113 189 }
mcimadamore@1113 190
mcimadamore@1113 191 @Override
mcimadamore@1113 192 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
mcimadamore@1113 193 return source;
mcimadamore@1113 194 }
mcimadamore@1113 195 }
mcimadamore@1113 196 }

mercurial