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?

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




Back to the top