test/tools/javac/tree/DocCommentToplevelTest.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial