jjg@482: /* ohair@554: * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. jjg@482: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@482: * jjg@482: * This code is free software; you can redistribute it and/or modify it jjg@482: * under the terms of the GNU General Public License version 2 only, as jjg@482: * published by the Free Software Foundation. jjg@482: * jjg@482: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@482: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@482: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@482: * version 2 for more details (a copy is included in the LICENSE file that jjg@482: * accompanied this code). jjg@482: * jjg@482: * You should have received a copy of the GNU General Public License version jjg@482: * 2 along with this work; if not, write to the Free Software Foundation, jjg@482: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@482: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@482: */ jjg@482: jjg@482: /* jjg@482: * @test jjg@482: * @bug 6654037 jjg@482: * @summary JCTree.pos may be incorrect for BinaryTrees jjg@482: */ jjg@482: jjg@482: import com.sun.source.tree.BinaryTree; jjg@482: import com.sun.source.tree.ClassTree; jjg@482: import com.sun.source.tree.CompilationUnitTree; jjg@482: import com.sun.source.tree.MethodTree; jjg@482: import com.sun.source.tree.VariableTree; jjg@482: import com.sun.tools.javac.api.JavacTaskImpl; jjg@482: import com.sun.tools.javac.tree.JCTree; jjg@482: import java.net.URI; jjg@482: import java.util.Arrays; jjg@482: import javax.tools.JavaCompiler; jjg@482: import javax.tools.JavaFileObject; jjg@482: import javax.tools.SimpleJavaFileObject; jjg@482: import javax.tools.ToolProvider; jjg@482: jjg@482: public class T6654037 { jjg@482: jjg@482: public static void main(String[] args) throws Exception { jjg@482: final String bootPath = System.getProperty("sun.boot.class.path"); //NOI18N jjg@482: final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); jjg@482: assert tool != null; jjg@482: jjg@482: String code = "package test; public class Test {private void test() {Object o = null; boolean b = o != null && o instanceof String;} private Test() {}}"; jjg@482: jjg@482: JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, Arrays.asList("-bootclasspath", bootPath, "-Xjcov"), null, Arrays.asList(new MyFileObject(code))); jjg@482: CompilationUnitTree cut = ct.parse().iterator().next(); jjg@482: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); jjg@482: MethodTree method = (MethodTree) clazz.getMembers().get(0); jjg@482: VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1); jjg@482: BinaryTree cond = (BinaryTree) condSt.getInitializer(); jjg@482: JCTree condJC = (JCTree) cond; jjg@482: jjg@482: if (condJC.pos != 93) jjg@482: throw new IllegalStateException("Unexpected position=" + condJC.pos); jjg@482: } jjg@482: jjg@482: static class MyFileObject extends SimpleJavaFileObject { jjg@482: private String text; jjg@482: public MyFileObject(String text) { jjg@482: super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); jjg@482: this.text = text; jjg@482: } jjg@482: @Override jjg@482: public CharSequence getCharContent(boolean ignoreEncodingErrors) { jjg@482: return text; jjg@482: } jjg@482: } jjg@482: }