Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] pointcut for any method of any type except those of anonymous inner classes

> One more question. I'm still quiet new in aspectj, so please don't blame me,
> if it doesn't make a sense.
> Would it be possible to have joinpoint for access to critical section? I
> mean to synchronized block or to synchronized method?

Yes it would be possible and AspectJ supports it.  It is the
lock()/unlock() pointcuts which are activated when you turn on the
-Xjoinpoints:synchronization option to the compiler.

It is discussed here:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=123759

Simple sample:

before(Useful2 o ): lock() && args(o) {
		activeTimer = System.currentTimeMillis();
		currentObject = o;
}
	
after(Useful2 o ): unlock() && args(o) {
		if (o!=currentObject) {
			throw new RuntimeException("Unlocking on incorrect thing?!?");
		}
		if (activeTimer!=0) {
			locktimer+=(System.currentTimeMillis()-activeTimer);
			iterations++;
			activeTimer=0;
		}
}

On the topic of anonymous types I've just committed a small extension
similar to hasmethod/hasfield type patterns.  It enables you to check
the category of the type.  It is called is(...) and you may specify:
ClassType/AnonymousType/InnerType/AnnotationType/EnumType/AspectType/InterfaceType.
 Your example would become:

execution(* *(..)) && !within(is(AnonymousType))

or you could use it in an execution pointcut:

execution(* (!is(AnonymousType) && com.foo..*).*(..))

this will be in todays dev build.

Andy


On 19 March 2010 04:58, Peter Kvokacka <kvokacka@xxxxxxxxx> wrote:
> Thank you for your response.
>
> !within(anonymous *) was exactly what I was looking for. It's a pity that
> it's not yet supported.
> I looked at link you sent. I think it would be really great if things
> mentioned there will be supported in one of future releases.
>
> I am writing pertype aspect that I will be able to turn on/off by setting
> boolean property. It will never do anything with anonymous classes. So
> thanks for the workaround, but because it uses runtime check, it's easier
> for me to check just that boolean property. At least that's a plan, I hadn't
> time to give it try yet :).
>
>
> thanks
> Peter
>
>
> On 18. 3. 2010 17:52, Andy Clement wrote:
>
> What I expect you are looking for is something like
>
> pointcut all(): execution(* *.*(..)) && !within(anonymous *);
>
> But that isn't supported - see discussion in
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=44365 comment 2, which
> would allow interface/class/aspect/anonymous/inner.
>
> That bug also discusses the options "within(isAnonymous())" and
> "within(@AnonymousType *)".
>
> This example below will work for you, but isn't super optimal due to
> the runtime check:
>
> ---
> public aspect Demo {
>
> 	public static void main(String[] args) {
> 		new Foo().foo();
> 	}
> 	before(): execution(* *(..)) &&
> if(!thisJoinPointStaticPart.getSignature().getDeclaringType().isAnonymousClass()){
> 		System.out.println("advised "+thisJoinPoint);
> 	}
> }
>
> class Foo {
> 	Runnable r = new Runnable() {
> 		public void run() {
> 		}
> 	};
> 	
> 	class Bar {
> 		public void bar() {}
> 	}
> 	
> 	public void foo() {
> 		new Bar().bar();
> 		r.run();
> 	}
> }
> ---
>
> I am rev'ing the bytecode format for 1.6.9 so this would be a good
> time to put something like the changes discussed in that bug into AJ,
> if we can agree on the best syntax.
>
> Andy
>
>
> 2010/3/17 Peter Kvokacka <kvokacka@xxxxxxxxx>:
>
>
> Hello guys
>
> I'm struggling with writing pointcut in aspectj for any method of any type
> except those of anonymous inner classes.
>
> Does anybody know what should I add to
>     pointcut all() : execution(* *.*(..))
> if I want to exclude methods of anonymous inner classes?
>
> I can't google it out. I found only this
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=73050#c6
>
> Adrian Colyer 2005-11-04 08:15:01 EST
>
> I've just commited the fix for this and it will be available in the next
> published build. Anonymous types are
> now only matched by the name pattern "*" - any other pattern fails to match
> since anonymous types are
> treated as having no name.
>
> so I tried:
>     pointcut all() : execution(* *.*(..)) && !within(*.*)
>
> which surprisingly works but only for types that are in same package as
> aspect is.
>
> Peter
>
> _______________________________________________
> 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