com.googlecode.mjorm.ReflectionUtil Maven / Gradle / Ivy
package com.googlecode.mjorm;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Utilities for reflection related things.
*/
public final class ReflectionUtil {
/**
* Does nothing.
*/
private ReflectionUtil() { }
static { new ReflectionUtil(); }
/**
* Creates an instance of the given class.
* @param the type
* @param clazz the class
* @param args the arguments to pass to the constructor
* @return an instance of the object
* @throws InstantiationException on error
* @throws IllegalAccessException on error
* @throws InvocationTargetException on error
*/
@SuppressWarnings("unchecked")
public static T instantiate(Class clazz, Object... args)
throws InstantiationException,
IllegalAccessException,
InvocationTargetException {
if (args.length==0) {
return clazz.newInstance();
}
Class>[] types = new Class>[args.length];
for (int i=0; i c : clazz.getConstructors()) {
Class>[] paramTypes = c.getParameterTypes();
if (paramTypes.length!=args.length) {
continue;
}
boolean foundCtr = true;
for (int i=0; i clazz) {
try {
return Introspector.getBeanInfo(clazz);
} catch(Exception e) {
throw new IllegalStateException(e);
}
}
/**
* Returns the PropertyDescriptor for the given property.
* @param clazz the class
* @param name the getter name
* @return the PropertyDescriptor
*/
public static PropertyDescriptor findPropertyDescriptor(Class> clazz, String name) {
BeanInfo info = getBeanInfo(clazz);
for (PropertyDescriptor desc : info.getPropertyDescriptors()) {
if (desc.getName().equalsIgnoreCase(name)) {
return desc;
}
}
return null;
}
/**
* Finds the specified getter method on the specified class.
* @param clazz the class
* @param name the getter name
* @return the method
*/
public static Method findGetter(Class> clazz, String name) {
PropertyDescriptor pd = findPropertyDescriptor(clazz, name);
return (pd!=null) ? pd.getReadMethod() : null;
}
/**
* Finds the specified setter method on the specified class.
* @param clazz the class
* @param name the setter name
* @return the method
*/
public static Method findSetter(Class> clazz, String name) {
PropertyDescriptor pd = findPropertyDescriptor(clazz, name);
return (pd!=null) ? pd.getWriteMethod() : null;
}
}