java利用反射动态执行类方法
Person类public classPerson {
String name;
String day;publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicString getDay() {returnday;
}public voidsetDay(String day) {this.day =day;
}public void printlinfo() throwsRuntimeException, IOException{
System.out.println(name);
System.out.println(day);
String url="http://www.163.com";
openURL(url);
}public void openURL (String url)throwsRuntimeException,IOException{
String urlx=null;
urlx="rundll32 url.dll,FileProtocolHandler "+url;
Process p=Runtime.getRuntime().exec(""+urlx);
}
}
利用反映动态执行类的方法public static void print(byte[] b1,String tname,String tday) throwsInstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
MyClassLoade myClassLoader=newMyClassLoade();
Class c= myClassLoader.defineClass("Person", b1);
System.out.println(c.getName());
Object object=c.newInstance();
Field[] fields=c.getDeclaredFields();
System.out.println(fields.length);
String args="java.lang.String";
Method method1= c.getDeclaredMethod("setName", args.getClass()) ;
method1.invoke(object,newObject[] {tname});
Method method2= c.getDeclaredMethod("setDay", args.getClass()) ;
method2.invoke(object,newObject[] {tday});
Method method= c.getDeclaredMethod("printlinfo", null) ;
method.invoke(object);
}
参考:利用反射实现JavaBean的自动赋值importjava.lang.reflect.Method;import java.util.*;importjavax.servlet.http.HttpServletRequest;importcom.sns.exception.ApplicationException;public final classParameterUtil {public static voidsetFormBean(HttpServletRequest request, Object bean) {
Class c=bean.getClass();
Method[] ms=c.getMethods();for(int i=0; i
String name=ms.getName();if(name.startsWith("set")) {
Class[] cc=ms.getParameterTypes();if(cc.length==1) {
String type= cc[0].getName(); //parameter type
try{//get property name:
String prop = Character.toLowerCase(name.charAt(3)) + name.substring(4);//get parameter value:
String param =getString(request, prop);if(param!=null && !param.equals("")) {//ms.setAccessible(true);
if(type.equals("java.lang.String")) {
ms.invoke(bean,newObject[] {param});
}else if(type.equals("int") || type.equals("java.lang.Integer")) {
ms.invoke(bean,new Object[] {newInteger(param)});
}else if(type.equals("long") || type.equals("java.lang.Long")) {
ms.invoke(bean,new Object[] {newLong(param)});
}else if(type.equals("boolean") || type.equals("java.lang.Boolean")) {
ms.invoke(bean,newObject[] { Boolean.valueOf(param) });
}else if(type.equals("java.util.Date")) {
Date date=DateUtil.parseDateTime(param);if(date!=null)
ms.invoke(bean,newObject[] {date});
}
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
}
}
每当发现JavaBean中的setXxx()方法时,便自动寻找表单的对应字段xxx,如果找到,就利用反射调用此方法,将对应的字段值赋给JavaBean。
由于表单传递的变量名和值全部是字符串,因此需要做某些转换。目前,该程序能处理的数据类型包括:boolean,Boolean,int,Integer,long,Long,Date,不被支持的数据类型被自动忽略。你也可以很方便地添加新的类型。
请 注意,只有public的set方法能够被调用。如果你希望private或protected的set方法也能被调用,请将红色标识的 getMethods()改为getDeclaredMethods(),以便获得包括private和protected在内的所有方法,并将 ms.setAccessible(true);的注释去掉,以便能正确调用private和protected方法。
反射是Java语言非常强大的功能,但是由于反射会破坏对象封装,并且反射调用的速度较慢,因此,只能在必要的工具类中使用。
#Java