langtools/test/tools/javac/T6654037.java
author hseigel
Wed, 02 Mar 2016 23:48:41 +0000
changeset 36397 c487ced7231c
parent 30730 d3ce7619db2c
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27579
diff changeset
     2
 * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
     4
 *
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
     8
 *
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    13
 * accompanied this code).
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    14
 *
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4870
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4870
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4870
diff changeset
    21
 * questions.
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    22
 */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    23
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    24
/*
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    25
 * @test
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    26
 * @bug 6654037
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    27
 * @summary JCTree.pos may be incorrect for BinaryTrees
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27579
diff changeset
    28
 * @modules jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27579
diff changeset
    29
 *          jdk.compiler/com.sun.tools.javac.tree
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    30
 */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    31
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    32
import com.sun.source.tree.BinaryTree;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    33
import com.sun.source.tree.ClassTree;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    34
import com.sun.source.tree.CompilationUnitTree;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    35
import com.sun.source.tree.MethodTree;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    36
import com.sun.source.tree.VariableTree;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    37
import com.sun.tools.javac.api.JavacTaskImpl;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    38
import com.sun.tools.javac.tree.JCTree;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    39
import java.net.URI;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    40
import java.util.Arrays;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    41
import javax.tools.JavaCompiler;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    42
import javax.tools.JavaFileObject;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    43
import javax.tools.SimpleJavaFileObject;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    44
import javax.tools.ToolProvider;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    45
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    46
public class T6654037 {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    47
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    48
    public static void main(String[] args) throws Exception {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    49
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    50
        assert tool != null;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    51
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    52
        String code = "package test; public class Test {private void test() {Object o = null; boolean b = o != null && o instanceof String;} private Test() {}}";
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    53
27579
d1a63c99cdd5 8049367: Modular Run-Time Images
chegar
parents: 5520
diff changeset
    54
        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, Arrays.asList("-Xjcov"), null, Arrays.asList(new MyFileObject(code)));
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    55
        CompilationUnitTree cut = ct.parse().iterator().next();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    56
        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    57
        MethodTree method = (MethodTree) clazz.getMembers().get(0);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    58
        VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    59
        BinaryTree cond = (BinaryTree) condSt.getInitializer();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    60
        JCTree condJC = (JCTree) cond;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    61
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    62
        if (condJC.pos != 93)
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    63
            throw new IllegalStateException("Unexpected position=" + condJC.pos);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    64
    }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    65
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    66
    static class MyFileObject extends SimpleJavaFileObject {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    67
        private String text;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    68
        public MyFileObject(String text) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    69
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    70
            this.text = text;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    71
        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    72
        @Override
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    73
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    74
            return text;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    75
        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    76
    }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    77
}