-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update unsafe class defining to work on Java 17+ (close #1529)
Java 17 has removed Unsafe.defineAnonymousClass in favor of Lookup.defineHiddenClass PiperOrigin-RevId: 399274715
- Loading branch information
Showing
10 changed files
with
277 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
core/src/com/google/inject/internal/aop/AnonymousClassDefiner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (C) 2021 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.inject.internal.aop; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* {@link ClassDefiner} that defines classes using {@code sun.misc.Unsafe#defineAnonymousClass}. | ||
* | ||
* @author [email protected] (Stuart McCulloch) | ||
*/ | ||
final class AnonymousClassDefiner implements ClassDefiner { | ||
|
||
private static final Object THE_UNSAFE; | ||
private static final Method ANONYMOUS_DEFINE_METHOD; | ||
|
||
static { | ||
try { | ||
Class<?> unsafeType = Class.forName("sun.misc.Unsafe"); | ||
Field theUnsafeField = unsafeType.getDeclaredField("theUnsafe"); | ||
theUnsafeField.setAccessible(true); | ||
THE_UNSAFE = theUnsafeField.get(null); | ||
ANONYMOUS_DEFINE_METHOD = | ||
unsafeType.getMethod("defineAnonymousClass", Class.class, byte[].class, Object[].class); | ||
} catch (ReflectiveOperationException e) { | ||
throw new ExceptionInInitializerError(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Class<?> define(Class<?> hostClass, byte[] bytecode) throws Exception { | ||
return (Class<?>) ANONYMOUS_DEFINE_METHOD.invoke(THE_UNSAFE, hostClass, bytecode, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
core/src/com/google/inject/internal/aop/GeneratedClassDefiner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (C) 2021 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.inject.internal.aop; | ||
|
||
import java.util.function.BiFunction; | ||
|
||
/** | ||
* {@link ClassDefiner} that defines classes using a generated access function. | ||
* | ||
* @author [email protected] (Stuart McCulloch) | ||
*/ | ||
final class GeneratedClassDefiner implements ClassDefiner { | ||
|
||
private final BiFunction<ClassLoader, byte[], Class<?>> defineAccess; | ||
|
||
GeneratedClassDefiner(BiFunction<ClassLoader, byte[], Class<?>> defineAccess) { | ||
this.defineAccess = defineAccess; | ||
} | ||
|
||
@Override | ||
public Class<?> define(Class<?> hostClass, byte[] bytecode) { | ||
return defineAccess.apply(hostClass.getClassLoader(), bytecode); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
core/src/com/google/inject/internal/aop/HiddenClassDefiner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (C) 2021 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.inject.internal.aop; | ||
|
||
import java.lang.invoke.MethodHandles.Lookup; | ||
import java.lang.reflect.Array; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
|
||
/** | ||
* {@link ClassDefiner} that defines classes using {@code MethodHandles.Lookup#defineHiddenClass}. | ||
* | ||
* @author [email protected] (Stuart McCulloch) | ||
*/ | ||
final class HiddenClassDefiner implements ClassDefiner { | ||
|
||
private static final Object THE_UNSAFE; | ||
private static final Object TRUSTED_LOOKUP_BASE; | ||
private static final Object TRUSTED_LOOKUP_OFFSET; | ||
private static final Method GET_OBJECT_METHOD; | ||
private static final Object HIDDEN_CLASS_OPTIONS; | ||
private static final Method HIDDEN_DEFINE_METHOD; | ||
|
||
static { | ||
try { | ||
Class<?> unsafeType = Class.forName("sun.misc.Unsafe"); | ||
Field theUnsafeField = unsafeType.getDeclaredField("theUnsafe"); | ||
theUnsafeField.setAccessible(true); | ||
THE_UNSAFE = theUnsafeField.get(null); | ||
Field trustedLookupField = Lookup.class.getDeclaredField("IMPL_LOOKUP"); | ||
Method baseMethod = unsafeType.getMethod("staticFieldBase", Field.class); | ||
TRUSTED_LOOKUP_BASE = baseMethod.invoke(THE_UNSAFE, trustedLookupField); | ||
Method offsetMethod = unsafeType.getMethod("staticFieldOffset", Field.class); | ||
TRUSTED_LOOKUP_OFFSET = offsetMethod.invoke(THE_UNSAFE, trustedLookupField); | ||
GET_OBJECT_METHOD = unsafeType.getMethod("getObject", Object.class, long.class); | ||
HIDDEN_CLASS_OPTIONS = classOptions("NESTMATE"); | ||
HIDDEN_DEFINE_METHOD = | ||
Lookup.class.getMethod( | ||
"defineHiddenClass", byte[].class, boolean.class, HIDDEN_CLASS_OPTIONS.getClass()); | ||
} catch (ReflectiveOperationException e) { | ||
throw new ExceptionInInitializerError(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Class<?> define(Class<?> hostClass, byte[] bytecode) throws Exception { | ||
Lookup trustedLookup = | ||
(Lookup) GET_OBJECT_METHOD.invoke(THE_UNSAFE, TRUSTED_LOOKUP_BASE, TRUSTED_LOOKUP_OFFSET); | ||
Lookup definedLookup = | ||
(Lookup) | ||
HIDDEN_DEFINE_METHOD.invoke( | ||
trustedLookup.in(hostClass), bytecode, false, HIDDEN_CLASS_OPTIONS); | ||
return definedLookup.lookupClass(); | ||
} | ||
|
||
/** Creates {@link MethodHandles.Lookup.ClassOption} array with the named options. */ | ||
@SuppressWarnings("unchecked") | ||
private static Object classOptions(String... options) throws ClassNotFoundException { | ||
@SuppressWarnings("rawtypes") // Unavoidable, only way to use Enum.valueOf | ||
Class optionClass = Class.forName(Lookup.class.getName() + "$ClassOption"); | ||
Object classOptions = Array.newInstance(optionClass, options.length); | ||
for (int i = 0; i < options.length; i++) { | ||
Array.set(classOptions, i, Enum.valueOf(optionClass, options[i])); | ||
} | ||
return classOptions; | ||
} | ||
} |
Oops, something went wrong.