Uploaded image for project: 'CCC Migration Project'
  1. CCC Migration Project
  2. CCC-8173776

More javax.lang.model improvements to support modules

XMLWordPrintable

    • source, behavioral
    • low
    • Changing newly added interface methods to defaults, a more compatible way of adding methods. Clarifying when the previous behavior of an enclosing element on a package may be allowed.
    • Java API
    • SE

      Summary

      Update several methods of the utility interface javax/lang/model/util/Elements to be default methods and clarify handling of modules in other existing methods.

      Problem

      Further examination of the javax.lang.model support for modules revealed a small number of oversights and areas for improvement. These should be fixed.

      Solution

      Add default methods where appropriate, correct/refine enclosing element of a package when running in a non-module environment.

      Specification

      --- old/src/java.compiler/share/classes/javax/lang/model/element/Element.java   2017-02-01 12:13:24.828591070 -0800
      +++ new/src/java.compiler/share/classes/javax/lang/model/element/Element.java   2017-02-01 12:13:24.728591066 -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},
      --- old/src/java.compiler/share/classes/javax/lang/model/element/PackageElement.java    2017-02-01 12:13:25.148591081 -0800
      +++ new/src/java.compiler/share/classes/javax/lang/model/element/PackageElement.java    2017-02-01 12:13:25.044591078 -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 12:13:25.480591093 -0800
      +++ new/src/java.compiler/share/classes/javax/lang/model/type/NoType.java       2017-02-01 12:13:25.380591090 -0800
      @@ -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}.
      --- old/src/java.compiler/share/classes/javax/lang/model/util/Elements.java     2017-02-01 12:13:25.784591104 -0800
      +++ new/src/java.compiler/share/classes/javax/lang/model/util/Elements.java     2017-02-01 12:13:25.680591100 -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

            darcy Joe Darcy
            darcy Joe Darcy
            Jonathan Gibbons
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: