Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6346506

jsr 269 Elements.getTypeElement(String s) will throw NPE for a unknown class.

XMLWordPrintable

      '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)

            sseligmasunw Scott Seligman (Inactive)
            vvegurusunw Viswadeep Veguru (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: