[LANG-873] Add FieldUtils getDeclaredFields() to return all the fields defined in the given class and super classes.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1455751 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2013-03-12 23:23:04 +00:00
parent 22a83a7597
commit 035e5c7115
2 changed files with 451 additions and 303 deletions

View File

@ -18,17 +18,18 @@
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.ClassUtils;
/** /**
* Utilities for working with fields by reflection. Adapted and refactored * Utilities for working with fields by reflection. Adapted and refactored from the dormant [reflect] Commons sandbox
* from the dormant [reflect] Commons sandbox component. * component.
* <p> * <p>
* The ability is provided to break the scoping restrictions coded by the * The ability is provided to break the scoping restrictions coded by the programmer. This can allow fields to be
* programmer. This can allow fields to be changed that shouldn't be. This * changed that shouldn't be. This facility should be used with care.
* facility should be used with care. *
*
* @since 2.5 * @since 2.5
* @version $Id$ * @version $Id$
*/ */
@ -37,21 +38,22 @@ public class FieldUtils {
/** /**
* FieldUtils instances should NOT be constructed in standard programming. * FieldUtils instances should NOT be constructed in standard programming.
* <p> * <p>
* This constructor is public to permit tools that require a JavaBean instance * This constructor is public to permit tools that require a JavaBean instance to operate.
* to operate.
*/ */
public FieldUtils() { public FieldUtils() {
super(); super();
} }
/** /**
* Gets an accessible <code>Field</code> by name respecting scope. * Gets an accessible <code>Field</code> by name respecting scope. Superclasses/interfaces will be considered.
* Superclasses/interfaces will be considered. *
* * @param cls
* @param cls the class to reflect, must not be null * the class to reflect, must not be null
* @param fieldName the field name to obtain * @param fieldName
* the field name to obtain
* @return the Field object * @return the Field object
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException
* if the class or field name is null
*/ */
public static Field getField(final Class<?> cls, final String fieldName) { public static Field getField(final Class<?> cls, final String fieldName) {
final Field field = getField(cls, fieldName, false); final Field field = getField(cls, fieldName, false);
@ -60,16 +62,19 @@ public static Field getField(final Class<?> cls, final String fieldName) {
} }
/** /**
* Gets an accessible <code>Field</code> by name breaking scope * Gets an accessible <code>Field</code> by name breaking scope if requested. Superclasses/interfaces will be
* if requested. Superclasses/interfaces will be considered. * considered.
* *
* @param cls the class to reflect, must not be null * @param cls
* @param fieldName the field name to obtain * the class to reflect, must not be null
* @param forceAccess whether to break scope restrictions using the * @param fieldName
* <code>setAccessible</code> method. <code>False</code> will only * the field name to obtain
* match public fields. * @param forceAccess
* whether to break scope restrictions using the <code>setAccessible</code> method. <code>False</code>
* will only match public fields.
* @return the Field object * @return the Field object
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException
* if the class or field name is null
*/ */
public static Field getField(final Class<?> cls, final String fieldName, final boolean forceAccess) { public static Field getField(final Class<?> cls, final String fieldName, final boolean forceAccess) {
if (cls == null) { if (cls == null) {
@ -88,7 +93,7 @@ public static Field getField(final Class<?> cls, final String fieldName, final b
// priority order for lookup: // priority order for lookup:
// searchclass private/protected/package/public // searchclass private/protected/package/public
// superclass protected/package/public // superclass protected/package/public
// private/different package blocks access to further superclasses // private/different package blocks access to further superclasses
// implementedinterface public // implementedinterface public
// check up the superclass hierarchy // check up the superclass hierarchy
@ -117,8 +122,8 @@ public static Field getField(final Class<?> cls, final String fieldName, final b
try { try {
final Field test = ((Class<?>) class1).getField(fieldName); final Field test = ((Class<?>) class1).getField(fieldName);
if (match != null) { if (match != null) {
throw new IllegalArgumentException("Reference to field " + fieldName + " is ambiguous relative to " + cls throw new IllegalArgumentException("Reference to field " + fieldName + " is ambiguous relative to " + cls +
+ "; a matching field exists on two or more implemented interfaces."); "; a matching field exists on two or more implemented interfaces.");
} }
match = test; match = test;
} catch (final NoSuchFieldException ex) { // NOPMD } catch (final NoSuchFieldException ex) { // NOPMD
@ -129,28 +134,34 @@ public static Field getField(final Class<?> cls, final String fieldName, final b
} }
/** /**
* Gets an accessible <code>Field</code> by name respecting scope. * Gets an accessible <code>Field</code> by name respecting scope. Only the specified class will be considered.
* Only the specified class will be considered. *
* * @param cls
* @param cls the class to reflect, must not be null * the class to reflect, must not be null
* @param fieldName the field name to obtain * @param fieldName
* the field name to obtain
* @return the Field object * @return the Field object
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException
* if the class or field name is null
*/ */
public static Field getDeclaredField(final Class<?> cls, final String fieldName) { public static Field getDeclaredField(final Class<?> cls, final String fieldName) {
return getDeclaredField(cls, fieldName, false); return getDeclaredField(cls, fieldName, false);
} }
/** /**
* Gets an accessible <code>Field</code> by name breaking scope * Gets an accessible <code>Field</code> by name breaking scope if requested. Only the specified class will be
* if requested. Only the specified class will be considered. * considered.
* *
* @param cls the class to reflect, must not be null * @param cls
* @param fieldName the field name to obtain * the class to reflect, must not be null
* @param forceAccess whether to break scope restrictions using the * @param fieldName
* <code>setAccessible</code> method. False will only match public fields. * the field name to obtain
* @param forceAccess
* whether to break scope restrictions using the <code>setAccessible</code> method. False will only
* match public fields.
* @return the Field object * @return the Field object
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException
* if the class or field name is null
*/ */
public static Field getDeclaredField(final Class<?> cls, final String fieldName, final boolean forceAccess) { public static Field getDeclaredField(final Class<?> cls, final String fieldName, final boolean forceAccess) {
if (cls == null) { if (cls == null) {
@ -176,12 +187,47 @@ public static Field getDeclaredField(final Class<?> cls, final String fieldName,
return null; return null;
} }
/**
* Gets all declared fields of the given class and its parents (if any).
*
* @param cls
* the class to query
* @return an array of Fields (maybe an empty array).
* @since 3.2
*/
public static Field[] getDeclaredFields(Class<?> cls) {
if (cls == null) {
throw new IllegalArgumentException("The class must not be null");
}
List<Field[]> fieldArrayList = new ArrayList<Field[]>();
int fieldCount = 0;
Class<?> queryClass = cls;
while (queryClass != null) {
final Field[] declaredFields = queryClass.getDeclaredFields();
fieldCount += declaredFields.length;
fieldArrayList.add(declaredFields);
queryClass = queryClass.getSuperclass();
}
Field fields[] = new Field[fieldCount];
int fieldIndex = 0;
for (Field[] fieldArray : fieldArrayList) {
for (Field field : fieldArray) {
fields[fieldIndex++] = field;
}
}
return fields;
}
/** /**
* Reads an accessible static Field. * Reads an accessible static Field.
* @param field to read *
* @param field
* to read
* @return the field value * @return the field value
* @throws IllegalArgumentException if the field is null or not static * @throws IllegalArgumentException
* @throws IllegalAccessException if the field is not accessible * if the field is null or not static
* @throws IllegalAccessException
* if the field is not accessible
*/ */
public static Object readStaticField(final Field field) throws IllegalAccessException { public static Object readStaticField(final Field field) throws IllegalAccessException {
return readStaticField(field, false); return readStaticField(field, false);
@ -189,12 +235,16 @@ public static Object readStaticField(final Field field) throws IllegalAccessExce
/** /**
* Reads a static Field. * Reads a static Field.
* @param field to read *
* @param forceAccess whether to break scope restrictions using the * @param field
* <code>setAccessible</code> method. * to read
* @param forceAccess
* whether to break scope restrictions using the <code>setAccessible</code> method.
* @return the field value * @return the field value
* @throws IllegalArgumentException if the field is null or not static * @throws IllegalArgumentException
* @throws IllegalAccessException if the field is not made accessible * if the field is null or not static
* @throws IllegalAccessException
* if the field is not made accessible
*/ */
public static Object readStaticField(final Field field, final boolean forceAccess) throws IllegalAccessException { public static Object readStaticField(final Field field, final boolean forceAccess) throws IllegalAccessException {
if (field == null) { if (field == null) {
@ -208,11 +258,16 @@ public static Object readStaticField(final Field field, final boolean forceAcces
/** /**
* Reads the named public static field. Superclasses will be considered. * Reads the named public static field. Superclasses will be considered.
* @param cls the class to reflect, must not be null *
* @param fieldName the field name to obtain * @param cls
* the class to reflect, must not be null
* @param fieldName
* the field name to obtain
* @return the value of the field * @return the value of the field
* @throws IllegalArgumentException if the class is null, the field name is null or if the field could not be found * @throws IllegalArgumentException
* @throws IllegalAccessException if the field is not accessible * if the class is null, the field name is null or if the field could not be found
* @throws IllegalAccessException
* if the field is not accessible
*/ */
public static Object readStaticField(final Class<?> cls, final String fieldName) throws IllegalAccessException { public static Object readStaticField(final Class<?> cls, final String fieldName) throws IllegalAccessException {
return readStaticField(cls, fieldName, false); return readStaticField(cls, fieldName, false);
@ -220,69 +275,83 @@ public static Object readStaticField(final Class<?> cls, final String fieldName)
/** /**
* Reads the named static field. Superclasses will be considered. * Reads the named static field. Superclasses will be considered.
* @param cls the class to reflect, must not be null *
* @param fieldName the field name to obtain * @param cls
* @param forceAccess whether to break scope restrictions using the * the class to reflect, must not be null
* <code>setAccessible</code> method. <code>False</code> will only * @param fieldName
* match public fields. * the field name to obtain
* @param forceAccess
* whether to break scope restrictions using the <code>setAccessible</code> method. <code>False</code>
* will only match public fields.
* @return the Field object * @return the Field object
* @throws IllegalArgumentException if the class is null, the field name is null or if the field could not be found * @throws IllegalArgumentException
* @throws IllegalAccessException if the field is not made accessible * if the class is null, the field name is null or if the field 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) public static Object readStaticField(final Class<?> cls, final String fieldName, final boolean forceAccess) throws IllegalAccessException {
throws IllegalAccessException {
final Field field = getField(cls, fieldName, forceAccess); final Field field = getField(cls, fieldName, forceAccess);
if (field == null) { if (field == null) {
throw new IllegalArgumentException("Cannot locate field " + fieldName + " on " + cls); throw new IllegalArgumentException("Cannot locate field " + fieldName + " on " + cls);
} }
//already forced access above, don't repeat it here: // already forced access above, don't repeat it here:
return readStaticField(field, false); return readStaticField(field, false);
} }
/** /**
* Gets a static Field value by name. The field must be public. * Gets a static Field value by name. The field must be public. Only the specified class will be considered.
* Only the specified class will be considered. *
* * @param cls
* @param cls the class to reflect, must not be null * the class to reflect, must not be null
* @param fieldName the field name to obtain * @param fieldName
* the field name to obtain
* @return the value of the field * @return the value of the field
* @throws IllegalArgumentException if the class is null, the field name is null or if the field could not be found * @throws IllegalArgumentException
* @throws IllegalAccessException if the field is not accessible * if the class is null, the field name is null or if the field could not be found
* @throws IllegalAccessException
* if the field is not accessible
*/ */
public static Object readDeclaredStaticField(final Class<?> cls, final String fieldName) throws IllegalAccessException { public static Object readDeclaredStaticField(final Class<?> cls, final String fieldName) throws IllegalAccessException {
return readDeclaredStaticField(cls, fieldName, false); return readDeclaredStaticField(cls, fieldName, false);
} }
/** /**
* Gets a static Field value by name. Only the specified class will * Gets a static Field value by name. Only the specified class will be considered.
* be considered. *
* * @param cls
* @param cls the class to reflect, must not be null * the class to reflect, must not be null
* @param fieldName the field name to obtain * @param fieldName
* @param forceAccess whether to break scope restrictions using the * the field name to obtain
* <code>setAccessible</code> method. <code>False</code> will only * @param forceAccess
* match public fields. * whether to break scope restrictions using the <code>setAccessible</code> method. <code>False</code>
* will only match public fields.
* @return the Field object * @return the Field object
* @throws IllegalArgumentException if the class is null, the field name is null or if the field could not be found * @throws IllegalArgumentException
* @throws IllegalAccessException if the field is not made accessible * if the class is null, the field name is null or if the field 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) public static Object readDeclaredStaticField(final Class<?> cls, final String fieldName, final boolean forceAccess) throws IllegalAccessException {
throws IllegalAccessException {
final Field field = getDeclaredField(cls, fieldName, forceAccess); final Field field = getDeclaredField(cls, fieldName, forceAccess);
if (field == null) { if (field == null) {
throw new IllegalArgumentException("Cannot locate declared field " + cls.getName() + "." + fieldName); throw new IllegalArgumentException("Cannot locate declared field " + cls.getName() + "." + fieldName);
} }
//already forced access above, don't repeat it here: // already forced access above, don't repeat it here:
return readStaticField(field, false); return readStaticField(field, false);
} }
/** /**
* Reads an accessible Field. * Reads an accessible Field.
* @param field the field to use *
* @param target the object to call on, may be null for static fields * @param field
* the field to use
* @param target
* the object to call on, may be null for static fields
* @return the field value * @return the field value
* @throws IllegalArgumentException if the field is null * @throws IllegalArgumentException
* @throws IllegalAccessException if the field is not accessible * if the field is null
* @throws IllegalAccessException
* if the field is not accessible
*/ */
public static Object readField(final Field field, final Object target) throws IllegalAccessException { public static Object readField(final Field field, final Object target) throws IllegalAccessException {
return readField(field, target, false); return readField(field, target, false);
@ -290,13 +359,18 @@ public static Object readField(final Field field, final Object target) throws Il
/** /**
* Reads a Field. * Reads a Field.
* @param field the field to use *
* @param target the object to call on, may be null for static fields * @param field
* @param forceAccess whether to break scope restrictions using the * the field to use
* <code>setAccessible</code> method. * @param target
* the object to call on, may be null for static fields
* @param forceAccess
* whether to break scope restrictions using the <code>setAccessible</code> method.
* @return the field value * @return the field value
* @throws IllegalArgumentException if the field is null * @throws IllegalArgumentException
* @throws IllegalAccessException if the field is not made accessible * if the field is null
* @throws IllegalAccessException
* if the field is not made accessible
*/ */
public static Object readField(final Field field, final Object target, final boolean forceAccess) throws IllegalAccessException { public static Object readField(final Field field, final Object target, final boolean forceAccess) throws IllegalAccessException {
if (field == null) { if (field == null) {
@ -312,11 +386,16 @@ public static Object readField(final Field field, final Object target, final boo
/** /**
* Reads the named public field. Superclasses will be considered. * Reads the named public field. Superclasses will be considered.
* @param target the object to reflect, must not be null *
* @param fieldName the field name to obtain * @param target
* the object to reflect, must not be null
* @param fieldName
* the field name to obtain
* @return the value of the field * @return the value of the field
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException
* @throws IllegalAccessException if the named field is not public * if the class or field name is null
* @throws IllegalAccessException
* if the named field is not public
*/ */
public static Object readField(final Object target, final String fieldName) throws IllegalAccessException { public static Object readField(final Object target, final String fieldName) throws IllegalAccessException {
return readField(target, fieldName, false); return readField(target, fieldName, false);
@ -324,14 +403,19 @@ public static Object readField(final Object target, final String fieldName) thro
/** /**
* Reads the named field. Superclasses will be considered. * Reads the named field. Superclasses will be considered.
* @param target the object to reflect, must not be null *
* @param fieldName the field name to obtain * @param target
* @param forceAccess whether to break scope restrictions using the * the object to reflect, must not be null
* <code>setAccessible</code> method. <code>False</code> will only * @param fieldName
* match public fields. * the field name to obtain
* @param forceAccess
* whether to break scope restrictions using the <code>setAccessible</code> method. <code>False</code>
* will only match public fields.
* @return the field value * @return the field value
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException
* @throws IllegalAccessException if the named field is not made accessible * if the class or field name is null
* @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 { public static Object readField(final Object target, final String fieldName, final boolean forceAccess) throws IllegalAccessException {
if (target == null) { if (target == null) {
@ -342,37 +426,44 @@ public static Object readField(final Object target, final String fieldName, fina
if (field == null) { if (field == null) {
throw new IllegalArgumentException("Cannot locate field " + fieldName + " on " + cls); throw new IllegalArgumentException("Cannot locate field " + fieldName + " on " + cls);
} }
//already forced access above, don't repeat it here: // already forced access above, don't repeat it here:
return readField(field, target); return readField(field, target);
} }
/** /**
* Reads the named public field. Only the class of the specified object will be considered. * Reads the named public field. Only the class of the specified object will be considered.
* @param target the object to reflect, must not be null *
* @param fieldName the field name to obtain * @param target
* the object to reflect, must not be null
* @param fieldName
* the field name to obtain
* @return the value of the field * @return the value of the field
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException
* @throws IllegalAccessException if the named field is not public * if the class or field name is null
* @throws IllegalAccessException
* if the named field is not public
*/ */
public static Object readDeclaredField(final Object target, final String fieldName) throws IllegalAccessException { public static Object readDeclaredField(final Object target, final String fieldName) throws IllegalAccessException {
return readDeclaredField(target, fieldName, false); return readDeclaredField(target, fieldName, false);
} }
/** /**
* <p<>Gets a Field value by name. Only the class of the specified * <p<>Gets a Field value by name. Only the class of the specified object will be considered.
* object will be considered. *
* * @param target
* @param target the object to reflect, must not be null * the object to reflect, must not be null
* @param fieldName the field name to obtain * @param fieldName
* @param forceAccess whether to break scope restrictions using the * the field name to obtain
* <code>setAccessible</code> method. <code>False</code> will only * @param forceAccess
* match public fields. * whether to break scope restrictions using the <code>setAccessible</code> method. <code>False</code>
* will only match public fields.
* @return the Field object * @return the Field object
* @throws IllegalArgumentException if <code>target</code> or <code>fieldName</code> is null * @throws IllegalArgumentException
* @throws IllegalAccessException if the field is not made accessible * if <code>target</code> or <code>fieldName</code> is null
* @throws IllegalAccessException
* if the field is not made accessible
*/ */
public static Object readDeclaredField(final Object target, final String fieldName, final boolean forceAccess) public static Object readDeclaredField(final Object target, final String fieldName, final boolean forceAccess) throws IllegalAccessException {
throws IllegalAccessException {
if (target == null) { if (target == null) {
throw new IllegalArgumentException("target object must not be null"); throw new IllegalArgumentException("target object must not be null");
} }
@ -381,16 +472,21 @@ public static Object readDeclaredField(final Object target, final String fieldNa
if (field == null) { if (field == null) {
throw new IllegalArgumentException("Cannot locate declared field " + cls.getName() + "." + fieldName); throw new IllegalArgumentException("Cannot locate declared field " + cls.getName() + "." + fieldName);
} }
//already forced access above, don't repeat it here: // already forced access above, don't repeat it here:
return readField(field, target); return readField(field, target);
} }
/** /**
* Writes a public static Field. * Writes a public static Field.
* @param field to write *
* @param value to set * @param field
* @throws IllegalArgumentException if the field is null or not static * to write
* @throws IllegalAccessException if the field is not public or is final * @param value
* to set
* @throws IllegalArgumentException
* if the field is null or not static
* @throws IllegalAccessException
* if the field is not public or is final
*/ */
public static void writeStaticField(final Field field, final Object value) throws IllegalAccessException { public static void writeStaticField(final Field field, final Object value) throws IllegalAccessException {
writeStaticField(field, value, false); writeStaticField(field, value, false);
@ -398,13 +494,18 @@ public static void writeStaticField(final Field field, final Object value) throw
/** /**
* Writes a static Field. * Writes a static Field.
* @param field to write *
* @param value to set * @param field
* @param forceAccess whether to break scope restrictions using the * to write
* <code>setAccessible</code> method. <code>False</code> will only * @param value
* match public fields. * to set
* @throws IllegalArgumentException if the field is null or not static * @param forceAccess
* @throws IllegalAccessException if the field is not made accessible or is final * whether to break scope restrictions using the <code>setAccessible</code> method. <code>False</code>
* will only match public fields.
* @throws IllegalArgumentException
* if the field is null or not static
* @throws IllegalAccessException
* if the field is not made accessible or is final
*/ */
public static void writeStaticField(final Field field, final Object value, final boolean forceAccess) throws IllegalAccessException { public static void writeStaticField(final Field field, final Object value, final boolean forceAccess) throws IllegalAccessException {
if (field == null) { if (field == null) {
@ -418,11 +519,17 @@ public static void writeStaticField(final Field field, final Object value, final
/** /**
* Writes a named public static Field. Superclasses will be considered. * Writes a named public static Field. Superclasses will be considered.
* @param cls Class on which the Field is to be found *
* @param fieldName to write * @param cls
* @param value to set * Class on which the Field is to be found
* @throws IllegalArgumentException if the field cannot be located or is not static * @param fieldName
* @throws IllegalAccessException if the field is not public or is final * to write
* @param value
* to set
* @throws IllegalArgumentException
* if the field cannot be located or is not static
* @throws IllegalAccessException
* if the field is not public or is final
*/ */
public static void writeStaticField(final Class<?> cls, final String fieldName, final Object value) throws IllegalAccessException { public static void writeStaticField(final Class<?> cls, final String fieldName, final Object value) throws IllegalAccessException {
writeStaticField(cls, fieldName, value, false); writeStaticField(cls, fieldName, value, false);
@ -430,14 +537,20 @@ public static void writeStaticField(final Class<?> cls, final String fieldName,
/** /**
* Writes a named static Field. Superclasses will be considered. * Writes a named static Field. Superclasses will be considered.
* @param cls Class on which the Field is to be found *
* @param fieldName to write * @param cls
* @param value to set * Class on which the Field is to be found
* @param forceAccess whether to break scope restrictions using the * @param fieldName
* <code>setAccessible</code> method. <code>False</code> will only * to write
* match public fields. * @param value
* @throws IllegalArgumentException if the field cannot be located or is not static * to set
* @throws IllegalAccessException if the field is not made accessible or is final * @param forceAccess
* whether to break scope restrictions using the <code>setAccessible</code> method. <code>False</code>
* will only match public fields.
* @throws IllegalArgumentException
* if the field cannot be located or is not static
* @throws IllegalAccessException
* if the field is not made accessible or is final
*/ */
public static void writeStaticField(final Class<?> cls, final String fieldName, final Object value, final boolean forceAccess) public static void writeStaticField(final Class<?> cls, final String fieldName, final Object value, final boolean forceAccess)
throws IllegalAccessException { throws IllegalAccessException {
@ -445,51 +558,68 @@ public static void writeStaticField(final Class<?> cls, final String fieldName,
if (field == null) { if (field == null) {
throw new IllegalArgumentException("Cannot locate field " + fieldName + " on " + cls); throw new IllegalArgumentException("Cannot locate field " + fieldName + " on " + cls);
} }
//already forced access above, don't repeat it here: // already forced access above, don't repeat it here:
writeStaticField(field, value); writeStaticField(field, value);
} }
/** /**
* Writes a named public static Field. Only the specified class will be considered. * Writes a named public static Field. Only the specified class will be considered.
* @param cls Class on which the Field is to be found *
* @param fieldName to write * @param cls
* @param value to set * Class on which the Field is to be found
* @throws IllegalArgumentException if the field cannot be located or is not static * @param fieldName
* @throws IllegalAccessException if the field is not public or is final * to write
* @param value
* to set
* @throws IllegalArgumentException
* if the field cannot be located or is not static
* @throws IllegalAccessException
* if the field is not public or is final
*/ */
public static void writeDeclaredStaticField(final Class<?> cls, final String fieldName, final Object value) public static void writeDeclaredStaticField(final Class<?> cls, final String fieldName, final Object value) throws IllegalAccessException {
throws IllegalAccessException {
writeDeclaredStaticField(cls, fieldName, value, false); writeDeclaredStaticField(cls, fieldName, value, false);
} }
/** /**
* Writes a named static Field. Only the specified class will be considered. * Writes a named static Field. Only the specified class will be considered.
* @param cls Class on which the Field is to be found *
* @param fieldName to write * @param cls
* @param value to set * Class on which the Field is to be found
* @param forceAccess whether to break scope restrictions using the * @param fieldName
* <code>setAccessible</code> method. <code>False</code> will only * to write
* match public fields. * @param value
* @throws IllegalArgumentException if the field cannot be located or is not static * to set
* @throws IllegalAccessException if the field is not made accessible or is final * @param forceAccess
*/ * whether to break scope restrictions using the <code>setAccessible</code> method. <code>False</code>
* will only match public fields.
* @throws IllegalArgumentException
* if the field cannot be located or is not static
* @throws IllegalAccessException
* if the field is not made accessible or is final
*/
public static void writeDeclaredStaticField(final Class<?> cls, final String fieldName, final Object value, final boolean forceAccess) public static void writeDeclaredStaticField(final Class<?> cls, final String fieldName, final Object value, final boolean forceAccess)
throws IllegalAccessException { throws IllegalAccessException {
final Field field = getDeclaredField(cls, fieldName, forceAccess); final Field field = getDeclaredField(cls, fieldName, forceAccess);
if (field == null) { if (field == null) {
throw new IllegalArgumentException("Cannot locate declared field " + cls.getName() + "." + fieldName); throw new IllegalArgumentException("Cannot locate declared field " + cls.getName() + "." + fieldName);
} }
//already forced access above, don't repeat it here: // already forced access above, don't repeat it here:
writeField(field, (Object) null, value); writeField(field, (Object) null, value);
} }
/** /**
* Writes an accessible field. * Writes an accessible field.
* @param field to write *
* @param target the object to call on, may be null for static fields * @param field
* @param value to set * to write
* @throws IllegalArgumentException if the field is null * @param target
* @throws IllegalAccessException if the field is not accessible or is final * the object to call on, may be null for static fields
* @param value
* to set
* @throws IllegalArgumentException
* if the field is null
* @throws IllegalAccessException
* if the field is not accessible or is final
*/ */
public static void writeField(final Field field, final Object target, final Object value) throws IllegalAccessException { public static void writeField(final Field field, final Object target, final Object value) throws IllegalAccessException {
writeField(field, target, value, false); writeField(field, target, value, false);
@ -497,17 +627,23 @@ public static void writeField(final Field field, final Object target, final Obje
/** /**
* Writes a field. * Writes a field.
* @param field to write *
* @param target the object to call on, may be null for static fields * @param field
* @param value to set * to write
* @param forceAccess whether to break scope restrictions using the * @param target
* <code>setAccessible</code> method. <code>False</code> will only * the object to call on, may be null for static fields
* match public fields. * @param value
* @throws IllegalArgumentException if the field is null * to set
* @throws IllegalAccessException if the field is not made accessible or is final * @param forceAccess
* whether to break scope restrictions using the <code>setAccessible</code> method. <code>False</code>
* will only match public fields.
* @throws IllegalArgumentException
* if the field is null
* @throws IllegalAccessException
* if the field is not made accessible or is final
*/ */
public static void writeField(final Field field, final Object target, final Object value, final boolean forceAccess) public static void writeField(final Field field, final Object target, final Object value, final boolean forceAccess)
throws IllegalAccessException { throws IllegalAccessException {
if (field == null) { if (field == null) {
throw new IllegalArgumentException("The field must not be null"); throw new IllegalArgumentException("The field must not be null");
} }
@ -521,11 +657,17 @@ public static void writeField(final Field field, final Object target, final Obje
/** /**
* Writes a public field. Superclasses will be considered. * Writes a public field. Superclasses will be considered.
* @param target the object to reflect, must not be null *
* @param fieldName the field name to obtain * @param target
* @param value to set * the object to reflect, must not be null
* @throws IllegalArgumentException if <code>target</code> or <code>fieldName</code> is null * @param fieldName
* @throws IllegalAccessException if the field is not accessible * the field name to obtain
* @param value
* to set
* @throws IllegalArgumentException
* if <code>target</code> or <code>fieldName</code> is null
* @throws IllegalAccessException
* if the field is not accessible
*/ */
public static void writeField(final Object target, final String fieldName, final Object value) throws IllegalAccessException { public static void writeField(final Object target, final String fieldName, final Object value) throws IllegalAccessException {
writeField(target, fieldName, value, false); writeField(target, fieldName, value, false);
@ -533,14 +675,20 @@ public static void writeField(final Object target, final String fieldName, final
/** /**
* Writes a field. Superclasses will be considered. * Writes a field. Superclasses will be considered.
* @param target the object to reflect, must not be null *
* @param fieldName the field name to obtain * @param target
* @param value to set * the object to reflect, must not be null
* @param forceAccess whether to break scope restrictions using the * @param fieldName
* <code>setAccessible</code> method. <code>False</code> will only * the field name to obtain
* match public fields. * @param value
* @throws IllegalArgumentException if <code>target</code> or <code>fieldName</code> is null * to set
* @throws IllegalAccessException if the field is not made accessible * @param forceAccess
* whether to break scope restrictions using the <code>setAccessible</code> method. <code>False</code>
* will only match public fields.
* @throws IllegalArgumentException
* if <code>target</code> or <code>fieldName</code> is null
* @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) public static void writeField(final Object target, final String fieldName, final Object value, final boolean forceAccess)
throws IllegalAccessException { throws IllegalAccessException {
@ -552,17 +700,23 @@ public static void writeField(final Object target, final String fieldName, final
if (field == null) { if (field == null) {
throw new IllegalArgumentException("Cannot locate declared field " + cls.getName() + "." + fieldName); throw new IllegalArgumentException("Cannot locate declared field " + cls.getName() + "." + fieldName);
} }
//already forced access above, don't repeat it here: // already forced access above, don't repeat it here:
writeField(field, target, value); writeField(field, target, value);
} }
/** /**
* Writes a public field. Only the specified class will be considered. * Writes a public field. Only the specified class will be considered.
* @param target the object to reflect, must not be null *
* @param fieldName the field name to obtain * @param target
* @param value to set * the object to reflect, must not be null
* @throws IllegalArgumentException if <code>target</code> or <code>fieldName</code> is null * @param fieldName
* @throws IllegalAccessException if the field is not made accessible * the field name to obtain
* @param value
* to set
* @throws IllegalArgumentException
* if <code>target</code> or <code>fieldName</code> is null
* @throws IllegalAccessException
* if the field is not made accessible
*/ */
public static void writeDeclaredField(final Object target, final String fieldName, final Object value) throws IllegalAccessException { public static void writeDeclaredField(final Object target, final String fieldName, final Object value) throws IllegalAccessException {
writeDeclaredField(target, fieldName, value, false); writeDeclaredField(target, fieldName, value, false);
@ -570,14 +724,20 @@ public static void writeDeclaredField(final Object target, final String fieldNam
/** /**
* Writes a public field. Only the specified class will be considered. * Writes a public field. Only the specified class will be considered.
* @param target the object to reflect, must not be null *
* @param fieldName the field name to obtain * @param target
* @param value to set * the object to reflect, must not be null
* @param forceAccess whether to break scope restrictions using the * @param fieldName
* <code>setAccessible</code> method. <code>False</code> will only * the field name to obtain
* match public fields. * @param value
* @throws IllegalArgumentException if <code>target</code> or <code>fieldName</code> is null * to set
* @throws IllegalAccessException if the field is not made accessible * @param forceAccess
* whether to break scope restrictions using the <code>setAccessible</code> method. <code>False</code>
* will only match public fields.
* @throws IllegalArgumentException
* if <code>target</code> or <code>fieldName</code> is null
* @throws IllegalAccessException
* if the field is not made accessible
*/ */
public static void writeDeclaredField(final Object target, final String fieldName, final Object value, final boolean forceAccess) public static void writeDeclaredField(final Object target, final String fieldName, final Object value, final boolean forceAccess)
throws IllegalAccessException { throws IllegalAccessException {
@ -589,7 +749,7 @@ public static void writeDeclaredField(final Object target, final String fieldNam
if (field == null) { if (field == null) {
throw new IllegalArgumentException("Cannot locate declared field " + cls.getName() + "." + fieldName); throw new IllegalArgumentException("Cannot locate declared field " + cls.getName() + "." + fieldName);
} }
//already forced access above, don't repeat it here: // already forced access above, don't repeat it here:
writeField(field, target, value); writeField(field, target, value);
} }
} }

View File

@ -16,6 +16,7 @@
*/ */
package org.apache.commons.lang3.reflect; package org.apache.commons.lang3.reflect;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@ -28,6 +29,7 @@
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.reflect.testbed.Ambig; import org.apache.commons.lang3.reflect.testbed.Ambig;
import org.apache.commons.lang3.reflect.testbed.Foo; import org.apache.commons.lang3.reflect.testbed.Foo;
import org.apache.commons.lang3.reflect.testbed.PrivatelyShadowedChild; import org.apache.commons.lang3.reflect.testbed.PrivatelyShadowedChild;
@ -40,6 +42,7 @@
/** /**
* Unit tests FieldUtils * Unit tests FieldUtils
*
* @version $Id$ * @version $Id$
*/ */
public class FieldUtilsTest { public class FieldUtilsTest {
@ -73,7 +76,7 @@ public void testConstructor() {
assertTrue(Modifier.isPublic(FieldUtils.class.getModifiers())); assertTrue(Modifier.isPublic(FieldUtils.class.getModifiers()));
assertFalse(Modifier.isFinal(FieldUtils.class.getModifiers())); assertFalse(Modifier.isFinal(FieldUtils.class.getModifiers()));
} }
@Test @Test
public void testGetField() { public void testGetField() {
assertEquals(Foo.class, FieldUtils.getField(PublicChild.class, "VALUE").getDeclaringClass()); assertEquals(Foo.class, FieldUtils.getField(PublicChild.class, "VALUE").getDeclaringClass());
@ -82,14 +85,10 @@ public void testGetField() {
assertNull(FieldUtils.getField(PublicChild.class, "i")); assertNull(FieldUtils.getField(PublicChild.class, "i"));
assertNull(FieldUtils.getField(PublicChild.class, "d")); assertNull(FieldUtils.getField(PublicChild.class, "d"));
assertEquals(Foo.class, FieldUtils.getField(PubliclyShadowedChild.class, "VALUE").getDeclaringClass()); assertEquals(Foo.class, FieldUtils.getField(PubliclyShadowedChild.class, "VALUE").getDeclaringClass());
assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "s") assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "s").getDeclaringClass());
.getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "b").getDeclaringClass());
assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "b") assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "i").getDeclaringClass());
.getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "d").getDeclaringClass());
assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "i")
.getDeclaringClass());
assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "d")
.getDeclaringClass());
assertEquals(Foo.class, FieldUtils.getField(PrivatelyShadowedChild.class, "VALUE").getDeclaringClass()); assertEquals(Foo.class, FieldUtils.getField(PrivatelyShadowedChild.class, "VALUE").getDeclaringClass());
assertEquals(parentClass, FieldUtils.getField(PrivatelyShadowedChild.class, "s").getDeclaringClass()); assertEquals(parentClass, FieldUtils.getField(PrivatelyShadowedChild.class, "s").getDeclaringClass());
assertNull(FieldUtils.getField(PrivatelyShadowedChild.class, "b")); assertNull(FieldUtils.getField(PrivatelyShadowedChild.class, "b"));
@ -97,16 +96,16 @@ public void testGetField() {
assertNull(FieldUtils.getField(PrivatelyShadowedChild.class, "d")); assertNull(FieldUtils.getField(PrivatelyShadowedChild.class, "d"));
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testGetFieldIllegalArgumentException1() { public void testGetFieldIllegalArgumentException1() {
FieldUtils.getField(null, "none"); FieldUtils.getField(null, "none");
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testGetFieldIllegalArgumentException2() { public void testGetFieldIllegalArgumentException2() {
FieldUtils.getField(PublicChild.class, null); FieldUtils.getField(PublicChild.class, null);
} }
@Test @Test
public void testGetFieldForceAccess() { public void testGetFieldForceAccess() {
assertEquals(PublicChild.class, FieldUtils.getField(PublicChild.class, "VALUE", true).getDeclaringClass()); assertEquals(PublicChild.class, FieldUtils.getField(PublicChild.class, "VALUE", true).getDeclaringClass());
@ -115,34 +114,36 @@ public void testGetFieldForceAccess() {
assertEquals(parentClass, FieldUtils.getField(PublicChild.class, "i", true).getDeclaringClass()); assertEquals(parentClass, FieldUtils.getField(PublicChild.class, "i", true).getDeclaringClass());
assertEquals(parentClass, FieldUtils.getField(PublicChild.class, "d", true).getDeclaringClass()); assertEquals(parentClass, FieldUtils.getField(PublicChild.class, "d", true).getDeclaringClass());
assertEquals(Foo.class, FieldUtils.getField(PubliclyShadowedChild.class, "VALUE", true).getDeclaringClass()); assertEquals(Foo.class, FieldUtils.getField(PubliclyShadowedChild.class, "VALUE", true).getDeclaringClass());
assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "s", true) assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "s", true).getDeclaringClass());
.getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "b", true).getDeclaringClass());
assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "b", true) assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "i", true).getDeclaringClass());
.getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "d", true).getDeclaringClass());
assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "i", true)
.getDeclaringClass());
assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "d", true)
.getDeclaringClass());
assertEquals(Foo.class, FieldUtils.getField(PrivatelyShadowedChild.class, "VALUE", true).getDeclaringClass()); assertEquals(Foo.class, FieldUtils.getField(PrivatelyShadowedChild.class, "VALUE", true).getDeclaringClass());
assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "s", true) assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "s", true).getDeclaringClass());
.getDeclaringClass()); assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "b", true).getDeclaringClass());
assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "b", true) assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "i", true).getDeclaringClass());
.getDeclaringClass()); assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "d", true).getDeclaringClass());
assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "i", true)
.getDeclaringClass());
assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "d", true)
.getDeclaringClass());
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testGetFieldForceAccessIllegalArgumentException1() { public void testGetFieldForceAccessIllegalArgumentException1() {
FieldUtils.getField(null, "none", true); FieldUtils.getField(null, "none", true);
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testGetFieldForceAccessIllegalArgumentException2() { public void testGetFieldForceAccessIllegalArgumentException2() {
FieldUtils.getField(PublicChild.class, null, true); FieldUtils.getField(PublicChild.class, null, true);
} }
@Test
public void testGetDeclaredFields() {
assertArrayEquals(new Field[0], FieldUtils.getDeclaredFields(Object.class));
final Field[] fieldsNumber = Number.class.getDeclaredFields();
assertArrayEquals(fieldsNumber, FieldUtils.getDeclaredFields(Number.class));
final Field[] fieldsInteger = Integer.class.getDeclaredFields();
assertArrayEquals(ArrayUtils.addAll(fieldsInteger, fieldsNumber), FieldUtils.getDeclaredFields(Integer.class));
assertEquals(5, FieldUtils.getDeclaredFields(PublicChild.class).length);
}
@Test @Test
public void testGetDeclaredField() { public void testGetDeclaredField() {
@ -152,14 +153,10 @@ public void testGetDeclaredField() {
assertNull(FieldUtils.getDeclaredField(PublicChild.class, "i")); assertNull(FieldUtils.getDeclaredField(PublicChild.class, "i"));
assertNull(FieldUtils.getDeclaredField(PublicChild.class, "d")); assertNull(FieldUtils.getDeclaredField(PublicChild.class, "d"));
assertNull(FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "VALUE")); assertNull(FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "VALUE"));
assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "s") assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "s").getDeclaringClass());
.getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "b").getDeclaringClass());
assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "b") assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "i").getDeclaringClass());
.getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "d").getDeclaringClass());
assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "i")
.getDeclaringClass());
assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "d")
.getDeclaringClass());
assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "VALUE")); assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "VALUE"));
assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "s")); assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "s"));
assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "b")); assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "b"));
@ -167,65 +164,56 @@ public void testGetDeclaredField() {
assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "d")); assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "d"));
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testGetDeclaredFieldAccessIllegalArgumentException1() { public void testGetDeclaredFieldAccessIllegalArgumentException1() {
FieldUtils.getDeclaredField(null, "none"); FieldUtils.getDeclaredField(null, "none");
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testGetDeclaredFieldAccessIllegalArgumentException2() { public void testGetDeclaredFieldAccessIllegalArgumentException2() {
FieldUtils.getDeclaredField(PublicChild.class, null); FieldUtils.getDeclaredField(PublicChild.class, null);
} }
@Test @Test
public void testGetDeclaredFieldForceAccess() { public void testGetDeclaredFieldForceAccess() {
assertEquals(PublicChild.class, FieldUtils.getDeclaredField(PublicChild.class, "VALUE", true) assertEquals(PublicChild.class, FieldUtils.getDeclaredField(PublicChild.class, "VALUE", true).getDeclaringClass());
.getDeclaringClass());
assertNull(FieldUtils.getDeclaredField(PublicChild.class, "s", true)); assertNull(FieldUtils.getDeclaredField(PublicChild.class, "s", true));
assertNull(FieldUtils.getDeclaredField(PublicChild.class, "b", true)); assertNull(FieldUtils.getDeclaredField(PublicChild.class, "b", true));
assertNull(FieldUtils.getDeclaredField(PublicChild.class, "i", true)); assertNull(FieldUtils.getDeclaredField(PublicChild.class, "i", true));
assertNull(FieldUtils.getDeclaredField(PublicChild.class, "d", true)); assertNull(FieldUtils.getDeclaredField(PublicChild.class, "d", true));
assertNull(FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "VALUE", true)); assertNull(FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "VALUE", true));
assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "s", true) assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "s", true).getDeclaringClass());
.getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "b", true).getDeclaringClass());
assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "b", true) assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "i", true).getDeclaringClass());
.getDeclaringClass()); assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "d", true).getDeclaringClass());
assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "i", true)
.getDeclaringClass());
assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "d", true)
.getDeclaringClass());
assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "VALUE", true)); assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "VALUE", true));
assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "s", true) assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "s", true).getDeclaringClass());
.getDeclaringClass()); assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "b", true).getDeclaringClass());
assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "b", true) assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "i", true).getDeclaringClass());
.getDeclaringClass()); assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "d", true).getDeclaringClass());
assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "i", true)
.getDeclaringClass());
assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "d", true)
.getDeclaringClass());
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testGetDeclaredFieldForceAccessIllegalArgumentException1() { public void testGetDeclaredFieldForceAccessIllegalArgumentException1() {
FieldUtils.getDeclaredField(null, "none", true); FieldUtils.getDeclaredField(null, "none", true);
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testGetDeclaredFieldForceAccessIllegalArgumentException2() { public void testGetDeclaredFieldForceAccessIllegalArgumentException2() {
FieldUtils.getDeclaredField(PublicChild.class, null, true); FieldUtils.getDeclaredField(PublicChild.class, null, true);
} }
@Test @Test
public void testReadStaticField() throws Exception { public void testReadStaticField() throws Exception {
assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(Foo.class, "VALUE"))); assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(Foo.class, "VALUE")));
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testReadStaticFieldIllegalArgumentException1() throws Exception { public void testReadStaticFieldIllegalArgumentException1() throws Exception {
FieldUtils.readStaticField(null); FieldUtils.readStaticField(null);
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testReadStaticFieldIllegalArgumentException2() throws Exception { public void testReadStaticFieldIllegalArgumentException2() throws Exception {
assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(Foo.class, "VALUE"))); assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(Foo.class, "VALUE")));
final Field nonStaticField = FieldUtils.getField(PublicChild.class, "s"); final Field nonStaticField = FieldUtils.getField(PublicChild.class, "s");
@ -239,12 +227,12 @@ public void testReadStaticFieldForceAccess() throws Exception {
assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(PublicChild.class, "VALUE"))); assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(PublicChild.class, "VALUE")));
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testReadStaticFieldForceAccessIllegalArgumentException1() throws Exception { public void testReadStaticFieldForceAccessIllegalArgumentException1() throws Exception {
FieldUtils.readStaticField(null, true); FieldUtils.readStaticField(null, true);
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testReadStaticFieldForceAccessIllegalArgumentException2() throws Exception { public void testReadStaticFieldForceAccessIllegalArgumentException2() throws Exception {
final Field nonStaticField = FieldUtils.getField(PublicChild.class, "s", true); final Field nonStaticField = FieldUtils.getField(PublicChild.class, "s", true);
assumeNotNull(nonStaticField); assumeNotNull(nonStaticField);
@ -257,28 +245,28 @@ public void testReadNamedStaticField() throws Exception {
assertEquals(Foo.VALUE, FieldUtils.readStaticField(PubliclyShadowedChild.class, "VALUE")); assertEquals(Foo.VALUE, FieldUtils.readStaticField(PubliclyShadowedChild.class, "VALUE"));
assertEquals(Foo.VALUE, FieldUtils.readStaticField(PrivatelyShadowedChild.class, "VALUE")); assertEquals(Foo.VALUE, FieldUtils.readStaticField(PrivatelyShadowedChild.class, "VALUE"));
assertEquals(Foo.VALUE, FieldUtils.readStaticField(PublicChild.class, "VALUE")); assertEquals(Foo.VALUE, FieldUtils.readStaticField(PublicChild.class, "VALUE"));
try { try {
FieldUtils.readStaticField(null, "none"); FieldUtils.readStaticField(null, "none");
fail("null class should cause an IllegalArgumentException"); fail("null class should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// expected // expected
} }
try { try {
FieldUtils.readStaticField(Foo.class, null); FieldUtils.readStaticField(Foo.class, null);
fail("null field name should cause an IllegalArgumentException"); fail("null field name should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// expected // expected
} }
try { try {
FieldUtils.readStaticField(Foo.class, "does_not_exist"); FieldUtils.readStaticField(Foo.class, "does_not_exist");
fail("a field that doesn't exist should cause an IllegalArgumentException"); fail("a field that doesn't exist should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// expected // expected
} }
try { try {
FieldUtils.readStaticField(PublicChild.class, "s"); FieldUtils.readStaticField(PublicChild.class, "s");
fail("non-static field should cause an IllegalArgumentException"); fail("non-static field should cause an IllegalArgumentException");
@ -293,28 +281,28 @@ public void testReadNamedStaticFieldForceAccess() throws Exception {
assertEquals(Foo.VALUE, FieldUtils.readStaticField(PubliclyShadowedChild.class, "VALUE", true)); assertEquals(Foo.VALUE, FieldUtils.readStaticField(PubliclyShadowedChild.class, "VALUE", true));
assertEquals(Foo.VALUE, FieldUtils.readStaticField(PrivatelyShadowedChild.class, "VALUE", true)); assertEquals(Foo.VALUE, FieldUtils.readStaticField(PrivatelyShadowedChild.class, "VALUE", true));
assertEquals("child", FieldUtils.readStaticField(PublicChild.class, "VALUE", true)); assertEquals("child", FieldUtils.readStaticField(PublicChild.class, "VALUE", true));
try { try {
FieldUtils.readStaticField(null, "none", true); FieldUtils.readStaticField(null, "none", true);
fail("null class should cause an IllegalArgumentException"); fail("null class should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// expected // expected
} }
try { try {
FieldUtils.readStaticField(Foo.class, null, true); FieldUtils.readStaticField(Foo.class, null, true);
fail("null field name should cause an IllegalArgumentException"); fail("null field name should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// expected // expected
} }
try { try {
FieldUtils.readStaticField(Foo.class, "does_not_exist", true); FieldUtils.readStaticField(Foo.class, "does_not_exist", true);
fail("a field that doesn't exist should cause an IllegalArgumentException"); fail("a field that doesn't exist should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// expected // expected
} }
try { try {
FieldUtils.readStaticField(PublicChild.class, "s", false); FieldUtils.readStaticField(PublicChild.class, "s", false);
fail("non-static field should cause an IllegalArgumentException"); fail("non-static field should cause an IllegalArgumentException");
@ -382,9 +370,9 @@ public void testReadField() throws Exception {
assertEquals(D0, FieldUtils.readField(parentD, publicChild)); assertEquals(D0, FieldUtils.readField(parentD, publicChild));
assertEquals(D0, FieldUtils.readField(parentD, publiclyShadowedChild)); assertEquals(D0, FieldUtils.readField(parentD, publiclyShadowedChild));
assertEquals(D0, FieldUtils.readField(parentD, privatelyShadowedChild)); assertEquals(D0, FieldUtils.readField(parentD, privatelyShadowedChild));
try { try {
FieldUtils.readField((Field)null, publicChild); FieldUtils.readField((Field) null, publicChild);
fail("a null field should cause an IllegalArgumentException"); fail("a null field should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// expected // expected
@ -413,9 +401,9 @@ public void testReadFieldForceAccess() throws Exception {
assertEquals(D0, FieldUtils.readField(parentD, publicChild, true)); assertEquals(D0, FieldUtils.readField(parentD, publicChild, true));
assertEquals(D0, FieldUtils.readField(parentD, publiclyShadowedChild, true)); assertEquals(D0, FieldUtils.readField(parentD, publiclyShadowedChild, true));
assertEquals(D0, FieldUtils.readField(parentD, privatelyShadowedChild, true)); assertEquals(D0, FieldUtils.readField(parentD, privatelyShadowedChild, true));
try { try {
FieldUtils.readField((Field)null, publicChild, true); FieldUtils.readField((Field) null, publicChild, true);
fail("a null field should cause an IllegalArgumentException"); fail("a null field should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// expected // expected
@ -427,21 +415,21 @@ public void testReadNamedField() throws Exception {
assertEquals("s", FieldUtils.readField(publicChild, "s")); assertEquals("s", FieldUtils.readField(publicChild, "s"));
assertEquals("ss", FieldUtils.readField(publiclyShadowedChild, "s")); assertEquals("ss", FieldUtils.readField(publiclyShadowedChild, "s"));
assertEquals("s", FieldUtils.readField(privatelyShadowedChild, "s")); assertEquals("s", FieldUtils.readField(privatelyShadowedChild, "s"));
try { try {
FieldUtils.readField(publicChild, null); FieldUtils.readField(publicChild, null);
fail("a null field name should cause an IllegalArgumentException"); fail("a null field name should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// expected // expected
} }
try { try {
FieldUtils.readField((Object)null, "none"); FieldUtils.readField((Object) null, "none");
fail("a null target should cause an IllegalArgumentException"); fail("a null target should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// expected // expected
} }
try { try {
assertEquals(Boolean.FALSE, FieldUtils.readField(publicChild, "b")); assertEquals(Boolean.FALSE, FieldUtils.readField(publicChild, "b"));
fail("expected IllegalArgumentException"); fail("expected IllegalArgumentException");
@ -497,16 +485,16 @@ public void testReadNamedFieldForceAccess() throws Exception {
assertEquals(D0, FieldUtils.readField(publicChild, "d", true)); assertEquals(D0, FieldUtils.readField(publicChild, "d", true));
assertEquals(D1, FieldUtils.readField(publiclyShadowedChild, "d", true)); assertEquals(D1, FieldUtils.readField(publiclyShadowedChild, "d", true));
assertEquals(D1, FieldUtils.readField(privatelyShadowedChild, "d", true)); assertEquals(D1, FieldUtils.readField(privatelyShadowedChild, "d", true));
try { try {
FieldUtils.readField(publicChild, null, true); FieldUtils.readField(publicChild, null, true);
fail("a null field name should cause an IllegalArgumentException"); fail("a null field name should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// expected // expected
} }
try { try {
FieldUtils.readField((Object)null, "none", true); FieldUtils.readField((Object) null, "none", true);
fail("a null target should cause an IllegalArgumentException"); fail("a null target should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// expected // expected
@ -521,14 +509,14 @@ public void testReadDeclaredNamedField() throws Exception {
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// expected // expected
} }
try { try {
FieldUtils.readDeclaredField((Object)null, "none"); FieldUtils.readDeclaredField((Object) null, "none");
fail("a null target should cause an IllegalArgumentException"); fail("a null target should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// expected // expected
} }
try { try {
assertEquals("s", FieldUtils.readDeclaredField(publicChild, "s")); assertEquals("s", FieldUtils.readDeclaredField(publicChild, "s"));
fail("expected IllegalArgumentException"); fail("expected IllegalArgumentException");
@ -591,14 +579,14 @@ public void testReadDeclaredNamedFieldForceAccess() throws Exception {
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// expected // expected
} }
try { try {
FieldUtils.readDeclaredField((Object)null, "none", true); FieldUtils.readDeclaredField((Object) null, "none", true);
fail("a null target should cause an IllegalArgumentException"); fail("a null target should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// expected // expected
} }
try { try {
assertEquals("s", FieldUtils.readDeclaredField(publicChild, "s", true)); assertEquals("s", FieldUtils.readDeclaredField(publicChild, "s", true));
fail("expected IllegalArgumentException"); fail("expected IllegalArgumentException");
@ -1135,7 +1123,7 @@ public void testWriteDeclaredNamedFieldForceAccess() throws Exception {
assertEquals(Double.valueOf(0.0), FieldUtils.readDeclaredField(privatelyShadowedChild, "d", true)); assertEquals(Double.valueOf(0.0), FieldUtils.readDeclaredField(privatelyShadowedChild, "d", true));
} }
@Test(expected=IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testAmbig() { public void testAmbig() {
FieldUtils.getField(Ambig.class, "VALUE"); FieldUtils.getField(Ambig.class, "VALUE");
} }