22
22
import org .jetbrains .annotations .Nullable ;
23
23
import org .slf4j .Logger ;
24
24
import org .slf4j .LoggerFactory ;
25
+ import sun .misc .Unsafe ;
25
26
26
27
import javax .tools .JavaCompiler ;
27
28
import javax .tools .StandardJavaFileManager ;
28
29
import javax .tools .ToolProvider ;
29
30
import java .io .*;
30
31
import java .lang .management .ManagementFactory ;
32
+ import java .lang .reflect .AccessibleObject ;
33
+ import java .lang .reflect .Field ;
31
34
import java .lang .reflect .InvocationTargetException ;
32
35
import java .lang .reflect .Method ;
33
36
import java .nio .charset .Charset ;
@@ -50,9 +53,14 @@ public enum CompilerUtils {
50
53
51
54
static {
52
55
try {
56
+ Field theUnsafe = Unsafe .class .getDeclaredField ("theUnsafe" );
57
+ theUnsafe .setAccessible (true );
58
+ Unsafe u = (Unsafe ) theUnsafe .get (null );
53
59
DEFINE_CLASS_METHOD = ClassLoader .class .getDeclaredMethod ("defineClass" , String .class , byte [].class , int .class , int .class );
54
- DEFINE_CLASS_METHOD .setAccessible (true );
55
- } catch (NoSuchMethodException e ) {
60
+ Field f = AccessibleObject .class .getDeclaredField ("override" );
61
+ long offset = u .objectFieldOffset (f );
62
+ u .putBoolean (DEFINE_CLASS_METHOD , offset , true );
63
+ } catch (NoSuchMethodException | IllegalAccessException | NoSuchFieldException e ) {
56
64
throw new AssertionError (e );
57
65
}
58
66
}
0 commit comments