Skip to content

Commit e7513e6

Browse files
samebGuice Team
authored and
Guice Team
committed
Breaks some conformance around sun.misc.Unsafe usage.
PiperOrigin-RevId: 529476888
1 parent bb931fe commit e7513e6

File tree

4 files changed

+19
-53
lines changed

4 files changed

+19
-53
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
33
name: continuous-integration
44
env:
5-
USE_BAZEL_VERSION: '6.1.2'
5+
USE_BAZEL_VERSION: '4.2.2'
66
USE_JAVA_DISTRIBUTION: 'zulu'
77
USE_JAVA_VERSION: '11'
88

core/src/com/google/inject/internal/aop/AnonymousClassDefiner.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.inject.internal.aop;
1818

19+
import java.lang.reflect.Field;
1920
import java.lang.reflect.Method;
2021

2122
/**
@@ -25,16 +26,17 @@
2526
*/
2627
final class AnonymousClassDefiner implements ClassDefiner {
2728

28-
private static final sun.misc.Unsafe THE_UNSAFE;
29+
private static final Object THE_UNSAFE;
2930
private static final Method ANONYMOUS_DEFINE_METHOD;
3031

3132
static {
3233
try {
33-
THE_UNSAFE = UnsafeGetter.getUnsafe();
34-
// defineAnonymousClass was removed in JDK17, so we must refer to it reflectively.
34+
Class<?> unsafeType = Class.forName("sun.misc.Unsafe");
35+
Field theUnsafeField = unsafeType.getDeclaredField("theUnsafe");
36+
theUnsafeField.setAccessible(true);
37+
THE_UNSAFE = theUnsafeField.get(null);
3538
ANONYMOUS_DEFINE_METHOD =
36-
sun.misc.Unsafe.class.getMethod(
37-
"defineAnonymousClass", Class.class, byte[].class, Object[].class);
39+
unsafeType.getMethod("defineAnonymousClass", Class.class, byte[].class, Object[].class);
3840
} catch (ReflectiveOperationException e) {
3941
throw new ExceptionInInitializerError(e);
4042
}

core/src/com/google/inject/internal/aop/HiddenClassDefiner.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,22 @@
2828
*/
2929
final class HiddenClassDefiner implements ClassDefiner {
3030

31-
private static final sun.misc.Unsafe THE_UNSAFE;
32-
private static final Object TRUSTED_LOOKUP_BASE;
33-
private static final long TRUSTED_LOOKUP_OFFSET;
31+
private static final Object THE_UNSAFE;
32+
private static final Object TRUSTED_LOOKUP_OFFSET;
33+
private static final Method GET_OBJECT_METHOD;
3434
private static final Object HIDDEN_CLASS_OPTIONS;
3535
private static final Method HIDDEN_DEFINE_METHOD;
3636

3737
static {
3838
try {
39-
THE_UNSAFE = UnsafeGetter.getUnsafe();
39+
Class<?> unsafeType = Class.forName("sun.misc.Unsafe");
40+
Field theUnsafeField = unsafeType.getDeclaredField("theUnsafe");
41+
theUnsafeField.setAccessible(true);
42+
THE_UNSAFE = theUnsafeField.get(null);
4043
Field trustedLookupField = Lookup.class.getDeclaredField("IMPL_LOOKUP");
41-
TRUSTED_LOOKUP_BASE = THE_UNSAFE.staticFieldBase(trustedLookupField);
42-
TRUSTED_LOOKUP_OFFSET = THE_UNSAFE.staticFieldOffset(trustedLookupField);
44+
Method offsetMethod = unsafeType.getMethod("staticFieldOffset", Field.class);
45+
TRUSTED_LOOKUP_OFFSET = offsetMethod.invoke(THE_UNSAFE, trustedLookupField);
46+
GET_OBJECT_METHOD = unsafeType.getMethod("getObject", Object.class, long.class);
4347
HIDDEN_CLASS_OPTIONS = classOptions("NESTMATE");
4448
HIDDEN_DEFINE_METHOD =
4549
Lookup.class.getMethod(
@@ -52,7 +56,7 @@ final class HiddenClassDefiner implements ClassDefiner {
5256
@Override
5357
public Class<?> define(Class<?> hostClass, byte[] bytecode) throws Exception {
5458
Lookup trustedLookup =
55-
(Lookup) THE_UNSAFE.getObject(TRUSTED_LOOKUP_BASE, TRUSTED_LOOKUP_OFFSET);
59+
(Lookup) GET_OBJECT_METHOD.invoke(THE_UNSAFE, Lookup.class, TRUSTED_LOOKUP_OFFSET);
5660
Lookup definedLookup =
5761
(Lookup)
5862
HIDDEN_DEFINE_METHOD.invoke(

core/src/com/google/inject/internal/aop/UnsafeGetter.java

-40
This file was deleted.

0 commit comments

Comments
 (0)