Skip to content

GH-3765 Resolve Lookup ctor lazily via reflection #3781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2021 the original author or authors.
* Copyright 2015-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.function.Supplier;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
Expand Down Expand Up @@ -119,29 +120,36 @@ private Lookup getLookup(Class<?> declaringClass, Method privateLookupIn) {
*/
OPEN {

@Nullable
private final transient Constructor<Lookup> constructor;

{
Constructor<Lookup> ctor = null;
try {
ctor = Lookup.class.getDeclaredConstructor(Class.class);
ReflectionUtils.makeAccessible(ctor);
}
catch (Exception ex) {
// this is the signal that we are on Java 9 (encapsulated) and can't use the accessible constructor
// approach.
if (!ex.getClass().getName().equals("java.lang.reflect.InaccessibleObjectException")) {
throw new IllegalStateException(ex);
}
}
this.constructor = ctor;
}
private volatile boolean constructorResolved;

private Constructor<Lookup> constructor;

private final Supplier<Constructor<Lookup>> constructorSupplier =
() -> {
if (!this.constructorResolved) {
Constructor<Lookup> ctor = null;
try {
ctor = Lookup.class.getDeclaredConstructor(Class.class);
ReflectionUtils.makeAccessible(ctor);
}
catch (Exception ex) {
// this is the signal that we are on Java 9 (encapsulated) and can't use the accessible
// constructor approach.
if (!ex.getClass().getName().equals("java.lang.reflect.InaccessibleObjectException")) {
throw new IllegalStateException(ex);
}
}
this.constructor = ctor;
this.constructorResolved = true;
}
return this.constructor;
};

@Override
MethodHandle lookup(Method method) throws ReflectiveOperationException {
if (this.constructor != null) {
return this.constructor.newInstance(method.getDeclaringClass())
Constructor<Lookup> lookupConstructor = this.constructorSupplier.get();
if (lookupConstructor != null) {
return lookupConstructor.newInstance(method.getDeclaringClass())
.unreflectSpecial(method, method.getDeclaringClass());
}
else {
Expand All @@ -151,7 +159,7 @@ MethodHandle lookup(Method method) throws ReflectiveOperationException {

@Override
boolean isAvailable() {
return this.constructor != null;
return this.constructorSupplier.get() != null;
}

},
Expand All @@ -160,7 +168,6 @@ boolean isAvailable() {
* Fallback {@link MethodHandle} lookup using {@link MethodHandles#lookup() public lookup}.
*/
FALLBACK {

@Override
MethodHandle lookup(Method method) throws ReflectiveOperationException {
return doLookup(method, MethodHandles.lookup());
Expand Down Expand Up @@ -191,7 +198,7 @@ private static MethodHandle doLookup(Method method, Lookup lookup) throws Reflec
* @return the {@link MethodHandleLookup}
* @throws IllegalStateException if no {@link MethodHandleLookup} is available.
*/
public static MethodHandleLookup getMethodHandleLookup() {
static MethodHandleLookup getMethodHandleLookup() {
for (MethodHandleLookup it : MethodHandleLookup.values()) {
if (it.isAvailable()) {
return it;
Expand Down