Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Bug Collection + declare parents?

Hello everyone,

I found a situation where i get a ClassCastException that might not be thrown.
With declare parents I force a class to implement an interface, in an aspect i put an object of this class in a collection of the interface's type. And everythig works fine thus far.
Then when i extract the object from the collection i get a ClassCastException.


Here's an example that illustrates what i'm describing you.

I've a simple interface:

public interface MyInterface {
void myMethod();
}

and a class that i force to implement it by declare parents.

public class ClassImplementingInterf {
//if the the interface methods are present in the implementing class there's no cast error
/* 
public void myMethod(){ //NB: commented, not present.
System.out.println("myMethod");
}
*/
}

then I have an abstract aspect:

public abstract aspect MyAspect {
private ArrayList<MyInterface> x = new ArrayList<MyInterface>();
abstract pointcut interfacemethod();
abstract pointcut mypointcut();

void around(): mypointcut(){
ClassImplementingInterf obj = new ClassImplementingInterf();
x.add(obj); //working
MyInterface var = (MyInterface) obj; //working
ClassImplementingInterf fromListWithCast = (ClassImplementingInterf) x.get(0); //working
System.out.println("Just before MyInterface fromLst = x.get(0)");
MyInterface fromLst = x.get(0); //ClassCastException

System.out.println("**** intercepted OtherClass.method ***** "); //not reached!
}
void around(): interfacemethod(){
System.out.println("**** intercepted interface method **** ");
}
}

and it's concrete aspect:

public aspect ConcreteAspect extends MyAspect {
declare parents: ClassImplementingInterf implements MyInterface;

pointcut mypointcut(): call(public void OtherClass.method(int));
pointcut interfacemethod(): call(void *.*.myMethod());
}

and an other class (useless, i use it only for intercept the method)
public class OtherClass {
public void method(int n){
System.out.println(n*2);
}
}

The main class is:
public class ClassMain {
public static void main(String[] args) {
OtherClass o = new OtherClass();
MyInterface ci = new ClassImplementingInterf();
ci.myMethod(); //this works!! the class can act like it has the interface!
o.method(15);
}
}


As you can see i can put an ClassImplementingInterf instance in a MyInterface variable or collection, and I can also cast it to MyInterface.
But when extracting it from a collection it doesn't work.

I think there's some kind of reflective controls when extracting an element from a collection that fails when i do it.
Sure enough if i add to ClassImplementingInterf a method that has the same signature of the interface the ClassCastException disappears and everything works fine.

I've attached to this message a .rar package that contains my code.

Best Regards,
--
Luca Savoja.

Attachment: provaBug.rar
Description: application/rar


Back to the top