Skip to main content

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

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




Back to the top