8173776: More javax.lang.model improvements to support modules
authordarcy
Wed, 01 Feb 2017 17:04:24 -0800
changeset 43571 a153580d1741
parent 43570 c7b68caf3ab4
child 43572 786800fd7cf0
8173776: More javax.lang.model improvements to support modules Reviewed-by: jjg, jlahoda
langtools/src/java.compiler/share/classes/javax/lang/model/element/Element.java
langtools/src/java.compiler/share/classes/javax/lang/model/element/PackageElement.java
langtools/src/java.compiler/share/classes/javax/lang/model/type/NoType.java
langtools/src/java.compiler/share/classes/javax/lang/model/util/Elements.java
langtools/test/tools/javac/processing/model/element/TestPackageElement.java
--- a/langtools/src/java.compiler/share/classes/javax/lang/model/element/Element.java	Wed Feb 01 15:16:49 2017 -0800
+++ b/langtools/src/java.compiler/share/classes/javax/lang/model/element/Element.java	Wed Feb 01 17:04:24 2017 -0800
@@ -139,7 +139,7 @@
      *
      * <li> If this is a {@linkplain
      * PackageElement#getEnclosingElement package}, its module is
-     * returned.
+     * returned if such a module exists. Otherwise, {@code null} is returned.
      *
      * <li> If this is a {@linkplain
      * TypeParameterElement#getEnclosingElement type parameter},
--- a/langtools/src/java.compiler/share/classes/javax/lang/model/element/PackageElement.java	Wed Feb 01 15:16:49 2017 -0800
+++ b/langtools/src/java.compiler/share/classes/javax/lang/model/element/PackageElement.java	Wed Feb 01 17:04:24 2017 -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();
--- a/langtools/src/java.compiler/share/classes/javax/lang/model/type/NoType.java	Wed Feb 01 15:16:49 2017 -0800
+++ b/langtools/src/java.compiler/share/classes/javax/lang/model/type/NoType.java	Wed Feb 01 17:04:24 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -34,6 +34,7 @@
  * <ul>
  * <li>{@link TypeKind#VOID VOID} - corresponds to the keyword {@code void}.
  * <li>{@link TypeKind#PACKAGE PACKAGE} - the pseudo-type of a package element.
+ * <li>{@link TypeKind#MODULE MODULE} - the pseudo-type of a module element.
  * <li>{@link TypeKind#NONE NONE} - used in other cases
  *   where no actual type is appropriate; for example, the superclass
  *   of {@code java.lang.Object}.
--- a/langtools/src/java.compiler/share/classes/javax/lang/model/util/Elements.java	Wed Feb 01 15:16:49 2017 -0800
+++ b/langtools/src/java.compiler/share/classes/javax/lang/model/util/Elements.java	Wed Feb 01 17:04:24 2017 -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
--- a/langtools/test/tools/javac/processing/model/element/TestPackageElement.java	Wed Feb 01 15:16:49 2017 -0800
+++ b/langtools/test/tools/javac/processing/model/element/TestPackageElement.java	Wed Feb 01 17:04:24 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -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);
+    }
 }