[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:
parent
22a83a7597
commit
035e5c7115
|
@ -18,16 +18,17 @@ 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.
|
||||
* 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
|
||||
* @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.
|
||||
* 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.
|
||||
* @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) {
|
||||
|
@ -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.
|
||||
* 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
|
||||
* @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.
|
||||
* 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.
|
||||
* @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) {
|
||||
|
@ -176,12 +187,47 @@ public class FieldUtils {
|
|||
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.
|
||||
* @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.
|
||||
* 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
|
||||
* @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.
|
||||
* 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.
|
||||
* @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.
|
||||
* <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.
|
||||
* @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,33 +558,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:
|
||||
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 {
|
||||
|
@ -479,17 +603,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, (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,14 +627,20 @@ 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 {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
@ -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,12 +96,12 @@ 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);
|
||||
}
|
||||
|
@ -115,35 +114,37 @@ 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() {
|
||||
assertNull(FieldUtils.getDeclaredField(PublicChild.class, "VALUE"));
|
||||
|
@ -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,50 +164,41 @@ 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);
|
||||
}
|
||||
|
@ -220,12 +208,12 @@ public class FieldUtilsTest {
|
|||
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);
|
||||
}
|
||||
|
||||
@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);
|
||||
|
@ -384,7 +372,7 @@ public class FieldUtilsTest {
|
|||
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
|
||||
|
@ -415,7 +403,7 @@ public class FieldUtilsTest {
|
|||
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
|
||||
|
@ -436,7 +424,7 @@ public class FieldUtilsTest {
|
|||
}
|
||||
|
||||
try {
|
||||
FieldUtils.readField((Object)null, "none");
|
||||
FieldUtils.readField((Object) null, "none");
|
||||
fail("a null target should cause an IllegalArgumentException");
|
||||
} catch (final IllegalArgumentException e) {
|
||||
// expected
|
||||
|
@ -506,7 +494,7 @@ public class FieldUtilsTest {
|
|||
}
|
||||
|
||||
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
|
||||
|
@ -523,7 +511,7 @@ public class FieldUtilsTest {
|
|||
}
|
||||
|
||||
try {
|
||||
FieldUtils.readDeclaredField((Object)null, "none");
|
||||
FieldUtils.readDeclaredField((Object) null, "none");
|
||||
fail("a null target should cause an IllegalArgumentException");
|
||||
} catch (final IllegalArgumentException e) {
|
||||
// expected
|
||||
|
@ -593,7 +581,7 @@ public class FieldUtilsTest {
|
|||
}
|
||||
|
||||
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
|
||||
|
@ -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");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue