2222import org .jetbrains .annotations .Nullable ;
2323import org .slf4j .Logger ;
2424import org .slf4j .LoggerFactory ;
25+ import sun .misc .Unsafe ;
2526
2627import javax .tools .JavaCompiler ;
2728import javax .tools .StandardJavaFileManager ;
2829import javax .tools .ToolProvider ;
2930import java .io .*;
3031import java .lang .management .ManagementFactory ;
32+ import java .lang .reflect .AccessibleObject ;
33+ import java .lang .reflect .Field ;
3134import java .lang .reflect .InvocationTargetException ;
3235import java .lang .reflect .Method ;
3336import java .nio .charset .Charset ;
@@ -50,9 +53,14 @@ public enum CompilerUtils {
5053
5154 static {
5255 try {
56+ Field theUnsafe = Unsafe .class .getDeclaredField ("theUnsafe" );
57+ theUnsafe .setAccessible (true );
58+ Unsafe u = (Unsafe ) theUnsafe .get (null );
5359 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 ) {
5664 throw new AssertionError (e );
5765 }
5866 }
0 commit comments