package test.utils.classproxy;

import static org.hamcrest.Matchers.is;

import java.lang.reflect.Method;

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;
import com.firefly.utils.classproxy.MethodFilter;

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,
                (handler, originalInstance, 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,
                (handler, originalInstance, 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));
    }

    @Test
    public void testFilter() throws Throwable {
        Fee origin = new Fee();

        Fee fee = (Fee) ClassProxyFactoryUsingJavassist.INSTANCE.createProxy(origin,
                (handler, originalInstance, args) -> {
                    System.out.println("filter 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("filter method 1 end...");
                    if (handler.method().getName().equals("hello")) {
                        ret = ret + " filter 1";
                    }
                    return ret;
                }, method -> !method.getName().equals("testInt"));
        System.out.println(fee.getClass().getCanonicalName());
        Assert.assertThat(fee.hello(), is("hello fee filter 1"));
        Assert.assertThat(fee.testInt(25), is(25));
    }

    public static void main(String[] args) throws Throwable {
        new TestClassProxyFactoryUsingJavassist().test();
    }
}

@@ -36,75 +36,124 @@
 ort 
-static org.hamcrest.Matchers.is;%0A%0Aimport java.lang.reflect.Method;%0A
+com.firefly.utils.classproxy.ClassProxyFactory;%0Aimport com.firefly.utils.classproxy.ClassProxyFactoryUsingJavassist;
 %0Aimp
@@ -201,227 +201,233 @@
 st;%0A
-%0A
 import 
-com.firefly.utils.ReflectUtils.MethodProxy;%0Aimport com.firefly.utils.classproxy.ClassProxy;%0Aimport com.firefly.utils.classproxy.ClassProxyFactoryUsingJavass
+org.junit.runner.RunWith;%0Aimport org.junit.runners.Parameterized;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.L
 ist;%0A
+%0A
 import 
-com.firefly.utils.classproxy.MethodFilter;%0A
+static org.hamcrest.Matchers.is;%0A%0A@RunWith(Parameterized.class)
 %0Apub
@@ -471,24 +471,72 @@
 avassist %7B%0A%0A
+    @Parameterized.Parameter%0A    public Run r;%0A%0A
     public s
@@ -911,32 +911,548 @@
        %7D%0A    %7D%0A%0A
+    static class Run %7B%0A        ClassProxyFactory classProxyFactory;%0A        String name;%0A%0A        @Override%0A        public String toString() %7B%0A            return name;%0A        %7D%0A    %7D%0A%0A    @Parameterized.Parameters(name = %22%7B0%7D%22)%0A    public static Collection%3CRun%3E data() %7B%0A        List%3CRun%3E list = new ArrayList%3C%3E();%0A        Run run = new Run();%0A        run.classProxyFactory = ClassProxyFactoryUsingJavassist.INSTANCE;%0A        run.name = %22javassist proxy factory%22;%0A        list.add(run);%0A        return list;%0A    %7D%0A%0A
     @Test%0A    pu
@@ -1536,33 +1536,35 @@
 Fee fee = (Fee) 
-C
+r.c
 lassProxyFactory
@@ -1563,39 +1563,16 @@
 yFactory
-UsingJavassist.INSTANCE
 .createP
@@ -2463,33 +2463,35 @@
 ee fee2 = (Fee) 
-C
+r.c
 lassProxyFactory
@@ -2490,39 +2490,16 @@
 yFactory
-UsingJavassist.INSTANCE
 .createP
@@ -3506,17 +3506,19 @@
 = (Fee) 
-C
+r.c
 lassProx
@@ -3525,39 +3525,16 @@
 yFactory
-UsingJavassist.INSTANCE
 .createP
@@ -4440,24 +4440,26 @@
 5));%0A    %7D%0A%0A
+//
     public s
@@ -4500,32 +4500,34 @@
 ows Throwable %7B%0A
+//
         new Test
@@ -4564,20 +4564,22 @@
 t().test();%0A
+//
     %7D%0A%7D%0A

clean up code