mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8315458: Implement JEP 463: Implicitly Declared Classes and Instance Main Method (Second Preview)
Reviewed-by: jlahoda, mcimadamore, vromero, rriggs, alanb, mchung
This commit is contained in:
parent
03759e892d
commit
04ad98ed32
48 changed files with 612 additions and 664 deletions
|
@ -190,30 +190,21 @@ import sun.reflect.misc.ReflectUtil;
|
|||
* a class or interface is hidden has no bearing on the characteristics
|
||||
* exposed by the methods of class {@code Class}.
|
||||
*
|
||||
* <h2><a id=unnamedClasses>Unnamed Classes</a></h2>
|
||||
*
|
||||
* A {@code class} file representing an {@linkplain #isUnnamedClass unnamed class}
|
||||
* is generated by a Java compiler from a source file for an unnamed class.
|
||||
* The {@code Class} object representing an unnamed class is top-level,
|
||||
* {@linkplain #isSynthetic synthetic}, and {@code final}. While an
|
||||
* unnamed class does <em>not</em> have a name in its Java source
|
||||
* form, several of the name-related methods of {@code java.lang.Class}
|
||||
* do return non-null and non-empty results for the {@code Class}
|
||||
* object representing an unnamed class.
|
||||
* <h2><a id=implicitClasses>Implicit Classes</a></h2>
|
||||
*
|
||||
* Conventionally, a Java compiler, starting from a source file for an
|
||||
* unnamed class, say {@code HelloWorld.java}, creates a
|
||||
* implicit class, say {@code HelloWorld.java}, creates a
|
||||
* similarly-named {@code class} file, {@code HelloWorld.class}, where
|
||||
* the class stored in that {@code class} file is named {@code
|
||||
* "HelloWorld"}, matching the base names of the source and {@code
|
||||
* class} files.
|
||||
*
|
||||
* For the {@code Class} object of an unnamed class {@code
|
||||
* For the {@code Class} object of an implicit class {@code
|
||||
* HelloWorld}, the methods to get the {@linkplain #getName name} and
|
||||
* {@linkplain #getTypeName type name} return results
|
||||
* equal to {@code "HelloWorld"}. The {@linkplain #getSimpleName
|
||||
* simple name} of such an unnamed class is the empty string and the
|
||||
* {@linkplain #getCanonicalName canonical name} is {@code null}.
|
||||
* simple name} of such an implicit class is {@code "HelloWorld"} and the
|
||||
* {@linkplain #getCanonicalName canonical name} is {@code "HelloWorld"}.
|
||||
*
|
||||
* @param <T> the type of the class modeled by this {@code Class}
|
||||
* object. For example, the type of {@code String.class} is {@code
|
||||
|
@ -1809,7 +1800,7 @@ public final class Class<T> implements java.io.Serializable,
|
|||
/**
|
||||
* Returns the simple name of the underlying class as given in the
|
||||
* source code. An empty string is returned if the underlying class is
|
||||
* {@linkplain #isAnonymousClass() anonymous} or {@linkplain #isUnnamedClass() unnamed}.
|
||||
* {@linkplain #isAnonymousClass() anonymous}.
|
||||
* A {@linkplain #isSynthetic() synthetic class}, one not present
|
||||
* in source code, can have a non-empty name including special
|
||||
* characters, such as "{@code $}".
|
||||
|
@ -1822,9 +1813,6 @@ public final class Class<T> implements java.io.Serializable,
|
|||
* @since 1.5
|
||||
*/
|
||||
public String getSimpleName() {
|
||||
if (isUnnamedClass()) {
|
||||
return "";
|
||||
}
|
||||
ReflectionData<T> rd = reflectionData();
|
||||
String simpleName = rd.simpleName;
|
||||
if (simpleName == null) {
|
||||
|
@ -1874,7 +1862,6 @@ public final class Class<T> implements java.io.Serializable,
|
|||
* <ul>
|
||||
* <li>a {@linkplain #isLocalClass() local class}
|
||||
* <li>a {@linkplain #isAnonymousClass() anonymous class}
|
||||
* <li>an {@linkplain #isUnnamedClass() unnamed class}
|
||||
* <li>a {@linkplain #isHidden() hidden class}
|
||||
* <li>an array whose component type does not have a canonical name</li>
|
||||
* </ul>
|
||||
|
@ -1894,9 +1881,6 @@ public final class Class<T> implements java.io.Serializable,
|
|||
* @since 1.5
|
||||
*/
|
||||
public String getCanonicalName() {
|
||||
if (isUnnamedClass()) {
|
||||
return null;
|
||||
}
|
||||
ReflectionData<T> rd = reflectionData();
|
||||
String canonicalName = rd.canonicalName;
|
||||
if (canonicalName == null) {
|
||||
|
@ -1931,33 +1915,12 @@ public final class Class<T> implements java.io.Serializable,
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return {@code true} if and only if the underlying class
|
||||
* is an unnamed class}
|
||||
*
|
||||
* @apiNote
|
||||
* An unnamed class is not an {@linkplain #isAnonymousClass anonymous class}.
|
||||
*
|
||||
* @since 21
|
||||
*
|
||||
* @jls 7.3 Compilation Units
|
||||
*/
|
||||
@PreviewFeature(feature=PreviewFeature.Feature.UNNAMED_CLASSES,
|
||||
reflective=true)
|
||||
public boolean isUnnamedClass() {
|
||||
return PreviewFeatures.isEnabled() && isSynthetic()
|
||||
&& isTopLevelClass()
|
||||
&& Modifier.isFinal(getModifiers());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns {@code true} if and only if the underlying class
|
||||
* is an anonymous class.
|
||||
*
|
||||
* @apiNote
|
||||
* An anonymous class is not a {@linkplain #isHidden() hidden class}.
|
||||
* An anonymous class is not an {@linkplain #isUnnamedClass() unnamed class}.
|
||||
*
|
||||
* @return {@code true} if and only if this class is an anonymous class.
|
||||
* @since 1.5
|
||||
|
@ -2922,6 +2885,21 @@ public final class Class<T> implements java.io.Serializable,
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the most specific {@code Method} object of this class, super class or
|
||||
* interface that have the specified method name and parameter types.
|
||||
*
|
||||
* @param publicOnly true if only public methods are examined, otherwise all methods
|
||||
* @param name the name of the method
|
||||
* @param parameterTypes the parameter array
|
||||
* @return the {@code Method} object for the method found from this class matching
|
||||
* the specified name and parameters, or null if not found
|
||||
*/
|
||||
Method findMethod(boolean publicOnly, String name, Class<?>... parameterTypes) {
|
||||
PublicMethods.MethodList res = getMethodsRecursive(name, parameterTypes, true, publicOnly);
|
||||
return res == null ? null : getReflectionFactory().copyMethod(res.getMostSpecific());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@code Constructor} object that reflects the specified
|
||||
* constructor of the class represented by this
|
||||
|
@ -3750,7 +3728,7 @@ public final class Class<T> implements java.io.Serializable,
|
|||
PublicMethods.MethodList res = getMethodsRecursive(
|
||||
name,
|
||||
parameterTypes == null ? EMPTY_CLASS_ARRAY : parameterTypes,
|
||||
/* includeStatic */ true);
|
||||
/* includeStatic */ true, /* publicOnly */ true);
|
||||
return res == null ? null : res.getMostSpecific();
|
||||
}
|
||||
|
||||
|
@ -3759,9 +3737,10 @@ public final class Class<T> implements java.io.Serializable,
|
|||
// via ReflectionFactory.copyMethod.
|
||||
private PublicMethods.MethodList getMethodsRecursive(String name,
|
||||
Class<?>[] parameterTypes,
|
||||
boolean includeStatic) {
|
||||
// 1st check declared public methods
|
||||
Method[] methods = privateGetDeclaredMethods(/* publicOnly */ true);
|
||||
boolean includeStatic,
|
||||
boolean publicOnly) {
|
||||
// 1st check declared methods
|
||||
Method[] methods = privateGetDeclaredMethods(publicOnly);
|
||||
PublicMethods.MethodList res = PublicMethods.MethodList
|
||||
.filter(methods, name, parameterTypes, includeStatic);
|
||||
// if there is at least one match among declared methods, we need not
|
||||
|
@ -3775,15 +3754,14 @@ public final class Class<T> implements java.io.Serializable,
|
|||
// we must consult the superclass (if any) recursively...
|
||||
Class<?> sc = getSuperclass();
|
||||
if (sc != null) {
|
||||
res = sc.getMethodsRecursive(name, parameterTypes, includeStatic);
|
||||
res = sc.getMethodsRecursive(name, parameterTypes, includeStatic, publicOnly);
|
||||
}
|
||||
|
||||
// ...and coalesce the superclass methods with methods obtained
|
||||
// from directly implemented interfaces excluding static methods...
|
||||
for (Class<?> intf : getInterfaces(/* cloneArray */ false)) {
|
||||
res = PublicMethods.MethodList.merge(
|
||||
res, intf.getMethodsRecursive(name, parameterTypes,
|
||||
/* includeStatic */ false));
|
||||
res, intf.getMethodsRecursive(name, parameterTypes, /* includeStatic */ false, publicOnly));
|
||||
}
|
||||
|
||||
return res;
|
||||
|
|
|
@ -2345,6 +2345,9 @@ public final class System {
|
|||
public List<Method> getDeclaredPublicMethods(Class<?> klass, String name, Class<?>... parameterTypes) {
|
||||
return klass.getDeclaredPublicMethods(name, parameterTypes);
|
||||
}
|
||||
public Method findMethod(Class<?> klass, boolean publicOnly, String name, Class<?>... parameterTypes) {
|
||||
return klass.findMethod(publicOnly, name, parameterTypes);
|
||||
}
|
||||
public jdk.internal.reflect.ConstantPool getConstantPool(Class<?> klass) {
|
||||
return klass.getConstantPool();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue