Invoker

Although Java has very powerful runtime type information (RTTI) I've found it awkward to use. A particular case in point was trying to set parameters on a JDBC Datasource Object from a configuration file. The Invoker static class, below can be used to easily set properties on any class, given the object, property name and desired value.

package uk.org.codex.utils.rtti;

import java.lang.reflect.Method;

/*******************************************************************************
* Static class designed to make setting properties easier from properties files
* or XML. It allows strings to be passed as parameter values for all basic
* types.
* @author Ian A. Newby
******************************************************************************/
public class Invoker {

/** Creates a new instance of Invoker */
private Invoker() {
}


/***************************************************************************
* Creates an object of an appropriate type to use in an invoke method.
* @param val The value to convert
* @param type The type required
* @return
**************************************************************************/
protected static Object convertArg(String val, Class type) {

if (val == null)
return null;
String v = val.trim();
if (String.class.isAssignableFrom(type)) {
return val;
} else if (Integer.TYPE.isAssignableFrom(type)) {
return new Integer(v);
} else if (Long.TYPE.isAssignableFrom(type)) {
return new Long(v);
} else if (Boolean.TYPE.isAssignableFrom(type)) {
if ("true".equalsIgnoreCase(v)) {
return Boolean.TRUE;
} else if ("false".equalsIgnoreCase(v)) {
return Boolean.FALSE;
}
}
return null;
}

/***************************************************************************
* This static method allows a property to be set. It adds 'set' to the
* paramName passed in and invokes a method of that name.
* @param object The object on which to set the param.
* @param paramName The name of the parameter to set
* @param value The value to set the parameter to.
* @throws java.lang.Exception
**************************************************************************/
public static void setProperty(Object object, String paramName, String value) throws Exception {

Class cl = object.getClass();
Class[] parameterTypes = new Class[] {};
String methodName = "set" + paramName;
Method method = null;
Class paramType = null;
Object arg = null;
try {
Method[] methods = cl.getMethods();
for (int i=0; i< methods.length; i++) {
if (methods[i].getName().equals(methodName)) {
Class[] params = methods[i].getParameterTypes();
if (params.length == 1) {
method = methods[i];
paramType = params[0];
}
}
}
if (method != null) {
try {
arg = convertArg(value, paramType);
} catch (Throwable t) {
throw new Exception("Conversion to type ["+paramType + "] failed. Reason: "+t);
}
if (arg == null) {
throw new Exception("Conversion to type ["+paramType +"] failed.");
}
try {
method.invoke(object, new Object[] { arg });
} catch (Exception ex) {
try {
Exception e = new Exception(ex.getCause());
throw e;
} catch (Exception e) {
throw ex;
}
}
} else {
throw new Exception("Method " + methodName + " not found");
}
} catch (Exception e) {
throw new Exception(e);
}
}
}
 

Last updated: 12 Mar 2008 20:15:56