You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2013/03/13 00:23:04 UTC
svn commit: r1455751 - in /commons/proper/lang/trunk/src:
main/java/org/apache/commons/lang3/reflect/FieldUtils.java
test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java
Author: ggregory
Date: Tue Mar 12 23:23:04 2013
New Revision: 1455751
URL: http://svn.apache.org/r1455751
Log:
[LANG-873] Add FieldUtils getDeclaredFields() to return all the fields defined in the given class and super classes.
Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/FieldUtils.java
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java
Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/FieldUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/FieldUtils.java?rev=1455751&r1=1455750&r2=1455751&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/FieldUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/reflect/FieldUtils.java Tue Mar 12 23:23:04 2013
@@ -18,17 +18,18 @@ package org.apache.commons.lang3.reflect
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.commons.lang3.ClassUtils;
/**
- * Utilities for working with fields by reflection. Adapted and refactored
- * from the dormant [reflect] Commons sandbox component.
+ * Utilities for working with fields by reflection. Adapted and refactored from the dormant [reflect] Commons sandbox
+ * component.
* <p>
- * The ability is provided to break the scoping restrictions coded by the
- * programmer. This can allow fields to be changed that shouldn't be. This
- * facility should be used with care.
- *
+ * The ability is provided to break the scoping restrictions coded by the programmer. This can allow fields to be
+ * changed that shouldn't be. This facility should be used with care.
+ *
* @since 2.5
* @version $Id$
*/
@@ -37,21 +38,22 @@ public class FieldUtils {
/**
* FieldUtils instances should NOT be constructed in standard programming.
* <p>
- * This constructor is public to permit tools that require a JavaBean instance
- * to operate.
+ * This constructor is public to permit tools that require a JavaBean instance to operate.
*/
public FieldUtils() {
super();
}
/**
- * Gets an accessible <code>Field</code> by name respecting scope.
- * Superclasses/interfaces will be considered.
- *
- * @param cls the class to reflect, must not be null
- * @param fieldName the field name to obtain
+ * Gets an accessible <code>Field</code> by name respecting scope. Superclasses/interfaces will be considered.
+ *
+ * @param cls
+ * the class to reflect, must not be null
+ * @param fieldName
+ * the field name to obtain
* @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 Field field = getField(cls, fieldName, false);
@@ -60,16 +62,19 @@ public class FieldUtils {
}
/**
- * Gets an accessible <code>Field</code> by name breaking scope
- * if requested. Superclasses/interfaces will be considered.
- *
- * @param cls the class to reflect, must not be null
- * @param fieldName 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.
+ * Gets an accessible <code>Field</code> by name breaking scope if requested. Superclasses/interfaces will be
+ * considered.
+ *
+ * @param cls
+ * the class to reflect, must not be null
+ * @param fieldName
+ * 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
- * @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) {
if (cls == null) {
@@ -88,7 +93,7 @@ public class FieldUtils {
// priority order for lookup:
// searchclass private/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
// check up the superclass hierarchy
@@ -117,8 +122,8 @@ public class FieldUtils {
try {
final Field test = ((Class<?>) class1).getField(fieldName);
if (match != null) {
- throw new IllegalArgumentException("Reference to field " + fieldName + " is ambiguous relative to " + cls
- + "; a matching field exists on two or more implemented interfaces.");
+ throw new IllegalArgumentException("Reference to field " + fieldName + " is ambiguous relative to " + cls +
+ "; a matching field exists on two or more implemented interfaces.");
}
match = test;
} catch (final NoSuchFieldException ex) { // NOPMD
@@ -129,28 +134,34 @@ public class FieldUtils {
}
/**
- * Gets an accessible <code>Field</code> by name respecting scope.
- * Only the specified class will be considered.
- *
- * @param cls the class to reflect, must not be null
- * @param fieldName the field name to obtain
+ * Gets an accessible <code>Field</code> by name respecting scope. Only the specified class will be considered.
+ *
+ * @param cls
+ * the class to reflect, must not be null
+ * @param fieldName
+ * the field name to obtain
* @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) {
return getDeclaredField(cls, fieldName, false);
}
/**
- * Gets an accessible <code>Field</code> by name breaking scope
- * if requested. Only the specified class will be considered.
- *
- * @param cls the class to reflect, must not be null
- * @param fieldName the field name to obtain
- * @param forceAccess whether to break scope restrictions using the
- * <code>setAccessible</code> method. False will only match public fields.
+ * Gets an accessible <code>Field</code> by name breaking scope if requested. Only the specified class will be
+ * considered.
+ *
+ * @param cls
+ * the class to reflect, must not be null
+ * @param fieldName
+ * 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
- * @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) {
if (cls == null) {
@@ -177,11 +188,46 @@ public class FieldUtils {
}
/**
+ * 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.
- * @param field to read
+ *
+ * @param field
+ * to read
* @return the field value
- * @throws IllegalArgumentException if the field is null or not static
- * @throws IllegalAccessException if the field is not accessible
+ * @throws IllegalArgumentException
+ * 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 {
return readStaticField(field, false);
@@ -189,12 +235,16 @@ public class FieldUtils {
/**
* Reads a static Field.
- * @param field to read
- * @param forceAccess whether to break scope restrictions using the
- * <code>setAccessible</code> method.
+ *
+ * @param field
+ * to read
+ * @param forceAccess
+ * whether to break scope restrictions using the <code>setAccessible</code> method.
* @return the field value
- * @throws IllegalArgumentException if the field is null or not static
- * @throws IllegalAccessException if the field is not made accessible
+ * @throws IllegalArgumentException
+ * 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 {
if (field == null) {
@@ -208,11 +258,16 @@ public class FieldUtils {
/**
* 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
- * @throws IllegalArgumentException 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
+ * @throws IllegalArgumentException
+ * 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 {
return readStaticField(cls, fieldName, false);
@@ -220,69 +275,83 @@ public class FieldUtils {
/**
* 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 forceAccess whether to break scope restrictions using the
- * <code>setAccessible</code> method. <code>False</code> will only
- * match public fields.
+ *
+ * @param cls
+ * the class to reflect, must not be null
+ * @param fieldName
+ * 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
- * @throws IllegalArgumentException 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
+ * @throws IllegalArgumentException
+ * 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)
- throws IllegalAccessException {
+ public static Object readStaticField(final Class<?> cls, final String fieldName, final boolean forceAccess) throws IllegalAccessException {
final Field field = getField(cls, fieldName, forceAccess);
if (field == null) {
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);
}
/**
- * Gets a static Field value by name. The field must be public.
- * Only the specified class will be considered.
- *
- * @param cls the class to reflect, must not be null
- * @param fieldName the field name to obtain
+ * Gets a static Field value by name. The field must be public. Only the specified class will be considered.
+ *
+ * @param cls
+ * the class to reflect, must not be null
+ * @param fieldName
+ * the field name to obtain
* @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 IllegalAccessException if the field is not accessible
+ * @throws IllegalArgumentException
+ * 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 {
return readDeclaredStaticField(cls, fieldName, false);
}
/**
- * Gets a static Field value by name. Only the specified class will
- * be considered.
- *
- * @param cls the class to reflect, must not be null
- * @param fieldName 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.
+ * Gets a static Field value by name. Only the specified class will be considered.
+ *
+ * @param cls
+ * the class to reflect, must not be null
+ * @param fieldName
+ * 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
- * @throws IllegalArgumentException 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
+ * @throws IllegalArgumentException
+ * 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)
- throws IllegalAccessException {
+ public static Object readDeclaredStaticField(final Class<?> cls, final String fieldName, final boolean forceAccess) throws IllegalAccessException {
final Field field = getDeclaredField(cls, fieldName, forceAccess);
if (field == null) {
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);
}
/**
* 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
- * @throws IllegalArgumentException if the field is null
- * @throws IllegalAccessException if the field is not accessible
+ * @throws IllegalArgumentException
+ * 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 {
return readField(field, target, false);
@@ -290,13 +359,18 @@ public class FieldUtils {
/**
* Reads a Field.
- * @param field the field to use
- * @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.
+ *
+ * @param field
+ * the field to use
+ * @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
- * @throws IllegalArgumentException if the field is null
- * @throws IllegalAccessException if the field is not made accessible
+ * @throws IllegalArgumentException
+ * 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 {
if (field == null) {
@@ -312,11 +386,16 @@ public class FieldUtils {
/**
* 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
- * @throws IllegalArgumentException if the class or field name is null
- * @throws IllegalAccessException if the named field is not public
+ * @throws IllegalArgumentException
+ * 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 {
return readField(target, fieldName, false);
@@ -324,14 +403,19 @@ public class FieldUtils {
/**
* 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 forceAccess whether to break scope restrictions using the
- * <code>setAccessible</code> method. <code>False</code> will only
- * match public fields.
+ *
+ * @param target
+ * the object to reflect, must not be null
+ * @param fieldName
+ * 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
- * @throws IllegalArgumentException if the class or field name is null
- * @throws IllegalAccessException if the named field is not made accessible
+ * @throws IllegalArgumentException
+ * 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 {
if (target == null) {
@@ -342,37 +426,44 @@ public class FieldUtils {
if (field == null) {
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);
}
/**
* 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
- * @throws IllegalArgumentException if the class or field name is null
- * @throws IllegalAccessException if the named field is not public
+ * @throws IllegalArgumentException
+ * 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 {
return readDeclaredField(target, fieldName, false);
}
/**
- * <p<>Gets a Field value by name. 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 forceAccess whether to break scope restrictions using the
- * <code>setAccessible</code> method. <code>False</code> will only
- * match public fields.
+ * <p<>Gets a Field value by name. 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 forceAccess
+ * whether to break scope restrictions using the <code>setAccessible</code> method. <code>False</code>
+ * will only match public fields.
* @return the Field object
- * @throws IllegalArgumentException if <code>target</code> or <code>fieldName</code> is null
- * @throws IllegalAccessException if the field is not made accessible
+ * @throws IllegalArgumentException
+ * 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)
- throws IllegalAccessException {
+ public static Object readDeclaredField(final Object target, final String fieldName, final boolean forceAccess) throws IllegalAccessException {
if (target == null) {
throw new IllegalArgumentException("target object must not be null");
}
@@ -381,16 +472,21 @@ public class FieldUtils {
if (field == null) {
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);
}
/**
* Writes a public static Field.
- * @param field to write
- * @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
+ *
+ * @param field
+ * to write
+ * @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 {
writeStaticField(field, value, false);
@@ -398,13 +494,18 @@ public class FieldUtils {
/**
* Writes a static Field.
- * @param field to write
- * @param value to set
- * @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 or not static
- * @throws IllegalAccessException if the field is not made accessible or is final
+ *
+ * @param field
+ * to write
+ * @param value
+ * to set
+ * @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 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 {
if (field == null) {
@@ -418,11 +519,17 @@ public class FieldUtils {
/**
* 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 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
+ *
+ * @param cls
+ * Class on which the Field is to be found
+ * @param fieldName
+ * 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 {
writeStaticField(cls, fieldName, value, false);
@@ -430,14 +537,20 @@ public class FieldUtils {
/**
* 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 value to set
- * @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
+ *
+ * @param cls
+ * 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>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)
throws IllegalAccessException {
@@ -445,51 +558,68 @@ public class FieldUtils {
if (field == null) {
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);
}
/**
* 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 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
+ *
+ * @param cls
+ * Class on which the Field is to be found
+ * @param fieldName
+ * 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)
- throws IllegalAccessException {
+ public static void writeDeclaredStaticField(final Class<?> cls, final String fieldName, final Object value) throws IllegalAccessException {
writeDeclaredStaticField(cls, fieldName, value, false);
}
/**
* 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 value to set
- * @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
- */
+ *
+ * @param cls
+ * 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>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)
throws IllegalAccessException {
final Field field = getDeclaredField(cls, fieldName, forceAccess);
if (field == null) {
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);
}
/**
* Writes an accessible field.
- * @param field to write
- * @param target 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
+ *
+ * @param field
+ * to write
+ * @param target
+ * 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 {
writeField(field, target, value, false);
@@ -497,17 +627,23 @@ public class FieldUtils {
/**
* Writes a field.
- * @param field to write
- * @param target the object to call on, may be null for static fields
- * @param value to set
- * @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
+ *
+ * @param field
+ * to write
+ * @param target
+ * the object to call on, may be null for static fields
+ * @param value
+ * to set
+ * @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)
- throws IllegalAccessException {
+ throws IllegalAccessException {
if (field == null) {
throw new IllegalArgumentException("The field must not be null");
}
@@ -521,11 +657,17 @@ public class FieldUtils {
/**
* 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 value to set
- * @throws IllegalArgumentException if <code>target</code> or <code>fieldName</code> is null
- * @throws IllegalAccessException if the field is not accessible
+ *
+ * @param target
+ * the object to reflect, must not be null
+ * @param fieldName
+ * 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 {
writeField(target, fieldName, value, false);
@@ -533,14 +675,20 @@ public class FieldUtils {
/**
* 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 value to set
- * @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
+ *
+ * @param target
+ * the object to reflect, must not be null
+ * @param fieldName
+ * the field name to obtain
+ * @param value
+ * to set
+ * @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)
throws IllegalAccessException {
@@ -552,17 +700,23 @@ public class FieldUtils {
if (field == null) {
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);
}
/**
* 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 value to set
- * @throws IllegalArgumentException if <code>target</code> or <code>fieldName</code> is null
- * @throws IllegalAccessException if the field is not made accessible
+ *
+ * @param target
+ * the object to reflect, must not be null
+ * @param fieldName
+ * 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 {
writeDeclaredField(target, fieldName, value, false);
@@ -570,14 +724,20 @@ public class FieldUtils {
/**
* 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 value to set
- * @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
+ *
+ * @param target
+ * the object to reflect, must not be null
+ * @param fieldName
+ * the field name to obtain
+ * @param value
+ * to set
+ * @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)
throws IllegalAccessException {
@@ -589,7 +749,7 @@ public class FieldUtils {
if (field == null) {
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);
}
}
Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java?rev=1455751&r1=1455750&r2=1455751&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/reflect/FieldUtilsTest.java Tue Mar 12 23:23:04 2013
@@ -16,6 +16,7 @@
*/
package org.apache.commons.lang3.reflect;
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -28,6 +29,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
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.Foo;
import org.apache.commons.lang3.reflect.testbed.PrivatelyShadowedChild;
@@ -40,6 +42,7 @@ import org.junit.Test;
/**
* Unit tests FieldUtils
+ *
* @version $Id$
*/
public class FieldUtilsTest {
@@ -73,7 +76,7 @@ public class FieldUtilsTest {
assertTrue(Modifier.isPublic(FieldUtils.class.getModifiers()));
assertFalse(Modifier.isFinal(FieldUtils.class.getModifiers()));
}
-
+
@Test
public void testGetField() {
assertEquals(Foo.class, FieldUtils.getField(PublicChild.class, "VALUE").getDeclaringClass());
@@ -82,14 +85,10 @@ public class FieldUtilsTest {
assertNull(FieldUtils.getField(PublicChild.class, "i"));
assertNull(FieldUtils.getField(PublicChild.class, "d"));
assertEquals(Foo.class, FieldUtils.getField(PubliclyShadowedChild.class, "VALUE").getDeclaringClass());
- assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "s")
- .getDeclaringClass());
- assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "b")
- .getDeclaringClass());
- assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "i")
- .getDeclaringClass());
- assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "d")
- .getDeclaringClass());
+ assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "s").getDeclaringClass());
+ assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "b").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(parentClass, FieldUtils.getField(PrivatelyShadowedChild.class, "s").getDeclaringClass());
assertNull(FieldUtils.getField(PrivatelyShadowedChild.class, "b"));
@@ -97,16 +96,16 @@ public class FieldUtilsTest {
assertNull(FieldUtils.getField(PrivatelyShadowedChild.class, "d"));
}
- @Test(expected=IllegalArgumentException.class)
+ @Test(expected = IllegalArgumentException.class)
public void testGetFieldIllegalArgumentException1() {
FieldUtils.getField(null, "none");
}
-
- @Test(expected=IllegalArgumentException.class)
+
+ @Test(expected = IllegalArgumentException.class)
public void testGetFieldIllegalArgumentException2() {
FieldUtils.getField(PublicChild.class, null);
}
-
+
@Test
public void testGetFieldForceAccess() {
assertEquals(PublicChild.class, FieldUtils.getField(PublicChild.class, "VALUE", true).getDeclaringClass());
@@ -115,34 +114,36 @@ public class FieldUtilsTest {
assertEquals(parentClass, FieldUtils.getField(PublicChild.class, "i", true).getDeclaringClass());
assertEquals(parentClass, FieldUtils.getField(PublicChild.class, "d", true).getDeclaringClass());
assertEquals(Foo.class, FieldUtils.getField(PubliclyShadowedChild.class, "VALUE", true).getDeclaringClass());
- assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "s", true)
- .getDeclaringClass());
- assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "b", true)
- .getDeclaringClass());
- assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "i", true)
- .getDeclaringClass());
- assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "d", true)
- .getDeclaringClass());
+ assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "s", true).getDeclaringClass());
+ assertEquals(PubliclyShadowedChild.class, FieldUtils.getField(PubliclyShadowedChild.class, "b", 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(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "s", true)
- .getDeclaringClass());
- assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "b", true)
- .getDeclaringClass());
- assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "i", true)
- .getDeclaringClass());
- assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "d", true)
- .getDeclaringClass());
+ assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "s", true).getDeclaringClass());
+ assertEquals(PrivatelyShadowedChild.class, FieldUtils.getField(PrivatelyShadowedChild.class, "b", 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() {
FieldUtils.getField(null, "none", true);
}
-
- @Test(expected=IllegalArgumentException.class)
+
+ @Test(expected = IllegalArgumentException.class)
public void testGetFieldForceAccessIllegalArgumentException2() {
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
public void testGetDeclaredField() {
@@ -152,14 +153,10 @@ public class FieldUtilsTest {
assertNull(FieldUtils.getDeclaredField(PublicChild.class, "i"));
assertNull(FieldUtils.getDeclaredField(PublicChild.class, "d"));
assertNull(FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "VALUE"));
- assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "s")
- .getDeclaringClass());
- assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "b")
- .getDeclaringClass());
- assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "i")
- .getDeclaringClass());
- assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "d")
- .getDeclaringClass());
+ assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "s").getDeclaringClass());
+ assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "b").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, "s"));
assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "b"));
@@ -167,65 +164,56 @@ public class FieldUtilsTest {
assertNull(FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "d"));
}
- @Test(expected=IllegalArgumentException.class)
+ @Test(expected = IllegalArgumentException.class)
public void testGetDeclaredFieldAccessIllegalArgumentException1() {
FieldUtils.getDeclaredField(null, "none");
- }
+ }
- @Test(expected=IllegalArgumentException.class)
+ @Test(expected = IllegalArgumentException.class)
public void testGetDeclaredFieldAccessIllegalArgumentException2() {
FieldUtils.getDeclaredField(PublicChild.class, null);
- }
+ }
@Test
public void testGetDeclaredFieldForceAccess() {
- assertEquals(PublicChild.class, FieldUtils.getDeclaredField(PublicChild.class, "VALUE", true)
- .getDeclaringClass());
+ assertEquals(PublicChild.class, FieldUtils.getDeclaredField(PublicChild.class, "VALUE", true).getDeclaringClass());
assertNull(FieldUtils.getDeclaredField(PublicChild.class, "s", true));
assertNull(FieldUtils.getDeclaredField(PublicChild.class, "b", true));
assertNull(FieldUtils.getDeclaredField(PublicChild.class, "i", true));
assertNull(FieldUtils.getDeclaredField(PublicChild.class, "d", true));
assertNull(FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "VALUE", true));
- assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "s", true)
- .getDeclaringClass());
- assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "b", true)
- .getDeclaringClass());
- assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "i", true)
- .getDeclaringClass());
- assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "d", true)
- .getDeclaringClass());
+ assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "s", true).getDeclaringClass());
+ assertEquals(PubliclyShadowedChild.class, FieldUtils.getDeclaredField(PubliclyShadowedChild.class, "b", 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));
- assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "s", true)
- .getDeclaringClass());
- assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "b", true)
- .getDeclaringClass());
- assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "i", true)
- .getDeclaringClass());
- assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "d", true)
- .getDeclaringClass());
+ assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "s", true).getDeclaringClass());
+ assertEquals(PrivatelyShadowedChild.class, FieldUtils.getDeclaredField(PrivatelyShadowedChild.class, "b", 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() {
FieldUtils.getDeclaredField(null, "none", true);
- }
+ }
- @Test(expected=IllegalArgumentException.class)
+ @Test(expected = IllegalArgumentException.class)
public void testGetDeclaredFieldForceAccessIllegalArgumentException2() {
FieldUtils.getDeclaredField(PublicChild.class, null, true);
- }
+ }
@Test
public void testReadStaticField() throws Exception {
assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(Foo.class, "VALUE")));
}
- @Test(expected=IllegalArgumentException.class)
+ @Test(expected = IllegalArgumentException.class)
public void testReadStaticFieldIllegalArgumentException1() throws Exception {
- FieldUtils.readStaticField(null);
+ FieldUtils.readStaticField(null);
}
- @Test(expected=IllegalArgumentException.class)
+ @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");
@@ -239,12 +227,12 @@ public class FieldUtilsTest {
assertEquals(Foo.VALUE, FieldUtils.readStaticField(FieldUtils.getField(PublicChild.class, "VALUE")));
}
- @Test(expected=IllegalArgumentException.class)
+ @Test(expected = IllegalArgumentException.class)
public void testReadStaticFieldForceAccessIllegalArgumentException1() throws Exception {
FieldUtils.readStaticField(null, true);
}
- @Test(expected=IllegalArgumentException.class)
+ @Test(expected = IllegalArgumentException.class)
public void testReadStaticFieldForceAccessIllegalArgumentException2() throws Exception {
final Field nonStaticField = FieldUtils.getField(PublicChild.class, "s", true);
assumeNotNull(nonStaticField);
@@ -257,28 +245,28 @@ public class FieldUtilsTest {
assertEquals(Foo.VALUE, FieldUtils.readStaticField(PubliclyShadowedChild.class, "VALUE"));
assertEquals(Foo.VALUE, FieldUtils.readStaticField(PrivatelyShadowedChild.class, "VALUE"));
assertEquals(Foo.VALUE, FieldUtils.readStaticField(PublicChild.class, "VALUE"));
-
+
try {
FieldUtils.readStaticField(null, "none");
fail("null class should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// expected
}
-
+
try {
FieldUtils.readStaticField(Foo.class, null);
fail("null field name should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// expected
}
-
+
try {
FieldUtils.readStaticField(Foo.class, "does_not_exist");
fail("a field that doesn't exist should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// expected
}
-
+
try {
FieldUtils.readStaticField(PublicChild.class, "s");
fail("non-static field should cause an IllegalArgumentException");
@@ -293,28 +281,28 @@ public class FieldUtilsTest {
assertEquals(Foo.VALUE, FieldUtils.readStaticField(PubliclyShadowedChild.class, "VALUE", true));
assertEquals(Foo.VALUE, FieldUtils.readStaticField(PrivatelyShadowedChild.class, "VALUE", true));
assertEquals("child", FieldUtils.readStaticField(PublicChild.class, "VALUE", true));
-
+
try {
FieldUtils.readStaticField(null, "none", true);
fail("null class should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// expected
}
-
+
try {
FieldUtils.readStaticField(Foo.class, null, true);
fail("null field name should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// expected
}
-
+
try {
FieldUtils.readStaticField(Foo.class, "does_not_exist", true);
fail("a field that doesn't exist should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// expected
}
-
+
try {
FieldUtils.readStaticField(PublicChild.class, "s", false);
fail("non-static field should cause an IllegalArgumentException");
@@ -382,9 +370,9 @@ public class FieldUtilsTest {
assertEquals(D0, FieldUtils.readField(parentD, publicChild));
assertEquals(D0, FieldUtils.readField(parentD, publiclyShadowedChild));
assertEquals(D0, FieldUtils.readField(parentD, privatelyShadowedChild));
-
+
try {
- FieldUtils.readField((Field)null, publicChild);
+ FieldUtils.readField((Field) null, publicChild);
fail("a null field should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// expected
@@ -413,9 +401,9 @@ public class FieldUtilsTest {
assertEquals(D0, FieldUtils.readField(parentD, publicChild, true));
assertEquals(D0, FieldUtils.readField(parentD, publiclyShadowedChild, true));
assertEquals(D0, FieldUtils.readField(parentD, privatelyShadowedChild, true));
-
+
try {
- FieldUtils.readField((Field)null, publicChild, true);
+ FieldUtils.readField((Field) null, publicChild, true);
fail("a null field should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// expected
@@ -427,21 +415,21 @@ public class FieldUtilsTest {
assertEquals("s", FieldUtils.readField(publicChild, "s"));
assertEquals("ss", FieldUtils.readField(publiclyShadowedChild, "s"));
assertEquals("s", FieldUtils.readField(privatelyShadowedChild, "s"));
-
+
try {
FieldUtils.readField(publicChild, null);
fail("a null field name should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// expected
}
-
+
try {
- FieldUtils.readField((Object)null, "none");
+ FieldUtils.readField((Object) null, "none");
fail("a null target should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// expected
}
-
+
try {
assertEquals(Boolean.FALSE, FieldUtils.readField(publicChild, "b"));
fail("expected IllegalArgumentException");
@@ -497,16 +485,16 @@ public class FieldUtilsTest {
assertEquals(D0, FieldUtils.readField(publicChild, "d", true));
assertEquals(D1, FieldUtils.readField(publiclyShadowedChild, "d", true));
assertEquals(D1, FieldUtils.readField(privatelyShadowedChild, "d", true));
-
+
try {
FieldUtils.readField(publicChild, null, true);
fail("a null field name should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// expected
}
-
+
try {
- FieldUtils.readField((Object)null, "none", true);
+ FieldUtils.readField((Object) null, "none", true);
fail("a null target should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// expected
@@ -521,14 +509,14 @@ public class FieldUtilsTest {
} catch (final IllegalArgumentException e) {
// expected
}
-
+
try {
- FieldUtils.readDeclaredField((Object)null, "none");
+ FieldUtils.readDeclaredField((Object) null, "none");
fail("a null target should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// expected
}
-
+
try {
assertEquals("s", FieldUtils.readDeclaredField(publicChild, "s"));
fail("expected IllegalArgumentException");
@@ -591,14 +579,14 @@ public class FieldUtilsTest {
} catch (final IllegalArgumentException e) {
// expected
}
-
+
try {
- FieldUtils.readDeclaredField((Object)null, "none", true);
+ FieldUtils.readDeclaredField((Object) null, "none", true);
fail("a null target should cause an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
// expected
}
-
+
try {
assertEquals("s", FieldUtils.readDeclaredField(publicChild, "s", true));
fail("expected IllegalArgumentException");
@@ -1135,7 +1123,7 @@ public class FieldUtilsTest {
assertEquals(Double.valueOf(0.0), FieldUtils.readDeclaredField(privatelyShadowedChild, "d", true));
}
- @Test(expected=IllegalArgumentException.class)
+ @Test(expected = IllegalArgumentException.class)
public void testAmbig() {
FieldUtils.getField(Ambig.class, "VALUE");
}