mirror of
https://github.com/google/guice.git
synced 2024-04-21 12:32:36 +00:00
Breaks some conformance around sun.misc.Unsafe usage.
PiperOrigin-RevId: 529476888
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
||||
name: continuous-integration
|
||||
env:
|
||||
USE_BAZEL_VERSION: '6.1.2'
|
||||
USE_BAZEL_VERSION: '4.2.2'
|
||||
USE_JAVA_DISTRIBUTION: 'zulu'
|
||||
USE_JAVA_VERSION: '11'
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package com.google.inject.internal.aop;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
@@ -25,16 +26,17 @@ import java.lang.reflect.Method;
|
||||
*/
|
||||
final class AnonymousClassDefiner implements ClassDefiner {
|
||||
|
||||
private static final sun.misc.Unsafe THE_UNSAFE;
|
||||
private static final Object THE_UNSAFE;
|
||||
private static final Method ANONYMOUS_DEFINE_METHOD;
|
||||
|
||||
static {
|
||||
try {
|
||||
THE_UNSAFE = UnsafeGetter.getUnsafe();
|
||||
// defineAnonymousClass was removed in JDK17, so we must refer to it reflectively.
|
||||
Class<?> unsafeType = Class.forName("sun.misc.Unsafe");
|
||||
Field theUnsafeField = unsafeType.getDeclaredField("theUnsafe");
|
||||
theUnsafeField.setAccessible(true);
|
||||
THE_UNSAFE = theUnsafeField.get(null);
|
||||
ANONYMOUS_DEFINE_METHOD =
|
||||
sun.misc.Unsafe.class.getMethod(
|
||||
"defineAnonymousClass", Class.class, byte[].class, Object[].class);
|
||||
unsafeType.getMethod("defineAnonymousClass", Class.class, byte[].class, Object[].class);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
throw new ExceptionInInitializerError(e);
|
||||
}
|
||||
|
||||
@@ -28,18 +28,22 @@ import java.lang.reflect.Method;
|
||||
*/
|
||||
final class HiddenClassDefiner implements ClassDefiner {
|
||||
|
||||
private static final sun.misc.Unsafe THE_UNSAFE;
|
||||
private static final Object TRUSTED_LOOKUP_BASE;
|
||||
private static final long TRUSTED_LOOKUP_OFFSET;
|
||||
private static final Object THE_UNSAFE;
|
||||
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 {
|
||||
THE_UNSAFE = UnsafeGetter.getUnsafe();
|
||||
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");
|
||||
TRUSTED_LOOKUP_BASE = THE_UNSAFE.staticFieldBase(trustedLookupField);
|
||||
TRUSTED_LOOKUP_OFFSET = THE_UNSAFE.staticFieldOffset(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(
|
||||
@@ -52,7 +56,7 @@ final class HiddenClassDefiner implements ClassDefiner {
|
||||
@Override
|
||||
public Class<?> define(Class<?> hostClass, byte[] bytecode) throws Exception {
|
||||
Lookup trustedLookup =
|
||||
(Lookup) THE_UNSAFE.getObject(TRUSTED_LOOKUP_BASE, TRUSTED_LOOKUP_OFFSET);
|
||||
(Lookup) GET_OBJECT_METHOD.invoke(THE_UNSAFE, Lookup.class, TRUSTED_LOOKUP_OFFSET);
|
||||
Lookup definedLookup =
|
||||
(Lookup)
|
||||
HIDDEN_DEFINE_METHOD.invoke(
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2023 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;
|
||||
|
||||
final class UnsafeGetter {
|
||||
|
||||
private UnsafeGetter() {}
|
||||
|
||||
static sun.misc.Unsafe getUnsafe() throws ReflectiveOperationException {
|
||||
try {
|
||||
return sun.misc.Unsafe.getUnsafe();
|
||||
} catch (SecurityException unusedFallbackToReflection) {
|
||||
}
|
||||
// Note that we do not do this in a privileged action because we expect we're already in a
|
||||
// privileged block (from UnsafeClassDefiner).
|
||||
Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
|
||||
for (java.lang.reflect.Field f : k.getDeclaredFields()) {
|
||||
f.setAccessible(true);
|
||||
Object x = f.get(null);
|
||||
if (k.isInstance(x)) {
|
||||
return k.cast(x);
|
||||
}
|
||||
}
|
||||
throw new NoSuchFieldError("the Unsafe");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user