aoqi@0: /* aoqi@0: * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 6993305 aoqi@0: * @summary starting position of a method without modifiers and with type parameters is incorrect aoqi@0: */ aoqi@0: aoqi@0: import java.io.File; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.StandardJavaFileManager; aoqi@0: aoqi@0: import com.sun.source.tree.CompilationUnitTree; aoqi@0: import com.sun.source.tree.MethodTree; aoqi@0: import com.sun.source.util.JavacTask; aoqi@0: import com.sun.source.util.SourcePositions; aoqi@0: import com.sun.source.util.TreeScanner; aoqi@0: import com.sun.source.util.Trees; aoqi@0: import com.sun.tools.javac.api.JavacTool; aoqi@0: import java.io.IOException; aoqi@0: aoqi@0: /* aoqi@0: * Test verifies the starting position of all methods by computing the start position aoqi@0: * of each method as the first non-white character on the first line containing aoqi@0: * (" " + methodName + "("), and then comparing this value against the reported aoqi@0: * value in the SourcePositions table. aoqi@0: */ aoqi@0: public class T6993305 { aoqi@0: void test1(T t) { } // this is the primary case to be tested aoqi@0: public void test2(T t) { } aoqi@0: @Deprecated void test3(T t) { } aoqi@0: aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new T6993305().run(); aoqi@0: } aoqi@0: aoqi@0: void run() throws Exception { aoqi@0: File testSrc = new File(System.getProperty("test.src")); aoqi@0: aoqi@0: JavacTool tool = JavacTool.create(); aoqi@0: StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); aoqi@0: aoqi@0: File f = new File(testSrc, T6993305.class.getSimpleName() + ".java"); aoqi@0: Iterable fos = fm.getJavaFileObjects(f); aoqi@0: JavacTask task = tool.getTask(null, fm, null, null, null, fos); aoqi@0: Iterable cus = task.parse(); aoqi@0: aoqi@0: TestScanner s = new TestScanner(); aoqi@0: s.scan(cus, task); aoqi@0: aoqi@0: if (errors > 0) aoqi@0: throw new Exception(errors + " errors occurred"); aoqi@0: } aoqi@0: aoqi@0: void error(String msg) { aoqi@0: System.err.println("Error: " + msg); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: int errors; aoqi@0: aoqi@0: class TestScanner extends TreeScanner { aoqi@0: CompilationUnitTree cu; aoqi@0: SourcePositions sourcePositions; aoqi@0: String source; aoqi@0: aoqi@0: void show(String label, int pos) { aoqi@0: System.err.println(label + ": " + aoqi@0: source.substring(pos, Math.min(source.length(), pos + 10))); aoqi@0: } aoqi@0: aoqi@0: @Override public Void visitCompilationUnit(CompilationUnitTree tree, JavacTask task) { aoqi@0: cu = tree; aoqi@0: Trees trees = Trees.instance(task); aoqi@0: sourcePositions = trees.getSourcePositions(); aoqi@0: try { aoqi@0: source = String.valueOf(tree.getSourceFile().getCharContent(true)); aoqi@0: } catch (IOException e) { aoqi@0: throw new Error(e); aoqi@0: } aoqi@0: return super.visitCompilationUnit(tree, task); aoqi@0: } aoqi@0: aoqi@0: // this is the core of the test aoqi@0: @Override public Void visitMethod(MethodTree tree, JavacTask task) { aoqi@0: String name = String.valueOf(tree.getName()); aoqi@0: int pos = source.indexOf(" " + name + "("); aoqi@0: while (source.charAt(pos - 1) != '\n') pos--; aoqi@0: while (source.charAt(pos) == ' ') pos++; aoqi@0: int expectedStart = pos; aoqi@0: int reportedStart = (int) sourcePositions.getStartPosition(cu, tree); aoqi@0: System.err.println("Method " + name aoqi@0: + " expectedStart:" + expectedStart aoqi@0: + " reportedStart:" + reportedStart); aoqi@0: if (expectedStart != reportedStart) { aoqi@0: error("Unexpected value for " + name); aoqi@0: show("expected", expectedStart); aoqi@0: show("reported", reportedStart); aoqi@0: } aoqi@0: return super.visitMethod(tree, task); aoqi@0: } aoqi@0: } aoqi@0: }