Refine search
/** * Reads a static {@link Field}. * * @param field * to read * @param forceAccess * whether to break scope restrictions using the * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. * @return the field value * @throws IllegalArgumentException * if the field is {@code null} or not {@code static} * @throws IllegalAccessException * if the field is not made accessible */ public static Object readStaticField(final Field field, final boolean forceAccess) throws IllegalAccessException { Validate.isTrue(field != null, "The field must not be null"); Validate.isTrue(Modifier.isStatic(field.getModifiers()), "The field '%s' is not static", field.getName()); return readField(field, (Object) null, forceAccess); }
/** * Reads the named {@link Field}. Superclasses will be considered. * * @param target * the object to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @param forceAccess * whether to break scope restrictions using the * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only * match {@code public} fields. * @return the field value * @throws IllegalArgumentException * if {@code target} is {@code null}, or the field name is blank or empty or could not be found * @throws IllegalAccessException * if the named field is not made accessible */ public static Object readField(final Object target, final String fieldName, final boolean forceAccess) throws IllegalAccessException { Validate.isTrue(target != null, "target object must not be null"); final Class<?> cls = target.getClass(); final Field field = getField(cls, fieldName, forceAccess); Validate.isTrue(field != null, "Cannot locate field %s on %s", fieldName, cls); // already forced access above, don't repeat it here: return readField(field, target, false); }
/** * Writes a static {@link Field}. * * @param field * to write * @param value * to set * @param forceAccess * whether to break scope restrictions using the * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only * match {@code public} fields. * @throws IllegalArgumentException * if the field is {@code null} or not {@code static}, or {@code value} is not assignable * @throws IllegalAccessException * if the field is not made accessible or is {@code final} */ public static void writeStaticField(final Field field, final Object value, final boolean forceAccess) throws IllegalAccessException { Validate.isTrue(field != null, "The field must not be null"); Validate.isTrue(Modifier.isStatic(field.getModifiers()), "The field %s.%s is not static", field.getDeclaringClass().getName(), field.getName()); writeField(field, (Object) null, value, forceAccess); }
/** * Reads the named {@code static} {@link Field}. Superclasses will be considered. * * @param cls * the {@link Class} to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @param forceAccess * whether to break scope restrictions using the * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only * match {@code public} fields. * @return the Field object * @throws IllegalArgumentException * if the class is {@code null}, or the field name is blank or empty, is not {@code static}, or could * not be found * @throws IllegalAccessException * if the field is not made accessible */ public static Object readStaticField(final Class<?> cls, final String fieldName, final boolean forceAccess) throws IllegalAccessException { final Field field = getField(cls, fieldName, forceAccess); Validate.isTrue(field != null, "Cannot locate field '%s' on %s", fieldName, cls); // already forced access above, don't repeat it here: return readStaticField(field, false); }
/** * Writes a named {@code static} {@link Field}. Superclasses will be considered. * * @param cls * {@link Class} on which the field is to be found * @param fieldName * to write * @param value * to set * @param forceAccess * whether to break scope restrictions using the * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only * match {@code public} fields. * @throws IllegalArgumentException * if {@code cls} is {@code null}, the field name is blank or empty, the field cannot be located or is * not {@code static}, or {@code value} is not assignable * @throws IllegalAccessException * if the field is not made accessible or is {@code final} */ public static void writeStaticField(final Class<?> cls, final String fieldName, final Object value, final boolean forceAccess) throws IllegalAccessException { final Field field = getField(cls, fieldName, forceAccess); Validate.isTrue(field != null, "Cannot locate field %s on %s", fieldName, cls); // already forced access above, don't repeat it here: writeStaticField(field, value, false); }
/** * Gets the value of a {@code static} {@link Field} by name. Only the specified class will be considered. * * @param cls * the {@link Class} to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @param forceAccess * whether to break scope restrictions using the * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only * match {@code public} fields. * @return the Field object * @throws IllegalArgumentException * if the class is {@code null}, or the field name is blank or empty, is not {@code static}, or could * not be found * @throws IllegalAccessException * if the field is not made accessible */ public static Object readDeclaredStaticField(final Class<?> cls, final String fieldName, final boolean forceAccess) throws IllegalAccessException { final Field field = getDeclaredField(cls, fieldName, forceAccess); Validate.isTrue(field != null, "Cannot locate declared field %s.%s", cls.getName(), fieldName); // already forced access above, don't repeat it here: return readStaticField(field, false); }
/** * Writes a {@link Field}. Superclasses will be considered. * * @param target * the object to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @param value * to set * @param forceAccess * whether to break scope restrictions using the * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only * match {@code public} fields. * @throws IllegalArgumentException * if {@code target} is {@code null}, {@code fieldName} is blank or empty or could not be found, or * {@code value} is not assignable * @throws IllegalAccessException * if the field is not made accessible */ public static void writeField(final Object target, final String fieldName, final Object value, final boolean forceAccess) throws IllegalAccessException { Validate.isTrue(target != null, "target object must not be null"); final Class<?> cls = target.getClass(); final Field field = getField(cls, fieldName, forceAccess); Validate.isTrue(field != null, "Cannot locate declared field %s.%s", cls.getName(), fieldName); // already forced access above, don't repeat it here: writeField(field, target, value, false); }
/** * Gets a {@link Field} value by name. Only the class of the specified object will be considered. * * @param target * the object to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @param forceAccess * whether to break scope restrictions using the * {@link java.lang.reflect.AccessibleObject#setAccessible(boolean)} method. {@code false} will only * match public fields. * @return the Field object * @throws IllegalArgumentException * if {@code target} is {@code null}, or the field name is blank or empty or could not be found * @throws IllegalAccessException * if the field is not made accessible */ public static Object readDeclaredField(final Object target, final String fieldName, final boolean forceAccess) throws IllegalAccessException { Validate.isTrue(target != null, "target object must not be null"); final Class<?> cls = target.getClass(); final Field field = getDeclaredField(cls, fieldName, forceAccess); Validate.isTrue(field != null, "Cannot locate declared field %s.%s", cls, fieldName); // already forced access above, don't repeat it here: return readField(field, target, false); }
/** * Writes a named {@code static} {@link Field}. Only the specified class will be considered. * * @param cls * {@link Class} on which the field is to be found * @param fieldName * to write * @param value * to set * @param forceAccess * whether to break scope restrictions using the {@code AccessibleObject#setAccessible(boolean)} method. * {@code false} will only match {@code public} fields. * @throws IllegalArgumentException * if {@code cls} is {@code null}, the field name is blank or empty, the field cannot be located or is * not {@code static}, or {@code value} is not assignable * @throws IllegalAccessException * if the field is not made accessible or is {@code final} */ public static void writeDeclaredStaticField(final Class<?> cls, final String fieldName, final Object value, final boolean forceAccess) throws IllegalAccessException { final Field field = getDeclaredField(cls, fieldName, forceAccess); Validate.isTrue(field != null, "Cannot locate declared field %s.%s", cls.getName(), fieldName); // already forced access above, don't repeat it here: writeField(field, (Object) null, value, false); }
/** * Gets an accessible {@link Field} by name respecting scope. Superclasses/interfaces will be considered. * * @param cls * the {@link Class} to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @return the Field object * @throws IllegalArgumentException * if the class is {@code null}, or the field name is blank or empty */ public static Field getField(final Class<?> cls, final String fieldName) { final Field field = getField(cls, fieldName, false); MemberUtils.setAccessibleWorkaround(field); return field; }
private void appendFields(final Class<?> clazz) { for (final Field field : FieldUtils.getAllFields(clazz)) { if (accept(field)) { try { diffBuilder.append(field.getName(), readField(field, left, true), readField(field, right, true)); } catch (final IllegalAccessException ex) { //this can't happen. Would get a Security exception instead //throw a runtime exception in case the impossible happens. throw new InternalError("Unexpected IllegalAccessException: " + ex.getMessage()); } } } }
/** * Reads the named {@code public} {@link Field}. Superclasses will be considered. * * @param target * the object to reflect, must not be {@code null} * @param fieldName * the field name to obtain * @return the value of the field * @throws IllegalArgumentException * if the class is {@code null}, or the field name is blank or empty or could not be found * @throws IllegalAccessException * if the named field is not {@code public} */ public static Object readField(final Object target, final String fieldName) throws IllegalAccessException { return readField(target, fieldName, false); }
@Test public void testWriteNamedFieldForceAccess() throws Exception { FieldUtils.writeField(publicChild, "s", "S", true); assertEquals("S", FieldUtils.readField(publicChild, "s", true)); FieldUtils.writeField(publicChild, "b", Boolean.TRUE, true); assertEquals(Boolean.TRUE, FieldUtils.readField(publicChild, "b", true)); FieldUtils.writeField(publicChild, "i", Integer.valueOf(1), true); assertEquals(Integer.valueOf(1), FieldUtils.readField(publicChild, "i", true)); FieldUtils.writeField(publicChild, "d", Double.valueOf(1.0), true); assertEquals(Double.valueOf(1.0), FieldUtils.readField(publicChild, "d", true)); FieldUtils.writeField(publiclyShadowedChild, "s", "S", true); assertEquals("S", FieldUtils.readField(publiclyShadowedChild, "s", true)); FieldUtils.writeField(publiclyShadowedChild, "b", Boolean.FALSE, true); assertEquals(Boolean.FALSE, FieldUtils.readField(publiclyShadowedChild, "b", true)); FieldUtils.writeField(publiclyShadowedChild, "i", Integer.valueOf(0), true); assertEquals(Integer.valueOf(0), FieldUtils.readField(publiclyShadowedChild, "i", true)); FieldUtils.writeField(publiclyShadowedChild, "d", Double.valueOf(0.0), true); assertEquals(Double.valueOf(0.0), FieldUtils.readField(publiclyShadowedChild, "d", true)); FieldUtils.writeField(privatelyShadowedChild, "s", "S", true); assertEquals("S", FieldUtils.readField(privatelyShadowedChild, "s", true)); FieldUtils.writeField(privatelyShadowedChild, "b", Boolean.FALSE, true); assertEquals(Boolean.FALSE, FieldUtils.readField(privatelyShadowedChild, "b", true)); FieldUtils.writeField(privatelyShadowedChild, "i", Integer.valueOf(0), true); assertEquals(Integer.valueOf(0), FieldUtils.readField(privatelyShadowedChild, "i", true)); FieldUtils.writeField(privatelyShadowedChild, "d", Double.valueOf(0.0), true); assertEquals(Double.valueOf(0.0), FieldUtils.readField(privatelyShadowedChild, "d", true)); }
@Test(expected = IllegalArgumentException.class) public void testReadStaticFieldIllegalArgumentException2() throws Exception { assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(Foo.class, "VALUE"))); final Field nonStaticField = FieldUtils.getField(PublicChild.class, "s"); assumeNotNull(nonStaticField); FieldUtils.readStaticField(nonStaticField); }
/** * Writes an accessible {@link Field}. * * @param field * to write * @param target * the object to call on, may be {@code null} for {@code static} fields * @param value * to set * @throws IllegalAccessException * if the field or target is {@code null}, the field is not accessible or is {@code final}, or * {@code value} is not assignable */ public static void writeField(final Field field, final Object target, final Object value) throws IllegalAccessException { writeField(field, target, value, false); }
@Test public void testReadField() throws Exception { final Field parentS = FieldUtils.getDeclaredField(parentClass, "s"); assertEquals("s", FieldUtils.readField(parentS, publicChild)); assertEquals("s", FieldUtils.readField(parentS, publiclyShadowedChild)); assertEquals("s", FieldUtils.readField(parentS, privatelyShadowedChild)); final Field parentB = FieldUtils.getDeclaredField(parentClass, "b", true); assertEquals(Boolean.FALSE, FieldUtils.readField(parentB, publicChild)); assertEquals(Boolean.FALSE, FieldUtils.readField(parentB, publiclyShadowedChild)); assertEquals(Boolean.FALSE, FieldUtils.readField(parentB, privatelyShadowedChild)); final Field parentI = FieldUtils.getDeclaredField(parentClass, "i", true); assertEquals(I0, FieldUtils.readField(parentI, publicChild)); assertEquals(I0, FieldUtils.readField(parentI, publiclyShadowedChild)); assertEquals(I0, FieldUtils.readField(parentI, privatelyShadowedChild)); final Field parentD = FieldUtils.getDeclaredField(parentClass, "d", true); assertEquals(D0, FieldUtils.readField(parentD, publicChild)); assertEquals(D0, FieldUtils.readField(parentD, publiclyShadowedChild)); assertEquals(D0, FieldUtils.readField(parentD, privatelyShadowedChild)); try { FieldUtils.readField(null, publicChild); fail("a null field should cause an IllegalArgumentException"); } catch (final IllegalArgumentException e) { // expected } }
/** * Gets all fields of the given class and its parents (if any) that are annotated with the given annotation. * @param cls * the {@link Class} to query * @param annotationCls * the {@link Annotation} that must be present on a field to be matched * @return an array of Fields (possibly empty). * @throws IllegalArgumentException * if the class or annotation are {@code null} * @since 3.4 */ public static Field[] getFieldsWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls) { final List<Field> annotatedFieldsList = getFieldsListWithAnnotation(cls, annotationCls); return annotatedFieldsList.toArray(new Field[annotatedFieldsList.size()]); }
private ReadWriteLock getLockInProcessQueue(ProcessQueue pq) { try { return (ReadWriteLock) FieldUtils.readDeclaredField(pq, "lockTreeMap", true); } catch (IllegalAccessException e) { return null; } } }
private Collection<Field> findOptions(Matcher<String> optionNameMatcher) { final Collection<Field> matchFields = new ArrayList<Field>(); for (final Field optionField : FieldUtils.getAllFields(GlobalOptions.class)) { if (!optionField.isAnnotationPresent(Option.class)) { continue; } final Option optionAnnotation = optionField.getAnnotation(Option.class); if (optionAnnotation != null && !optionNameMatcher.matching(optionAnnotation.name())) { continue; } matchFields.add(optionField); } return matchFields; }
private void injectResource(final Object provider) throws IllegalAccessException { final Field[] resourceFieldArray = FieldUtils.getFieldsWithAnnotation(provider.getClass(), Resource.class); if (ArrayUtils.isEmpty(resourceFieldArray)) { return; } for (final Field resourceField : resourceFieldArray) { final Class<?> fieldType = resourceField.getType(); // ConfigInfo注入 if (ConfigInfo.class.isAssignableFrom(fieldType)) { final ConfigInfo configInfo = new DefaultConfigInfo(cfg); FieldUtils.writeField(resourceField, provider, configInfo, true); } } }