jjg@469: /* ohair@554: * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. jjg@469: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@469: * jjg@469: * This code is free software; you can redistribute it and/or modify it jjg@469: * under the terms of the GNU General Public License version 2 only, as jjg@469: * published by the Free Software Foundation. jjg@469: * jjg@469: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@469: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@469: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@469: * version 2 for more details (a copy is included in the LICENSE file that jjg@469: * accompanied this code). jjg@469: * jjg@469: * You should have received a copy of the GNU General Public License version jjg@469: * 2 along with this work; if not, write to the Free Software Foundation, jjg@469: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@469: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@469: */ jjg@469: jjg@469: /* jjg@469: * @test jjg@469: * @bug 6472751 jjg@469: * @summary SourcePositions.getStartPos returns incorrect value for enum constants jjg@469: * @author Peter Ahe jjg@469: */ jjg@469: jjg@469: import com.sun.source.tree.CompilationUnitTree; jjg@469: import com.sun.source.tree.Tree; jjg@469: import com.sun.source.tree.Tree.Kind; jjg@469: import com.sun.source.util.JavacTask; jjg@469: import com.sun.source.util.SourcePositions; jjg@469: import com.sun.source.util.TreeScanner; jjg@469: import com.sun.source.util.Trees; jjg@469: import com.sun.tools.javac.util.List; jjg@469: import java.io.IOException; jjg@469: import java.net.URI; jjg@469: import javax.tools.JavaCompiler; jjg@469: import javax.tools.JavaFileObject; jjg@469: import javax.tools.SimpleJavaFileObject; jjg@469: import javax.tools.ToolProvider; jjg@469: jjg@469: public class T6472751 { jjg@469: static class MyFileObject extends SimpleJavaFileObject { jjg@469: public MyFileObject() { jjg@469: super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); jjg@469: } jjg@469: public CharSequence getCharContent(boolean ignoreEncodingErrors) { jjg@469: return "public enum Test { ABC, DEF; }"; jjg@469: } jjg@469: } jjg@469: static Trees trees; jjg@469: static SourcePositions positions; jjg@469: public static void main(String[] args) throws IOException { jjg@469: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); jjg@469: JavacTask task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject())); jjg@469: trees = Trees.instance(task); jjg@469: positions = trees.getSourcePositions(); jjg@469: Iterable asts = task.parse(); jjg@469: for (CompilationUnitTree ast : asts) { jjg@469: new MyVisitor().scan(ast, null); jjg@469: } jjg@469: } jjg@469: jjg@469: static class MyVisitor extends TreeScanner { jjg@469: @Override jjg@469: public Void scan(Tree node, Void ignored) { jjg@469: if (node == null) jjg@469: return null; jjg@469: Kind k = node.getKind(); jjg@469: long pos = positions.getStartPosition(null,node); jjg@469: System.out.format("%s: %s%n", k, pos); jjg@469: if (k != Kind.MODIFIERS && pos < 0) jjg@469: throw new Error("unexpected position found"); jjg@469: return super.scan(node, ignored); jjg@469: } jjg@469: } jjg@469: }