FileDocCategorySizeDatePackage
ReflectUtil.javaAPI DocApache Ant 1.704921Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.util

ReflectUtil

public class ReflectUtil extends Object
Utility class to handle reflection on java objects. The class contains static methods to call reflection methods, catch any exceptions, converting them to BuildExceptions.

Fields Summary
Constructors Summary
private ReflectUtil()
private constructor

    
Methods Summary
public static java.lang.ObjectgetField(java.lang.Object obj, java.lang.String fieldName)
Get the value of a field in an object.

param
obj the object to look at.
param
fieldName the name of the field in the object.
return
the value of the field.
throws
BuildException if there is an error.

        try {
            Field field = obj.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            return field.get(obj);
        } catch (Exception t) {
            throwBuildException(t);
            return null; // NotReached
        }
    
public static java.lang.Objectinvoke(java.lang.Object obj, java.lang.String methodName)
Call a method on the object with no parameters.

param
obj the object to invoke the method on.
param
methodName the name of the method to call
return
the object returned by the method

        try {
            Method method;
            method = obj.getClass().getMethod(
                methodName, (Class[]) null);
            return method.invoke(obj, (Object[]) null);
        } catch (Exception t) {
            throwBuildException(t);
            return null; // NotReached
        }
    
public static java.lang.Objectinvoke(java.lang.Object obj, java.lang.String methodName, java.lang.Class argType, java.lang.Object arg)
Call a method on the object with one argument.

param
obj the object to invoke the method on.
param
methodName the name of the method to call
param
argType the type of argument.
param
arg the value of the argument.
return
the object returned by the method

        try {
            Method method;
            method = obj.getClass().getMethod(
                methodName, new Class[] {argType});
            return method.invoke(obj, new Object[] {arg});
        } catch (Exception t) {
            throwBuildException(t);
            return null; // NotReached
        }
    
public static java.lang.Objectinvoke(java.lang.Object obj, java.lang.String methodName, java.lang.Class argType1, java.lang.Object arg1, java.lang.Class argType2, java.lang.Object arg2)
Call a method on the object with two argument.

param
obj the object to invoke the method on.
param
methodName the name of the method to call
param
argType1 the type of the first argument.
param
arg1 the value of the first argument.
param
argType2 the type of the second argument.
param
arg2 the value of the second argument.
return
the object returned by the method

        try {
            Method method;
            method = obj.getClass().getMethod(
                methodName, new Class[] {argType1, argType2});
            return method.invoke(obj, new Object[] {arg1, arg2});
        } catch (Exception t) {
            throwBuildException(t);
            return null; // NotReached
        }
    
public static voidthrowBuildException(java.lang.Exception t)
A method to convert an invocationTargetException to a buildexception and throw it.

param
t the invocation target exception.
throws
BuildException the converted exception.

        if (t instanceof InvocationTargetException) {
            Throwable t2 = ((InvocationTargetException) t)
                .getTargetException();
            if (t2 instanceof BuildException) {
                throw (BuildException) t2;
            }
            throw new BuildException(t2);
        } else {
            throw new BuildException(t);
        }