JDK-8190886 : package-info handling in RoundEnvironment.getElementsAnnotatedWith
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2017-11-07
  • Updated: 2018-11-03
  • Resolved: 2018-07-25
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 12
12 b05Fixed
Related Reports
CSR :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
My colleague Ron Shapiro noticed an issue with the implementation of RoundEnvironment.getElementsAnnotatedWith. The javadoc for that method specifies:

> Elements in a package are not considered included simply because a package-info file for that package was created.

However that doesn't match the behaviour we're seeing. Instead, getElementsAnnotatedWith is returning annotated members in a compilation with a package-info on the class path.

Reproduced using javac 9.0.1+11:

=== ./foo/A.java ===
package foo;
class A {
  B b;
}
=== ./foo/B.java ===
package foo;
@Deprecated
class B {}
=== ./foo/package-info.java ===
@Deprecated
package foo;
=== ./P.java ===
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.TypeElement;

@SupportedAnnotationTypes("*")
public class P extends AbstractProcessor {
  @Override
  public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    System.err.println(">>> " + roundEnv.getElementsAnnotatedWith(Deprecated.class));
    return false;
  }
}
===

$ javac P.java foo/B.java
$ javac -processor P -cp . -sourcepath : foo/A.java foo/package-info.java
...
>>> [foo, foo.B]

Note that getElementsAnnotatedWith returns foo.B, which is a member of the package foo, but which is not part of the current compilation.
Comments
Review thread: http://mail.openjdk.java.net/pipermail/compiler-dev/2018-July/012225.html
24-07-2018