[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[aspectj-users] Around, Constructors and ClassCastException
|
Title: Around, Constructors and ClassCastException
Hello,
I've looked a fair bit now for the answer to this question in the archives and the manuals, but I haven't seen anything yet. I am trying to see whether I can replace an object at runtime with a different implementation. I have had some success replacing subclasses, but I haven't been able to replace it with another implementation. I suspect a byte code limitation, but I thought I'd check here.
Essentially I'm trying to advise the following code …
Animal anAnimal = new SmallDog();
Where SmallDog implements Animal, and Animal is an interface that looks like …
public interface Animal {
void speak();
}
I was trying to replace the SmallDog() with a BigDog() with an aspect. I can do it if BigDog extends SmallDog, but being an Animal is insufficient for the aspect to work, I end up throwing a ClassCastException.
public aspect ConstructorReplaceCall {
public pointcut init(): call(test.*.new(..));
Object around(): init() {
if (someTestLogicIsTrue) {
return new BigDog(); // ok if BigDog extends SmallDog, not ok if BigDog only implements animal
} else{
return proceed();
}
}
}
Am I missing something or is this a fundamental 'limitation' of the Java bytecode that is generated ? (where limitation could mean you're insane to do otherwise :) )
Thanks in advance,
John.