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

If you control the subtypes, you can use the somewhat ugly idiom I noted to
pick out TSub. But I don't think there are enough use cases to justify
adding direct support into the type patterns. Indeed, in this use case I
think Michael really wants something like target but based on the static
type of the callee, rather than the statically determinable type of the
method being called (phew).

On the other, what would be valuable would be adding the proposed notion of
type definitions to AspectJ, so one can pick out a set of types without
having to control (weave into) them... but that's another topic.

-----Original Message-----
From: aspectj-users-bounces@xxxxxxxxxxx
[mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Gregor Kiczales
Sent: Tuesday, February 21, 2006 11:08 AM
To: aspectj-users@xxxxxxxxxxx
Subject: RE: [aspectj-users] Matching targets cast to a specific type

I seem to remember that we briefly considered a type pattern TSub, which
unlike T+ would match only subtypes of T, not T itself. I think we never
found a compelling use case.


> -----Original Message-----
> From: aspectj-users-bounces@xxxxxxxxxxx 
> [mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Ron Bodkin
> Sent: Tuesday, February 21, 2006 10:58 AM
> To: aspectj-users@xxxxxxxxxxx
> Subject: 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
> 
> 
> _______________________________________________
> 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