1 /* 2 * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6472751 27 * @summary SourcePositions.getStartPos returns incorrect value for enum constants 28 * @author Peter Ahe 29 * @modules jdk.compiler/com.sun.tools.javac.util 30 */ 31 32 import com.sun.source.tree.CompilationUnitTree; 33 import com.sun.source.tree.Tree; 34 import com.sun.source.tree.Tree.Kind; 35 import com.sun.source.util.JavacTask; 36 import com.sun.source.util.SourcePositions; 37 import com.sun.source.util.TreeScanner; 38 import com.sun.source.util.Trees; 39 import com.sun.tools.javac.util.List; 40 import java.io.IOException; 41 import java.net.URI; 42 import javax.tools.JavaCompiler; 43 import javax.tools.JavaFileObject; 44 import javax.tools.SimpleJavaFileObject; 45 import javax.tools.ToolProvider; 46 47 public class T6472751 { 48 static class MyFileObject extends SimpleJavaFileObject { MyFileObject()49 public MyFileObject() { 50 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 51 } getCharContent(boolean ignoreEncodingErrors)52 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 53 return "public enum Test { ABC, DEF; }"; 54 } 55 } 56 static Trees trees; 57 static SourcePositions positions; main(String[] args)58 public static void main(String[] args) throws IOException { 59 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 60 JavacTask task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject())); 61 trees = Trees.instance(task); 62 positions = trees.getSourcePositions(); 63 Iterable<? extends CompilationUnitTree> asts = task.parse(); 64 for (CompilationUnitTree ast : asts) { 65 new MyVisitor().scan(ast, null); 66 } 67 } 68 69 static class MyVisitor extends TreeScanner<Void,Void> { 70 @Override scan(Tree node, Void ignored)71 public Void scan(Tree node, Void ignored) { 72 if (node == null) 73 return null; 74 Kind k = node.getKind(); 75 long pos = positions.getStartPosition(null,node); 76 System.out.format("%s: %s%n", k, pos); 77 if (k != Kind.MODIFIERS && pos < 0) 78 throw new Error("unexpected position found"); 79 return super.scan(node, ignored); 80 } 81 } 82 } 83