JDK-6408241 : Elements.getTypeElement(String s) does not return null for non-existing classes
  • Type: Bug
  • Component: core-libs
  • Sub-Component: javax.annotation.processing
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2006-04-04
  • Updated: 2011-01-19
  • Resolved: 2006-05-13
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 b85Fixed
Related Reports
Relates :  
Relates :  
Description
The specification for this method says:
----
TypeElement getTypeElement(String name)

    Returns a type element given its fully qualified name.

    Parameters:
        name - fully qualified type name 
    Returns:
        the named type element, or *null if it cannot be found*
----

However null is never returned in cases when non-existing type names are passed.
Here is an example from 6346506:
----
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("non.existing.Class");
	// Case2 class doesn;t exisit in classpath to construct a Element.
        if ( case2 != null ) {
            System.out.println(" element received as "+case2.toString());
        }
        return true ;
    }
}
----

$JAVA_HOME/bin/javac -classpath $CLASSPATH -processor CaseAP Case1.java
 element received as non.existing.Class
 element received as non.existing.Class

Java(TM) SE Runtime Environment (build 1.6.0-beta2-b77)

Comments
EVALUATION Fixed as a result of the fix for 6308351: Apt causes NullPointerException running on a Simplified Chinese testcase. This CR will now be used as a vehicle for doing some additional testing of this fix.
08-05-2006

EVALUATION The bogus TypeElement that's returned is a time bomb. Call getKind on that element and the processor crashes. Elements.getTypeElement calls ClassReader.enterClass, which does not complete the resulting ClassSymbol before returning it. Could fix by calling loadClass instead, which completes the symbol and undoes the enter on failure. Then catch the CompletionFailure and return null as spec'ed.
26-04-2006