Utility class to handle reflection on java objects.
The class contains static methods to call reflection
methods, catch any exceptions, converting them
to BuildExceptions.
| Method from org.apache.tools.ant.util.ReflectUtil Detail: |
public static Object getField(Object obj,
String fieldName) throws BuildException {
try {
Field field = obj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(obj);
} catch (Exception t) {
throwBuildException(t);
return null; // NotReached
}
}
Get the value of a field in an object. |
public static Object invoke(Object obj,
String methodName) {
try {
Method method;
method = obj.getClass().getMethod(
methodName, (Class[]) null);
return method.invoke(obj, (Object[]) null);
} catch (Exception t) {
throwBuildException(t);
return null; // NotReached
}
}
Call a method on the object with no parameters. |
public static Object invoke(Object obj,
String methodName,
Class argType,
Object arg) {
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
}
}
Call a method on the object with one argument. |
public static Object invoke(Object obj,
String methodName,
Class argType1,
Object arg1,
Class argType2,
Object arg2) {
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
}
}
Call a method on the object with two argument. |
public static Object invokeStatic(Object obj,
String methodName) {
try {
Method method;
method = ((Class) obj).getMethod(
methodName, (Class[]) null);
return method.invoke(obj, (Object[]) null);
} catch (Exception t) {
throwBuildException(t);
return null; // NotReached
}
}
Call a method on the object with no parameters.
Note: Unlike the invoke method above, this
calls class or static methods, not instance methods. |
public static Object newInstance(Class ofClass,
Class[] argTypes,
Object[] args) {
try {
Constructor con = ofClass.getConstructor(argTypes);
return con.newInstance(args);
} catch (Exception t) {
throwBuildException(t);
return null; // NotReached
}
}
Create an instance of a class using the constructor matching
the given arguments. |
public static boolean respondsTo(Object o,
String methodName) throws BuildException {
try {
Method[] methods = o.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals(methodName)) {
return true;
}
}
return false;
} catch (Exception t) {
throw toBuildException(t);
}
}
A method to test if an object responds to a given
message (method call) |
public static void throwBuildException(Exception t) throws BuildException {
throw toBuildException(t);
}
A method to convert an invocationTargetException to
a buildexception and throw it. |
public static BuildException toBuildException(Exception t) {
if (t instanceof InvocationTargetException) {
Throwable t2 = ((InvocationTargetException) t)
.getTargetException();
if (t2 instanceof BuildException) {
return (BuildException) t2;
}
return new BuildException(t2);
} else {
return new BuildException(t);
}
}
A method to convert an invocationTargetException to
a buildexception. |