JDK-6346506 : jsr 269 Elements.getTypeElement(String s) will throw NPE for a unknown class.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: javax.annotation.processing
  • Affected Version: 6
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2005-11-05
  • Updated: 2010-08-19
  • Resolved: 2006-03-23
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 6
6 b78Fixed
Related Reports
Relates :  
Relates :  
Description
'Case2' doesn;t doesn't live in classpath for javac to construct a Element. The stacktrace shows a code path as JavacCompiler Code.
I am not sure which part it started passing a 'Null' value to next layers. This could be a JavacCompiler API. I am just guessing.

May be 'ElementNotFoundException' was required.

bash-3.00$ cat CaseAP.java
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.element.*;
import javax.lang.model.type.*;
import javax.lang.model.util.*;
import static javax.lang.model.SourceVersion.*;
import static javax.lang.model.type.TypeKind.*;

@SupportedAnnotationTypes("*")
@SupportedSourceVersion(RELEASE_6)
@SupportedOptions("-verbose")
public class CaseAP  extends  AbstractProcessor {
    public void init(ProcessingEnvironment penv) {
        super.init(penv);
    }
    public boolean process(Set<? extends TypeElement> typeElementSet,
            RoundEnvironment renv) {
        Elements elementUtils = processingEnv.getElementUtils();
        TypeElement case2 = elementUtils.getTypeElement("Case2");
	// Case2 class doesn;t exisit in classpath to construct a Element.
        if ( case2 != null ) {
            System.out.println(" element received as "+case2.toString());
        }
        return true ;
    }
}
bash-3.00$ cat Case1.java
public class Case1 {
}
bash-3.00$ $JAVA_HOME/bin/javac -classpath $CLASSPATH -processor CaseAP Case1.java


An annotation processor threw an uncaught exception.
Consult the following stack trace for details.
java.lang.NullPointerException
        at com.sun.tools.javac.main.JavaCompiler.resolveIdent(JavaCompiler.java:497)
        at com.sun.tools.javac.model.JavacElements.nameToSymbol(JavacElements.java:151)
        at com.sun.tools.javac.model.JavacElements.getTypeElement(JavacElements.java:134)
        at com.sun.tools.javac.model.JavacElements.getTypeElement(JavacElements.java:46)
        at CaseAP.process(CaseAP.java:19)
        at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:497)
        at com.sun.tools.javac.processing.JavacProcessingEnvironment.discovery(JavacProcessingEnvironment.java:421)
        at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:569)
        at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:843)
        at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:629)
        at com.sun.tools.javac.main.Main.compile(Main.java:756)
        at com.sun.tools.javac.main.Main.compile(Main.java:681)
        at com.sun.tools.javac.main.Main.compile(Main.java:670)
        at com.sun.tools.javac.Main.compile(Main.java:70)
        at com.sun.tools.javac.Main.main(Main.java:55)

bash-3.00$$JAVA_HOME/bin/java -version
java version "1.6.0-auto"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-auto-292b)
Java HotSpot(TM) Server VM (build 1.6.0-ea-b56, mixed mode)

Comments
SUGGESTED FIX Webrev of regtest: http://sa.sfbay/projects/langtools/bugid_summary.pl?bugid=6346506
11-03-2006

EVALUATION Will add a new regression test.
08-03-2006

SUGGESTED FIX Webrev of changes: http://sa.sfbay/projects/langtools/bugid_summary.pl?bugid=6374357
08-03-2006

EVALUATION Looks like this problem was fixed recently (for b75) with the putback for these: Date: Fri, 24 Feb 2006 13:19:22 -0800 (PST) 6194785: ParameterDeclaration.getSimpleName does not return actual name from class files 6352136: TypeMirror.asElement().getEnclosedElements() throws NPE 6374357: PackageElement.getEnclosedElements() throws ClassReader$BadClassFileException 6325201: make erasure non-destructive 6341534: PackageElement.getEnclosedElements results in NullPointerException from parse(JavaCompiler.java:429) 6341072: printError does not result in nonzero return code for javac when processing 6372395: javax.tools.SimpleJavaFileObject.openReader() throws undocumented exception (NullPointerException)
08-03-2006

EVALUATION NullPointerExceptions considered harmful.
01-12-2005