JDK-6430209 : spurious compiler error elicited by packageElement.getEnclosedElements()
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2006-05-25
  • Updated: 2010-08-12
  • Resolved: 2006-08-15
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Description
When processing a source file not declared to be in any package with a PackageElement obtained from Elements.getPackageElement(), getEnclosedElements() elicits a duplcate class error and this error:
"an annotation processor threw an uncaught exception.
Consult the following stack trace for details.
com.sun.tools.javac.jvm.ClassReader$BadClassFile: bad class file: ./dir1/test0.java
file does not contain class dir1.test0
Please remove or make sure it appears in the correct subdirectory of the classpath.
        at com.sun.tools.javac.main.JavaCompiler.complete(JavaCompiler.java:599)
        at com.sun.tools.javac.jvm.ClassReader.fillIn(ClassReader.java:1777)
        at com.sun.tools.javac.jvm.ClassReader.complete(ClassReader.java:1700)
...."

%javac -proc:none b6341534.java
%javac -proc:only -processor b6341534 -cp . ./src/*.java
---source files and processor souce below-------
% cat src/test0.java
//package dir1;
public class test0 { }

% cat src/test1.java
package dir1;
public class test1 { }
- - - - - - - - - - - - - - - - - - - -- - - -- - 
 % cat b6341534.java
import javax.annotation.processing.*;
import javax.lang.model.element.*;
import javax.lang.model.util.*;
import static javax.lang.model.util.ElementFilter.*;
import static javax.tools.Diagnostic.Kind.*;
import java.util.*;
import java.util.Set;

@SupportedAnnotationTypes({"*"})
@SupportedSourceVersion(javax.lang.model.SourceVersion.RELEASE_6)
public class b6341534 extends AbstractProcessor {
   static int r = 0;
        static Elements E = null;
        static Messager msgr = null;
        public void init(ProcessingEnvironment penv)  {
                processingEnv = penv;
                msgr = penv.getMessager();
                E = penv.getElementUtils();
        }
        //Create directory 'dir1' and a test class in dir1
        public boolean process(Set<? extends TypeElement> tes, RoundEnvironment renv)
        {
           if(!renv.errorRaised() &&  !renv.processingOver()){
                  r++;
                  for( TypeElement t : renv.getSpecifiedTypeElements() )
                          System.out.println("Round"+r+ ": " + t.toString());

                        try {
                        PackageElement PE = E.getPackageElement("dir1");
                   List<? extends Element> LEE = PE.getEnclosedElements();    /* <=This line elicits the error message.  */
                        for(Element e : LEE)    System.out.println("found " + e.toString() + " in dir1.");
                        }
                catch(NullPointerException npe) {
                         msgr.printMessage(ERROR,npe.toString());
                         //npe.printStackTrace();
                         return false;
                        }
                }
                if( renv.errorRaised() ) {      msgr.printMessage(ERROR, "FAILED");}
                return true;
        }
}

Comments
EVALUATION Short term being being tracked in 6441871
21-06-2006

EVALUATION $ javac -proc:none b6341534.java $ javac -proc:only -processor b6341534 -cp . ./src/*.java Round1: test0 Round1: dir1.test1 found dir1.test1 in dir1. $ However, the error message is talking about ./dir1/test0.java so I copied src/test0.java to dir1. Then an error occurs. The error is right but unfriendly. I assume that this problem goes away with 6348499.
18-06-2006