找到你要的答案

Q:How to get the enclosing type with aspectj

Q:如何用AspectJ得到封闭类型

please consider this :

public class A
{
  private B bMemeber;
  private Properties myProperties;
}

and

public class B { 
 private Object field;
 public void setField(Object o){...}
}

I have an aspect

    @After("execution(* B.setField(..)) &&! within(PropertiesAspect)")
    public void afterSetField(JoinPoint jp)
    {....}

my question is : is it possible to get the enclosing type of Busing aspectj in my afterSetField as I need to test on some properties contained in the A object

请考虑这个:

public class A
{
  private B bMemeber;
  private Properties myProperties;
}

public class B { 
 private Object field;
 public void setField(Object o){...}
}

我有一个方面

    @After("execution(* B.setField(..)) &&! within(PropertiesAspect)")
    public void afterSetField(JoinPoint jp)
    {....}

我的问题是:是否有可能得到封闭式套管AspectJ在我aftersetfield我需要一些性能测试中所包含的对象

answer1: 回答1:

What you want is not easily possible because it is not what AOP was made for. An instance of class B has no idea about whether it is assigned to a member of any other class or is part of any collection or array. The JVM garbage collector does reference counting for internal purposes (freeing memory after an object is no longer referenced), but this is nothing you have access to from an aspect. What you can do with AspectJ is

  • intercept whenever a value or object is assigned to a member variable (set()) pointcut) or when that member is read (get() pointcut). By the way, these pointcuts do not work for local variables, only for members. But when a method is called upon any object that happens to be assigned to a member variable, technically that variable's value does not change, it is still the same (mutable) object, i.e. a method call or an internal state change in a referenced object does not trigger set() in the referencing object, only in the referenced object itself.
  • intercept method calls or executions, which you already do.

I am not sure that what you want makes any practical sense, but what you can do with some overhead is manual reference bookkeeping like this:

  • Whenever an object of interest is assigned to a member variable, intercept it via set() pointcut and remember it by putting it into a collection maintained by the aspect.
  • By the way, at the same time you also have to remove the old member object from the collection if (and only if!) it is no longer referenced.
  • In your execution() pointcut, find out if the object in question is in the internal collection, retrieve the info about the owning object(s) from the collection and do with it whatever you like.

I hope this explanation is comprehensible even without sample code. Feel free to ask follow-up questions.

你想要的是不容易的因为它不是AOP所为。B类的实例不知道它是否被分配给任何其他类的成员,或是任何集合或数组的一部分。JVM垃圾收集器能够参考用于内部目的计数(释放内存在一个对象不再被引用),但这不是你可以从一个方面。你可以做什么AspectJ

  • intercept whenever a value or object is assigned to a member variable (set()) pointcut) or when that member is read (get() pointcut). By the way, these pointcuts do not work for local variables, only for members. But when a method is called upon any object that happens to be assigned to a member variable, technically that variable's value does not change, it is still the same (mutable) object, i.e. a method call or an internal state change in a referenced object does not trigger set() in the referencing object, only in the referenced object itself.
  • intercept method calls or executions, which you already do.

我不知道你想要什么有什么实际意义,但你可以做一些开销是手工参考簿记这样:

  • Whenever an object of interest is assigned to a member variable, intercept it via set() pointcut 和 remember it by putting it into a collection maintained by the aspect.
  • By the way, at the same time you also have to remove the old member object from the collection if (和 only if!) it is no longer referenced.
  • In your execution() pointcut, find out if the object in question is in the internal collection, retrieve the info about the owning object(s) from the collection 和 do with it whatever you like.

我希望这个解释是可以理解的,即使没有示例代码。随时问后续问题。

java  aspectj