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

Compiler Tree API TreePath class generates NullPointerException from Iterator

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 6u14
    • Fix Version/s: 7
    • Component/s: tools
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b81
    • CPU:
      x86
    • OS:
      windows_xp
    • Verification:
      Not verified

      Backports

        Description

        FULL PRODUCT VERSION :
        java version "1.6.0_14"
        Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
        Java HotSpot(TM) Client VM (build 14.0-b16, mixed mode, sharing)

        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows XP [Version 5.1.2600]

        A DESCRIPTION OF THE PROBLEM :
        The Iterator in the com.sun.source.util.TreePath class never initializes the "private TreePath curr;" variable. Every time you try to use the iterator it throws a NullPointerException.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        create a com.sun.source.util.TreePathScanner<Object, Trees> object and implement any of the visitXYZ methods.
        call the getCurrentPath() method and iterate through the results.
        You will get a NullPointerException every time.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Not to get a NullPointerException
        ACTUAL -
        NullPointerException

        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        NullPointerException

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        package com.nglm.analyzer;

        import java.util.*;

        import javax.annotation.processing.*;
        import javax.lang.model.*;
        import javax.lang.model.element.*;

        import com.sun.source.util.*;

        @SupportedSourceVersion(SourceVersion.RELEASE_6)
        @SupportedAnnotationTypes("*")
        public class CodeProcessor extends AbstractProcessor {

          private Trees trees;

          @Override
          public void init(ProcessingEnvironment pe) {
              super.init(pe);
              trees = Trees.instance(pe);
          }

          @Override
          public boolean process(Set<? extends TypeElement> arg0, RoundEnvironment roundEnvironment) {
            // Scanner class to scan through various component elements
            CodeVisitor visitor = new CodeVisitor();

            for (Element e : roundEnvironment.getRootElements()) {
                TreePath tp = trees.getPath(e);
                visitor.scan(tp, trees);
            }

            return true;
          }

        }

        package com.nglm.analyzer;

        import com.sun.source.tree.*;
        import com.sun.source.util.*;

        public class CodeVisitor extends TreePathScanner<Object, Trees> {

          @Override
          public Object visitMethodInvocation(MethodInvocationTree node, Trees p) {
            System.out.print("current path: ");
            for (Tree t : getCurrentPath()) {
              System.out.print('/');
              System.out.print(t);
            }
            System.out.println();
            System.out.println("parent path: " + getCurrentPath().getParentPath());
            System.out.println("method select: " + node.getMethodSelect().toString());
            for (ExpressionTree arg : node.getArguments()) {
              System.out.println("argument: " + arg.toString());
            }
            return super.visitMethodInvocation(node, p);
          }

          @Override
          public Object visitExpressionStatement(ExpressionStatementTree node, Trees p) {
            ExpressionTree t = node.getExpression();
            System.out.println("expression statement: " + t.toString());
            return super.visitExpressionStatement(node, p);
          }

        }

        Then run javac -processor com.nglm.analyzer.CodeProcessor ...
        ---------- END SOURCE ----------

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  jjg Jonathan Gibbons
                  Reporter:
                  ndcosta Nelson Dcosta (Inactive)
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  0 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved:
                    Imported:
                    Indexed: