package amery.jdk.basic.proxy;

import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.MethodDelegation;
import org.aopalliance.intercept.Interceptor;

import static net.bytebuddy.matcher.ElementMatchers.named;

/**
 * 2 * @Author: han,zhansheng
 * 3 * @Date: 2019/10/21 10:37 AM
 * 4
 */
public class ByteBuddyTest {

    // 拦截DB.hello()方法，并委托给Interceptor中的静态方法处理
    public static void main(String[] args) {
        try {
            String helloWorld = new ByteBuddy().subclass(DB.class)
                    .method(named("hello"))
                    .intercept(MethodDelegation.to(Interceptor.class))
                    .make()
                    .load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.INJECTION)
                    .getLoaded()
                    .newInstance()
                    .hello("World");

            System.out.println(helloWorld);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    class Foo {
        public String bar() { return null; }
        public String foo() { return null; }
        public String foo(Object o) { return null; }
    }

    class DB {
        public String hello(String name) {
            System.out.println("DB:" + name);
            return null;
        }
    }
}

@@ -392,91 +392,413 @@
     
-//%C2%A0%E6%8B%A6%E6%88%AADB.hello()%E6%96%B9%E6%B3%95%EF%BC%8C%E5%B9%B6%E5%A7%94%E6%89%98%E7%BB%99Interceptor%E4%B8%AD%E7%9A%84%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95%E5%A4%84%E7%90%86%0A
+public static void main(String%5B%5D args) %7B%0A        test();%0A    %7D%0A%0A    class Foo %7B%0A        public String bar() %7B return null; %7D%0A        public String foo() %7B return null; %7D%0A  
     
+  
 public 
-static void main(String%5B%5D args
+String foo(Object o) %7B return null; %7D%0A    %7D%0A%0A    class DB %7B%0A        public String hello(String name) %7B%0A            System.out.println(%22DB:%22 + name);%0A            return null;%0A        %7D%0A    %7D%0A%0A    private static void test(
 ) %7B%0A
@@ -1448,319 +1448,7 @@
    %7D
-%0A%0A    class Foo %7B%0A        public String bar() %7B return null; %7D%0A        public String foo() %7B return null; %7D%0A        public String foo(Object o) %7B return null; %7D%0A    %7D%0A%0A    class DB %7B%0A        public String hello(String name) %7B%0A            System.out.println(%22DB:%22 + name);%0A            return null;%0A        %7D%0A    %7D
 %0A%7D%0A

add byte buddy test