简体   繁体   English

如何通过环绕构造函数替换类型的实例?

[英]How can I replace the instanced of a type by wrapping around the constructor?

I have a legacy lib that creates instances of BeanProxy . 我有一个遗留的lib,它创建了BeanProxy实例。 Unfortunately this implementation has a flaw I would like to fix. 不幸的是,这个实现有一个我想修复的缺陷。 As I don't want to start working with patched libs, I wanted to create an Aspect wrapping the construction of BeanProxy and return an instance of my modified BeanProxy sub-type. 由于我不想开始使用修补的库,我想创建一个包装BeanProxy构造的BeanProxy并返回我修改过的BeanProxy子类型的实例。

I created the following Aspect and it is correctly woven and called whenever a new instance of BeanProxy is created: 我创建了以下Aspect,只要创建了一个新的BeanProxy实例,它就会被正确编织和调用:

@Aspect
public class CWebBeanProxyInjectingAspect {

    @Pointcut("execution(public flex.messaging.io.BeanProxy.new(..))")
    void createBeanProxy() {}

    @Around("createBeanProxy()")
    public Object createAlternateBeanProxy(final ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("createAlternateBeanProxy");
        final Object result = pjp.proceed();
        System.out.println(result);
        return result;
    }
}

Unfortunately result is always null ... what am I doing wrong? 不幸的是result总是null ......我做错了什么? What do I have to change? 我需要改变什么? I should mention, that I am using AspectJ LoadTimeWeaving and the spring-instrument-3.1.1.RELEASE.jar as agent. 我应该提一下,我使用AspectJ LoadTimeWeaving和spring-instrument-3.1.1.RELEASE.jar作为代理。

Constructor execution don't return anything (is void ). 构造函数execution不返回任何内容( void )。 If you want to return the created object, use call in your pointcut: 如果要返回创建的对象, call在切入点中使用call

  @Pointcut("call(public flex.messaging.io.BeanProxy.new(..))")
    void createBeanProxy() {}

see Contstructor call and Constructor execution in http://www.eclipse.org/aspectj/doc/next/progguide/semantics-joinPoints.html 请参阅http://www.eclipse.org/aspectj/doc/next/progguide/semantics-joinPoints.html中的 Contstructor调用构造函数执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用多个参数对变体函数进行稳定包装? - How can I make a stable wrapping around variant function with several parameters? 我可以替换构造函数中的getter函数(例如,使用super)以及如何? - Can i replace the getter function in the constructor (with super for example) and how? 如何用通配符替换此通用类型 - How can I replace this generic type with a wildcard 如何更改实例化类变量 - how do I change instanced class variables 如何重新实例化由 javascript(Nashorn 引擎)实例化的 class - how can I re-instantiate a class instanced by javascript (Nashorn Engine) 如何从另一个类中的public类型的参数化构造函数中调用默认类型的参数化构造函数? - How can I call parameterized constructor of default type from a parameterized constructor of type public which is in another class? 如何在Java中实现构造函数包装? - How to implement constructor wrapping in Java? 如何在构造函数中传递枚举类型的值? - How can I pass a value of type enum in a constructor? 如何避免滥用具有多个相同类型参数的构造函数? - How can I avoid misuse of a constructor with multiple parameters of the same type? 如何在仍然保持类型的同时访问我正在使用的构造函数的构造函数 - How can I access the constructor of the constructor I'm using while still maintaining the type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM