Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Monitoring all method calls exception

getDeclaringType() only returns you simply the type and not the
instance.  But you can use:

before(): someOldPointcut() {
  Object theTarget = thisJoinPoint.getTarget();// to get the object instance
}

Or, if they all implement takeDown() then I presume that is in some
interface somewhere?

interface TakeDownable {
  public void takeDown();
}

then I can write the pointcut in terms of that:

pointcut p(TakeDownable td):  call(* *(..)) && target(td);

before(TakeDownable td): p(td) {
  td.takeDown();
}

Andy.



On 08/11/2007, Rob Austin <austirob@xxxxxxxxxxxxxx> wrote:
> Andy,
>
> Another question off the back of this one it you don't mind.
>
> Is it possible to use getSignature().getDeclaringType() to
> get a reference to the object to which it refers?
>
> Essentially, I'm writing some code which monitors certain obects. If a
> method is called on one of these objects by some code which is not
> authorised to do so I want to terminate the object before it is compromised,
> so I want to get a reference to it to call its takeDown() method. The
> objects may be of different types but they all have a takeDown() method, but
> as I don't know the target type in advance, I can't use && target() I
> presume.
>
>
> Really appreciate your help
>
> Thanks
>
> Rob
>
>
>
>
>
>
> On 07/11/2007, Rob Austin <austirob@xxxxxxxxxxxxxx> wrote:
> > That's marvellous Andy, thanks so much
> >
> >
> >
> > On 07/11/2007, Andy Clement <andrew.clement@xxxxxxxxx > wrote:
> > > Your pointcut will catch static method calls.  At the call to static
> > > method there is no target instance, and so thisJoinPoint.getTarget()
> > > will return NULL, hence the NPE.  If you want the classname of the
> > > target, use getSignature().getDeclaringType()
> > >
> > > You should write !within(mypackage.bookTrading.*) and not
> > > within(!mypackage.bookTrading.*) as the latter will not
> exclude inner
> > > types and it looks like you have one of those because of the access$0
> > > shown in your output.  I imagine you really also want to exclude
> > > subtypes too, so it would be !within(mypackage.bookTrading..*)
> > >
> > > cheers,
> > > Andy.
> > >
> > >
> > > On 06/11/2007, Rob Austin <austirob@xxxxxxxxxxxxxx> wrote:
> > > >
> > > > Hi,
> > > >
> > > > I want to write an aspect that logs any call to my Package (myPackage)
> from
> > > > outside of it - those I don't expect in other words.
> > > >
> > > > I'm facing the following problems with my newbie code:
> > > > 1. I  thought I could restrict my advice to  calls made outside of my
> > > > package by using within(! myPackage.bookTrading.* ) , but as you can
> see in
> > > > the output below, it still seems to include calls made from within my
> > > > package.
> > > >
> > > > 2. Where a call is to a method contained in a jar rather than in code
> for
> > > > which the source is included in the project, thisJoinPoint
> > > > .getTarget().getClass errors out with a nasty message, something along
> the
> > > > lines of "Attempt to send a message to a non object value". I assume
> that
> > > > this behaviour would be expected, but any ideas how I can trap for
> this
> > > > rather than letting an error be raised.
> > > >
> > > > Really appreciate any help,
> > > >
> > > > Thanks
> > > >
> > > > Rob
> > > >
> > > >
> > > >
> > > > public aspect WatchMethodCalls {
> > > >
> > > >
> > > > pointcut WatchAll(): call(* myPackage ..*.*(..))
> > > > && within(! myPackage.bookTrading.*) && within(! WatchMethodCalls);
> > > >
> > > >
> > > >
> > > > before( ) :  WatchAll( )
> > > >
> > > > {
> > > >     System.out.println("thisJoinPoint.getTarget ().getSignature() "
> > > > +thisJoinPointStaticPart .getSignature());
> > > >     System.out.println("thisJoinPoint.getTarget().getSourceLocation()
> " +
> > > > thisJoinPointStaticPart .getSourceLocation());
> > > >     System.out.println("thisJoinPoint.getTarget().getClass() " +
> > > > thisJoinPoint .getTarget().getClass());
> > > >
> > > >
> > > >
> > > > }
> > > >
> > > > Sample output;
> > > >
> > > > thisJoinPoint.getTarget ().getSignature() Vector
> > > >
> myPackage.bookTrading.BookBuyerAgentMalicious.access$0
> (BookBuyerAgentMalicious)
> > > > thisJoinPoint.getTarget().getSourceLocation()
> > > > BookBuyerAgentMalicious.java:288
> > > >
> > > > Error occurred in WatchAll: java.lang.NullPointerException
> > > > at
> > > >
> apoptotic.BlockMethodCalls.ajc$before$apoptotic_BlockMethodCalls$1$2e232b3e
> > > > (BlockMethodCalls.aj:22)
> > > > at
> > > >
> apoptotic.bookTrading.BookBuyerAgentMalicious$BookNegotiator.action
> (BookBuyerAgentMalicious.java:288)
> > > > at
> > > >
> jade.core.behaviours.Behaviour.actionWrapper(Behaviour.java
> :340)
> > > >
> > > >
> > > >
> > > >
> > > > _______________________________________________
> > > > aspectj-users mailing list
> > > > aspectj-users@xxxxxxxxxxx
> > > >
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
> > > >
> > > >
> > > _______________________________________________
> > > aspectj-users mailing list
> > > aspectj-users@xxxxxxxxxxx
> > > https://dev.eclipse.org/mailman/listinfo/aspectj-users
> > >
> >
> >
>
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
>


Back to the top