langtools/test/tools/javac/T6654037.java
author mikejwre
Wed, 09 Jun 2010 18:56:41 -0700
changeset 5634 895b66935810
parent 5520 86e4b9a9da40
child 27579 d1a63c99cdd5
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
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4870
diff changeset
     2
 * Copyright (c) 2008, 2010, 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
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    28
 */
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    29
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    30
import com.sun.source.tree.BinaryTree;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    31
import com.sun.source.tree.ClassTree;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    32
import com.sun.source.tree.CompilationUnitTree;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    33
import com.sun.source.tree.MethodTree;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    34
import com.sun.source.tree.VariableTree;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    35
import com.sun.tools.javac.api.JavacTaskImpl;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    36
import com.sun.tools.javac.tree.JCTree;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    37
import java.net.URI;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    38
import java.util.Arrays;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    39
import javax.tools.JavaCompiler;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    40
import javax.tools.JavaFileObject;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    41
import javax.tools.SimpleJavaFileObject;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    42
import javax.tools.ToolProvider;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    43
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    44
public class T6654037 {
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 static void main(String[] args) throws Exception {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    47
        final String bootPath = System.getProperty("sun.boot.class.path"); //NOI18N
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    48
        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    49
        assert tool != null;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    50
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    51
        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
    52
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    53
        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, Arrays.asList("-bootclasspath", bootPath, "-Xjcov"), null, Arrays.asList(new MyFileObject(code)));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    54
        CompilationUnitTree cut = ct.parse().iterator().next();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    55
        ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    56
        MethodTree method = (MethodTree) clazz.getMembers().get(0);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    57
        VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    58
        BinaryTree cond = (BinaryTree) condSt.getInitializer();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    59
        JCTree condJC = (JCTree) cond;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    60
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    61
        if (condJC.pos != 93)
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    62
            throw new IllegalStateException("Unexpected position=" + condJC.pos);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    63
    }
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
    static class MyFileObject extends SimpleJavaFileObject {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    66
        private String text;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    67
        public MyFileObject(String text) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    68
            super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    69
            this.text = text;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    70
        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    71
        @Override
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    72
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    73
            return text;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents:
diff changeset
    74
        }
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
}