test/tools/javac/T6472751.java

Wed, 14 Nov 2018 10:18:25 -0800

author
diazhou
date
Wed, 14 Nov 2018 10:18:25 -0800
changeset 3762
7909abb85562
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u201-b04 for changeset a7f48b9dfb82

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

mercurial