@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; }
public FieldTypeProvider(Field field) { this.fieldName = field.getName(); this.declaringClass = field.getDeclaringClass(); this.field = field; }
public static boolean isPublicInstanceField(Field field) { return Modifier.isPublic(field.getModifiers()) && !Modifier.isStatic(field.getModifiers()) && !Modifier.isFinal(field.getModifiers()) && !field.isSynthetic(); }
/** * 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); } }
@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); } }
@Override @Nullable public Object getValue() throws Exception { try { ReflectionUtils.makeAccessible(this.field); return this.field.get(getWrappedInstance()); } catch (IllegalAccessException ex) { throw new InvalidPropertyException(getWrappedClass(), this.field.getName(), "Field is not accessible", ex); } }
public RuntimeTestWalker(ShadowMatch shadowMatch) { try { ReflectionUtils.makeAccessible(residualTestField); this.runtimeTest = (Test) residualTestField.get(shadowMatch); } catch (IllegalAccessException ex) { throw new IllegalStateException(ex); } }
/** * 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 void deserialize(AbstractHessianInput in, Object obj) throws IOException { Object value = null; try { Type[] types = ((ParameterizedType)_field.getGenericType()).getActualTypeArguments(); value = in.readObject(_field.getType(), isPrimitive(types[0]) ? (Class<?>)types[0] : null ); _field.set(obj, value); } catch (Exception e) { logDeserializeError(_field, obj, value, e); } } }
Field field = targetClass.getDeclaredField(fieldName); field.setAccessible(true); field.set(object, value);
public static <T extends Annotation> T getAnnotation(Class<?> clazz, String fieldName, Class<T> annotationClass) { try { Field field = getDeclaredField(clazz, fieldName); if (!field.isAccessible()) { field.setAccessible(true); } return field.getAnnotation(annotationClass); } catch (NoSuchFieldException e) { return null; } }
static void logDeserializeError(Field field, Object obj, Object value, Throwable e) throws IOException { String fieldName = (field.getDeclaringClass().getName() + "." + field.getName()); if (e instanceof HessianFieldException) throw (HessianFieldException) e; else if (e instanceof IOException) throw new HessianFieldException(fieldName + ": " + e.getMessage(), e); if (value != null) throw new HessianFieldException(fieldName + ": " + value.getClass().getName() + " (" + value + ")" + " cannot be assigned to '" + field.getType().getName() + "'", e); else throw new HessianFieldException(fieldName + ": " + field.getType().getName() + " cannot be assigned from null", e); }
private boolean isTransient(Class c, Field field) { if (Modifier.isTransient(field.getModifiers())) return true; while (c.getName().indexOf("$") >= 0) { c = c.getSuperclass(); // patch fuer reallive queries, kontraktor spore } if ( field.getName().startsWith("this$") && c.getAnnotation(AnonymousTransient.class) != null ) return true; return (c.getAnnotation(Transient.class) != null && field.getAnnotation(Serialize.class) == null); }
protected void storeIndex(Field field, Object me, Object arg) { if (field.getAnnotation(RIndex.class) != null) { NamingScheme namingScheme = objectBuilder.getNamingScheme(me.getClass().getSuperclass()); String indexName = namingScheme.getIndexName(me.getClass().getSuperclass(), field.getName()); RSetMultimap<Object, Object> map = redisson.getSetMultimap(indexName, namingScheme.getCodec()); map.put(arg, ((RLiveObject) me).getLiveObjectId()); } }
@Override public void setValue(@Nullable Object value) throws Exception { try { ReflectionUtils.makeAccessible(this.field); this.field.set(getWrappedInstance(), value); } catch (IllegalAccessException ex) { throw new InvalidPropertyException(getWrappedClass(), this.field.getName(), "Field is not accessible", ex); } } }
private FieldSetter(Field field) { this.field = field; field.setAccessible(true); }