331
332 private static Object staticFieldValue(Class<?> c, String name) {
333 try {
334 return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
335 @Override
336 public Object run() throws Exception {
337 Field f = c.getDeclaredField(name);
338 return f.get(null);
339 }
340 });
341 } catch (PrivilegedActionException e) {
342 throw new InternalError(e.getCause());
343 }
344 }
345
346 @Override
347 public Class<?> install(final String className, final byte[] bytecode) {
348 try {
349 ANONYMOUS_INSTALLED_SCRIPT_COUNT.increment();
350 // Workaround: define it as a hidden nestmate so that the hostLookup can find private members
351 return hostLookup.defineHiddenClass(bytecode, true, ClassOption.NESTMATE, ClassOption.WEAK).lookupClass();
352 } catch (IllegalAccessException e) {
353 throw new InternalError(e);
354 }
355 }
356
357 @Override
358 public void initialize(final Collection<Class<?>> classes, final Source source, final Object[] constants) {
359 AccessController.doPrivileged(new PrivilegedAction<Void>() {
360 @Override
361 public Void run() {
362 for (final Class<?> clazz : classes) {
363 try {
364 //use reflection to write source and constants table to installed classes
365 MethodHandle sourceField = hostLookup.findStaticSetter(clazz, SOURCE.symbolName(), Source.class);
366 sourceField.invokeExact(source);
367 MethodHandle constantsField = hostLookup.findStaticSetter(clazz, CONSTANTS.symbolName(), Object[].class);
368 constantsField.invokeExact(constants);
369 } catch (Throwable e) {
370 throw new RuntimeException(e);
371 }
|
331
332 private static Object staticFieldValue(Class<?> c, String name) {
333 try {
334 return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
335 @Override
336 public Object run() throws Exception {
337 Field f = c.getDeclaredField(name);
338 return f.get(null);
339 }
340 });
341 } catch (PrivilegedActionException e) {
342 throw new InternalError(e.getCause());
343 }
344 }
345
346 @Override
347 public Class<?> install(final String className, final byte[] bytecode) {
348 try {
349 ANONYMOUS_INSTALLED_SCRIPT_COUNT.increment();
350 // Workaround: define it as a hidden nestmate so that the hostLookup can find private members
351 return hostLookup.defineHiddenClass(bytecode, true, ClassOption.NESTMATE).lookupClass();
352 } catch (IllegalAccessException e) {
353 throw new InternalError(e);
354 }
355 }
356
357 @Override
358 public void initialize(final Collection<Class<?>> classes, final Source source, final Object[] constants) {
359 AccessController.doPrivileged(new PrivilegedAction<Void>() {
360 @Override
361 public Void run() {
362 for (final Class<?> clazz : classes) {
363 try {
364 //use reflection to write source and constants table to installed classes
365 MethodHandle sourceField = hostLookup.findStaticSetter(clazz, SOURCE.symbolName(), Source.class);
366 sourceField.invokeExact(source);
367 MethodHandle constantsField = hostLookup.findStaticSetter(clazz, CONSTANTS.symbolName(), Object[].class);
368 constantsField.invokeExact(constants);
369 } catch (Throwable e) {
370 throw new RuntimeException(e);
371 }
|