langtools/test/tools/javac/T6472751.java
changeset 4704 5206047418c2
child 5520 86e4b9a9da40
equal deleted inserted replaced
4703:800e1750ff18 4704:5206047418c2
       
     1 /*
       
     2  * Copyright 2006-2010 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 6472751
       
    27  * @summary SourcePositions.getStartPos returns incorrect value for enum constants
       
    28  * @author Peter Ahe
       
    29  */
       
    30 
       
    31 import com.sun.source.tree.CompilationUnitTree;
       
    32 import com.sun.source.tree.Tree;
       
    33 import com.sun.source.tree.Tree.Kind;
       
    34 import com.sun.source.util.JavacTask;
       
    35 import com.sun.source.util.SourcePositions;
       
    36 import com.sun.source.util.TreeScanner;
       
    37 import com.sun.source.util.Trees;
       
    38 import com.sun.tools.javac.util.List;
       
    39 import java.io.IOException;
       
    40 import java.net.URI;
       
    41 import javax.tools.JavaCompiler;
       
    42 import javax.tools.JavaFileObject;
       
    43 import javax.tools.SimpleJavaFileObject;
       
    44 import javax.tools.ToolProvider;
       
    45 
       
    46 public class T6472751 {
       
    47     static class MyFileObject extends SimpleJavaFileObject {
       
    48         public MyFileObject() {
       
    49             super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
       
    50         }
       
    51         public CharSequence getCharContent(boolean ignoreEncodingErrors) {
       
    52             return "public enum Test { ABC, DEF; }";
       
    53         }
       
    54     }
       
    55     static Trees trees;
       
    56     static SourcePositions positions;
       
    57     public static void main(String[] args) throws IOException {
       
    58         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
       
    59         JavacTask task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
       
    60         trees = Trees.instance(task);
       
    61         positions = trees.getSourcePositions();
       
    62         Iterable<? extends CompilationUnitTree> asts = task.parse();
       
    63         for (CompilationUnitTree ast : asts) {
       
    64             new MyVisitor().scan(ast, null);
       
    65         }
       
    66     }
       
    67 
       
    68     static class MyVisitor extends TreeScanner<Void,Void> {
       
    69         @Override
       
    70         public Void scan(Tree node, Void ignored) {
       
    71             if (node == null)
       
    72                 return null;
       
    73             Kind k = node.getKind();
       
    74             long pos = positions.getStartPosition(null,node);
       
    75             System.out.format("%s: %s%n", k, pos);
       
    76             if (k != Kind.MODIFIERS && pos < 0)
       
    77                 throw new Error("unexpected position found");
       
    78             return super.scan(node, ignored);
       
    79         }
       
    80     }
       
    81 }