CGLib 混合示例

发布于 2024-10-26 22:19:43 字数 155 浏览 6 评论 0原文

有人能给我一个 Java CGLib Mixin 类用法的好例子吗?我一直在挖掘它们,但没有一个看起来足够简单。

Can somebody give me a good example for Java CGLib Mixin class usage? I've been digging around none of them seems simple enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幸福不弃 2024-11-02 22:19:43

很简单:

import static org.junit.Assert.*;
import net.sf.cglib.proxy.Mixin;

import org.junit.Before;
import org.junit.Test;


public class MixinTest {

    @Test
    public void test() {
        Mixin mixin = Mixin.create(new Object[]{ new Class1(), new Class2() });
        assertEquals(1, ((Interface1)mixin).method1());
        assertEquals(2, ((Interface2)mixin).method2());
    }

    private interface Interface1 {
        public int method1();
    }

    private interface Interface2 {
        public int method2();
    }

    private static class Class1 implements Interface1 {

        @Override
        public int method1() {
            return 1;
        }

    }

    private static class Class2 implements Interface2 {

        @Override
        public int method2() {
            return 2;
        }

    }

}

我希望有帮助。

Easy enough:

import static org.junit.Assert.*;
import net.sf.cglib.proxy.Mixin;

import org.junit.Before;
import org.junit.Test;


public class MixinTest {

    @Test
    public void test() {
        Mixin mixin = Mixin.create(new Object[]{ new Class1(), new Class2() });
        assertEquals(1, ((Interface1)mixin).method1());
        assertEquals(2, ((Interface2)mixin).method2());
    }

    private interface Interface1 {
        public int method1();
    }

    private interface Interface2 {
        public int method2();
    }

    private static class Class1 implements Interface1 {

        @Override
        public int method1() {
            return 1;
        }

    }

    private static class Class2 implements Interface2 {

        @Override
        public int method2() {
            return 2;
        }

    }

}

I hope that helps.

下壹個目標 2024-11-02 22:19:43

这个问题比仅基于接口的 mixin 情况更广泛,所以这里是带有 2 个任意类的 CGLIB mixin 的示例:

import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.Locale;

import net.sf.cglib.proxy.Mixin;
import net.sf.cglib.proxy.Mixin.Generator;

public class CglibTest {

    public static void main(String[] args) throws Exception {
        Generator gen = new Generator();
        gen.setStyle(Mixin.STYLE_EVERYTHING);
        gen.setDelegates(new Object[]{ Charset.defaultCharset(), Locale.getDefault()});
        Mixin mixin = gen.create();
        System.out.println(invokeMethod(mixin, "displayName"));
        System.out.println(invokeMethod(mixin, "getCountry"));
    }  


    public static Object invokeMethod(Object target, String methodName) throws Exception {
        Method method = target.getClass().getMethod(methodName);
        return method.invoke(target);
    }

}

The question is broader than only interface-bases mixin case, so here's the example for CGLIB mixin with 2 arbitrary classes:

import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.Locale;

import net.sf.cglib.proxy.Mixin;
import net.sf.cglib.proxy.Mixin.Generator;

public class CglibTest {

    public static void main(String[] args) throws Exception {
        Generator gen = new Generator();
        gen.setStyle(Mixin.STYLE_EVERYTHING);
        gen.setDelegates(new Object[]{ Charset.defaultCharset(), Locale.getDefault()});
        Mixin mixin = gen.create();
        System.out.println(invokeMethod(mixin, "displayName"));
        System.out.println(invokeMethod(mixin, "getCountry"));
    }  


    public static Object invokeMethod(Object target, String methodName) throws Exception {
        Method method = target.getClass().getMethod(methodName);
        return method.invoke(target);
    }

}
玩套路吗 2024-11-02 22:19:43

此示例与高级增强器一起使用,并且像 mixin 一样工作:
http://www.jroller.com/melix/entry/alternative_to_delegate_pattern_with

This example work with advanced enhancer and work like mixin:
http://www.jroller.com/melix/entry/alternative_to_delegate_pattern_with

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文