25
25
26
26
import javax .tools .*;
27
27
import javax .tools .JavaFileObject .Kind ;
28
- import java .io .*;
28
+ import java .io .ByteArrayInputStream ;
29
+ import java .io .IOException ;
30
+ import java .io .InputStream ;
31
+ import java .io .OutputStream ;
29
32
import java .lang .reflect .AccessibleObject ;
30
33
import java .lang .reflect .Field ;
31
34
import java .lang .reflect .InvocationTargetException ;
@@ -42,15 +45,21 @@ class MyJavaFileManager implements JavaFileManager {
42
45
private static final long OVERRIDE_OFFSET ;
43
46
44
47
static {
48
+ long offset = 0 ;
45
49
try {
46
50
Field theUnsafe = Unsafe .class .getDeclaredField ("theUnsafe" );
47
51
theUnsafe .setAccessible (true );
48
52
unsafe = (Unsafe ) theUnsafe .get (null );
49
- Field f = AccessibleObject .class .getDeclaredField ("override" );
50
- OVERRIDE_OFFSET = unsafe .objectFieldOffset (f );
51
53
} catch (Exception ex ) {
52
54
throw new AssertionError (ex );
53
55
}
56
+ try {
57
+ Field f = AccessibleObject .class .getDeclaredField ("override" );
58
+ offset = unsafe .objectFieldOffset (f );
59
+ } catch (NoSuchFieldException e ) {
60
+ offset = 0 ;
61
+ }
62
+ OVERRIDE_OFFSET = offset ;
54
63
}
55
64
56
65
private final StandardJavaFileManager fileManager ;
@@ -197,7 +206,10 @@ private <T> T invokeNamedMethodIfAvailable(final Location location, final String
197
206
if (method .getName ().equals (name ) && method .getParameterTypes ().length == 1 &&
198
207
method .getParameterTypes ()[0 ] == Location .class ) {
199
208
try {
200
- unsafe .putBoolean (method , OVERRIDE_OFFSET , true );
209
+ if (OVERRIDE_OFFSET == 0 )
210
+ method .setAccessible (true );
211
+ else
212
+ unsafe .putBoolean (method , OVERRIDE_OFFSET , true );
201
213
return (T ) method .invoke (fileManager , location );
202
214
} catch (IllegalAccessException | InvocationTargetException e ) {
203
215
throw new UnsupportedOperationException ("Unable to invoke method " + name );
0 commit comments