Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] Matching targets cast to a specific type

Hi Michael,

I believe that your form using an if pointcut based on
thisJoinPointStaticPart is the only way to match the join points you want. 

If you wanted to test for calls that are or are not made to a method that is
overridden in a subclass of Foo with a static reference to such a subclass,
that could be done all at compile-time using:

   declare parents: Foo+ && !Foo implements SubFoo; //marker

You could then match call(* Foo.*(..)) && !call(* SubFoo.*(..)) or call(*
SubFoo.*(..)).

To the question of the efficiency of the if test, I'd be interested in
seeing performance measurements. The bytecode that is generated looks
relatively efficient (basically 3 method invocations and a comparison at
runtime for the normal case), so for many uses it would be acceptable.

It's worth noting that your if pointcut will actually match all the examples
of use you gave, even those you didn't want to match, if there's an
inter-type declaration for IFoo.foo and not a definition in Foo.foo ...
i.e., it is testing for the type that is defining the method as well as the
static type of the callee. Your pointcut works unless there are ITD's,
because all implementers of an interface must implement its methods. E.g.:

aspect TestPcd {
  public void IFoo.foo() {
  }

  pointcut IFooCall(): call(* IFoo.*(..));
  pointcut IFooOnlyCall(): IFooCall() &&
 
if(thisJoinPointStaticPart.getSignature().getDeclaringType()==IFoo.class);

  before() : IFooOnlyCall() {
    System.err.println(thisJoinPoint);
  }
}

interface IFoo {
  public void foo();
}

class Foo implements IFoo {
}

public class MatchSuperOnly {

   public static void main(String arg[]) {
      match();
      nomatch();
   }

   private static void match() {
	IFoo f = new Foo();
	f.foo(); // this
   }

   private static void nomatch() {
	Foo f = new Foo();
	f.foo(); // not this
      new Foo().foo(); // or this
   }
}

java MatchSuperOnly
call(void IFoo.foo())
call(void IFoo.foo())
call(void IFoo.foo())

-----Original Message-----
From: aspectj-users-bounces@xxxxxxxxxxx
[mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Michael Herrmann
Sent: Tuesday, February 21, 2006 9:30 AM
To: aspectj-users@xxxxxxxxxxx
Subject: [aspectj-users] Matching targets cast to a specific type

Hello again, 

I'd like a pointcut to match if and only if the target is declared as(cast
to) a specific type. I hope the following example helps understand what I
mean:

public interface IFoo {
public void foo();
}

public class Foo implements IFoo {
public void foo() {}
}

The pointcut should now match

IFoo f = new Foo();
f.foo(); // this

but not

Foo f = new Foo();
f.foo(); // this

or 

new Foo().foo();


The following pointcut does what I want:

before(): call(void IFoo.doSomething()) &&
if(thisJoinPointStaticPart.getSignature().getDeclaringType()==IFoo.class)

but costs performance since if(...) is evaluated at runtime. The information
I need for picking out the right join points is already present at
compile-time, though, so my question is: Is there a way of expressing this
pointcut using only compile-time functionality?

Thanks 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




Back to the top