首页 > 代码库 > java 反射提取类信息, 动态代理 和过滤某些方法演示

java 反射提取类信息, 动态代理 和过滤某些方法演示

java 反射提取类信息, 动态代理 和过滤某些方法演示

package org.rui.classts.reflects;


import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.regex.Pattern;

/**
 * 提取 类的  方法 构造器
 * @author lenovo
 *
 */
//{args:ShowMethods}
//查看一个类的所有方法和构造器
public class ShowMethods {
	private static String usage="usage:showMethod qualified.class.mane";
	
	private static Pattern p=Pattern.compile("\\w+\\.");
	
	public static void main(String[] args) throws ClassNotFoundException {
		
		
		//System.out.println(args[0]+"  :  "+args[1]);
		args=new String[1];
		args[0]="org.rui.classts.reflects.ShowMethods";
		//args[1]="java.awt.Color";
		
		if(args.length<1)
		{
			System.out.println(usage);
		    System.exit(0);
		}
		int lines=0;
		Class<?> c=Class.forName(args[0]);
		Method[] m=c.getMethods();	
		Constructor[] constructor=c.getConstructors();
		if(args.length==1)
		{
			for(Method mt:m)
			{
				//System.out.println("tostring:"+mt.toString());
				//去掉带 点和前面的字符 如  xx.ss.
				System.out.println("m1:"+
				p.matcher(mt.toString()).replaceAll("")
						);
			}
			//----------------------------------------
			for(Constructor con:constructor)
				System.out.println("c1:"+
						p.matcher(con.toString()).replaceAll("")
						);
			lines=m.length+constructor.length;
		}
		else
		{
			for(Method mt:m)
			{
				if(mt.toString().indexOf(args[1])!=-1)
				{
					System.out.println("m2:"+
							p.matcher(mt.toString()).replaceAll("")
									);
					lines++;
				}
				
			}
			//----------------------------------------
			for(Constructor con:constructor)
			{
				if(con.toString().indexOf(args[1])!=-1)
				{
					System.out.println("c2:"+
							p.matcher(con.toString()).replaceAll("")
							);
				lines++;
				}
				
			}
		}
		
		
	}

}

package org.rui.classts.reflects;

/**
 * 简单动态代理 实现
 * @author lenovo
 *
 */
interface Interface
{
	 void doSomething();
	void somethingElse(String arg);
}
//class------------------
class RealObject implements Interface{
	public void doSomething() {
		System.out.println("RealObject doSomething");
	}

	public void somethingElse(String arg) {
		System.out.println("RealObject somethingElse:"+arg);
	}
}
//class-SimpleProxy-----------------
class SimpleProxy implements Interface{
	 private Interface proxied;
	 
	 public SimpleProxy(Interface proxied){this.proxied=proxied;}
	 
	    //method
		public void doSomething() {
			System.out.println("SimpleProxy doSomething");
			proxied.doSomething();
		}

		public void somethingElse(String arg) {
			System.out.println("SimpleProxy somethingElse:"+arg);
			proxied.somethingElse(arg);
		}
	 }


public class SimpleProxyDemo {
	
	public static void consumer(Interface inta)
	{
		inta.doSomething();
		inta.somethingElse("bonobo");
	}
	 
     //main----------
	public static void main(String[] args) 
	{
		//Interface o=new RealObject();
		//真实对象去实现 
		consumer(new RealObject());
		//代理去实现
		consumer(new SimpleProxy(new RealObject()));
		
	}
	

}

package org.rui.classts.reflects;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//DynamicProxyHandler
class DynamicProxyHandler  implements InvocationHandler{

	private Object proxied;
	
	public DynamicProxyHandler(Object proxied)
	{
		this.proxied=proxied;
	}
	
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		System.out.println("proxy:=="+proxy.getClass()
				+"\nmethod:=="+method
				+" \nargs:"+args);
		
		if(args!=null)
			for(Object o:args)
				System.out.println("arg:"+o);
		//把 实际对象给  proxied
		return method.invoke(proxied, args);
		
	}
}

class SimpleDynamicProxy{
	public static void consumer(Interface iface)
	{
		iface.doSomething();
		iface.somethingElse("bonobo==");
	}
	
	public static void main(String[] args) {
		RealObject robj=new RealObject();
		consumer(robj);
		
		//代理
		//返回一个指定接口的代理类实例,该接口可以将方法调用指派到指定的调用处理程序。
		Interface proxy=(Interface) Proxy.newProxyInstance(
				Interface.class.getClassLoader(), // 定义代理类的类加载器
				new Class[]{Interface.class},// 代理类要实现的接口列表
				new DynamicProxyHandler(robj)//指派方法调用的调用处理程序 
				);
		consumer(proxy);
		
	}
}

package org.rui.classts.reflects;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
 * 动态代理过滤某些方法 的演示
 * @author lenovo
 *
 */
	
	class MethodSelector implements InvocationHandler
	{
		private Object obj;
		public MethodSelector(Object o){this.obj=o;}
		public Object invoke(Object proxy, Method method, Object[] args)
				throws Throwable 
		{
			//System.out.println("proxy:"+proxy.getClass().getSimpleName());
			//可以拦截特殊处理
			if(method.getName().equals("interesting"))
			{
				System.out.println("proxy detected the interesting method");
			}
		 return method.invoke(obj, args);
		}
	 }
	
	// interface
	interface SomeMethod
	{
		void boring1();
		void interesting(String arg);
		void boring2();
		void boring3();
	}
	//------------------------------
	class Implementation implements SomeMethod
	{
		public void boring1() 
		{
			System.out.println("boring1");
		}
		public void interesting(String arg) 
		{
			System.out.println("interesting:"+arg);
		}
		public void boring2()
		{
			System.out.println("boring2");
		}
	
		public void boring3()
		{
			System.out.println("boring3");
			
		}
		
	}


//
public class SelectingMethods {
	public static void main(String[] args) 
	{
		//代理测试
		SomeMethod proxy =(SomeMethod)Proxy.newProxyInstance(
				SomeMethod.class.getClassLoader(), // 定义代理类的类加载器
				new Class[]{SomeMethod.class},// 代理类要实现的接口列表
				new MethodSelector(new Implementation())//指派方法调用的调用处理程序 
				);
		
		proxy.boring1();
		proxy.boring2();
		proxy.interesting("bonobo");
		proxy.boring3();
		
		
	}

}

对比测试

package org.rui.classts;


import org.rui.classts.chilnd.*;

public class PetCount4 {
	
	public static void main(String[] args) {
		Pet p=new Dog();
		Class c=Pet.class;
		Class c1=Dog.class;
		Class c2=Cat.class;
		//对象对比 类
		if(p instanceof Dog){
			System.out.println("true");
		}else	System.out.println("fales");
		
		//class 对比 对象
		if(c.isInstance(p)){
			System.out.println("true");
		}else	System.out.println("fales");
		// class对比class
		if(c.isAssignableFrom(c1)){
			System.out.println("true");
		}else	System.out.println("fales");
		
		if(c2.isAssignableFrom(c1)){
			System.out.println("true");
		}else	System.out.println("fales");
		
		System.out.println("c==c1:"+(c==c1));
		System.out.println("c.equals(c1:"+(c.equals(c1)));
		
		System.out.println("c==Pet.class:"+(c==Pet.class));
		System.out.println("c.equals(Pet.class:"+(c.equals(Pet.class)));
		
	}
	
}