public class ReflectUtil {
public static Class reflectClass(String className){
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
public static Object getClassInstance(Class clazz,
HttpServletRequest request, HttpServletResponse response){
try {
Object ins = clazz.newInstance();
//invoke setHttpServlet of BaseAction to initialize instance of XXXAction
invokeMethodWithArgs(clazz.getSuperclass(), ins, "setHttpServlet",
new Class[]{HttpServletRequest.class, HttpServletResponse.class},
request, response);
setAttributesForInstance(clazz,ins,request,null);
return ins;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static void setAttributesForInstance(Class clazz, Object ins,
HttpServletRequest request, String prefix) throws Exception {
if(prefix!=null&&prefix.indexOf(".")!=-1) return; //不给二级属性赋值
Field[] fields = clazz.getDeclaredFields();
for(Field f : fields){
f.setAccessible(true);
String typeClass = f.getType().getSimpleName();
String fname = f.getName();
if(prefix!=null) fname = prefix+"."+fname;
String paramValue = request.getParameter(fname);
try {
if("int".equalsIgnoreCase(typeClass)||"integer".equalsIgnoreCase(typeClass)){
if(paramValue != null) f.set(ins, Integer.parseInt(paramValue));
}else if("float".equalsIgnoreCase(typeClass)){
if(paramValue != null) f.set(ins, Float.parseFloat(paramValue));
}else if("double".equalsIgnoreCase(typeClass)){
if(paramValue != null) f.set(ins, Double.parseDouble(paramValue));
}else if("long".equalsIgnoreCase(typeClass)){
if(paramValue != null) f.set(ins, Long.parseLong(paramValue));
}else if("short".equalsIgnoreCase(typeClass)){
if(paramValue != null) f.set(ins, Short.parseShort(paramValue));
}else if("boolean".equalsIgnoreCase(typeClass)){
if(paramValue != null) f.set(ins, Boolean.parseBoolean(paramValue));
}else if("object".equalsIgnoreCase(typeClass)||"string".equalsIgnoreCase(typeClass)){
if(paramValue != null) f.set(ins, paramValue);
}else{
Object fieldIns = f.getType().getConstructor().newInstance();
f.set(ins, fieldIns);
setAttributesForInstance(f.getType(), fieldIns, request, fname);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
public static Object invokeMethodNoArgs(Class clazz, Object classInstance, String methodName){
Object result = null;
try {
Method method = clazz.getMethod(methodName);
method.setAccessible(true);
result = method.invoke(classInstance);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static Object invokeMethodWithArgs(Class clazz, Object classInstance, String methodName, Class[] argType, Object...args){
Object result = null;
try {
Method method = clazz.getDeclaredMethod(methodName, argType);
Class returnType = method.getReturnType();
method.setAccessible(true);
result = method.invoke(classInstance, args);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
分享到:
相关推荐
免费共享,很详细的介绍了反射机制的原理,适合追根究底的java学习者
java教程ppt,讲述了java reflect.,对大家应该很有帮助
java反射的一些总结知识点,这是java中反射比较好入门的资料
java反射技术,DaoFactory,通过反射获取接口对象,然后使用接口中的方法。
JAVA REFLECT (java 反射) 取得类的继承结构 - 类所在的包; |- public Package getPackage() - 类的声明方式; |-取得全类名:public String getName() |-只取类名: public String getSimpleName() - 类所继承...
java 映射 说明 可以帮助大家理解java reflect
http://blog.csdn.net/two_water/article/details/50675688 博客中的Demo
我看了看Java的反射机制,挺好的,但是JDK翻译的不怎么清楚,我自己写了两个Example,希望对大家有帮助。
9.3 Java反射reflect
java反射(reflect)
scrcpy投屏 AssertionError: java.lang.reflect.InvocationTargetException 安装这个版本的scrcpy即可。
java.lang.reflect.Proxy 学习资料 讲解 例子 源码 java.lang.reflect.Proxy 学习资料 讲解 例子 源码 java.lang.reflect.Proxy 学习资料 讲解 例子 源码
JAVA反射的基础,基础,但也有价值,可以看看了解, 通过反射可以是程序的扩展性增强!
解决axis2-CodegenWizardPlugin的BUG,java.lang.reflect.InvocationTargetException 带有相关jar 包及相关文件
Java反射的使用 这个小例子简单介绍了Java使用反射的各种方法,帮助大家快速掌握Java反射的基本使用方法。例子程序是用Android Studio编写的,如果你使用Eclipse,新建一个工程,拷贝src下面的几个类过去就可以了....
NULL 博文链接:https://inter12.iteye.com/blog/1288117
本文章的原代码. 博文链接:https://yidwo.iteye.com/blog/42995