[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
RE: [aspectj-users] around advice for constructor pointcut
|
Nilesh,
It sounds like you are looking for a way to do caching (i.e., return an
existing object and not create a new one). For that, you can use AspectJ's
polymorphic Object return in around advice. E.g.,
aspect Cache {
pointcut constructingTypeInFoo() : call(com.foo..*.new(..))
Object around() : constructingTypeInFoo() {
Object cached = getCache(thisJoinPoint.getSignature(),
thisJoinPoint.getArgs());
if (cached == null) {
cached = proceed();
putCache(thisJoinPoint.getSignature(), thisJoinPoint.getArgs(),
cached);
}
return cached;
}
Object getCache(Object key1, Object key2) { /* use map with
concatenated key */ }
void putCache(Object key1, Object key2, Object value) { /* use map with
concatenated key */ }
Map map = new ConcurrentHashMap();
}
Note that advising AspectJ's constructor call like this will allocate a
garbage object that won't be constructed if there's a cache hit (which will
be eligible for GC). See also Adrian's blog entries for more caching with
aspects:
http://www.aspectprogrammer.org/blogs/adrian/2004/06/implementing_ca.html
and
http://www.aspectprogrammer.org/blogs/adrian/2004/06/implementing_ca_1.html
If I misunderstood and you are trying to enforce not creating objects as a
policy, you could use:
import pkg.BadType;
aspect Policy {
declare error: execution(BadType.new(..)): "don't construct a bad type
EVER";
}
or
aspect Policy {
declare error: call(BadType+.new(..)) && within(foo..*):
"don't construct a bad type or any subtype from within foo or any
subpackage";
}
or even
aspect Policy {
before() : execution(new(..)) && this(BadType) {
throw new IllegalStateException("Should never be constructing a bad
type");
}
}
-----Original Message-----
From: aspectj-users-bounces@xxxxxxxxxxx
[mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Nilesh Jain
Sent: Tuesday, January 17, 2006 9:43 PM
To: aspectj-users@xxxxxxxxxxx
Subject: [aspectj-users] around advice for constructor pointcut
Hi ,
I need to prevent creation of an object using aspect. For this I have
declared a pointcut with constructor signature. But I cannot use that
pointcut with around advice because around advice need us to specify
a return type. Please help me out this. Is there another way to
prevent construction of an object?
Thanks.
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users