import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class Reflection {
private String className;
/**
* 得到某个对象的公共属性
*
* @param owner,
* fieldName
* @return 该属性对象
* @throws Exception
*
*/
public Object getProperty(Object owner, String fieldName) throws Exception {
Class ownerClass = owner.getClass();
Field field = ownerClass.getField(fieldName);
Object property = field.get(owner);
return property;
}
/**
* 得到某类的静态公共属性
*
* @param className
* 类名
* @param fieldName
* 属性名
* @return 该属性对象
* @throws Exception
*/
public Object getStaticProperty(String className, String fieldName)
throws Exception {
Class ownerClass = Class.forName(className);
Field field = ownerClass.getField(fieldName);
Object property = field.get(ownerClass);
return property;
}
/**
* 执行某对象方法
*
* @param owner
* 对象
* @param methodName
* 方法名
* @param args
* 参数
* @return 方法返回值
* @throws Exception
*/
public Object invokeMethod(Object owner, String methodName, Object[] args)
throws Exception {
Class ownerClass = owner.getClass();
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
return method.invoke(owner, args);
}
/**
* 执行某类的静态方法
*
* @param className
* 类名
* @param methodName
* 方法名
* @param args
* 参数数组
* @return 执行方法返回的结果
* @throws Exception
*/
public Object invokeStaticMethod(String className, String methodName,
Object[] args) throws Exception {
Class ownerClass = Class.forName(className);
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName, argsClass);
return method.invoke(null, args);
}
/**
* 新建实例
*
* @param className
* 类名
* @param args
* 构造函数的参数
* @return 新建的实例
* @throws Exception
*/
public Object newInstance(String className, Object[] args) throws Exception {
Class newoneClass = Class.forName(className);
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Constructor cons = newoneClass.getConstructor(argsClass);
return cons.newInstance(args);
}
/**
* 是不是某个类的实例
*
* @param obj
* 实例
* @param cls
* 类
* @return 如果 obj 是此类的实例,则返回 true
*/
public boolean isInstance(Object obj, Class cls) {
return cls.isInstance(obj);
}
/**
* 得到数组中的某个元素
*
* @param array
* 数组
* @param index
* 索引
* @return 返回指定数组对象中索引组件的值
*/
public Object getByArray(Object array, int index) {
return Array.get(array, index);
}
/**
* 打印该类所有的方法
*
* @param 一个类
*
*/
public static void printMethods(Class cl) {
Method[] methods = cl.getDeclaredMethods();
String[] method = new String[methods.length];
for (Method m : methods) {
Class retType = m.getReturnType();
String name = m.getName();
// print modifiers, return type and method name
System.out.print(" " + Modifier.toString(m.getModifiers()));
System.out.print(" " + retType.getName() + " " + name + "(");
// print parameter types
Class[] paramTypes = m.getParameterTypes();
for (int j = 0; j < paramTypes.length; j++) {
if (j > 0)
System.out.print(", ");
System.out.print(paramTypes[j].getName());
}
System.out.println(");");
}
}
/**
* 获取该类的所有属性
*
* @param 一个类
*/
public String[] printFields(Class cl) {
Field[] fields = cl.getDeclaredFields();
String[] fied = new String[fields.length];
int i = 0;
for (Field f : fields) {
Class type = f.getType();
String name = f.getName();
String fieldName = "";
fieldName = fieldName + name + " " + type.getName();
fied[i++] = fieldName;
// System.out.print(" " + Modifier.toString(f.getModifiers()));
// System.out.println(" " + type.getName() + " " + name + ";");
}
return fied;
}
/**
* 打印类的所有构造方法
*
* @param 一个类
*/
public void printConstructors(Class cl) {
Constructor[] constructors = cl.getDeclaredConstructors();
for (Constructor c : constructors) {
String name = c.getName();
System.out.print(" " + Modifier.toString(c.getModifiers()));
System.out.print(" " + name + "(");
// print parameter types
Class[] paramTypes = c.getParameterTypes();
for (int j = 0; j < paramTypes.length; j++) {
if (j > 0)
System.out.print(", ");
System.out.print(paramTypes[j].getName());
}
System.out.println(");");
}
}
public Class getClassForString() throws ClassNotFoundException {
Class cl = Class.forName(className);
return cl;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public static void main(String[] args) {
String className = "java.util.Date";
Reflection re = new Reflection();
re.setClassName(className);
try {
String[] test = re.printFields(re.getClassForString().getClass());
for(String s : test)
{
System.out.println(s);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
阅读(2345) | 评论(1) | 转发(0) |