<commit_before>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;
        }
    }
}<commit_msg>add byte buddy test<commit_after>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 {

    public static void main(String[] args) {
        test();
    }

    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;
        }
    }

    private static void test() {
        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();
        }
    }
}
{'fraction_non_alphanumeric': 0.10926928281461434, 'fraction_numerical': 0.010148849797023005, 'mean_word_length': 2.014271151885831, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6538741', 'n_tokens_mistral': 883, 'n_tokens_neox': 824, 'n_words': 169}