Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Re: withincode and overriden methods

thanks Ron and Matthew,

yes, that's indeed the solution based on the current semantics of withincode in AspectJ.

Just like what I understood "within" as opposed to "this", I tended to see "withincode" as opposed to "execution" in the same way, i.e. the former is just about the lexical structure and won't touch sub-classes, and the latter is about the code being executed and thus will match sub-classes.

But "picking out execution of implementing methods on an interface" is indeed a good use case of the current semantics of withincode. Maybe we should say withincode is not purely lexical structure based?

Ron Bodkin wrote:
Hi Linton,

I think it's important that withincode matches statically on signatures in
the same way that execution and call do, i.e., based on the "declaring
type". This leads to matches for overriding methods as you've observed,
which I think was the right choice (e.g., it makes it easy to pick out
execution of implementing methods on an interface). Regardless this is now
the established semantics for AspectJ and I definitely think it shouldn't
change.

However, for your requirement you could use:

withincode(void Foo.bar()) && !withincode(void (Foo+ && !Foo).bar())
to express the notion of within only the implementation of bar defined on
Foo but not in overriding methods, without having to enumerate the
subtypes...

-----Original Message-----
From: aspectj-users-bounces@xxxxxxxxxxx
[mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Linton Ye
Sent: Monday, July 10, 2006 10:57 AM
To: aspectj-users@xxxxxxxxxxx
Subject: [aspectj-users] withincode and overriden methods

Hi All,

Please see the following code, say, I would like to capture join points inside the Foo.bar() method ONLY, not in any of the overridden versions.

However, only pointcut 3 works. Pointcut 1 and 2 matches bar methods defined in both Foo and SubFoo.

Compared to this, within pointcut works more like what I expected, as in pointcut 4, within(Foo) only matches join points in class Foo, not in any of its subclasses, e.g. SubFoo.

Since withincode and within are used to match join points based on the lexical structure of the program, it seems more reasonable not to implicitly include the subclasses. To match subclasses, we can just use withincode(void Foo+.bar()).

Any comments will be appreciated.

public class Withincode {
	public static void main(String[] args) {
		Foo f = new SubFoo();
		f.bar();
	}	
	static void log(Object...objects) {
		for(Object o:objects)
			System.out.print(o);
		System.out.println();
	}	
	static class Foo {
		public void bar() {
			log("Foo");
		}
	}	
	static class SubFoo extends Foo {
		public void bar() {
			log("SubFoo");
		}
	}	
	static aspect SomeAspect {
		// 1
		before():withincode(void Foo.bar()){}
		// 2
		before():withincode(void (Foo && !SubFoo).bar()){}
		// 3
		before():!withincode(void SubFoo.bar())
			&&withincode(void Foo.bar()){}
		// 4
		before():within(Foo) {	}
	}
}

thanks,
linton

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users



Back to the top