-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use MethodHandles.Lookup IMPL_LOOKUP to support define class after jd…
…k 17. #2659
- Loading branch information
1 parent
6a43273
commit 82ab95c
Showing
3 changed files
with
70 additions
and
1 deletion.
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
44 changes: 44 additions & 0 deletions
44
common/src/main/java/com/taobao/arthas/common/UnsafeUtils.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,44 @@ | ||
package com.taobao.arthas.common; | ||
|
||
|
||
import java.lang.invoke.MethodHandles; | ||
import java.lang.reflect.Field; | ||
|
||
import sun.misc.Unsafe; | ||
|
||
/** | ||
* | ||
* @author hengyunabc 2023-09-21 | ||
* | ||
*/ | ||
public class UnsafeUtils { | ||
public static final Unsafe UNSAFE; | ||
private static MethodHandles.Lookup IMPL_LOOKUP; | ||
|
||
static { | ||
Unsafe unsafe = null; | ||
try { | ||
Field theUnsafeField = Unsafe.class.getDeclaredField("theUnsafe"); | ||
theUnsafeField.setAccessible(true); | ||
unsafe = (Unsafe) theUnsafeField.get(null); | ||
} catch (Throwable ignored) { | ||
// ignored | ||
} | ||
UNSAFE = unsafe; | ||
} | ||
|
||
public static MethodHandles.Lookup implLookup() { | ||
if (IMPL_LOOKUP == null) { | ||
Class<MethodHandles.Lookup> lookupClass = MethodHandles.Lookup.class; | ||
|
||
try { | ||
Field implLookupField = lookupClass.getDeclaredField("IMPL_LOOKUP"); | ||
long offset = UNSAFE.staticFieldOffset(implLookupField); | ||
IMPL_LOOKUP = (MethodHandles.Lookup) UNSAFE.getObject(UNSAFE.staticFieldBase(implLookupField), offset); | ||
} catch (Throwable e) { | ||
// ignored | ||
} | ||
} | ||
return IMPL_LOOKUP; | ||
} | ||
} |