Sat, 22 Dec 2018 13:10:38 -0800
Added tag jdk8u202-b25 for changeset 15d8c732039b
mcimadamore@1113 | 1 | /* |
jjg@1280 | 2 | * Copyright (c) 2011, 2012, 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.*; |
jjg@1280 | 32 | import com.sun.tools.javac.tree.DocCommentTable; |
mcimadamore@1113 | 33 | import com.sun.tools.javac.tree.JCTree; |
mcimadamore@1113 | 34 | |
mcimadamore@1113 | 35 | import java.net.URI; |
mcimadamore@1113 | 36 | import java.util.*; |
mcimadamore@1113 | 37 | import javax.tools.*; |
mcimadamore@1113 | 38 | |
mcimadamore@1113 | 39 | |
mcimadamore@1113 | 40 | public class DocCommentToplevelTest { |
mcimadamore@1113 | 41 | |
mcimadamore@1113 | 42 | enum PackageKind { |
mcimadamore@1113 | 43 | HAS_PKG("package pkg;"), |
mcimadamore@1113 | 44 | NO_PKG(""); |
mcimadamore@1113 | 45 | |
mcimadamore@1113 | 46 | String pkgStr; |
mcimadamore@1113 | 47 | |
mcimadamore@1113 | 48 | PackageKind(String pkgStr) { |
mcimadamore@1113 | 49 | this.pkgStr = pkgStr; |
mcimadamore@1113 | 50 | } |
mcimadamore@1113 | 51 | } |
mcimadamore@1113 | 52 | |
mcimadamore@1113 | 53 | enum ImportKind { |
mcimadamore@1113 | 54 | ZERO(""), |
mcimadamore@1113 | 55 | ONE("import java.lang.*;"), |
mcimadamore@1113 | 56 | TWO("import java.lang.*; import java.util.*;"); |
mcimadamore@1113 | 57 | |
mcimadamore@1113 | 58 | String importStr; |
mcimadamore@1113 | 59 | |
mcimadamore@1113 | 60 | ImportKind(String importStr) { |
mcimadamore@1113 | 61 | this.importStr = importStr; |
mcimadamore@1113 | 62 | } |
mcimadamore@1113 | 63 | } |
mcimadamore@1113 | 64 | |
mcimadamore@1113 | 65 | enum ModifierKind { |
mcimadamore@1113 | 66 | DEFAULT(""), |
mcimadamore@1113 | 67 | PUBLIC("public"); |
mcimadamore@1113 | 68 | |
mcimadamore@1113 | 69 | String modStr; |
mcimadamore@1113 | 70 | |
mcimadamore@1113 | 71 | ModifierKind(String modStr) { |
mcimadamore@1113 | 72 | this.modStr = modStr; |
mcimadamore@1113 | 73 | } |
mcimadamore@1113 | 74 | } |
mcimadamore@1113 | 75 | |
mcimadamore@1113 | 76 | enum ToplevelDocKind { |
mcimadamore@1113 | 77 | HAS_DOC("/** Toplevel! */"), |
mcimadamore@1113 | 78 | NO_DOC(""); |
mcimadamore@1113 | 79 | |
mcimadamore@1113 | 80 | String docStr; |
mcimadamore@1113 | 81 | |
mcimadamore@1113 | 82 | ToplevelDocKind(String docStr) { |
mcimadamore@1113 | 83 | this.docStr = docStr; |
mcimadamore@1113 | 84 | } |
mcimadamore@1113 | 85 | } |
mcimadamore@1113 | 86 | |
mcimadamore@1113 | 87 | static int errors; |
mcimadamore@1113 | 88 | static int checks; |
mcimadamore@1113 | 89 | |
mcimadamore@1113 | 90 | public static void main(String... args) throws Exception { |
mcimadamore@1113 | 91 | //create default shared JavaCompiler - reused across multiple compilations |
mcimadamore@1113 | 92 | JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
mcimadamore@1113 | 93 | StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null); |
mcimadamore@1113 | 94 | |
mcimadamore@1113 | 95 | for (PackageKind pk : PackageKind.values()) { |
mcimadamore@1113 | 96 | for (ImportKind ik : ImportKind.values()) { |
mcimadamore@1113 | 97 | for (ModifierKind mk1 : ModifierKind.values()) { |
mcimadamore@1113 | 98 | for (ModifierKind mk2 : ModifierKind.values()) { |
mcimadamore@1113 | 99 | for (ToplevelDocKind tdk : ToplevelDocKind.values()) { |
mcimadamore@1113 | 100 | new DocCommentToplevelTest(pk, ik, mk1, mk2, tdk).run(comp, fm); |
mcimadamore@1113 | 101 | } |
mcimadamore@1113 | 102 | } |
mcimadamore@1113 | 103 | } |
mcimadamore@1113 | 104 | } |
mcimadamore@1113 | 105 | } |
mcimadamore@1113 | 106 | |
mcimadamore@1113 | 107 | if (errors > 0) |
mcimadamore@1113 | 108 | throw new AssertionError(errors + " errors found"); |
mcimadamore@1113 | 109 | |
mcimadamore@1113 | 110 | System.out.println(checks + " checks were made"); |
mcimadamore@1113 | 111 | } |
mcimadamore@1113 | 112 | |
mcimadamore@1113 | 113 | PackageKind pk; |
mcimadamore@1113 | 114 | ImportKind ik; |
mcimadamore@1113 | 115 | ModifierKind mk1; |
mcimadamore@1113 | 116 | ModifierKind mk2; |
mcimadamore@1113 | 117 | ToplevelDocKind tdk; |
mcimadamore@1113 | 118 | JavaSource source; |
mcimadamore@1113 | 119 | |
mcimadamore@1113 | 120 | DocCommentToplevelTest(PackageKind pk, ImportKind ik, ModifierKind mk1, ModifierKind mk2, ToplevelDocKind tdk) { |
mcimadamore@1113 | 121 | this.pk = pk; |
mcimadamore@1113 | 122 | this.ik = ik; |
mcimadamore@1113 | 123 | this.mk1 = mk1; |
mcimadamore@1113 | 124 | this.mk2 = mk2; |
mcimadamore@1113 | 125 | this.tdk = tdk; |
mcimadamore@1113 | 126 | source = new JavaSource(); |
mcimadamore@1113 | 127 | } |
mcimadamore@1113 | 128 | |
mcimadamore@1113 | 129 | void run(JavaCompiler comp, JavaFileManager fm) throws Exception { |
mcimadamore@1113 | 130 | JavacTask task = (JavacTask)comp.getTask(null, fm, null, Arrays.asList("-printsource"), null, Arrays.asList(source)); |
mcimadamore@1113 | 131 | for (CompilationUnitTree cu: task.parse()) { |
mcimadamore@1113 | 132 | check(cu); |
mcimadamore@1113 | 133 | } |
mcimadamore@1113 | 134 | } |
mcimadamore@1113 | 135 | |
mcimadamore@1113 | 136 | void check(CompilationUnitTree cu) { |
mcimadamore@1113 | 137 | checks++; |
mcimadamore@1113 | 138 | |
mcimadamore@1113 | 139 | new TreeScanner<ClassTree,Void>() { |
mcimadamore@1113 | 140 | |
jjg@1280 | 141 | DocCommentTable docComments; |
mcimadamore@1113 | 142 | |
mcimadamore@1113 | 143 | @Override |
mcimadamore@1113 | 144 | public ClassTree visitCompilationUnit(CompilationUnitTree node, Void unused) { |
mcimadamore@1113 | 145 | docComments = ((JCTree.JCCompilationUnit)node).docComments; |
mcimadamore@1113 | 146 | boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC && |
mcimadamore@1113 | 147 | (pk != PackageKind.NO_PKG || ik != ImportKind.ZERO); |
jjg@1280 | 148 | boolean foundComment = docComments.hasComment((JCTree) node); |
mcimadamore@1113 | 149 | if (expectedComment != foundComment) { |
jjg@1280 | 150 | error("Unexpected comment " + docComments.getComment((JCTree) node) + " on toplevel"); |
mcimadamore@1113 | 151 | } |
mcimadamore@1113 | 152 | return super.visitCompilationUnit(node, null); |
mcimadamore@1113 | 153 | } |
mcimadamore@1113 | 154 | |
mcimadamore@1113 | 155 | @Override |
mcimadamore@1113 | 156 | public ClassTree visitClass(ClassTree node, Void unused) { |
mcimadamore@1113 | 157 | boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC && |
mcimadamore@1113 | 158 | pk == PackageKind.NO_PKG && ik == ImportKind.ZERO && |
mcimadamore@1113 | 159 | node.getSimpleName().toString().equals("First"); |
jjg@1280 | 160 | boolean foundComment = docComments.hasComment((JCTree) node); |
mcimadamore@1113 | 161 | if (expectedComment != foundComment) { |
jjg@1280 | 162 | error("Unexpected comment " + docComments.getComment((JCTree) node) + " on class " + node.getSimpleName()); |
mcimadamore@1113 | 163 | } |
mcimadamore@1113 | 164 | return super.visitClass(node, unused); |
mcimadamore@1113 | 165 | } |
mcimadamore@1113 | 166 | }.scan(cu, null); |
mcimadamore@1113 | 167 | } |
mcimadamore@1113 | 168 | |
mcimadamore@1113 | 169 | void error(String msg) { |
mcimadamore@1113 | 170 | System.err.println("Error: " + msg); |
mcimadamore@1113 | 171 | System.err.println("Source: " + source.source); |
mcimadamore@1113 | 172 | errors++; |
mcimadamore@1113 | 173 | } |
mcimadamore@1113 | 174 | |
mcimadamore@1113 | 175 | class JavaSource extends SimpleJavaFileObject { |
mcimadamore@1113 | 176 | |
mcimadamore@1113 | 177 | String template = "#D\n#P\n#I\n" + |
mcimadamore@1113 | 178 | "#M1 class First { }\n" + |
mcimadamore@1113 | 179 | "#M2 class Second { }\n"; |
mcimadamore@1113 | 180 | |
mcimadamore@1113 | 181 | String source; |
mcimadamore@1113 | 182 | |
mcimadamore@1113 | 183 | public JavaSource() { |
mcimadamore@1113 | 184 | super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
mcimadamore@1113 | 185 | source = template.replace("#P", pk.pkgStr) |
mcimadamore@1113 | 186 | .replace("#I", ik.importStr) |
mcimadamore@1113 | 187 | .replace("#M1", mk1.modStr) |
mcimadamore@1113 | 188 | .replace("#M2", mk2.modStr) |
mcimadamore@1113 | 189 | .replace("#D", tdk.docStr); |
mcimadamore@1113 | 190 | } |
mcimadamore@1113 | 191 | |
mcimadamore@1113 | 192 | @Override |
mcimadamore@1113 | 193 | public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
mcimadamore@1113 | 194 | return source; |
mcimadamore@1113 | 195 | } |
mcimadamore@1113 | 196 | } |
mcimadamore@1113 | 197 | } |