-
Bug
-
Resolution: Fixed
-
P3
-
6u14
-
b81
-
x86
-
windows_xp
-
Not verified
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-2179482 | OpenJDK6 | Jonathan Gibbons | P3 | Resolved | Fixed | b18 |
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 ----------
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 ----------
- backported by
-
JDK-2179482 Compiler Tree API TreePath class generates NullPointerException from Iterator
-
- Resolved
-
- relates to
-
JDK-6843077 JSR 308: Annotations on types
-
- Closed
-
-
JDK-6473148 TreePath.iterator() should document the iteration order
-
- Closed
-