Tue, 11 Jan 2011 08:05:28 -0800
6993305: starting position of a method without modifiers and with type parameters is incorrect
Reviewed-by: mcimadamore
src/share/classes/com/sun/tools/javac/parser/JavacParser.java | file | annotate | diff | comparison | revisions | |
test/tools/javac/tree/T6993305.java | file | annotate | diff | comparison | revisions |
1.1 --- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Mon Jan 10 15:08:31 2011 -0800 1.2 +++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Tue Jan 11 08:05:28 2011 -0800 1.3 @@ -2569,6 +2569,12 @@ 1.4 } else { 1.5 pos = S.pos(); 1.6 List<JCTypeParameter> typarams = typeParametersOpt(); 1.7 + // if there are type parameters but no modifiers, save the start 1.8 + // position of the method in the modifiers. 1.9 + if (typarams.nonEmpty() && mods.pos == Position.NOPOS) { 1.10 + mods.pos = pos; 1.11 + storeEnd(mods, pos); 1.12 + } 1.13 Name name = S.name(); 1.14 pos = S.pos(); 1.15 JCExpression type;
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/tree/T6993305.java Tue Jan 11 08:05:28 2011 -0800 2.3 @@ -0,0 +1,124 @@ 2.4 +/* 2.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 6993305 2.30 + * @summary starting position of a method without modifiers and with type parameters is incorrect 2.31 + */ 2.32 + 2.33 +import java.io.File; 2.34 +import javax.tools.JavaFileObject; 2.35 +import javax.tools.StandardJavaFileManager; 2.36 + 2.37 +import com.sun.source.tree.CompilationUnitTree; 2.38 +import com.sun.source.tree.MethodTree; 2.39 +import com.sun.source.util.JavacTask; 2.40 +import com.sun.source.util.SourcePositions; 2.41 +import com.sun.source.util.TreeScanner; 2.42 +import com.sun.source.util.Trees; 2.43 +import com.sun.tools.javac.api.JavacTool; 2.44 +import java.io.IOException; 2.45 + 2.46 +/* 2.47 + * Test verifies the starting position of all methods by computing the start position 2.48 + * of each method as the first non-white character on the first line containing 2.49 + * (" " + methodName + "("), and then comparing this value against the reported 2.50 + * value in the SourcePositions table. 2.51 + */ 2.52 +public class T6993305 { 2.53 + <T> void test1(T t) { } // this is the primary case to be tested 2.54 + public <T> void test2(T t) { } 2.55 + @Deprecated <T> void test3(T t) { } 2.56 + 2.57 + public static void main(String... args) throws Exception { 2.58 + new T6993305().run(); 2.59 + } 2.60 + 2.61 + void run() throws Exception { 2.62 + File testSrc = new File(System.getProperty("test.src")); 2.63 + 2.64 + JavacTool tool = JavacTool.create(); 2.65 + StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); 2.66 + 2.67 + File f = new File(testSrc, T6993305.class.getSimpleName() + ".java"); 2.68 + Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjects(f); 2.69 + JavacTask task = tool.getTask(null, fm, null, null, null, fos); 2.70 + Iterable<? extends CompilationUnitTree> cus = task.parse(); 2.71 + 2.72 + TestScanner s = new TestScanner(); 2.73 + s.scan(cus, task); 2.74 + 2.75 + if (errors > 0) 2.76 + throw new Exception(errors + " errors occurred"); 2.77 + } 2.78 + 2.79 + void error(String msg) { 2.80 + System.err.println("Error: " + msg); 2.81 + errors++; 2.82 + } 2.83 + 2.84 + int errors; 2.85 + 2.86 + class TestScanner extends TreeScanner<Void, JavacTask> { 2.87 + CompilationUnitTree cu; 2.88 + SourcePositions sourcePositions; 2.89 + String source; 2.90 + 2.91 + void show(String label, int pos) { 2.92 + System.err.println(label + ": " + 2.93 + source.substring(pos, Math.min(source.length(), pos + 10))); 2.94 + } 2.95 + 2.96 + @Override public Void visitCompilationUnit(CompilationUnitTree tree, JavacTask task) { 2.97 + cu = tree; 2.98 + Trees trees = Trees.instance(task); 2.99 + sourcePositions = trees.getSourcePositions(); 2.100 + try { 2.101 + source = String.valueOf(tree.getSourceFile().getCharContent(true)); 2.102 + } catch (IOException e) { 2.103 + throw new Error(e); 2.104 + } 2.105 + return super.visitCompilationUnit(tree, task); 2.106 + } 2.107 + 2.108 + // this is the core of the test 2.109 + @Override public Void visitMethod(MethodTree tree, JavacTask task) { 2.110 + String name = String.valueOf(tree.getName()); 2.111 + int pos = source.indexOf(" " + name + "("); 2.112 + while (source.charAt(pos - 1) != '\n') pos--; 2.113 + while (source.charAt(pos) == ' ') pos++; 2.114 + int expectedStart = pos; 2.115 + int reportedStart = (int) sourcePositions.getStartPosition(cu, tree); 2.116 + System.err.println("Method " + name 2.117 + + " expectedStart:" + expectedStart 2.118 + + " reportedStart:" + reportedStart); 2.119 + if (expectedStart != reportedStart) { 2.120 + error("Unexpected value for " + name); 2.121 + show("expected", expectedStart); 2.122 + show("reported", reportedStart); 2.123 + } 2.124 + return super.visitMethod(tree, task); 2.125 + } 2.126 + } 2.127 +}