changeset 54302:e81edc1f6f7e

8216404: Elements.getPackageOf should handle modules Reviewed-by: jlahoda
author darcy
date Thu, 10 Jan 2019 10:34:30 -0800
parents 7327a62f3c04
children 8bea4144b21c
files src/java.compiler/share/classes/javax/lang/model/util/Elements.java src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacElements.java test/langtools/tools/javac/processing/model/util/elements/TestGetPackageOf.java
diffstat 3 files changed, 13 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.compiler/share/classes/javax/lang/model/util/Elements.java	Thu Jan 10 10:28:51 2019 -0800
+++ b/src/java.compiler/share/classes/javax/lang/model/util/Elements.java	Thu Jan 10 10:34:30 2019 -0800
@@ -451,6 +451,7 @@
     /**
      * Returns the package of an element.  The package of a package is
      * itself.
+     * The package of a module is {@code null}.
      *
      * @param type the element being examined
      * @return the package of an element
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacElements.java	Thu Jan 10 10:28:51 2019 -0800
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacElements.java	Thu Jan 10 10:34:30 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2019, 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
@@ -436,7 +436,10 @@
 
     @DefinedBy(Api.LANGUAGE_MODEL)
     public PackageElement getPackageOf(Element e) {
-        return cast(Symbol.class, e).packge();
+        if (e.getKind() == ElementKind.MODULE)
+            return null;
+        else
+            return cast(Symbol.class, e).packge();
     }
 
     @DefinedBy(Api.LANGUAGE_MODEL)
--- a/test/langtools/tools/javac/processing/model/util/elements/TestGetPackageOf.java	Thu Jan 10 10:28:51 2019 -0800
+++ b/test/langtools/tools/javac/processing/model/util/elements/TestGetPackageOf.java	Thu Jan 10 10:34:30 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2006, 2019, 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,7 +23,7 @@
 
 /*
  * @test
- * @bug 6453386
+ * @bug 6453386 8216404
  * @summary Test Elements.getPackageOf
  * @author  Joseph D. Darcy
  * @library /tools/javac/lib
@@ -56,6 +56,7 @@
             TypeElement    stringElt   = eltUtils.getTypeElement("java.lang.String");
             PackageElement javaLangPkg = eltUtils.getPackageElement("java.lang");
             PackageElement unnamedPkg  = eltUtils.getPackageElement("");
+            ModuleElement  moduleElt   = eltUtils.getModuleElement("java.base");
             PackageElement pkg = null;
 
             if (!javaLangPkg.equals(pkg=eltUtils.getPackageOf(stringElt) ) )
@@ -66,6 +67,10 @@
 
             if (!unnamedPkg.equals(pkg=eltUtils.getPackageOf(unnamedPkg) ) )
                 throw new RuntimeException("Unexpected package for unnamed pkg: " + pkg);
+
+            if (eltUtils.getPackageOf(moduleElt) != null)
+                throw new RuntimeException("Unexpected package for module" +
+                                           moduleElt.getSimpleName());
         }
         return true;
     }