Wed, 13 Jan 2010 17:39:44 -0800
6472751: SourcePositions.getStartPos returns incorrect value for enum constants
6567414: javac compiler reports no source file or line on enum constant declaration error
Reviewed-by: darcy
1.1 --- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Mon Jan 11 16:18:05 2010 -0800 1.2 +++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Wed Jan 13 17:39:44 2010 -0800 1.3 @@ -2613,12 +2613,12 @@ 1.4 body = toP(F.at(identPos).AnonymousClassDef(mods1, defs)); 1.5 } 1.6 if (args.isEmpty() && body == null) 1.7 - createPos = Position.NOPOS; 1.8 - JCIdent ident = F.at(Position.NOPOS).Ident(enumName); 1.9 + createPos = identPos; 1.10 + JCIdent ident = F.at(identPos).Ident(enumName); 1.11 JCNewClass create = F.at(createPos).NewClass(null, typeArgs, ident, args, body); 1.12 - if (createPos != Position.NOPOS) 1.13 + if (createPos != identPos) 1.14 storeEnd(create, S.prevEndPos()); 1.15 - ident = F.at(Position.NOPOS).Ident(enumName); 1.16 + ident = F.at(identPos).Ident(enumName); 1.17 JCTree result = toP(F.at(pos).VarDef(mods, name, ident, create)); 1.18 attach(result, dc); 1.19 return result;
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/T6472751.java Wed Jan 13 17:39:44 2010 -0800 2.3 @@ -0,0 +1,81 @@ 2.4 +/* 2.5 + * Copyright 2006-2010 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 2.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 2.24 + * have any questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 6472751 2.30 + * @summary SourcePositions.getStartPos returns incorrect value for enum constants 2.31 + * @author Peter Ahe 2.32 + */ 2.33 + 2.34 +import com.sun.source.tree.CompilationUnitTree; 2.35 +import com.sun.source.tree.Tree; 2.36 +import com.sun.source.tree.Tree.Kind; 2.37 +import com.sun.source.util.JavacTask; 2.38 +import com.sun.source.util.SourcePositions; 2.39 +import com.sun.source.util.TreeScanner; 2.40 +import com.sun.source.util.Trees; 2.41 +import com.sun.tools.javac.util.List; 2.42 +import java.io.IOException; 2.43 +import java.net.URI; 2.44 +import javax.tools.JavaCompiler; 2.45 +import javax.tools.JavaFileObject; 2.46 +import javax.tools.SimpleJavaFileObject; 2.47 +import javax.tools.ToolProvider; 2.48 + 2.49 +public class T6472751 { 2.50 + static class MyFileObject extends SimpleJavaFileObject { 2.51 + public MyFileObject() { 2.52 + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 2.53 + } 2.54 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 2.55 + return "public enum Test { ABC, DEF; }"; 2.56 + } 2.57 + } 2.58 + static Trees trees; 2.59 + static SourcePositions positions; 2.60 + public static void main(String[] args) throws IOException { 2.61 + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 2.62 + JavacTask task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject())); 2.63 + trees = Trees.instance(task); 2.64 + positions = trees.getSourcePositions(); 2.65 + Iterable<? extends CompilationUnitTree> asts = task.parse(); 2.66 + for (CompilationUnitTree ast : asts) { 2.67 + new MyVisitor().scan(ast, null); 2.68 + } 2.69 + } 2.70 + 2.71 + static class MyVisitor extends TreeScanner<Void,Void> { 2.72 + @Override 2.73 + public Void scan(Tree node, Void ignored) { 2.74 + if (node == null) 2.75 + return null; 2.76 + Kind k = node.getKind(); 2.77 + long pos = positions.getStartPosition(null,node); 2.78 + System.out.format("%s: %s%n", k, pos); 2.79 + if (k != Kind.MODIFIERS && pos < 0) 2.80 + throw new Error("unexpected position found"); 2.81 + return super.scan(node, ignored); 2.82 + } 2.83 + } 2.84 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/T6567414.java Wed Jan 13 17:39:44 2010 -0800 3.3 @@ -0,0 +1,11 @@ 3.4 +/* 3.5 + * @test /nodynamiccopyright/ 3.6 + * @bug 6567414 3.7 + * @summary javac compiler reports no source file or line on enum constant declaration error 3.8 + * @compile/fail/ref=T6567414.out -XDrawDiagnostics T6567414.java 3.9 + */ 3.10 +enum Test { 3.11 + FOO; 3.12 + Test() throws Exception {} 3.13 +} 3.14 +
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/T6567414.out Wed Jan 13 17:39:44 2010 -0800 4.3 @@ -0,0 +1,2 @@ 4.4 +T6567414.java:8:3: compiler.err.unreported.exception.need.to.catch.or.throw: java.lang.Exception 4.5 +1 error