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

NullPointerException getting the Scope using Java Compiler Tree API

XMLWordPrintable

    • x86
    • windows_7

      FULL PRODUCT VERSION :


      ADDITIONAL OS VERSION INFORMATION :
      windows 7, also tested with jdk 18, and on mac with 22.

      A DESCRIPTION OF THE PROBLEM :
      cd /Users/simpatico/Documents/NetBeansProjects/ScopeTest; JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home "/Applications/NetBeans/NetBeans 7.0 Beta.app/Contents/Resources/NetBeans/java/maven/bin/mvn" install
      Scanning for projects...
                                                                              
      ------------------------------------------------------------------------
      Building ScopeTest 1.0-SNAPSHOT
      ------------------------------------------------------------------------

      [resources:resources]
      Using 'UTF-8' encoding to copy filtered resources.
      skip non existing resourceDirectory /Users/simpatico/Documents/NetBeansProjects/ScopeTest/src/main/resources

      [compiler:compile]
      Compiling 3 source files to /Users/simpatico/Documents/NetBeansProjects/ScopeTest/target/classes

      [resources:testResources]
      Using 'UTF-8' encoding to copy filtered resources.
      skip non existing resourceDirectory /Users/simpatico/Documents/NetBeansProjects/ScopeTest/src/test/resources

      [compiler:testCompile]
      Compiling 1 source file to /Users/simpatico/Documents/NetBeansProjects/ScopeTest/target/test-classes

      [surefire:test]
      Downloading: http://repo1.maven.org/maven2/org/apache/maven/surefire/surefire-junit/2.5/surefire-junit-2.5.pom
             
      Downloaded: http://repo1.maven.org/maven2/org/apache/maven/surefire/surefire-junit/2.5/surefire-junit-2.5.pom (2 KB at 1.2 KB/sec)
      Downloading: http://repo1.maven.org/maven2/org/apache/maven/surefire/surefire-junit/2.5/surefire-junit-2.5.jar
              
      Downloaded: http://repo1.maven.org/maven2/org/apache/maven/surefire/surefire-junit/2.5/surefire-junit-2.5.jar (16 KB at 21.9 KB/sec)
      Surefire report directory: /Users/simpatico/Documents/NetBeansProjects/ScopeTest/target/surefire-reports

      -------------------------------------------------------
       T E S T S
      -------------------------------------------------------
      Running com.mysimpatico.scopetest.AppTest
      [parsing started /Users/simpatico/Documents/NetBeansProjects/ScopeTest/src/main/java/com/mysimpatico/scopetest/AnnClass.java]
      [parsing completed 25ms]
      [search path for source files: /Users/simpatico/Documents/NetBeansProjects/ScopeTest/target/test-classes,/Users/simpatico/Documents/NetBeansProjects/ScopeTest/target/classes,/Users/simpatico/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar,.]
      [search path for class files: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jsfd.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar,/System/Library/Frameworks/JavaVM.framework/Frameworks/JavaRuntimeSupport.framework/Resources/Java/JavaRuntimeSupport.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/ui.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/laf.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/sunrsasign.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jsse.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jce.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/charsets.jar,/Library/Java/Extensions/javaws.jar,/System/Library/Java/Extensions/AppleScriptEngine.jar,/System/Library/Java/Extensions/dns_sd.jar,/System/Library/Java/Extensions/j3daudio.jar,/System/Library/Java/Extensions/j3dcore.jar,/System/Library/Java/Extensions/j3dutils.jar,/System/Library/Java/Extensions/jai_codec.jar,/System/Library/Java/Extensions/jai_core.jar,/System/Library/Java/Extensions/mlibwrapper_jai.jar,/System/Library/Java/Extensions/MRJToolkit.jar,/System/Library/Java/Extensions/QTJava.zip,/System/Library/Java/Extensions/vecmath.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/apple_provider.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/dnsns.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/localedata.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/sunjce_provider.jar,/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/sunpkcs11.jar,/Users/simpatico/Documents/NetBeansProjects/ScopeTest/target/test-classes,/Users/simpatico/Documents/NetBeansProjects/ScopeTest/target/classes,/Users/simpatico/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar,.]
      [loading java/lang/Object.class(java/lang:Object.class)]
      [loading /Users/simpatico/Documents/NetBeansProjects/ScopeTest/target/classes/com/mysimpatico/scopetest/SampleAnn.class]
      [loading java/lang/annotation/Annotation.class(java/lang/annotation:Annotation.class)]
      [loading java/lang/annotation/Target.class(java/lang/annotation:Target.class)]
      [loading java/lang/annotation/ElementType.class(java/lang/annotation:ElementType.class)]
      [loading java/lang/annotation/Retention.class(java/lang/annotation:Retention.class)]
      [loading java/lang/annotation/RetentionPolicy.class(java/lang/annotation:RetentionPolicy.class)]
      Round 1:
              input files: {com.mysimpatico.scopetest.AnnClass}
              annotations: [com.mysimpatico.scopetest.SampleAnn]
              last round: false


      An annotation processor threw an uncaught exception.
      Consult the following stack trace for details.
      java.lang.NullPointerException
              at com.sun.tools.javac.api.JavacTaskImpl.enter(JavacTaskImpl.java:276)
              at com.sun.tools.javac.api.JavacTrees.getAttrContext(JavacTrees.java:214)
              at com.sun.tools.javac.api.JavacTrees.getScope(JavacTrees.java:184)
              at com.sun.tools.javac.api.JavacTrees.getScope(JavacTrees.java:63)
              at com.mysimpatico.scopetest.SampleAnnProc.process(SampleAnnProc.java:41)
              at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:625)
              at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:554)
              at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:699)
              at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:981)
              at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:727)
              at com.sun.tools.javac.main.Main.compile(Main.java:353)
              at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:115)
              at com.mysimpatico.scopetest.AppTest.testApp(AppTest.java:61)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:597)
              at junit.framework.TestCase.runTest(TestCase.java:154)
              at junit.framework.TestCase.runBare(TestCase.java:127)
              at junit.framework.TestResult$1.protect(TestResult.java:106)
              at junit.framework.TestResult.runProtected(TestResult.java:124)
              at junit.framework.TestResult.run(TestResult.java:109)
              at junit.framework.TestCase.run(TestCase.java:118)
              at junit.framework.TestSuite.runTest(TestSuite.java:208)
              at junit.framework.TestSuite.run(TestSuite.java:203)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:597)
              at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:213)
              at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:115)
              at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:102)
              at org.apache.maven.surefire.Surefire.run(Surefire.java:180)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:597)
              at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
              at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)
      Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.097 sec

      Results :

      Tests run: 1, Failures: 0, Errors: 0, Skipped: 0


      [jar:jar]
      Building jar: /Users/simpatico/Documents/NetBeansProjects/ScopeTest/target/ScopeTest-1.0-SNAPSHOT.jar

      [install:install]
      Installing /Users/simpatico/Documents/NetBeansProjects/ScopeTest/target/ScopeTest-1.0-SNAPSHOT.jar to /Users/simpatico/.m2/repository/com/mysimpatico/ScopeTest/1.0-SNAPSHOT/ScopeTest-1.0-SNAPSHOT.jar
      Installing /Users/simpatico/Documents/NetBeansProjects/ScopeTest/pom.xml to /Users/simpatico/.m2/repository/com/mysimpatico/ScopeTest/1.0-SNAPSHOT/ScopeTest-1.0-SNAPSHOT.pom
      ------------------------------------------------------------------------
      BUILD SUCCESS
      ------------------------------------------------------------------------
        Total time: 14.644s
      Finished at: Sun Feb 06 17:34:36 CET 2011
      Final Memory: 12M/81M
      ------------------------------------------------------------------------


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      compile build code provided below. Can also download as maven project from: http://www.speedyshare.com/files/26714968/ScopeTest.zip


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package com.mysimpatico.scopetest;

      import junit.framework.*;
      import java.io.*;
      import java.util.*;
      import javax.annotation.processing.*;
      import javax.tools.JavaCompiler.*;
      import javax.tools.*;

      public class AppTest extends TestCase{

          public AppTest( String testName )
          {
              super( testName );
          }


          public static Test suite()
          {
              return new TestSuite( AppTest.class );
          }

               static File getFile(final File dir, final String... dirs) {
              File ret = dir;
              for (String dir1 : dirs) {
                  if (ret == null) {
                      ret = new File(dir1);
                  } else {
                      ret = new File(ret.getPath(), dir1);
                  }
              }
              return ret;
          }

          public void testApp()
          {
               final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

              final File workingdir = new File(System.getProperty("user.dir"));
              final File src = getFile(workingdir, "src", "main", "java", "com", "mysimpatico", "scopetest");
              final File annotatedFile = new File(src, "AnnClass.java");
              final StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
              CompilationTask task = compiler.getTask(null, null, null, Arrays.asList("-verbose"), null, fm.getJavaFileObjectsFromFiles(Arrays.asList(annotatedFile)));

              task.setProcessors(Arrays.<Processor>asList(new SampleAnnProc()));
              task.call();
          }
      }


      package com.mysimpatico.scopetest;

      import com.sun.source.util.TreePath;
      import java.util.*;
      import javax.annotation.processing.*;
      import javax.lang.model.*;
      import javax.lang.model.element.TypeElement;
      import com.sun.source.util.Trees;
      import javax.lang.model.element.Element;


      @SupportedAnnotationTypes("com.mysimpatico.scopetest.SampleAnn")
      @SupportedSourceVersion(SourceVersion.RELEASE_6)
      public class SampleAnnProc extends AbstractProcessor {

          protected Trees trees;

          @Override
          public void init(ProcessingEnvironment processingEnv) {
              super.init(processingEnv);
              trees = Trees.instance(processingEnv);
          }

          protected Set<? extends Element> getElementsAnnotated(final RoundEnvironment roundEnv, Set<? extends TypeElement> annotations) {
              final Set<Element> annotatatedElements = new HashSet<Element>();
              for (TypeElement ann : annotations) {
                  final Set<? extends Element> annElements = roundEnv.getElementsAnnotatedWith(ann);
                  annotatatedElements.addAll(annElements);
              }
              return annotatatedElements;
          }

          @Override
          public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
              for (final Element e : getElementsAnnotated(roundEnv, annotations)) {
                  final TreePath treePath = trees.getPath(e);
                  treePath.getClass(); //so it's not null
                  com.sun.source.tree.Scope scope = trees.getScope(treePath);
              }
              return true;
          }
      }


      package com.mysimpatico.scopetest;

      import java.lang.annotation.ElementType;
      import java.lang.annotation.Target;


      @Target(ElementType.METHOD)
      public @interface SampleAnn {

      }


      package com.mysimpatico.scopetest;

      public class AnnClass {

          @SampleAnn
          public void helloWorld(){

          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      compile using Runtime().exec

            jlahoda Jan Lahoda
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: