test/tools/javac/tree/TreePosRoundsTest.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 3938
93012e2a5d1d
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 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 6985205 6986246
aoqi@0 27 * @summary access to tree positions and doc comments may be lost across annotation processing rounds
aoqi@0 28 * @build TreePosRoundsTest
aoqi@0 29 * @compile -proc:only -processor TreePosRoundsTest TreePosRoundsTest.java
aoqi@0 30 * @run main TreePosRoundsTest
aoqi@0 31 */
aoqi@0 32
aoqi@0 33 import java.io.*;
aoqi@0 34 import java.util.*;
aoqi@0 35 import javax.annotation.processing.*;
aoqi@0 36 import javax.lang.model.*;
aoqi@0 37 import javax.lang.model.element.*;
aoqi@0 38 import javax.tools.*;
aoqi@0 39
aoqi@0 40 import com.sun.source.tree.*;
aoqi@0 41 import com.sun.source.util.*;
aoqi@0 42 import javax.tools.JavaCompiler.CompilationTask;
aoqi@0 43
aoqi@0 44 // This test is an annotation processor that performs multiple rounds of
aoqi@0 45 // processing, and on each round, it checks that source positions are
aoqi@0 46 // available and correct.
aoqi@0 47 //
aoqi@0 48 // The test can be run directly as a processor from the javac command line
aoqi@0 49 // or via JSR 199 by invoking the main program.
aoqi@0 50
aoqi@0 51 @SupportedAnnotationTypes("*")
aoqi@0 52 public class TreePosRoundsTest extends AbstractProcessor {
aoqi@0 53 public static void main(String... args) throws Exception {
aoqi@0 54 String testSrc = System.getProperty("test.src");
aoqi@0 55 String testClasses = System.getProperty("test.classes");
aoqi@0 56 JavaCompiler c = ToolProvider.getSystemJavaCompiler();
aoqi@0 57 StandardJavaFileManager fm = c.getStandardFileManager(null, null, null);
aoqi@0 58 String thisName = TreePosRoundsTest.class.getName();
aoqi@0 59 File thisFile = new File(testSrc, thisName + ".java");
aoqi@0 60 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(thisFile);
aoqi@0 61 List<String> options = Arrays.asList(
aoqi@0 62 "-proc:only",
aoqi@0 63 "-processor", thisName,
aoqi@0 64 "-processorpath", testClasses);
aoqi@0 65 CompilationTask t = c.getTask(null, fm, null, options, null, files);
aoqi@0 66 boolean ok = t.call();
aoqi@0 67 if (!ok)
aoqi@0 68 throw new Exception("processing failed");
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 Filer filer;
aoqi@0 72 Messager messager;
aoqi@0 73 Trees trees;
aoqi@0 74
aoqi@0 75 @Override
aoqi@0 76 public SourceVersion getSupportedSourceVersion() {
aoqi@0 77 return SourceVersion.latest();
aoqi@0 78 }
aoqi@0 79
aoqi@0 80 @Override
aoqi@0 81 public void init(ProcessingEnvironment pEnv) {
aoqi@0 82 super.init(pEnv);
aoqi@0 83 filer = pEnv.getFiler();
aoqi@0 84 messager = pEnv.getMessager();
aoqi@0 85 trees = Trees.instance(pEnv);
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 int round = 0;
aoqi@0 89
aoqi@0 90 @Override
aoqi@0 91 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
aoqi@0 92 round++;
aoqi@0 93
aoqi@0 94 // Scan trees for elements, verifying source tree positions
aoqi@0 95 for (Element e: roundEnv.getRootElements()) {
aoqi@0 96 try {
aoqi@0 97 TreePath p = trees.getPath(e);
aoqi@0 98 new TestTreeScanner(p.getCompilationUnit(), trees).scan(trees.getPath(e), null);
aoqi@0 99 } catch (IOException ex) {
aoqi@0 100 messager.printMessage(Diagnostic.Kind.ERROR,
aoqi@0 101 "Cannot get source: " + ex, e);
aoqi@0 102 }
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 final int MAXROUNDS = 3;
aoqi@0 106 if (round < MAXROUNDS)
aoqi@0 107 generateSource("Gen" + round);
aoqi@0 108
aoqi@0 109 return true;
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 void generateSource(String name) {
aoqi@0 113 StringBuilder text = new StringBuilder();
aoqi@0 114 text.append("class ").append(name).append("{\n");
aoqi@0 115 text.append(" int one = 1;\n");
aoqi@0 116 text.append(" int two = 2;\n");
aoqi@0 117 text.append(" int three = one + two;\n");
aoqi@0 118 text.append("}\n");
aoqi@0 119
aoqi@0 120 try {
aoqi@0 121 JavaFileObject fo = filer.createSourceFile(name);
aoqi@0 122 Writer out = fo.openWriter();
aoqi@0 123 try {
aoqi@0 124 out.write(text.toString());
aoqi@0 125 } finally {
aoqi@0 126 out.close();
aoqi@0 127 }
aoqi@0 128 } catch (IOException e) {
aoqi@0 129 throw new Error(e);
aoqi@0 130 }
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 class TestTreeScanner extends TreePathScanner<Void,Void> {
aoqi@0 134 TestTreeScanner(CompilationUnitTree unit, Trees trees) throws IOException {
aoqi@0 135 this.unit = unit;
aoqi@0 136 JavaFileObject sf = unit.getSourceFile();
aoqi@0 137 source = sf.getCharContent(true).toString();
aoqi@0 138 sourcePositions = trees.getSourcePositions();
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 @Override
aoqi@0 142 public Void visitVariable(VariableTree tree, Void _) {
aoqi@0 143 check(getCurrentPath());
aoqi@0 144 return super.visitVariable(tree, _);
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 void check(TreePath tp) {
aoqi@0 148 Tree tree = tp.getLeaf();
aoqi@0 149
aoqi@0 150 String expect = tree.toString();
aoqi@0 151 if (tree.getKind() == Tree.Kind.VARIABLE) {
aoqi@0 152 // tree.toString() does not know enough context to add ";",
aoqi@0 153 // so deal with that manually...
aoqi@0 154 Tree.Kind enclKind = tp.getParentPath().getLeaf().getKind();
aoqi@0 155 //System.err.println(" encl: " +enclKind);
aoqi@0 156 if (enclKind == Tree.Kind.CLASS || enclKind == Tree.Kind.BLOCK)
aoqi@0 157 expect += ";";
aoqi@0 158 }
aoqi@0 159 //System.err.println("expect: " + expect);
aoqi@0 160
aoqi@0 161 int start = (int)sourcePositions.getStartPosition(unit, tree);
aoqi@0 162 if (start == Diagnostic.NOPOS) {
aoqi@0 163 messager.printMessage(Diagnostic.Kind.ERROR, "start pos not set for " + trim(tree));
aoqi@0 164 return;
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 int end = (int)sourcePositions.getEndPosition(unit, tree);
aoqi@0 168 if (end == Diagnostic.NOPOS) {
aoqi@0 169 messager.printMessage(Diagnostic.Kind.ERROR, "end pos not set for " + trim(tree));
aoqi@0 170 return;
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 String found = source.substring(start, end);
aoqi@0 174 //System.err.println(" found: " + found);
aoqi@0 175
aoqi@0 176 // allow for long lines, in which case just compare beginning and
aoqi@0 177 // end of the strings
aoqi@0 178 boolean equal;
aoqi@0 179 if (found.contains("\n")) {
aoqi@0 180 String head = found.substring(0, found.indexOf("\n"));
aoqi@0 181 String tail = found.substring(found.lastIndexOf("\n")).trim();
aoqi@0 182 equal = expect.startsWith(head) && expect.endsWith(tail);
aoqi@0 183 } else {
aoqi@0 184 equal = expect.equals(found);
aoqi@0 185 }
aoqi@0 186
aoqi@0 187 if (!equal) {
aoqi@0 188 messager.printMessage(Diagnostic.Kind.ERROR,
aoqi@0 189 "unexpected value found: '" + found + "'; expected: '" + expect + "'");
aoqi@0 190 }
aoqi@0 191 }
aoqi@0 192
aoqi@0 193 String trim(Tree tree) {
aoqi@0 194 final int MAXLEN = 32;
aoqi@0 195 String s = tree.toString().replaceAll("\\s+", " ").trim();
aoqi@0 196 return (s.length() < MAXLEN) ? s : s.substring(0, MAXLEN);
aoqi@0 197
aoqi@0 198 }
aoqi@0 199
aoqi@0 200 CompilationUnitTree unit;
aoqi@0 201 SourcePositions sourcePositions;
aoqi@0 202 String source;
aoqi@0 203 }
aoqi@0 204
aoqi@0 205 }

mercurial