< prev index next >
src/java.base/share/classes/java/lang/Class.java
Print this page
@@ -423,10 +423,11 @@
* {@link ModuleReader#open(String)} to read the bytes of a class file
* in a module.</li>
* </ul>
*
* @since 9
+ * @spec JPMS
*/
@CallerSensitive
public static Class<?> forName(Module module, String name) {
Objects.requireNonNull(module);
Objects.requireNonNull(name);
@@ -817,10 +818,11 @@
* loader for this class is returned.
*
* @return the module that this class or interface is a member of
*
* @since 9
+ * @spec JPMS
*/
public Module getModule() {
return module;
}
@@ -922,10 +924,12 @@
*
* <p>If this class represents an array type, a primitive type or void,
* this method returns {@code null}.
*
* @return the package of this class.
+ * @revised 9
+ * @spec JPMS
*/
public Package getPackage() {
if (isPrimitive() || isArray()) {
return null;
}
@@ -949,24 +953,34 @@
* #isAnonymousClass() anonymous class}, then this method is equivalent to
* invoking {@code getPackageName()} on the {@link #getDeclaringClass
* declaring class} of the {@link #getEnclosingMethod enclosing method} or
* {@link #getEnclosingConstructor enclosing constructor}.
*
- * <p> This method returns {@code null} if this class represents an array type,
- * a primitive type or void.
+ * <p> If this class represents an array type then this method returns the
+ * package name of the element type. If this class represents a primitive
+ * type or void then the package name "{@code java.lang}" is returned.
*
* @return the fully qualified package name
*
* @since 9
+ * @spec JPMS
* @jls 6.7 Fully Qualified Names
*/
public String getPackageName() {
String pn = this.packageName;
- if (pn == null && !isArray() && !isPrimitive()) {
- String cn = getName();
+ if (pn == null) {
+ Class<?> c = this;
+ while (c.isArray()) {
+ c = c.getComponentType();
+ }
+ if (c.isPrimitive()) {
+ pn = "java.lang";
+ } else {
+ String cn = c.getName();
int dot = cn.lastIndexOf('.');
pn = (dot != -1) ? cn.substring(0, dot).intern() : "";
+ }
this.packageName = pn;
}
return pn;
}
@@ -2489,25 +2503,30 @@
/**
* Finds a resource with a given name.
*
* <p> If this class is in a named {@link Module Module} then this method
- * will attempt to find the resource in the module by means of the absolute
- * resource name, subject to the rules for encapsulation specified in the
- * {@code Module} {@link Module#getResourceAsStream getResourceAsStream}
- * method.
+ * will attempt to find the resource in the module. This is done by
+ * delegating to the module's class loader {@link
+ * ClassLoader#findResource(String,String) findResource(String,String)}
+ * method, invoking it with the module name and the absolute name of the
+ * resource. Resources in named modules are subject to the rules for
+ * encapsulation specified in the {@code Module} {@link
+ * Module#getResourceAsStream getResourceAsStream} method and so this
+ * method returns {@code null} when the resource is a
+ * non-"{@code .class}" resource in a package that is not open to the
+ * caller's module.
*
* <p> Otherwise, if this class is not in a named module then the rules for
* searching resources associated with a given class are implemented by the
* defining {@linkplain ClassLoader class loader} of the class. This method
* delegates to this object's class loader. If this object was loaded by
* the bootstrap class loader, the method delegates to {@link
* ClassLoader#getSystemResourceAsStream}.
*
- * <p> Before finding a resource in the caller's module or delegation to a
- * class loader, an absolute resource name is constructed from the given
- * resource name using this algorithm:
+ * <p> Before delegation, an absolute resource name is constructed from the
+ * given resource name using this algorithm:
*
* <ul>
*
* <li> If the {@code name} begins with a {@code '/'}
* (<code>'\u002f'</code>), then the absolute name of the resource is the
@@ -2530,11 +2549,15 @@
* resource with this name is found, the resource is in a package
* that is not {@link Module#isOpen(String, Module) open} to at
* least the caller module, or access to the resource is denied
* by the security manager.
* @throws NullPointerException If {@code name} is {@code null}
+ *
+ * @see Module#getResourceAsStream(String)
* @since 1.1
+ * @revised 9
+ * @spec JPMS
*/
@CallerSensitive
public InputStream getResourceAsStream(String name) {
name = resolveName(name);
@@ -2583,14 +2606,20 @@
/**
* Finds a resource with a given name.
*
* <p> If this class is in a named {@link Module Module} then this method
- * will attempt to find the resource in the module by means of the absolute
- * resource name, subject to the rules for encapsulation specified in the
- * {@code Module} {@link Module#getResourceAsStream getResourceAsStream}
- * method.
+ * will attempt to find the resource in the module. This is done by
+ * delegating to the module's class loader {@link
+ * ClassLoader#findResource(String,String) findResource(String,String)}
+ * method, invoking it with the module name and the absolute name of the
+ * resource. Resources in named modules are subject to the rules for
+ * encapsulation specified in the {@code Module} {@link
+ * Module#getResourceAsStream getResourceAsStream} method and so this
+ * method returns {@code null} when the resource is a
+ * non-"{@code .class}" resource in a package that is not open to the
+ * caller's module.
*
* <p> Otherwise, if this class is not in a named module then the rules for
* searching resources associated with a given class are implemented by the
* defining {@linkplain ClassLoader class loader} of the class. This method
* delegates to this object's class loader. If this object was loaded by
@@ -2625,10 +2654,12 @@
* {@link Module#isOpen(String, Module) open} to at least the caller
* module, or access to the resource is denied by the security
* manager.
* @throws NullPointerException If {@code name} is {@code null}
* @since 1.1
+ * @revised 9
+ * @spec JPMS
*/
@CallerSensitive
public URL getResource(String name) {
name = resolveName(name);
< prev index next >