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

     1 /*
     2  * Copyright (c) 2006, 2010, 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  */
    24 /*
    25  * @test
    26  * @bug 6472751
    27  * @summary SourcePositions.getStartPos returns incorrect value for enum constants
    28  * @author Peter Ahe
    29  */
    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;
    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     }
    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 }

mercurial