Skip to content

Commit

Permalink
Breaks some conformance around sun.misc.Unsafe usage.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 529476888
  • Loading branch information
sameb authored and Guice Team committed May 4, 2023
1 parent bb931fe commit e7513e6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.inject.internal.aop;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
Expand All @@ -25,16 +26,17 @@
*/
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);
}
Expand Down
18 changes: 11 additions & 7 deletions core/src/com/google/inject/internal/aop/HiddenClassDefiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,22 @@
*/
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(
Expand All @@ -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(
Expand Down
40 changes: 0 additions & 40 deletions core/src/com/google/inject/internal/aop/UnsafeGetter.java

This file was deleted.

0 comments on commit e7513e6

Please sign in to comment.