日期:2014-05-20 浏览次数:20830 次
import java.io.FileInputStream; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Properties; public class ReflectFactory { private static final String FILENAME = "reflect.ini"; private Properties prop; private Map<String, String> objTool = new HashMap<String, String>(); /** * 初始化 读取配置文件 */ public void init() { prop = new Properties(); try { prop.load(new FileInputStream(FILENAME)); Enumeration<?> keys = prop.keys(); while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); String value = prop.getProperty(key); objTool.put(key, value); } }catch (Exception e) { System.out.println("读配置文件异常"); } } /** * 根据类名(完整包名)创建实例 * @param className * @return 类的实例 */ private Object createObject(String className) { try { System.out.println(className); Class<?> clazz = Class.forName(className); return clazz.newInstance(); }catch (Exception e) { System.out.println("创建类的实例异常"); } return null; } /** * 根据 类名、方法名 调用相应的方法 * @param cName * @param mtdName * @param params */ public void invokeMethod(String cName, String mtdName, Object[] params) { String className = objTool.get(cName); Object target = createObject(className); Class<?> targetClass = target.getClass(); int length = params.length; Class<?>[] paramTypes = new Class<?>[length]; for (int i = 0; i < length; i++) { paramTypes[i] = params[i].getClass(); } try { Method method = targetClass.getMethod(mtdName, paramTypes); method.invoke(target, params); }catch (Exception e) { System.out.println("执行异常"); } } /** * 反射显示 类 的内容(属性、方法) * @param cName 类名 */ public void showClass(String cName) { StringBuffer sbuf = new StringBuffer(1024); String className = objTool.get(cName); Object target = createObject(className); Class<?> targetClass = target.getClass(); sbuf.append("类名:"); sbuf.append(className); sbuf.append("\n\n"); sbuf.append(Modifier.toString(targetClass.getModifiers())); sbuf.append(" class "); sbuf.append(cName); sbuf.append("\n{\n"); writeFiels(sbuf, targetClass); writeConstructor(sbuf, targetClass); writeMethods(sbuf, targetClass); sbuf.append("}"); System.out.println(sbuf.toString()); } /** * 获取类的 方法 信息 * @param sbuf 存储信息容器 * @param targetClass 目标类 */ private void writeMethods(StringBuffer sbuf, Class<?> targetClass) { Method[] methods = targetClass.getDeclaredMethods(); for (Method m : methods) { sbuf.append("\t"); sbuf.append(Modifier.toString(m.getModifiers())); sbuf.append(" "); String returnType = m.getReturnType().getName(); sbuf.append(returnType.substring(returnType.lastIndexOf('.') + 1)); sbuf.append(" "); sbuf.append(m.getName()); sbuf.append("("); Class<?>[] paramTypes = m.getParameterTypes(); for (int i = 0; i < paramTypes.length; i++) { if (i > 0) sbuf.append(", "); String paramName = paramTypes[i].getName(); sbuf .append(paramName.substring(paramName.lastIndexOf('.') + 1)); } sbuf.append(");\n"); } } /** * 获取类的 构造函数 信息 * @param sbuf 存储信息容器 * @param targetClass 目标类 */ private void writeConstructor(StringBuffer sbuf, Class<?> targetClass) { Constructor[] constructors = targetClass.getDeclaredConstructors(); for (Constructor c : constructors) { sbuf.append("\t"); sbuf.append(Modifier.toString(c.getModifiers())); sbuf.append(" "); String name = c.getName(); sbuf.append(name.substring(name.lastIndexOf('.') + 1)); sbuf.append("("); Class<?>[] paramTypes = c.getParameterTypes(); for (int i = 0; i < paramTypes.length; i++) { if (i > 0) sbuf.append(", "); sbuf.append(paramTypes[i]); } sbuf.append(");\n"); } sbuf.append("\n"); } /** * 获取类的 属性 信息 * @param sbuf 存储信息容器 * @param targetClass 目标类 */ private void writeFiels(StringBuffer sbuf, Class<?> targetClass) { Field[] fields = targetClass.getDeclaredFields(); for (Field f : fields) { sbuf.append("\t"); sbuf.append(Modifier.toString(f.getModifiers())); sbuf.append(" "); String fieldType = f.getType().getName(); sbuf.append(fieldType.substring(fieldType.lastIndexOf('.') + 1)); sbuf.append(" "); sbuf.append(f.getName()); sbuf.append(";\n"); } sbuf.append("\n"); } /** * @param args */ public static void main(String[] args) { ReflectFactory rf = new ReflectFactory(); rf.init(); Object[] params = {}; rf.invokeMethod("Person", "main", params); } }