8281001: Class::forName(String) defaults to system class loader if the caller is null

Reviewed-by: mchung
This commit is contained in:
Tim Prinzing 2022-06-08 16:21:55 +00:00 committed by Mandy Chung
parent c68419f2f7
commit b92ce2699b
4 changed files with 12 additions and 7 deletions

View file

@ -357,6 +357,11 @@ public final class Class<T> implements java.io.Serializable,
* A call to {@code forName("X")} causes the class named
* {@code X} to be initialized.
*
* <p>
* In cases where this method is called from a context where there is no
* caller frame on the stack (e.g. when called directly from a JNI
* attached thread), the system class loader is used.
*
* @param className the fully qualified name of the desired class.
* @return the {@code Class} object for the class with the
* specified name.
@ -380,7 +385,9 @@ public final class Class<T> implements java.io.Serializable,
@CallerSensitiveAdapter
private static Class<?> forName(String className, Class<?> caller)
throws ClassNotFoundException {
return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
ClassLoader loader = (caller == null) ? ClassLoader.getSystemClassLoader()
: ClassLoader.getClassLoader(caller);
return forName0(className, true, loader, caller);
}
/**