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