package test.utils.classproxy;

import static org.hamcrest.Matchers.is;

import org.junit.Assert;
import org.junit.Test;

import com.firefly.utils.ReflectUtils.MethodProxy;
import com.firefly.utils.classproxy.ClassProxy;
import com.firefly.utils.classproxy.ClassProxyFactoryUsingJavassist;

public class TestClassProxyFactoryUsingJavassist {

	public static class Fee {
		protected void testProtected() {
			
		}
		
		public void testVoid(String str, Long l) {
			
		}
		
		public int testInt(int i) {
			return i;
		}
		
		public Void testParameters(String str, int i, Long l) {
			return null;
		}
		
		public String hello() {
			return "hello fee";
		}
	}
	
	@Test
	public void test() throws Throwable {
		Fee origin = new Fee();
		
		Fee fee = (Fee)ClassProxyFactoryUsingJavassist.INSTANCE.createProxy(origin, new ClassProxy(){

			@Override
			public Object intercept(MethodProxy handler, Object originalInstance, Object[] args) {
				System.out.println("intercept method 1: " + handler.method().getName() + "|" + originalInstance.getClass().getCanonicalName());
				if(handler.method().getName().equals("testInt")) {
					args[0] = 1;
				}
				Object ret = handler.invoke(originalInstance, args);
				System.out.println("intercept method 1 end...");
				if(handler.method().getName().equals("hello"))  {
					ret = ret + " intercept 1";
				}
				return ret;
			}}, null);
		System.out.println(fee.getClass().getCanonicalName());
		Assert.assertThat(fee.hello(), is("hello fee intercept 1"));
		Assert.assertThat(fee.testInt(25), is(1));
		
		Fee fee2 = (Fee)ClassProxyFactoryUsingJavassist.INSTANCE.createProxy(fee, new ClassProxy(){

			@Override
			public Object intercept(MethodProxy handler, Object originalInstance, Object[] args) {
				System.out.println("intercept method 2: " + handler.method().getName()  + "|" + originalInstance.getClass().getCanonicalName());
				if(handler.method().getName().equals("testInt")) {
					args[0] = 2;
				}
				Object ret = handler.invoke(originalInstance, args);
				System.out.println("intercept method 2 end...");
				
				if(handler.method().getName().equals("hello"))  {
					ret = ret + " intercept 2";
				}
				return ret;
			}}, null);
		System.out.println(fee2.getClass().getCanonicalName());
		Assert.assertThat(fee2.hello(), is("hello fee intercept 1 intercept 2"));
		Assert.assertThat(fee.testInt(25), is(1));
	}
	
	public static void main(String[] args) throws Throwable {
		new TestClassProxyFactoryUsingJavassist().test();
	}
}

@@ -66,16 +66,50 @@
 rs.is;%0A%0A
+import java.lang.reflect.Method;%0A%0A
 import o
@@ -316,16 +316,66 @@
 vassist;
+%0Aimport com.firefly.utils.classproxy.MethodFilter;
 %0A%0Apublic
@@ -2457,16 +2457,1029 @@
 );%0A%09%7D%0A%09%0A
+%09@Test%0A%09public void testFilter() throws Throwable %7B%0A%09%09Fee origin = new Fee();%0A%09%09%0A%09%09Fee fee = (Fee)ClassProxyFactoryUsingJavassist.INSTANCE.createProxy(origin, new ClassProxy()%7B%0A%0A%09%09%09@Override%0A%09%09%09public Object intercept(MethodProxy handler, Object originalInstance, Object%5B%5D args) %7B%0A%09%09%09%09System.out.println(%22filter method 1: %22 + handler.method().getName() + %22%7C%22 + originalInstance.getClass().getCanonicalName());%0A%09%09%09%09if(handler.method().getName().equals(%22testInt%22)) %7B%0A%09%09%09%09%09args%5B0%5D = 1;%0A%09%09%09%09%7D%0A%09%09%09%09Object ret = handler.invoke(originalInstance, args);%0A%09%09%09%09System.out.println(%22filter method 1 end...%22);%0A%09%09%09%09if(handler.method().getName().equals(%22hello%22))  %7B%0A%09%09%09%09%09ret = ret + %22 filter 1%22;%0A%09%09%09%09%7D%0A%09%09%09%09return ret;%0A%09%09%09%7D%7D, new MethodFilter()%7B%0A%0A%09%09%09%09@Override%0A%09%09%09%09public boolean accept(Method method) %7B%0A%09%09%09%09%09return !method.getName().equals(%22testInt%22);%0A%09%09%09%09%7D%7D);%0A%09%09System.out.println(fee.getClass().getCanonicalName());%0A%09%09Assert.assertThat(fee.hello(), is(%22hello fee filter 1%22));%0A%09%09Assert.assertThat(fee.testInt(25), is(25));%0A%09%7D%0A%09%0A
 %09public 

add test case