So – I actually found one way to do
it with the class name. Not sure if this is right or not – I just started
learning this last night K
How efficient is this
call thisJoinPoint.getSignature().getDeclaringTypeName()?
Feels as though
the thisJoinPoint.getSignature() would be quite expensive .... is it?
pointcut cache(): call(INamingSessionCache+.new(..));
private HashMap map = new HashMap();
Object around (): cache()
{
String
typeName = thisJoinPoint.getSignature().getDeclaringTypeName();
// cache would
typically be a session based cache not a global cache
Object
reference = map.get(typeName);
if(reference != null)
{
System.out.println("found reference
in cache: "+
reference);
return reference;
}
else
{
try
{
reference
= proceed();
}
catch(Exception e)
{
// log the exception ... not to stderr tho
e.printStackTrace(System.err);
// what do I do with
this?
// throw
new SoftException(e);
return null;
}
System.out.println("Created object:
"+
reference);
map.put(typeName,
reference);
}
return reference;
}
cheers,
Jody
-----Original Message-----
From:
aspectj-users-admin@xxxxxxxxxxx [mailto:aspectj-users-admin@xxxxxxxxxxx] On Behalf Of Jody Brownell
Sent: Thursday, October 21, 2004
12:53 PM
To: aspectj-users@xxxxxxxxxxx
Subject: RE: [aspectj-users]
trying to write a simple caching aspect
I wouldn't try to do
something like this with a constructor -- that would severely change the
semantics of "new", which the caller expects to always return a new
instance of something.
JB> That is what the
tag interface is for (ITransparentSessionCache) – the developer MUST
subscribe explicitly to this caching functionality.
if we force the use of
factories – we would have to develop a significant amount of code just to
use aspectj.
I seen the factory
example in the aspectj in action
(excellent book BTW!!!!)... I am trying to avoid that if I can – I need
something a little more
seamless... at least
until we are through the prototyping/POC stages. Caching on construction would
be a HUGE bonus for us.
I do agree with you
though it could be quite dangerous.... but extremely elegant at the same
time... IMO.
-----Original Message-----
From:
aspectj-users-admin@xxxxxxxxxxx [mailto:aspectj-users-admin@xxxxxxxxxxx] On Behalf Of Scott Stanchfield
Sent: Thursday, October 21, 2004
12:29 PM
To: aspectj-users@xxxxxxxxxxx
Subject: RE: [aspectj-users]
trying to write a simple caching aspect
Not sure
how to do something like this with AspectJ, but it raised a red flag for
me:
I
wouldn't try to do something like this with a constructor -- that would
severely change the semantics of "new", which the caller expects to
always return a new instance of something.
If you
had some factory methods that you added caching to, this would be fine (again,
depending on the semantics of the methods), but be careful how things are
documented. If the factory method guarantees a new object (which is what
"new" does), you shouldn't change the semantics like this, as the
caller code may have been written to rely on this behavior.
(This is
one of the things that scares me about AspectJ...)
-- Scott
From: aspectj-users-admin@xxxxxxxxxxx
[mailto:aspectj-users-admin@xxxxxxxxxxx] On
Behalf Of Jody Brownell
Sent: Thursday, October 21, 2004
11:18 AM
To: aspectj-users@xxxxxxxxxxx
Subject: [aspectj-users] trying to
write a simple caching aspect
Hi – I am
new to aspectj, I am working through some example and trying to develop a few
aspects as a proof of concept.
The first aspect I am trying to develop is a real
simple caching aspect which caches objects based on the implementation of a tag
interface.
What I would like to do is when a constructor is
called, check if the class implements an interface, if it does check the cache.
If there is
no reference in the cache, proceed, cache the object
and then return it. Otherwise return the cached instance.
What I need to be able to do is get the name of the
class the constructor is being called on. My pointcut should really select all
constructors in the system (maybe confined to a
package) with any number of arguments. Once I have the class object or the
name,
I can determine if the cache should be consulted.
Is this possible? If so could someone provide me with
an example of the pointcut and advice. I would provide what I have
already but I
am stumped as to how I can capture class of the object
I am trying to instantiate. My problem is I would like to capture the class as
context in
an around advice using a target() like construct.
Thanks in advance.
Jody b.