Solely Javadoc fixes.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137420 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2003-07-14 22:29:04 +00:00
parent 00dc5a8684
commit faf34a0bb7
4 changed files with 295 additions and 217 deletions

View File

@ -58,27 +58,30 @@
import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.ArrayUtils;
/** /**
* <code>ConstructorUtils</code> contains utility methods for working for * <p><code>ConstructorUtils</code> contains utility methods for working for
* constructors by reflection. * constructors by reflection.</p>
* <p> *
* The ability is provided to break the scoping restrictions coded by the * <p>The ability is provided to break the scoping restrictions coded by the
* programmer. This can allow classes to be created that shouldn't be, for * programmer. This can allow classes to be created that shouldn't be, for
* example new instances of an enumerated type. Thus, this facility should * example new instances of an enumerated type. Thus, this facility should
* be used with care. * be used with care.</p>
* *
* @author <a href="mailto:scolebourne@apache.org">Stephen Colebourne</a> * @author <a href="mailto:scolebourne@apache.org">Stephen Colebourne</a>
* @version $Id: ConstructorUtils.java,v 1.1 2002/10/24 23:12:54 scolebourne Exp $ * @version $Id: ConstructorUtils.java,v 1.2 2003/07/14 22:29:03 bayard Exp $
*/ */
public class ConstructorUtils { public class ConstructorUtils {
/** An empty constructor array */ /**
* An empty constructor array.
*/
public static final Constructor[] EMPTY_CONSTRUCTOR_ARRAY = new Constructor[0]; public static final Constructor[] EMPTY_CONSTRUCTOR_ARRAY = new Constructor[0];
/** /**
* ConstructorUtils instances should NOT be constructed in standard programming. * <p>ConstructorUtils instances should NOT be constructed in standard programming.</p>
* Instead, the class should be used as <code>ConstructorUtils.newInstance(...)</code>. *
* <p>Instead, the class should be used as <code>ConstructorUtils.newInstance(...)</code>.
* This constructor is public to permit tools that require a JavaBean instance * This constructor is public to permit tools that require a JavaBean instance
* to operate. * to operate.</p>
*/ */
public ConstructorUtils() { public ConstructorUtils() {
} }
@ -86,30 +89,35 @@ public ConstructorUtils() {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/** /**
* Gets a public <code>Constructor</code> object by matching the * <p>Gets a public <code>Constructor</code> object by matching the
* parameter types as per the Java Language Specification. * parameter types as per the Java Language Specification.</p>
* *
* @param cls Class object to find constructor for, must not be null * @param cls Class object to find constructor for, must not
* @param types array of Class objects representing parameter types, may be null * be <code>null</code>
* @param types array of Class objects representing parameter
* types, may be <code>null</code>
* @return Constructor object * @return Constructor object
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
* @throws IllegalArgumentException if the class is null * @throws IllegalArgumentException if the class is <code>null</code>
*/ */
public static Constructor getConstructor(Class cls, Class[] types) { public static Constructor getConstructor(Class cls, Class[] types) {
return getConstructor(cls, types, false); return getConstructor(cls, types, false);
} }
/** /**
* Gets a public <code>Constructor</code> object by matching the * <p>Gets a public <code>Constructor</code> object by matching the
* parameter types as per the Java Language Specification. * parameter types as per the Java Language Specification.</p>
* *
* @param cls Class object to find constructor for, must not be null * @param cls Class object to find constructor for, must not
* @param types array of Class objects representing parameter types, may be null * be <code>null</code>
* @param breakScope whether to break scope restrictions using the * @param types array of Class objects representing parameter
* <code>setAccessible</code> method. False will only match public methods. * types, may be <code>null</code>
* @param breakScope whether to break scope restrictions using
* the <code>setAccessible</code> method. <code>False</code> will
* only match public methods.
* @return Constructor object * @return Constructor object
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
* @throws IllegalArgumentException if the class is null * @throws IllegalArgumentException if the class is <code>null</code>
*/ */
public static Constructor getConstructor(Class cls, Class[] types, boolean breakScope) { public static Constructor getConstructor(Class cls, Class[] types, boolean breakScope) {
if (cls == null) { if (cls == null) {
@ -151,30 +159,34 @@ public static Constructor getConstructor(Class cls, Class[] types, boolean break
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/** /**
* Gets a public <code>Constructor</code> object by exactly matching the * <p>Gets a public <code>Constructor</code> object by exactly matching the
* parameter types. * parameter types.</p>
* *
* @param cls Class object to find constructor for, must not be null * @param cls Class object to find constructor for, must not
* @param types array of Class objects representing parameter types, may be null * be <code>null</code>
* @param types array of Class objects representing parameter
* types, may be <code>null</code>
* @return Constructor object * @return Constructor object
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
* @throws IllegalArgumentException if the class is null * @throws IllegalArgumentException if the class is <code>null</code>
*/ */
public static Constructor getConstructorExact(Class cls, Class[] types) { public static Constructor getConstructorExact(Class cls, Class[] types) {
return getConstructorExact(cls, types, false); return getConstructorExact(cls, types, false);
} }
/** /**
* Gets a <code>Constructor</code> object by exactly matching the * <p>Gets a <code>Constructor</code> object by exactly matching the
* parameter types. * parameter types.</p>
* *
* @param cls Class object to find constructor for, must not be null * @param cls Class object to find constructor for, must not
* @param types array of Class objects representing parameter types, may be null * be <code>null</code>
* @param types array of Class objects representing parameter types, may
* be <code>null</code>
* @param breakScope whether to break scope restrictions using the * @param breakScope whether to break scope restrictions using the
* <code>setAccessible</code> method. False will only match public methods. * <code>setAccessible</code> method. False will only match public methods.
* @return Constructor object * @return Constructor object
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
* @throws IllegalArgumentException if the class is null * @throws IllegalArgumentException if the class is <code>null</code>
*/ */
public static Constructor getConstructorExact(Class cls, Class[] types, boolean breakScope) { public static Constructor getConstructorExact(Class cls, Class[] types, boolean breakScope) {
if (cls == null) { if (cls == null) {
@ -206,41 +218,48 @@ public static Constructor getConstructorExact(Class cls, Class[] types, boolean
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/** /**
* Creates a new instance using a <code>Constructor</code> and parameters. * <p>Creates a new instance using a <code>Constructor</code> and parameters.</p>
* *
* @param con Class object to find constructor for, must not be null * @param con Class object to find constructor for, must not
* @param param the single parameter to pass to the constructor, may be null * be <code>null</code>
* @param param the single parameter to pass to the constructor, may
* be <code>null</code>
* @return the newly created object * @return the newly created object
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
* @throws IllegalArgumentException if the constructor is null * @throws IllegalArgumentException if the constructor is <code>null</code>
*/ */
public static Object newInstance(Constructor con, Object param) { public static Object newInstance(Constructor con, Object param) {
return newInstance(con, new Object[] {param}, false); return newInstance(con, new Object[] {param}, false);
} }
/** /**
* Creates a new instance using a <code>Constructor</code> and parameters. * <p>Creates a new instance using a <code>Constructor</code> and parameters.</p>
* *
* @param con Class object to find constructor for, must not be null * @param con Class object to find constructor for, must not
* @param params array of objects to pass as parameters, may be null * be <code>null</code>
* @param params array of objects to pass as parameters, may
* be <code>null</code>
* @return the newly created object * @return the newly created object
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
* @throws IllegalArgumentException if the constructor is null * @throws IllegalArgumentException if the constructor is <code>null</code>
*/ */
public static Object newInstance(Constructor con, Object[] params) { public static Object newInstance(Constructor con, Object[] params) {
return newInstance(con, params, false); return newInstance(con, params, false);
} }
/** /**
* Creates a new instance using a <code>Constructor</code> and parameters. * <p>Creates a new instance using a <code>Constructor</code> and parameters.</p>
* *
* @param con Class object to find constructor for, must not be null * @param con Class object to find constructor for, must not
* @param params array of objects to pass as parameters, may be null * be <code>null</code>
* @param params array of objects to pass as parameters, may
* be <code>null</code>
* @param breakScope whether to break scope restrictions using the * @param breakScope whether to break scope restrictions using the
* <code>setAccessible</code> method. False will only match public methods. * <code>setAccessible</code> method. <code>False</code> will only
* match public methods.
* @return the newly created object * @return the newly created object
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
* @throws IllegalArgumentException if the constructor is null * @throws IllegalArgumentException if the constructor is <code>null</code>
*/ */
public static Object newInstance(Constructor con, Object[] params, boolean breakScope) { public static Object newInstance(Constructor con, Object[] params, boolean breakScope) {
if (con == null) { if (con == null) {
@ -266,7 +285,7 @@ public static Object newInstance(Constructor con, Object[] params, boolean break
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/** /**
* Creates a new instance of the specified <code>Class</code> by name. * <p>Creates a new instance of the specified <code>Class</code> by name.</p>
* *
* @param className String class name to instantiate, must not be empty * @param className String class name to instantiate, must not be empty
* @return the newly created object * @return the newly created object
@ -278,9 +297,10 @@ public static Object newInstance(String className) {
} }
/** /**
* Creates a new instance of the specified <code>Class</code> by name. * <p>Creates a new instance of the specified <code>Class</code> by name.</p>
* If the constructor is not public, <code>setAccessible(true)</code> *
* is used to make it accessible. * <p>If the constructor is not public, <code>setAccessible(true)</code>
* is used to make it accessible.</p>
* *
* @param className String class name to instantiate, must not be empty * @param className String class name to instantiate, must not be empty
* @param breakScope whether to break scope restrictions using the * @param breakScope whether to break scope restrictions using the
@ -297,28 +317,30 @@ public static Object newInstance(String className, boolean breakScope) {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/** /**
* Creates a new instance of the specified <code>Class</code>. * <p>Creates a new instance of the specified <code>Class</code>.</p>
* *
* @param cls Class object to instantiate, must not be null * @param cls Class object to instantiate, must not be <code>null</code>
* @return the newly created object * @return the newly created object
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
* @throws IllegalArgumentException if the class is null * @throws IllegalArgumentException if the class is <code>null</code>
*/ */
public static Object newInstance(Class cls) { public static Object newInstance(Class cls) {
return newInstance(cls, false); return newInstance(cls, false);
} }
/** /**
* Creates a new instance of the specified <code>Class</code>. * <p>Creates a new instance of the specified <code>Class</code>.</p>
* If the constructor is not public, <code>setAccessible(true)</code> *
* is used to make it accessible. * <p>If the constructor is not public, <code>setAccessible(true)</code>
* is used to make it accessible.</p>
* *
* @param cls Class object to instantiate, must not be null * @param cls Class object to instantiate, must not be <code>null</code>
* @param breakScope whether to break scope restrictions using the * @param breakScope whether to break scope restrictions using the
* <code>setAccessible</code> method. False will only match public methods. * <code>setAccessible</code> method. <code>False</code> will only
* match public methods.
* @return the newly created object * @return the newly created object
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
* @throws IllegalArgumentException if the class is null * @throws IllegalArgumentException if the class is <code>null</code>
*/ */
public static Object newInstance(Class cls, boolean breakScope) { public static Object newInstance(Class cls, boolean breakScope) {
if (breakScope) { if (breakScope) {
@ -346,35 +368,44 @@ public static Object newInstance(Class cls, boolean breakScope) {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/** /**
* Creates a new instance of the specified <code>Class</code>. * <p>Creates a new instance of the specified <code>Class</code>.</p>
* The constructor is found by matching the *
* parameter types as per the Java Language Specification. * <p>The constructor is found by matching the
* parameter types as per the Java Language Specification.</p>
* *
* @param cls Class object to instantiate, must not be null * @param cls Class object to instantiate, must not
* @param types array of Class objects representing parameter types, may be null * be <code>null</code>
* @param params array of objects to pass as parameters, may be null * @param types array of Class objects representing parameter types,
* may be <code>null</code>
* @param params array of objects to pass as parameters, may
* be <code>null</code>
* @return the newly created object * @return the newly created object
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
* @throws IllegalArgumentException if the class is null * @throws IllegalArgumentException if the class is <code>null</code>
*/ */
public static Object newInstance(Class cls, Class[] types, Object[] params) { public static Object newInstance(Class cls, Class[] types, Object[] params) {
return newInstance(cls, types, params, false); return newInstance(cls, types, params, false);
} }
/** /**
* Creates a new instance of the specified <code>Class</code>. * <p>Creates a new instance of the specified <code>Class</code>.</p>
* The constructor is found by matching the *
* parameter types as per the Java Language Specification. * <p>The constructor is found by matching the
* parameter types as per the Java Language Specification.</p>
* *
* @param cls Class object to instantiate, must not be null * @param cls Class object to instantiate, must not
* @param types array of Class objects representing parameter types, may be null * be <code>null</code>
* @param params array of objects to pass as parameters, may be null * @param types array of Class objects representing parameter types,
* may be <code>null</code>
* @param params array of objects to pass as parameters, may
* be <code>null</code>
* @param breakScope whether to break scope restrictions using the * @param breakScope whether to break scope restrictions using the
* <code>setAccessible</code> method. False will only match public methods. * <code>setAccessible</code> method. <code>False</code> will only
* match public methods.
* @return the newly created object * @return the newly created object
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
* @throws IllegalArgumentException if the types and params lengths differ * @throws IllegalArgumentException if the types and params lengths differ
* @throws IllegalArgumentException if the class is null * @throws IllegalArgumentException if the class is <code>null</code>
*/ */
public static Object newInstance(Class cls, Class[] types, Object[] params, boolean breakScope) { public static Object newInstance(Class cls, Class[] types, Object[] params, boolean breakScope) {
if (ArrayUtils.isSameLength(types, params) == false) { if (ArrayUtils.isSameLength(types, params) == false) {
@ -387,33 +418,42 @@ public static Object newInstance(Class cls, Class[] types, Object[] params, bool
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/** /**
* Creates a new instance of the specified <code>Class</code>. * <p>Creates a new instance of the specified <code>Class</code>.</p>
* The constructor is found by matching the parameter types exactly. *
* <p>The constructor is found by matching the parameter types exactly.</p>
* *
* @param cls Class object to instantiate, must not be null * @param cls Class object to instantiate, must not
* @param types array of Class objects representing parameter types, may be null * be <code>null</code>
* @param params array of objects to pass as parameters, may be null * @param types array of Class objects representing parameter types,
* may be <code>null</code>
* @param params array of objects to pass as parameters, may
* be <code>null</code>
* @return the newly created object * @return the newly created object
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
* @throws IllegalArgumentException if the class is null * @throws IllegalArgumentException if the class is <code>null</code>
*/ */
public static Object newInstanceExact(Class cls, Class[] types, Object[] params) { public static Object newInstanceExact(Class cls, Class[] types, Object[] params) {
return newInstanceExact(cls, types, params, false); return newInstanceExact(cls, types, params, false);
} }
/** /**
* Creates a new instance of the specified <code>Class</code>. * <p>Creates a new instance of the specified <code>Class</code>.</p>
* The constructor is found by matching the parameter types exactly. *
* <p>The constructor is found by matching the parameter types exactly.</p>
* *
* @param cls Class object to instantiate, must not be null * @param cls Class object to instantiate, must not
* @param types array of Class objects representing parameter types, may be null * be <code>null</code>
* @param params array of objects to pass as parameters, may be null * @param types array of Class objects representing parameter types,
* may be <code>null</code>
* @param params array of objects to pass as parameters, may
* be <code>null</code>
* @param breakScope whether to break scope restrictions using the * @param breakScope whether to break scope restrictions using the
* <code>setAccessible</code> method. False will only match public methods. * <code>setAccessible</code> method. <code>False</code> will only match
* public methods.
* @return the newly created object * @return the newly created object
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
* @throws IllegalArgumentException if the types and params lengths differ * @throws IllegalArgumentException if the types and params lengths differ
* @throws IllegalArgumentException if the class is null * @throws IllegalArgumentException if the class is <code>null</code>
*/ */
public static Object newInstanceExact(Class cls, Class[] types, Object[] params, boolean breakScope) { public static Object newInstanceExact(Class cls, Class[] types, Object[] params, boolean breakScope) {
if (ArrayUtils.isSameLength(types, params) == false) { if (ArrayUtils.isSameLength(types, params) == false) {

View File

@ -56,26 +56,29 @@
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
/** /**
* <code>FieldUtils</code> contains utility methods for working with * <p><code>FieldUtils</code> contains utility methods for working with
* fields by reflection. * fields by reflection.</p>
* <p> *
* The ability is provided to break the scoping restrictions coded by the * <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 * programmer. This can allow fields to be changed that shouldn't be. This
* facility should be used with care. * facility should be used with care.</p>
* *
* @author <a href="mailto:scolebourne@apache.org">Stephen Colebourne</a> * @author <a href="mailto:scolebourne@apache.org">Stephen Colebourne</a>
* @version $Id: FieldUtils.java,v 1.1 2002/10/24 23:12:54 scolebourne Exp $ * @version $Id: FieldUtils.java,v 1.2 2003/07/14 22:29:04 bayard Exp $
*/ */
public class FieldUtils { public class FieldUtils {
/** An empty field array */ /**
* An empty field array.
*/
public static final Field[] EMPTY_FIELD_ARRAY = new Field[0]; public static final Field[] EMPTY_FIELD_ARRAY = new Field[0];
/** /**
* FieldUtils instances should NOT be constructed in standard programming. * <p>FieldUtils instances should NOT be constructed in standard programming.</p>
* Instead, the class should be used as <code>FieldUtils.getField(cls, name)</code>. *
* <p>Instead, the class should be used as <code>FieldUtils.getField(cls, name)</code>.
* This constructor is public to permit tools that require a JavaBean instance * This constructor is public to permit tools that require a JavaBean instance
* to operate. * to operate.</p>
*/ */
public FieldUtils() { public FieldUtils() {
} }
@ -83,13 +86,14 @@ public FieldUtils() {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/** /**
* Gets an accessible Field by name repecting scope. * <p>Gets an accessible <code>Field</code> by name repecting scope.
* Superclasses/interfaces will be considered. * Superclasses/interfaces will be considered.</p>
* *
* @param cls the class to reflect, must not be null * @param cls the class to reflect, must not be <code>null</code>
* @param fieldName the field name to obtain * @param fieldName the field name to obtain
* @return the Field object * @return the Field object
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException if the class or field name
* is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Field getField(Class cls, String fieldName) { public static Field getField(Class cls, String fieldName) {
@ -97,15 +101,17 @@ public static Field getField(Class cls, String fieldName) {
} }
/** /**
* Gets an accessible Field by name breaking scope if requested. * <p>Gets an accessible <code>Field</code> by name breaking scope
* Superclasses/interfaces will be considered. * if requested. Superclasses/interfaces will be considered.</p>
* *
* @param cls the class to reflect, must not be null * @param cls the class to reflect, must not be <code>null</code>
* @param fieldName the field name to obtain * @param fieldName the field name to obtain
* @param breakScope whether to break scope restrictions using the * @param breakScope whether to break scope restrictions using the
* <code>setAccessible</code> method. False will only match public fields. * <code>setAccessible</code> method. <code>False</code> will only
* match public fields.
* @return the Field object * @return the Field object
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException if the class or field name
* is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Field getField(Class cls, String fieldName, boolean breakScope) { public static Field getField(Class cls, String fieldName, boolean breakScope) {
@ -189,13 +195,14 @@ public static Field getField(Class cls, String fieldName, boolean breakScope) {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/** /**
* Gets an accessible Field by name respecting scope. * <p>Gets an accessible <code>Field</code> by name respecting scope.
* Only the specified class will be considered. * Only the specified class will be considered.</p>
* *
* @param cls the class to reflect, must not be null * @param cls the class to reflect, must not be <code>null</code>
* @param fieldName the field name to obtain * @param fieldName the field name to obtain
* @return the Field object * @return the Field object
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException if the class or field name
* is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Field getFieldExact(Class cls, String fieldName) { public static Field getFieldExact(Class cls, String fieldName) {
@ -203,15 +210,16 @@ public static Field getFieldExact(Class cls, String fieldName) {
} }
/** /**
* Gets an accessible Field by name breaking scope if requested. * <p>Gets an accessible <code>Field</code> by name breaking scope
* Only the specified class will be considered. * if requested. Only the specified class will be considered.</p>
* *
* @param cls the class to reflect, must not be null * @param cls the class to reflect, must not be <code>null</code>
* @param fieldName the field name to obtain * @param fieldName the field name to obtain
* @param breakScope whether to break scope restrictions using the * @param breakScope whether to break scope restrictions using the
* <code>setAccessible</code> method. False will only match public fields. * <code>setAccessible</code> method. False will only match public fields.
* @return the Field object * @return the Field object
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException if the class or field name
* is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Field getFieldExact(Class cls, String fieldName, boolean breakScope) { public static Field getFieldExact(Class cls, String fieldName, boolean breakScope) {
@ -247,11 +255,12 @@ public static Field getFieldExact(Class cls, String fieldName, boolean breakScop
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/** /**
* Gets a static Field value from a Field object. * <p>Gets a static Field value from a <code>Field</code> object.</p>
* *
* @param field the field to use * @param field the field to use
* @return the field value * @return the field value
* @throws IllegalArgumentException if the field is null or not static * @throws IllegalArgumentException if the field is
* <code>null</code> or not static
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Object getStaticFieldValue(Field field) { public static Object getStaticFieldValue(Field field) {
@ -265,13 +274,15 @@ public static Object getStaticFieldValue(Field field) {
} }
/** /**
* Gets a static Field value from a Field object. * <p>Gets a static Field value from a <code>Field</code> object.</p>
* *
* @param field the field to use * @param field the field to use
* @param breakScope whether to break scope restrictions using the * @param breakScope whether to break scope restrictions using the
* <code>setAccessible</code> method. False will only match public methods. * <code>setAccessible</code> method. <code>False</code> will only
* match public methods.
* @return the field value * @return the field value
* @throws IllegalArgumentException if the field is null or not static * @throws IllegalArgumentException if the field is <code>null</code>
* or not static
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Object getStaticFieldValue(Field field, boolean breakScope) { public static Object getStaticFieldValue(Field field, boolean breakScope) {
@ -285,12 +296,13 @@ public static Object getStaticFieldValue(Field field, boolean breakScope) {
} }
/** /**
* Gets a Field value from a Field object. * <p>Gets a Field value from a <code>Field</code> object.</p>
* *
* @param field the field to use * @param field the field to use
* @param object the object to call on, may be null for static fields * @param object the object to call on, may be <code>null</code>
* for static fields
* @return the field value * @return the field value
* @throws IllegalArgumentException if the field is null * @throws IllegalArgumentException if the field is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Object getFieldValue(Field field, Object object) { public static Object getFieldValue(Field field, Object object) {
@ -298,14 +310,16 @@ public static Object getFieldValue(Field field, Object object) {
} }
/** /**
* Gets a Field value from a Field object. * <p>Gets a Field value from a Field object.</p>
* *
* @param field the field to use * @param field the field to use
* @param object the object to call on, may be null for static fields * @param object the object to call on, may be <code>null</code>
* for static fields
* @param breakScope whether to break scope restrictions using the * @param breakScope whether to break scope restrictions using the
* <code>setAccessible</code> method. False will only match public methods. * <code>setAccessible</code> method. <code>False</code> will only
* match public methods.
* @return the field value * @return the field value
* @throws IllegalArgumentException if the field is null * @throws IllegalArgumentException if the field is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Object getFieldValue(Field field, Object object, boolean breakScope) { public static Object getFieldValue(Field field, Object object, boolean breakScope) {
@ -332,13 +346,14 @@ public static Object getFieldValue(Field field, Object object, boolean breakScop
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/** /**
* Gets a static Field value by name. The field must be public. * <p>Gets a static Field value by name. The field must be public.
* Superclasses will be considered. * Superclasses will be considered.</p>
* *
* @param cls the class to reflect, must not be null * @param cls the class to reflect, must not be <code>null</code>
* @param fieldName the field name to obtain * @param fieldName the field name to obtain
* @return the value of the field * @return the value of the field
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException if the class or field name
* is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Object getStaticFieldValue(Class cls, String fieldName) { public static Object getStaticFieldValue(Class cls, String fieldName) {
@ -346,15 +361,17 @@ public static Object getStaticFieldValue(Class cls, String fieldName) {
} }
/** /**
* Gets a static Field value by name. * <p>Gets a static Field value by name. Only the specified class
* Only the specified class will be considered. * will be considered.</p>
* *
* @param cls the class to reflect, must not be null * @param cls the class to reflect, must not be <code>null</code>
* @param fieldName the field name to obtain * @param fieldName the field name to obtain
* @param breakScope whether to break scope restrictions using the * @param breakScope whether to break scope restrictions using the
* <code>setAccessible</code> method. False will only match public fields. * <code>setAccessible</code> method. <code>False</code> will only
* match public fields.
* @return the Field object * @return the Field object
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException if the class or field name
* is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Object getStaticFieldValue(Class cls, String fieldName, boolean breakScope) { public static Object getStaticFieldValue(Class cls, String fieldName, boolean breakScope) {
@ -379,13 +396,14 @@ public static Object getStaticFieldValue(Class cls, String fieldName, boolean br
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/** /**
* Gets a static Field value by name. The field must be public. * <p>Gets a static Field value by name. The field must be public.
* Only the specified class will be considered. * Only the specified class will be considered.</p>
* *
* @param cls the class to reflect, must not be null * @param cls the class to reflect, must not be <code>null</code>
* @param fieldName the field name to obtain * @param fieldName the field name to obtain
* @return the value of the field * @return the value of the field
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException if the class or field name
* is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Object getStaticFieldValueExact(Class cls, String fieldName) { public static Object getStaticFieldValueExact(Class cls, String fieldName) {
@ -393,15 +411,17 @@ public static Object getStaticFieldValueExact(Class cls, String fieldName) {
} }
/** /**
* Gets a static Field value by name. * <p>Gets a static Field value by name. Only the specified class will
* Only the specified class will be considered. * be considered.</p>
* *
* @param cls the class to reflect, must not be null * @param cls the class to reflect, must not be <code>null</code>
* @param fieldName the field name to obtain * @param fieldName the field name to obtain
* @param breakScope whether to break scope restrictions using the * @param breakScope whether to break scope restrictions using the
* <code>setAccessible</code> method. False will only match public fields. * <code>setAccessible</code> method. <code>False</code> will only
* match public fields.
* @return the Field object * @return the Field object
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException if the class or field name
* is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Object getStaticFieldValueExact(Class cls, String fieldName, boolean breakScope) { public static Object getStaticFieldValueExact(Class cls, String fieldName, boolean breakScope) {
@ -426,13 +446,14 @@ public static Object getStaticFieldValueExact(Class cls, String fieldName, boole
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/** /**
* Gets a Field value by name. The field must be public. * <p>Gets a Field value by name. The field must be public. Superclasses
* Superclasses will be considered. * will be considered.</p>
* *
* @param object the object to reflect, must not be null * @param object the object to reflect, must not be <code>null</code>
* @param fieldName the field name to obtain * @param fieldName the field name to obtain
* @return the value of the field * @return the value of the field
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException if the class or field name
* is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Object getFieldValue(Object object, String fieldName) { public static Object getFieldValue(Object object, String fieldName) {
@ -440,15 +461,17 @@ public static Object getFieldValue(Object object, String fieldName) {
} }
/** /**
* Gets a Field value by name. * <p>Gets a Field value by name. Only the specified class will be
* Only the specified class will be considered. * considered.</p>
* *
* @param object the object to reflect, must not be null * @param object the object to reflect, must not be <code>null</code>
* @param fieldName the field name to obtain * @param fieldName the field name to obtain
* @param breakScope whether to break scope restrictions using the * @param breakScope whether to break scope restrictions using the
* <code>setAccessible</code> method. False will only match public fields. * <code>setAccessible</code> method. <code>False</code> will only
* match public fields.
* @return the Field object * @return the Field object
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException if the class or field name
* is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Object getFieldValue(Object object, String fieldName, boolean breakScope) { public static Object getFieldValue(Object object, String fieldName, boolean breakScope) {
@ -459,13 +482,14 @@ public static Object getFieldValue(Object object, String fieldName, boolean brea
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/** /**
* Gets a Field value by name. The field must be public. * <p>Gets a Field value by name. The field must be public.
* Only the class of the specified object will be considered. * Only the class of the specified object will be considered.</p>
* *
* @param object the object to reflect, must not be null * @param object the object to reflect, must not be <code>null</code>
* @param fieldName the field name to obtain * @param fieldName the field name to obtain
* @return the value of the field * @return the value of the field
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException if the class or field name
* is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Object getFieldValueExact(Object object, String fieldName) { public static Object getFieldValueExact(Object object, String fieldName) {
@ -473,15 +497,17 @@ public static Object getFieldValueExact(Object object, String fieldName) {
} }
/** /**
* Gets a Field value by name. * <p<>Gets a Field value by name. Only the class of the specified
* Only the class of the specified object will be considered. * object will be considered.</p>
* *
* @param object the object to reflect, must not be null * @param object the object to reflect, must not be <code>null</code>
* @param fieldName the field name to obtain * @param fieldName the field name to obtain
* @param breakScope whether to break scope restrictions using the * @param breakScope whether to break scope restrictions using the
* <code>setAccessible</code> method. False will only match public fields. * <code>setAccessible</code> method. <code>False</code> will only
* match public fields.
* @return the Field object * @return the Field object
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException if the class or field name
* is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Object getFieldValueExact(Object object, String fieldName, boolean breakScope) { public static Object getFieldValueExact(Object object, String fieldName, boolean breakScope) {

View File

@ -60,12 +60,12 @@
import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.ArrayUtils;
/** /**
* <code>MethodUtils</code> contains utility methods for working for * <p><code>MethodUtils</code> contains utility methods for working for
* methods by reflection. * methods by reflection.</p>
* <p> *
* The ability is provided to break the scoping restrictions coded by the * <p>The ability is provided to break the scoping restrictions coded by the
* programmer. This can break an implementation if used incorrectly. This * programmer. This can break an implementation if used incorrectly. This
* facility should be used with care. * facility should be used with care.</p>
* *
* @author <a href="mailto:scolebourne@apache.org">Stephen Colebourne</a> * @author <a href="mailto:scolebourne@apache.org">Stephen Colebourne</a>
* @author Based on code from <code>BeanUtils</code> by: Craig R. McClanahan * @author Based on code from <code>BeanUtils</code> by: Craig R. McClanahan
@ -76,20 +76,23 @@
* @author Jan Sorensen * @author Jan Sorensen
* @author Robert Burrell Donkin * @author Robert Burrell Donkin
* @author Gary Gregory * @author Gary Gregory
* @version $Id: MethodUtils.java,v 1.11 2003/01/25 13:01:38 scolebourne Exp $ * @version $Id: MethodUtils.java,v 1.12 2003/07/14 22:29:04 bayard Exp $
*/ */
public class MethodUtils { public class MethodUtils {
public static final boolean debug = false; public static final boolean debug = false;
/** An empty method array */ /**
* An empty method array.
*/
public static final Method[] EMPTY_METHOD_ARRAY = new Method[0]; public static final Method[] EMPTY_METHOD_ARRAY = new Method[0];
/** /**
* MethodUtils instances should NOT be constructed in standard programming. * <p>MethodUtils instances should NOT be constructed in standard programming.</p>
* Instead, the class should be used as <code>MethodUtils.getMethod(cls, name)</code>. *
* <p>Instead, the class should be used as <code>MethodUtils.getMethod(cls, name)</code>.
* This constructor is public to permit tools that require a JavaBean instance * This constructor is public to permit tools that require a JavaBean instance
* to operate. * to operate.</p>
*/ */
public MethodUtils() { public MethodUtils() {
} }
@ -97,13 +100,14 @@ public MethodUtils() {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/** /**
* Gets a Method by name. The method must be public and take no parameters. * <p>Gets a <code>Method</code> by name. The method must be public and take
* Superclasses will be considered. * no parameters. Superclasses will be considered.</p>
* *
* @param cls the class to reflect, must not be null * @param cls the class to reflect, must not be <code>null</code>
* @param methodName the field name to obtain * @param methodName the field name to obtain
* @return the Method object * @return the Method object
* @throws IllegalArgumentException if the class or method name is null * @throws IllegalArgumentException if the class or method name
* is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Method getMethod(Class cls, String methodName) { public static Method getMethod(Class cls, String methodName) {
@ -111,13 +115,14 @@ public static Method getMethod(Class cls, String methodName) {
} }
/** /**
* Gets a Method by name. The method must be public. * <p>Gets a <code>Method</code> by name. The method must be public.
* Superclasses will be considered. * Superclasses will be considered.</p>
* *
* @param cls the class to reflect, must not be null * @param cls the class to reflect, must not be <code>null</code>
* @param methodName the field name to obtain * @param methodName the field name to obtain
* @return the Method object * @return the Method object
* @throws IllegalArgumentException if the class or method name is null * @throws IllegalArgumentException if the class or method name
* is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Method getMethod(Class cls, String methodName, Class paramType) { public static Method getMethod(Class cls, String methodName, Class paramType) {
@ -126,13 +131,14 @@ public static Method getMethod(Class cls, String methodName, Class paramType) {
} }
/** /**
* Gets a Method by name. The method must be public. * <p>Gets a <code>Method</code> by name.</p> The method must be public.
* Superclasses will be considered. * Superclasses will be considered.</p>
* *
* @param cls the class to reflect, must not be null * @param cls the class to reflect, must not be <code>null</code>
* @param methodName the field name to obtain * @param methodName the field name to obtain
* @return the Method object * @return the Method object
* @throws IllegalArgumentException if the class or method name is null * @throws IllegalArgumentException if the class or method name
* is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Method getMethod(Class cls, String methodName, Class[] paramTypes) { public static Method getMethod(Class cls, String methodName, Class[] paramTypes) {
@ -140,15 +146,16 @@ public static Method getMethod(Class cls, String methodName, Class[] paramTypes)
} }
/** /**
* Gets a Method by name. * <p>Gets a <code>Method</code> by name. Superclasses will be considered.</p>
* Superclasses will be considered.
* *
* @param cls the class to reflect, must not be null * @param cls the class to reflect, must not be <code>null</code>
* @param methodName the method name to obtain * @param methodName the method name to obtain
* @param breakScope whether to break scope restrictions using the * @param breakScope whether to break scope restrictions using the
* <code>setAccessible</code> method. False will only match public fields. * <code>setAccessible</code> method. <code>False</code> will only
* match public fields.
* @return the Method object * @return the Method object
* @throws IllegalArgumentException if the class or field name is null * @throws IllegalArgumentException if the class or field name
* is <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Method getMethod(Class cls, String methodName, Class[] paramTypes, boolean breakScope) { public static Method getMethod(Class cls, String methodName, Class[] paramTypes, boolean breakScope) {
@ -215,8 +222,8 @@ public static Method getMethod(Class cls, String methodName, Class[] paramTypes,
/** /**
* <p>Return an accessible method (that is, one that can be invoked via * <p>Return an accessible method (that is, one that can be invoked via
* reflection) that implements the specified Method. If no such method * reflection) that implements the specified <code>Method</code>. If
* can be found, return <code>null</code>.</p> * no such method can be found, return <code>null</code>.</p>
* *
* @param method The method that we wish to call * @param method The method that we wish to call
*/ */
@ -270,12 +277,14 @@ public static Method getMethod(Method method) {
* {@link #invokeMethod(Object object,String methodName,Object [] args)}. * {@link #invokeMethod(Object object,String methodName,Object [] args)}.
* </p> * </p>
* *
* @param objectToInvoke invoke method on this object, must not be null * @param objectToInvoke invoke method on this object, must
* @param methodName get method with this name, must not be null * not be <code>null</code>
* @param arg use this argument, must not be null * @param methodName get method with this name, must not
* * be <code>null</code>
* @param arg use this argument, must not be <code>null</code>
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
* @throws IllegalArgumentException if any parameter is null * @throws IllegalArgumentException if any parameter is
* <code>null</code>
*/ */
public static Object invokeMethod( public static Object invokeMethod(
Object objectToInvoke, Object objectToInvoke,
@ -305,15 +314,18 @@ public static Object invokeMethod(
* would match a <code>boolean</code> primitive.</p> * would match a <code>boolean</code> primitive.</p>
* *
* <p> This is a convenient wrapper for * <p> This is a convenient wrapper for
* {@link #invokeMethod(Object object,String methodName,Object [] args,Class[] parameterTypes)}. * {@link #invokeMethod(Object object, String methodName, Object[] args, Class[] parameterTypes)}.
* </p> * </p>
* *
* @param objectToInvoke invoke method on this object, must not be null * @param objectToInvoke invoke method on this object, must not
* @param methodName get method with this name, must not be null * be <code>null</code>
* @param args use these arguments - treat null as empty array * @param methodName get method with this name, must not
* * be <code>null</code>
* @param args use these arguments - treat <code>null</code>
* as empty array
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
* @throws IllegalArgumentException if the objectToInvoke, methodName or any argument is null * @throws IllegalArgumentException if the objectToInvoke, methodName
* or any argument is <code>null</code>
*/ */
public static Object invokeMethod( public static Object invokeMethod(
Object objectToInvoke, Object objectToInvoke,
@ -354,8 +366,8 @@ public static Object invokeMethod(
* @param object invoke method on this object * @param object invoke method on this object
* @param methodName get method with this name * @param methodName get method with this name
* @param args use these arguments - treat null as empty array * @param args use these arguments - treat null as empty array
* @param parameterTypes match these parameters - treat null as empty array * @param parameterTypes match these parameters - treat
* * <code>null</code> as empty array
* @throws ReflectionException if an error occurs during reflection * @throws ReflectionException if an error occurs during reflection
*/ */
public static Object invokeMethod( public static Object invokeMethod(

View File

@ -55,25 +55,25 @@
import org.apache.commons.lang.exception.NestableRuntimeException; import org.apache.commons.lang.exception.NestableRuntimeException;
/** /**
* Exception thrown when the Reflection process fails. The original * <p>Exception thrown when the Reflection process fails. The original
* error is wrapped within this one. * error is wrapped within this one.</p>
* *
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a> * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: ReflectionException.java,v 1.1 2002/10/24 23:12:54 scolebourne Exp $ * @version $Id: ReflectionException.java,v 1.2 2003/07/14 22:29:04 bayard Exp $
*/ */
public class ReflectionException extends NestableRuntimeException { public class ReflectionException extends NestableRuntimeException {
/** /**
* Constructs a new <code>ReflectionException</code> without specified * <p>Constructs a new <code>ReflectionException</code> without specified
* detail message. * detail message.</p>
*/ */
public ReflectionException() { public ReflectionException() {
super(); super();
} }
/** /**
* Constructs a new <code>ReflectionException</code> with specified * <p>Constructs a new <code>ReflectionException</code> with specified
* detail message. * detail message.</p>
* *
* @param msg The error message. * @param msg The error message.
*/ */
@ -82,23 +82,23 @@ public ReflectionException(String msg) {
} }
/** /**
* Constructs a new <code>ReflectionException</code> with specified * <p>Constructs a new <code>ReflectionException</code> with specified
* nested <code>Throwable</code>. * nested <code>Throwable</code>.</p>
* *
* @param cause The exception or error that caused this exception * @param cause The exception or error that caused this exception
* to be thrown. * to be thrown.
*/ */
public ReflectionException(Throwable cause) { public ReflectionException(Throwable cause) {
super(cause); super(cause);
} }
/** /**
* Constructs a new <code>ReflectionException</code> with specified * <p>Constructs a new <code>ReflectionException</code> with specified
* detail message and nested <code>Throwable</code>. * detail message and nested <code>Throwable</code>.</p>
* *
* @param msg The error message. * @param msg The error message.
* @param cause The exception or error that caused this exception * @param cause The exception or error that caused this exception
* to be thrown. * to be thrown.
*/ */
public ReflectionException(String msg, Throwable cause) { public ReflectionException(String msg, Throwable cause) {
super(msg, cause); super(msg, cause);