--- old/src/java.compiler/share/classes/javax/lang/model/element/Element.java 2017-02-01 13:37:55.314533796 -0800
+++ new/src/java.compiler/share/classes/javax/lang/model/element/Element.java 2017-02-01 13:37:55.214583793 -0800
@@ -139,7 +139,7 @@
*
*
If this is a {@linkplain
* PackageElement#getEnclosingElement package}, its module is
- * returned.
+ * returned if such a module exists. Otherwise, {@code null} is returned.
*
* If this is a {@linkplain
* TypeParameterElement#getEnclosingElement type parameter},
--- old/src/java.compiler/share/classes/javax/lang/model/element/PackageElement.java 2017-02-01 13:37:55.654363809 -0800
+++ new/src/java.compiler/share/classes/javax/lang/model/element/PackageElement.java 2017-02-01 13:37:55.558411805 -0800
@@ -83,9 +83,16 @@
boolean isUnnamed();
/**
- * Returns the enclosing module.
+ * Returns the enclosing module if such a module exists; otherwise
+ * returns {@code null}.
*
- * @return the enclosing module
+ * One situation where a module does not exist for a package is if
+ * the environment does not include modules, such as an annotation
+ * processing environment configured for a {@linkplain
+ * javax.annotation.processing.ProcessingEnvironment#getSourceVersion
+ * source version} without modules.
+ *
+ * @return the enclosing module or {@code null} if no such module exists
*/
@Override
Element getEnclosingElement();
--- old/src/java.compiler/share/classes/javax/lang/model/type/NoType.java 2017-02-01 13:37:55.982199820 -0800
+++ new/src/java.compiler/share/classes/javax/lang/model/type/NoType.java 2017-02-01 13:37:55.882249817 -0800
@@ -34,6 +34,7 @@
*
* - {@link TypeKind#VOID VOID} - corresponds to the keyword {@code void}.
*
- {@link TypeKind#PACKAGE PACKAGE} - the pseudo-type of a package element.
+ *
- {@link TypeKind#MODULE MODULE} - the pseudo-type of a module element.
*
- {@link TypeKind#NONE NONE} - used in other cases
* where no actual type is appropriate; for example, the superclass
* of {@code java.lang.Object}.
--- old/src/java.compiler/share/classes/javax/lang/model/util/Elements.java 2017-02-01 13:37:56.294043832 -0800
+++ new/src/java.compiler/share/classes/javax/lang/model/util/Elements.java 2017-02-01 13:37:56.190095828 -0800
@@ -59,12 +59,17 @@
/**
* Returns a package given its fully qualified name, as seen from the given module.
*
+ * @implSpec The default implementation of this method returns
+ * {@code null}.
+ *
* @param name fully qualified package name, or an empty string for an unnamed package
* @param module module relative to which the lookup should happen
* @return the specified package, or {@code null} if it cannot be found
* @since 9
*/
- PackageElement getPackageElement(ModuleElement module, CharSequence name);
+ default PackageElement getPackageElement(ModuleElement module, CharSequence name) {
+ return null;
+ }
/**
* Returns a type element given its canonical name if the type element is unique in the environment.
@@ -79,12 +84,17 @@
/**
* Returns a type element given its canonical name, as seen from the given module.
*
+ * @implSpec The default implementation of this method returns
+ * {@code null}.
+ *
* @param name the canonical name
* @param module module relative to which the lookup should happen
* @return the named type element, or {@code null} if it cannot be found
* @since 9
*/
- TypeElement getTypeElement(ModuleElement module, CharSequence name);
+ default TypeElement getTypeElement(ModuleElement module, CharSequence name) {
+ return null;
+ }
/**
* Returns a module element given its fully qualified name.
@@ -95,11 +105,16 @@
* javax.annotation.processing.ProcessingEnvironment#getSourceVersion
* source version} without modules.
*
+ * @implSpec The default implementation of this method returns
+ * {@code null}.
+ *
* @param name the name
* @return the named module element, or {@code null} if it cannot be found
* @since 9
*/
- ModuleElement getModuleElement(CharSequence name);
+ default ModuleElement getModuleElement(CharSequence name) {
+ return null;
+ }
/**
* Returns the values of an annotation's elements, including defaults.
@@ -337,11 +352,16 @@
* javax.annotation.processing.ProcessingEnvironment#getSourceVersion
* source version} without modules.
*
+ * @implSpec The default implementation of this method returns
+ * {@code null}.
+ *
* @param type the element being examined
* @return the module of an element
* @since 9
*/
- ModuleElement getModuleOf(Element type);
+ default ModuleElement getModuleOf(Element type) {
+ return null;
+ }
/**
* Returns all members of a type element, whether inherited or
--- old/test/tools/javac/processing/model/element/TestPackageElement.java 2017-02-01 13:37:56.617881843 -0800
+++ new/test/tools/javac/processing/model/element/TestPackageElement.java 2017-02-01 13:37:56.509935839 -0800
@@ -23,16 +23,18 @@
/*
* @test
- * @bug 6449798 6399404
+ * @bug 6449798 6399404 8173776
* @summary Test basic workings of PackageElement
* @author Joseph D. Darcy
* @library /tools/javac/lib
* @modules java.compiler
* jdk.compiler
* @build JavacTestingAbstractProcessor TestPackageElement
- * @compile -processor TestPackageElement -proc:only TestPackageElement.java
+ * @compile -processor TestPackageElement -proc:only TestPackageElement.java
+ * @compile -processor TestPackageElement -proc:only --release 8 TestPackageElement.java
*/
+import java.util.Objects;
import java.util.Set;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
@@ -67,7 +69,22 @@
PackageElement javaLang = eltUtils.getPackageElement("java.lang");
if (javaLang.isUnnamed())
throw new RuntimeException("Package java.lang is unnamed!");
+
+ testEnclosingElement(javaLang);
}
return true;
}
+
+ void testEnclosingElement(PackageElement javaLang) {
+ SourceVersion version = processingEnv.getSourceVersion();
+ Element enclosing = javaLang.getEnclosingElement();
+ Element expectedEnclosing =
+ (version.compareTo(RELEASE_9) < 0) ? // No modules
+ null :
+ eltUtils.getModuleElement("java.base");
+
+ if (!Objects.equals(enclosing, expectedEnclosing))
+ throw new RuntimeException("Unexpected enclosing element under source version " +
+ version);
+ }
}