Java_通过反射调用类中的方法
先上一个基本的封装:
/** * 获取classType * * @param type * @param provinceCode * @param cityCode * @return * @throws Exception */ public Class<?> getClassType(String type, String provinceCode, String cityCode, String suffix) throws Exception { Class<?> classType = Class.forName("com.lkb.sb." + type + "." + provinceCode + "." + cityCode + suffix); return classType; } /** * 获取base * * @param className * @return * @throws Exception */ public Class<?> getClassType(String className) throws Exception { Class<?> classType = Class.forName(className); return classType; } /** * @param classType * @return * @throws Exception */ public Object getClassInstance(Class<?> classType) throws Exception { return getClassInstance(classType, null, null); } /** * 获取对象client * * @param classType * @param classes * @param objects * @return * @throws Exception */ public Object getClassInstance(Class<?> classType, Class[] classes, Object[] objects) throws Exception { Object client = classType.getConstructor(classes).newInstance(objects); return client; } /** * 执行不带参数的方法 * * @param classType * @param instance * @param method * @return * @throws Exception */ public Object implementMethod(Class<?> classType, Object instance, String method) throws Exception { return implementMethod(classType, instance, method, null, null); } /** * 执行方法 * * @param classType * @param instance * @param method * @param paramTypes * @param params * @return * @throws Exception */ public Object implementMethod(Class<?> classType, Object instance, String method, Class[] paramTypes, Object[] params) throws Exception { Method getStrMethod; Object result; if (paramTypes == null) { getStrMethod = classType.getMethod(method); result = getStrMethod.invoke(instance); } else { getStrMethod = classType.getMethod(method, paramTypes); result = getStrMethod.invoke(instance, params); } return result; }
进一步封装,通过实现接口的形式调用,这样就省略了每次反射方法时还要写上参数类型数组:
package com.lkb.autoCode.util; import com.lkb.autoCode.constant.AutoCodeConstant; import java.lang.reflect.Method; /** * DefultClassLoader 默认的类加载器 * * @author Lilin * @date 2016/5/24 */ public class DefultClassLoader<T> { private Class<?> classType = null; private T reflectClass = null; /** * @param fullClassName 全包路径 * @throws Exception */ public DefultClassLoader(String fullClassName) throws Exception { classType = getClassType(fullClassName); } /** * @param type 模板类型 * @param provinceCode 省Id * @param cityCode 市Id * @param className 类名称 * @throws Exception */ public DefultClassLoader(String type, String provinceCode, String cityCode, String className) throws Exception { classType = getClassType(type, provinceCode, cityCode, className); } /** * 获取classType * * @param type * @param provinceCode * @param cityCode * @return * @throws Exception */ public Class<?> getClassType(String type, String provinceCode, String cityCode, String suffix) throws Exception { Class<?> classType = Class.forName(AutoCodeConstant.BASE_SB_PACKAGE + "." + type + "." + provinceCode + "." + cityCode + suffix); return classType; } /** * 获取base * * @param fullClassName * @return * @throws Exception */ public Class<?> getClassType(String fullClassName) throws Exception { Class<?> classType = Class.forName(fullClassName); return classType; } /** * 获取对象client * @return * @throws Exception */ public T getClassInstance() throws Exception { return getClassInstance(null, null); } /** * 获取对象client * * @param classes * @param objects * @return * @throws Exception */ public T getClassInstance(Class[] classes, Object[] objects) throws Exception { T client = (T) classType.getConstructor(classes).newInstance(objects); return client; } /** * 执行不带参数的方法(忽略) * * @param classType * @param instance * @param method * @return * @throws Exception */ public Object implementMethod(Class<?> classType, Object instance, String method) throws Exception { return implementMethod(classType, instance, method, null, null); } /** * 执行方法(忽略) * * @param classType * @param instance * @param method * @param paramTypes * @param params * @return * @throws Exception */ public Object implementMethod(Class<?> classType, Object instance, String method, Class[] paramTypes, Object[] params) throws Exception { Method getStrMethod; Object result; if (paramTypes == null) { getStrMethod = classType.getMethod(method); result = getStrMethod.invoke(instance); } else { getStrMethod = classType.getMethod(method, paramTypes); result = getStrMethod.invoke(instance, params); } return result; } }
路漫漫其修远兮!!!
缺点:构造器的创建还得传递参数类型数组