find out instantiating object in the constructor of a class

后端 未结 3 981
花落未央
花落未央 2020-12-22 09:56

How can i get hold of the instantiating object from a constructor in java?

I want to store reference to the parent object for some GUI classes to simulate event bubb

3条回答
  •  暖寄归人
    2020-12-22 10:30

    Intercepting method calls (including constructors) without changing a ton of existing code is one thing Aspect-oriented programming was made for.

    Check out AspectJ for a start.

    With AspectJ, you can define a "pointcut" that specifies that you want to intercept constructor calls for a certain object or set of objects (using wildcards if need be), and within the interception code ("advice"), you will be given method context, which includes information about the both the calling method and object.

    You can even use AspectJ to add fields to your object's to store the parent reference without modifying their existing code (this is called "introduction").

提交回复
热议问题