多方法拦截和拦截器重用

时间:2017-04-20 12:53:19

标签: java byte-buddy

这是我的用例:我有一个类,我想拦截多个方法,但我不想拦截这个类的所有方法。我想使用相同拦截器类的不同实例来实现此目的。当我尝试这样做时,我看到了Byte Buddy无法理解的行为。我很确定这是我不理解和/或我做错的事情,但我很难过。我使用的是Byte Buddy 1.6.9。

我想拦截其方法的类:

public class MyClass
{
    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    public void firstInterceptedMethod()
    {
        logger.info("firstInterceptedMethod");
    }

    public void secondInterceptedMethod()
    {
        logger.info("secondInterceptedMethod");     
    }

    public void notInterceptedMethod()
    {
        logger.info("notInterceptedMethod");        
    }
}

拦截班:

public class MyInterceptor
{
    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    private final UUID identifier = UUID.randomUUID();

    @RuntimeType
    public Object methodCalled(@SuperCall Callable<?> superCall, @Origin Method method) throws Exception
    {
        logger.info("methodCalled: identifier: " + identifier);
        logger.info("methodCalled: method name: " + method.getName());

        return superCall.call();
    }

}

Byte Buddy测试/仪器:

public class MyTest
{
    @Test
    public void test() throws Exception
    {
        MyInterceptor firstMethodInterceptor = new MyInterceptor();
        MyInterceptor secondMethodInterceptor = new MyInterceptor();

        MyClass myClass = new ByteBuddy().subclass(MyClass.class)
                .method(ElementMatchers.named("firstInterceptedMethod"))
                        .intercept(MethodDelegation.to(firstMethodInterceptor))
                .method(ElementMatchers.named("secondInterceptedMethod"))
                        .intercept(MethodDelegation.to(secondMethodInterceptor))
                .make()
                .load(MyClass.class.getClassLoader())
                .getLoaded()
                .newInstance(); 

        myClass.firstInterceptedMethod();
        myClass.secondInterceptedMethod();
    }
}

输出:

04/20/2017 08:27:26:600 AM [main] bytebuddy.test.MyInterceptor methodCalled : INFO methodCalled: identifier: 04124951-f865-4815-8bd4-0b10c0c816a2
04/20/2017 08:27:26:600 AM [main] bytebuddy.test.MyInterceptor methodCalled : INFO methodCalled: identifier: 04124951-f865-4815-8bd4-0b10c0c816a2
04/20/2017 08:27:26:613 AM [main] bytebuddy.test.MyInterceptor methodCalled : INFO methodCalled: method name: firstInterceptedMethod
04/20/2017 08:27:26:613 AM [main] bytebuddy.test.MyInterceptor methodCalled : INFO methodCalled: method name: firstInterceptedMethod
04/20/2017 08:27:26:614 AM [main] bytebuddy.test.MyClass firstInterceptedMethod : INFO firstInterceptedMethod
04/20/2017 08:27:26:614 AM [main] bytebuddy.test.MyInterceptor methodCalled : INFO methodCalled: identifier: 79590462-b87d-4125-9e87-5481e1062b05
04/20/2017 08:27:26:614 AM [main] bytebuddy.test.MyInterceptor methodCalled : INFO methodCalled: identifier: 79590462-b87d-4125-9e87-5481e1062b05
04/20/2017 08:27:26:614 AM [main] bytebuddy.test.MyInterceptor methodCalled : INFO methodCalled: method name: secondInterceptedMethod
04/20/2017 08:27:26:614 AM [main] bytebuddy.test.MyInterceptor methodCalled : INFO methodCalled: method name: secondInterceptedMethod
04/20/2017 08:27:26:614 AM [main] bytebuddy.test.MyClass secondInterceptedMethod : INFO secondInterceptedMethod

我的问题是:为什么我看到每个拦截器被调用两次?当调用第一个方法时,第一个拦截器实例似乎被调用了两次,我不明白为什么会这样。与第二种方法类似的行为,只有第二个拦截器实例似乎被调用两次。我希望拦截器只能在每个截获的方法调用时调用一次。

如果我这样做:

    FirstMethodInterceptor firstMethodInterceptor = new FirstMethodInterceptor();
    SecondMethodInterceptor secondMethodInterceptor = new SecondMethodInterceptor();
然后我看到了我期望的行为;即,每个截取方法仅调用一次拦截器。我希望避免这种情况,因为我希望能够重复使用常见的&#39;拦截器没有看到这种行为。非常感谢洞察力,谢谢。

1 个答案:

答案 0 :(得分:1)

是的,像往常一样,我做错了。这是记录器的工件,而不是Byte Buddy。正在为同一个记录器实例注册多个日志记录处理程序,这就是造成这种情况的原因。