Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Set scope for code in advice to join point's target

fyi, the code below works, but because of "this", not "scope".

In case it trips you up: the ITD method is declared in the aspect
and considered in the *aspect* scope (i.e., has the same visibility as 
the aspect).  So the method won't act like it is in the scope of Foo 
(e.g., and see private members or be "within" Foo).

However, the code below works because for methods declared on other 
types (ITD's), an implicit or explicit "this" is a reference not 
to the aspect, but to the other type.

Below is a code snippet to demonstrate.

hth - Wes

 src $ cat Scope.java

public class Scope {
  private static int i;
  public static void main(String[] args) {}
}
aspect A {
  static int Scope.i() { return i; } // illegal
  void i() { return Scope.i; } // illegal
  before() : execution(void Scope.main(..)) {
    System.out.println("i(): " + i() + "Scope.i(): " + Scope.i());
  }
}

 src $ aspectj-1.5 Scope.java && j5 Scope
C:\home\wes\work\src\Scope.java:7 [error] The field Scope.i is not visible
static int Scope.i() { return i; } // illegal

C:\home\wes\work\src\Scope.java:8 [error] The field Scope.i is not visible
void i() { return Scope.i; } // illegal
                  ^^^^
....

> ------------Original Message------------
> From: "Michael Herrmann" <Michael_Herrmann@xxxxxx>
> To: aspectj-users@xxxxxxxxxxx
> Date: Sun, Feb-12-2006 10:35 AM
> Subject: Re: [aspectj-users] Set scope for code in advice to join point's	target
>
> That's exactly what I was looking for! Thank you very much. 
> 
> Kind regards, 
> Michael Herrmann
> 
> > You can't get AspectJ to do that in the advice code. However, you can
> > put the contents of your advice into another method, then use
> > intertype declaration to make that method a member of class Foo:
> > 
> > public aspect FooAspect {
> >     before(Foo f): call(void Foo.doSomething()) && target(f) {
> >     	f.advice();
> >     }
> > 
> >     void Foo.advice() {
> >         // magic construct
> >         doSomethingElse();
> >         System.out.println("Some String");
> >         doAnotherThing();
> >     }
> > }
> > 
> > HTH.
> > 
> > dean
> > 
> > --
> > Dean Wampler
> > http://www.aspectprogramming.com
> > http://www.newaspects.com
> > http://www.contract4j.org
> > 
> > On 2/12/06, Michael Herrmann <Michael_Herrmann@xxxxxx> wrote:
> > > Hello again,
> > >
> > > is it possible to set the scope for the execution of some code in 
> an
> > advice
> > > to the join point's target (I hope I've used the technical terms
> > correctly)?
> > > Let's assume I have a class Foo:
> > >
> > > public class Foo {
> > >         public void doSomething() {
> > >                 // ...
> > >         }
> > >
> > >         public void doSomethingElse() {
> > >                 // ...
> > >         }
> > >
> > >         public void doAnotherThing() {
> > >                 // ...
> > >         }
> > > }
> > >
> > > and the corresponding aspect, FooAspect:
> > >
> > > public aspect FooAspect {
> > >         before(Foo f): call(void Foo.doSomething()) && target(f) {
> > >                 f.doSomethingElse();
> > >                 System.out.println("Some String");
> > >                 f.doAnotherThing();
> > >         }
> > > }
> > >
> > > I now want to avoid having to write f. ... everytime. Rather, I'd 
> like
> > to
> > > tell AJ to execute a set of statements in the context of f. In 
> other
> > words:
> > > I want to execute a set of statements as if they were directly 
> written
> > in
> > > the source code of Foo:
> > >
> > > public aspect FooAspect {
> > >         before(Foo f): call(void Foo.doSomething()) && target(f) {
> > >                 // magic construct
> > >                 doSomethingElse();
> > >                 System.out.println("Some String");
> > >                 doAnotherThing();
> > >         }
> > > }
> > >
> > > I hope you understand what I mean. Thank you very much in advance,
> > > Michael Herrmann
> > >
> > > --
> > > 10 GB Mailbox, 100 FreeSMS/Monat http://www.gmx.net/de/go/topmail
> > > +++ GMX - die erste Adresse für Mail, Message, More +++
> > > _______________________________________________
> > > aspectj-users mailing list
> > > aspectj-users@xxxxxxxxxxx
> > > https://dev.eclipse.org/mailman/listinfo/aspectj-users
> > >
> > 
> > 
> > --
> > Dean Wampler
> > http://www.aspectprogramming.com
> > http://www.newaspects.com
> > http://www.contract4j.org
> > _______________________________________________
> > aspectj-users mailing list
> > aspectj-users@xxxxxxxxxxx
> > https://dev.eclipse.org/mailman/listinfo/aspectj-users
> > 
> 
> -- 
> Lust, ein paar Euro nebenbei zu verdienen? Ohne Kosten, ohne Risiko!
> Satte Provisionen für GMX Partner: http://www.gmx.net/de/go/partner
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
> 



Back to the top