要反射的Java类和方法
入参是三个字符串
出参是一个map
package com.tp.inter.pagedefine.cache;
public class GridFieldExcecutor {
public Map<String, Object> getCal(String applyCode, String systemId,
String moduleName) {
return this.cacheManager.get(KEY,
new String[] { applyCode, systemId, moduleName });
}
}
核心代码 我这里的
这里的obj是对象的值
package com.tp.inter.pagedefine.cache;
public class Test {
/**
* 根据页面查询页面合计的字段值
* @param request 页面编号和系统编号
* @param mouldName 模块module
* @return Map<String, Object>
* @author xiaowenbo
*/
@SuppressWarnings("unchecked")
private Map<String, Object> getCalMap(DataSet request, String mouldName) {
Map<String, Object> str = new HashMap<String, Object>();
String dsName = request.getStringParameter("dsName");
String dsSystemId = request.getStringParameter("dsSystemId");
if (StringUtils.isNotEmpty(dsSystemId) &&
StringUtils.isNotEmpty(dsName)) {
try {
String className = "com.tp.inter.pagedefine.cache.GridFieldExcecutor"; //要反射的java类
String methodName = "getCal"; //要反射的方法名
Object obj = BeanFactory.getBean(className);
Class<?> clazz = Class.forName(className);
// Java 反射机制 - 调用某个类的方法1
Method method = clazz.getMethod(methodName, String.class,
String.class, String.class);
str = (Map<String, Object>) method.invoke(obj, dsName,
dsSystemId, mouldName);
} catch (Exception e) {
e.printStackTrace();
}
}
return str;
}
}