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