United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6855236 Compiler Tree API TreePath class generates NullPointerException from Iterator
JDK-6855236 : Compiler Tree API TreePath class generates NullPointerException from Iterator

Details
Type:
Bug
Submit Date:
2009-06-26
Status:
Closed
Updated Date:
2011-07-25
Project Name:
JDK
Resolved Date:
2011-07-25
Component:
tools
OS:
windows_xp
Sub-Component:
javac
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
6u14
Fixed Versions:

Related Reports
Backport:
Relates:
Relates:

Sub Tasks

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

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

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

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



Hardware and Software, Engineered to Work Together