Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-dev] Context-dependent casting with aspects?

If you use around advice to implement your aspect, then casting should not
be an issue, i.e.

aspect Caching {
  CachedValue cachedX = new CachedValueImpl();

  Object around(): execution(* OriginalVersion.getValueX()) {
    // the around implementation will insert the correct cast here
    return cachedX.getValue();
  }

  Object around(Object val):
      execution(* OriginalVersion.setValueX(*)) && args(val)
  {
    return cachedX.putValue(val);
  }
}

This feature of around was designed to support your kind of problem so I'd
be surprised if it didn't work for you.

<dangerous-bend> There's an interesting type system issue here.  This
automatic casting in around means that the code above can generate a
ClassCastException at runtime even though the user didn't write a cast in
their code.  This and a related situation with the parameters to proceed are
the only places in AspectJ where Java's static type system is weakened by
AspectJ.  I've been told that Mitch Wand has written an interesting
theoretical paper on this issue, but I don't have the reference to hand.

At the time that we made this design decision we felt that it was so
important to enable the writing of this kind of aspect that it was worth
weakening the static type system in this localized way.  In the future I
hope we can improve this design a bit to make it clear to the user when
these runtime casts might be inserted without making it impossible to do so.
</dangerous-bend>

-Jim

-----Original Message-----
From: aspectj-dev-admin@xxxxxxxxxxx [mailto:aspectj-dev-admin@xxxxxxxxxxx]
On Behalf Of Wes Isberg
Sent: Wednesday, December 03, 2003 3:54 AM
To: aspectj-dev@xxxxxxxxxxx
Subject: Re: [aspectj-dev] Context-dependent casting with aspects?

Your question is nicely stated, and you're right that this
can't be done.  One alternative for user-defined types is to
augment the type itself with caching, or replace it with a
subclass that supports caching.  Another is to instead
externalize the caching factor by returning the cached value
from any field-get (or getter, if encapsulated) rather than
returning (or generating) the value.  Finally, you could
generate type-specific around advice that implements
your cast for all known types in your system, based on
the exact declared return type of your getter.

AspectJ might not be able to use your specific implementation,
but you might be able to implement caching differently
with AspectJ.

Wes


vbaggiol@xxxxxxxxxxxxx wrote:

> 
> Hi,
> I think sending an HTML message was not a good idea, so here it is again
in plain text.
> V.
> 
> ---
> 
> Hi,
> 
> I'm new to AOP, and I would like to start using it to introduce a caching
aspect on many classes of my project. I am afraid this is not possible,
because of the context-dependent casting I have to do.
> 
> Maybe a code snippet (below) shows best what I would like to do.
> 
> 1) Class OriginalVersion is the kind of code I have now, without caching.
The example shows a field of type "TypeX" (which could be any type) with a
getter and a setter methods.
> 2) CachedVersion is the kind of code I would like to obtain at the end.
The field is now a CachedValue, which I access from the getter and setter
methods.
> 
> The problem is, that I have to cast to (TypeX) in the getter method. For
every kind of variable type, I have to use a different cast: if valueX is of
type String, I have to cast to String, if it is of type Date, I have to
cast to date.
> 
> As far as I understand, I cannot o this with aspectj. I cannot write an
aspect that automatically casts to the right type.
> 
> Please tell me I'm wrong ;-)
> 
> Many thanks in advance for your help!
> Vito
> 
> public class OriginalVersion {
>   private TypeX valueX;
> 
>   public TypeX getValueX() { 
>     return valueX;
>    }
> 
>   public void setValueX(TypeX valueX) {
>     this.valueX = valueX;
>   }
> }
> 
> public class CachedVersion {
>   CachedValue cachedX = new CachedValueImpl();
> 
>   public TypeX getValueX() {
>     // the type-dependent cast is the problem
>     return (TypeX)cachedX.getValue();
>   }
> 
>   public void setValueX(TypeX valueX) {
>     cachedX.putValue(valueX);
>   }
> }
> 
> interface CachedValue {
>   public Object getValue();
>   public void putValue(Object valueX);
> }
> 
> Dr. Vito Baggiolini
> CERN AB/CO
> 1211 Geneva 23
> __________________________________________________
> Verpassen Sie keine eBay-Auktion und bieten Sie bequem
> und schnell über das Telefon mit http://www.telefonbieten.de
> 
> Ihre eMails auf dem Handy lesen - ohne Zeitverlust - 24h/Tag
> eMail, FAX, SMS, VoiceMail mit http://www.directbox.com
> 
> 
> _______________________________________________
> aspectj-dev mailing list
> aspectj-dev@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-dev
> 

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





Back to the top