private FieldSetter(Field field) { this.field = field; field.setAccessible(true); }
public FieldInfo(Field field, String columnName, Field hashFor, String hashForType){ this.field = field; this.columnName = columnName; this.hashFor = hashFor; this.hashForType = hashForType; field.setAccessible(true); if (hashFor != null) { hashFor.setAccessible(true); } }
public AntWithFindResourceClassLoader(ClassLoader parent, boolean parentFirst) { super(parent, parentFirst); try { Field $pathComponents = AntClassLoader.class.getDeclaredField("pathComponents"); $pathComponents.setAccessible(true); pathComponents = (Vector)$pathComponents.get(this); } catch (NoSuchFieldException | IllegalAccessException e) { throw new Error(e); } }
public long getContactAddr(Contact contact) { try { Field addrField = contact.getClass().getDeclaredField("addr"); addrField.setAccessible(true); return addrField.getLong(contact); } catch (Exception e) { e.printStackTrace(); return 0; } } }
@java.lang.SuppressWarnings("unchecked") private static void setCurrentRequest(RequestImpl request) { try { Field field = Stapler.class.getDeclaredField("CURRENT_REQUEST"); field.setAccessible(true); ((ThreadLocal<RequestImpl>) field.get(null)).set(request); } catch (Exception x) { LOG.log(Level.WARNING, "cannot mock Stapler request", x); } }
private static @Nullable Field getInheritableThreadLocalsField() { try { Field inheritableThreadLocals = Thread.class.getDeclaredField("inheritableThreadLocals"); inheritableThreadLocals.setAccessible(true); return inheritableThreadLocals; } catch (Throwable t) { logger.log( Level.INFO, "Couldn't access Thread.inheritableThreadLocals. Reference finalizer threads will " + "inherit thread local values."); return null; } }
@Override public sun.misc.Unsafe run() throws Exception { Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class; for (java.lang.reflect.Field f : k.getDeclaredFields()) { f.setAccessible(true); Object x = f.get(null); if (k.isInstance(x)) { return k.cast(x); } } throw new NoSuchFieldError("the Unsafe"); } });
public static Map<String, Field> getBeanPropertyFields(Class cl) { Map<String, Field> properties = new HashMap<String, Field>(); for (; cl != null; cl = cl.getSuperclass()) { Field[] fields = cl.getDeclaredFields(); for (Field field : fields) { if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) { continue; } field.setAccessible(true); properties.put(field.getName(), field); } } return properties; }
/** * Make the given field accessible, explicitly setting it accessible if * necessary. The {@code setAccessible(true)} method is only called * when actually necessary, to avoid unnecessary conflicts with a JVM * SecurityManager (if active). * @param field the field to make accessible * @see java.lang.reflect.Field#setAccessible */ @SuppressWarnings("deprecation") // on JDK 9 public static void makeAccessible(Field field) { if ((!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers()) || Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) { field.setAccessible(true); } }
public static Map<String, Field> getBeanPropertyFields(Class cl) { Map<String, Field> properties = new HashMap<String, Field>(); for (; cl != null; cl = cl.getSuperclass()) { Field[] fields = cl.getDeclaredFields(); for (Field field : fields) { if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) { continue; } field.setAccessible(true); properties.put(field.getName(), field); } } return properties; }
private static Map<?, ?> getSerializableFactoryMap() throws Exception { Field field = DefaultListableBeanFactory.class.getDeclaredField("serializableFactories"); field.setAccessible(true); return (Map<?, ?>) field.get(DefaultListableBeanFactory.class); }
@SuppressWarnings("unchecked") private static Map<ClassLoader, WebApplicationContext> getCurrentContextPerThreadFromContextLoader() { try { Field field = ContextLoader.class.getDeclaredField("currentContextPerThread"); field.setAccessible(true); return (Map<ClassLoader, WebApplicationContext>) field.get(null); } catch (Exception ex) { throw new IllegalStateException(ex); } }
private void checkHelperVersion(ClassLoader classLoader, String expectedHelperClassName) throws Exception { // Make sure we are actually running with the expected helper implementation Class<?> abstractFutureClass = classLoader.loadClass(AbstractFuture.class.getName()); Field helperField = abstractFutureClass.getDeclaredField("ATOMIC_HELPER"); helperField.setAccessible(true); assertEquals(expectedHelperClassName, helperField.get(null).getClass().getSimpleName()); }
private void checkHelperVersion(ClassLoader classLoader, String expectedHelperClassName) throws Exception { // Make sure we are actually running with the expected helper implementation Class<?> abstractFutureClass = classLoader.loadClass(AggregateFutureState.class.getName()); Field helperField = abstractFutureClass.getDeclaredField("ATOMIC_HELPER"); helperField.setAccessible(true); assertEquals(expectedHelperClassName, helperField.get(null).getClass().getSimpleName()); }
@GwtIncompatible // reflection private static int bucketsOf(HashMap<?, ?> hashMap) throws Exception { Field tableField = HashMap.class.getDeclaredField("table"); tableField.setAccessible(true); Object[] table = (Object[]) tableField.get(hashMap); // In JDK8, table is set lazily, so it may be null. return table == null ? 0 : table.length; }
/** * Resets MBeanServerFactory and ManagementFactory to a known consistent state. * This involves releasing all currently registered MBeanServers and resetting * the platformMBeanServer to null. */ public static void resetMBeanServers() throws Exception { for (MBeanServer server : MBeanServerFactory.findMBeanServer(null)) { MBeanServerFactory.releaseMBeanServer(server); } Field field = ManagementFactory.class.getDeclaredField("platformMBeanServer"); field.setAccessible(true); field.set(null, null); }
@Override public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { TestBean tb = (TestBean) bean; try { Field f = TestBean.class.getDeclaredField("name"); f.setAccessible(true); f.set(tb, nameSetOnField); return !skipPropertyPopulation; } catch (Exception ex) { fail("Unexpected exception: " + ex); // Keep compiler happy about return throw new IllegalStateException(); } } });
private void assertIsCompiled(Expression expression) { try { Field field = SpelExpression.class.getDeclaredField("compiledAst"); field.setAccessible(true); Object object = field.get(expression); assertNotNull(object); } catch (Exception ex) { fail(ex.toString()); } }
@Test public void defaultRefreshCheckDelay() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext(CONFIG); Advised advised = (Advised) context.getBean("testBean"); AbstractRefreshableTargetSource targetSource = ((AbstractRefreshableTargetSource) advised.getTargetSource()); Field field = AbstractRefreshableTargetSource.class.getDeclaredField("refreshCheckDelay"); field.setAccessible(true); long delay = ((Long) field.get(targetSource)).longValue(); assertEquals(5000L, delay); }
@Test public void isDefaultJndiEnvironmentAvailableFalse() throws Exception { Field builderField = NamingManager.class.getDeclaredField("initctx_factory_builder"); builderField.setAccessible(true); Object oldBuilder = builderField.get(null); builderField.set(null, null); try { assertThat(JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable(), equalTo(false)); } finally { builderField.set(null, oldBuilder); } }