Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Inner classes query

Isn't that just the convention for Java inner classes,
to supply the enclosing instance as the first parameter?

e.g., for
  class Outer {
    class Inner{}
  }

the bytecode invocation would be to

  Outer$Inner."<init>":(LOuter;)V

(There must be a VM/JLS entry on point.)

Note that this affects the join point signature and pointcut
matching.  For example, this pointcut would not match:

   call(Outer.Inner.new())

but this would:

   call(Outer.Inner.new(Outer))

and if you print thisJoinPoint.getSignature(), it will
show a parameter of type Outer.

So: I think this is a case where AspectJ is doing the
right thing in following the Java model, but that most
Java developers don't realize there is an implicit
argument for the enclosing instance, unless they have
constructed instances of inner classes like this:

  Outer o = new Outer();
  Inner i = o.new Inner();

I don't think we can or should omit the parameter,
and emitting warnings would be hard.  But we can
document this, probably along with some of the
other gotcha's with nested classes.

Other solutions?

Wes

neil loughran wrote:
Why does the trace for the aspect Foo below show  execution(Foo.Bar(Foo, String)) when I was expecting Foo.Bar(String) ?

When I move the inner class outside the aspect, I get the expected behaviour execution(Bar(String))

I'm sure I'm missing something :-)

Thanks Neil


aspect Foo
 {
after(String s) : args(s) && call(public void myMethod(String))
  {
  process(s);
  }
public void process(String s)
   {
   Bar p = new Bar(s); // trace on this line of code
   }
class Bar
     {
     public Bar(String s)
      {
} } }



Back to the top