Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6654037

JCTree.pos may be incorrect for BinaryTrees

XMLWordPrintable

    • x86
    • linux

      The JCTree.pos may contain incorrect offset for BinaryTrees in some cases. For example, for expression "o != null && o instanceof String", the JCTree.pos for tree corresponding to the "&&" operator points to the beginning of the "instanceof" operator, while is should point to the beginning of the "&&" operator. Tested on revision "1:e4dae1993f8b" cloned from http://hg.openjdk.java.net/jdk7/jdk7/langtools

      Test case:

      /*
       * @test @(#)T9999999.java
       * @bug 9999999
       * @summary
       */

      import com.sun.source.tree.BinaryTree;
      import com.sun.source.tree.ClassTree;
      import com.sun.source.tree.CompilationUnitTree;
      import com.sun.source.tree.MethodTree;
      import com.sun.source.tree.VariableTree;
      import com.sun.tools.javac.api.JavacTaskImpl;
      import com.sun.tools.javac.tree.JCTree;
      import java.net.URI;
      import java.util.Arrays;
      import javax.tools.JavaCompiler;
      import javax.tools.JavaFileObject;
      import javax.tools.SimpleJavaFileObject;
      import javax.tools.ToolProvider;

      public class T9999999 {
          
          public static void main(String[] args) throws Exception {
              final String bootPath = System.getProperty("sun.boot.class.path"); //NOI18N
              final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
              assert tool != null;

              String code = "package test; public class Test {private void test() {Object o = null; boolean b = o != null && o instanceof String;} private Test() {}}";

              JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, Arrays.asList("-bootclasspath", bootPath, "-Xjcov"), null, Arrays.asList(new MyFileObject(code)));
              CompilationUnitTree cut = ct.parse().iterator().next();
              ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
              MethodTree method = (MethodTree) clazz.getMembers().get(0);
              VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1);
              BinaryTree cond = (BinaryTree) condSt.getInitializer();
              JCTree condJC = (JCTree) cond;

              if (condJC.pos != 93)
                  throw new IllegalStateException("Unexpected position=" + condJC.pos);
          }
          
          static class MyFileObject extends SimpleJavaFileObject {
              private String text;
              public MyFileObject(String text) {
                  super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
                  this.text = text;
              }
              @Override
              public CharSequence getCharContent(boolean ignoreEncodingErrors) {
                  return text;
              }
          }
      }

            jjg Jonathan Gibbons
            jlahoda Jan Lahoda
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: