JDK-6855236 : Compiler Tree API TreePath class generates NullPointerException from Iterator
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6u14
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2009-06-26
  • Updated: 2011-07-25
  • Resolved: 2011-07-25
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 Other
7 b81Fixed OpenJDK6Fixed
Related Reports
Relates :  
Relates :  
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 ----------

Comments
EVALUATION Although the code itself has been fixed, there is no explicit test. There should be.
06-01-2010

EVALUATION Fixed in 7 as part of 308 work (6843077)
05-01-2010

EVALUATION Yes. Fix underway and will likely be pushed on Monday. Should backport to 6-open
26-06-2009