常用反射方法:
package reflection; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; public class testReflection { public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { String str="reflection.T"; //获取类 Class c=Class.forName(str); //获取类路径 URL z=c.getResource(""); URL s=c.getClassLoader().getResource(""); //得到class在类中的地址 ClassLoader d=c.getClassLoader(); //得到class,c所实现的接口Class[] f=c.getInterfaces()[0]; Class[] f=c.getInterfaces(); System.out.println(z); System.out.println(s); System.out.println(d); System.out.println(f); //getClassLoader().getResource(fileName) //用获取到的类new一个新的对象 Object o=c.newInstance(); //接收该对象中所有的方法 Method[] method = c.getMethods(); System.out.println("此元素上的注释:"+c.getDeclaredAnnotations()); for(Method m:method) { if(m.getName().equals("m")) { //方法选择第一个参数传递对象,后面的参数传递方法参数 m.invoke(o); System.out.println("哈希码:"+m.hashCode()); } if(m.getName().equals("b")) { m.invoke(o,1,2); //得到参数类型 for(Class paramtype : m.getParameterTypes()) { System.out.println("参数类型:"+paramtype.getName()); } } } } } /*
//instanceof方法测试args返回的是否是一个class实例
if(args[0] instanceof String)
* @param args * */ class T { public T(){ System.out.println("构造函数被调用了"); } public void m() { System.out.println("m函数被调用了"); } public void b(int i,int j) { System.out.println("b函数的结果:"+i+j); } }
一,利用反射自动为javabeen赋值
例 been:
package proxy; public class Notice{ String type; String name; String data; String address; String vill; String upFile; String num; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getData() { return data; } public void setData(String data) { this.data = data; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getVill() { return vill; } public void setVill(String vill) { this.vill = vill; } public String getUpFile() { return upFile; } public void setUpFile(String upFile) { this.upFile = upFile; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
插入数据类:
package cn.test; import java.lang.reflect.Field; import java.util.HashMap; public class ReflectDemo { static HashMap<String,String> data; static{ data=new HashMap<String,String>(); data.put("name","拟征收土地公告"); data.put("type","测试公告"); data.put("data","2017-01-12"); data.put("address","成都市青羊区"); data.put("vill","二道街"); data.put("upFile","文件具体的内容"); data.put("num","文号:国:001"); data.put("type","测试公告"); } public static void main(String[] args)throws Exception { Notice obj=new Notice(); test2(obj,data); System.out.println(obj.getAddress()); } public static void test2(Notice obj,HashMap<String,String> data) throws Exception { Class cls=obj.getClass(); //getDeclaredFields():获得某个类的所有声明的字段 Field[] fields = cls.getDeclaredFields(); for(Field field : fields){ String item=data.get(field.getName()); field.set(obj,item); } } }
二,动态代理
通过类实现InvocationHandler接口重写invoke方法实现
可在需要调用的方法前面首先调用其他方法,做到权限控制
实现类:
package proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class myProxyDemo implements InvocationHandler{ Object aa; public Object newProxy(Object aa) { this.aa=aa;
//返回一个指定接口的代理类实例(获取类加载地址,获取类实现的接口) return Proxy.newProxyInstance(aa.getClass().getClassLoader(),aa.getClass().getInterfaces(), this); } int count; public int count() { return count; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { befor(method,args);
//调用此实例 Object ad = method.invoke(this.aa,args); return null; }
//可通过重写此方法完成不同功能 public void befor(Method method, Object[] args){ System.out.println("----------"); } }
测试类:
public class TestProxy { //UserManager一个普通的javabeen public static void main(String[] args) { myProxyDemo handler=new myProxyDemo (); UserManager userManager=(UserManager) handler.newProxy(new UserManagerImpl()); userManager.addUser("ad", "da"); } }