Review Request: JDK-8235521: Replacement API for Unsafe::ensureClassInitialized
Paul Sandoz
paul.sandoz at oracle.com
Thu Jun 4 00:16:28 UTC 2020
Hi Mandy,
Did you consider an alternative name, such as:
/**
* Initializes {@code targetClass}, if not already initialized.
* ...
*/
public Class<?> initializeClass(Class<?> targetClass) ...
?
What about the companion Unsafe.shouldBeInitialized? I have seen (and written) code like this:
if (UNSAFE.shouldBeInitialized(cls)) {
UNSAFE.ensureClassInitialized(cls);
}
Although it seems redundant to perform the check, suggesting it is not needed.
I believe there is also some gnarly case during a clinit of C where ensureClassInitialized(C) is called and a subsequent call to shouldBeInitialized(C) returns true. Is that correct? If so it makes me reluctant to expose it and try and explain the edge cases.
Maybe if we don’t think Unsafe.shouldBeInitialized needs a supported equivalent we can still deprecate it?
Paul.
> On Jun 3, 2020, at 4:16 PM, Mandy Chung <mandy.chung at oracle.com> wrote:
>
> This proposes a new `Lookup::ensureClassInitialized` API as a replacement
> for `sun.misc.Unsafe::ensureClassInitialized`.The Lookup object must have
> the access to the given class being initialized.
>
> CSR: https://bugs.openjdk.java.net/browse/JDK-8245871
>
> webrev:
> http://cr.openjdk.java.net/~mchung/jdk15/webrevs/8235521/webrev.01/
>
> This patch converts the JDK use of `jdk.internal.misc.Unsafe::ensureClassInitialized` to
> call this new API where appropriate. `Unsafe::ensureClassInitialized` remains for java.lang.invoke.* internal implementation use.
>
> A framework can use `Lookup::privateLookupIn` to access a class in another module if the module authorizes it by opening the packages for it to access. Or a user can produce a less privileged lookup by Lookup::dropLookupMode and pass it to the framework.
>
> `Class::ensureInitialized` was considered as an alternative. The downside for this option is that it would be caller sensitive in order to ensure the caller has the proper access to this class. And it may not work for frameworks which have no access to user classes. It would have to fallback toClass::forName` limited solution.
>
> Thanks
> Mandy
>
More information about the core-libs-dev
mailing list