Cannot cast to custom interface

Hi,

i've been dealing with a problem for a few days. Hope someone can help me.

The situation:

I want to visualise different types of sorting allgorithms (bubble sort, quick sort etc.) The program has to be modular. So, every algorithm implemets my custom interface SortModule.

The algorithms shall run as threads, so they extend Thread.

Since it has to be plugable, i use a custom class loader.

A controll class loads all the classes it can find from a given directory (these are all implementations of sorting algorithms. This works well), builds new instances of them, and stars the threads.

The problem:

I want to repaint my graphic due to a time period (e.g. every 300ms).

So i have to monitor the status of the array, which is being sorted by -for example BubbleSort), which means, i have to get this array from my BubleSort-Class. And i can't achive this, since it's not possible to cast the dynamic generated instance of the class neither in the interface SortModule nor in itself (BubleSort).

Here is some code of the application:

Enumeration en = this.selectedAllgorithms.elements();

while(en.hasMoreElements())

{

String className = en.nextElement().toString();

String classPath ="path to the class"+className;

CustomClassLoader ccl =new CustomClassLoader();

Class c = ccl.loadClass( classPath );

Constructor[] theConstructors = c.getConstructors();

Class[] parameterTypes = theConstructors[0].getParameterTypes();

Constructor cons = c.getConstructor(parameterTypes);

Object argumentsList[] =new Object[2];

argumentsList[0] =this;

argumentsList[1] = this.data;

Thread sm = (Thread)cons.newInstance(argumentsList);/* at this point

type cast in neither SortModule nor directly for ex. BubbleSort works:

SortModule sm = (SortModule)cons.newInstance(argumentsList); )*/

sm.start();

}

Interface SortModule:

publicinterface SortModule{

publicvoid swap (int idx1,int idx2);

publicvoid draw(int index,int wert);

publicvoid drawArray();

publicint[] getField();

}

I also tried this:

BubbleSort bs = (BubbleSort) cons.newInstance(argumentsList);

and the result was:

Exception in thread "Thread-2" java.lang.ClassCastException: ub4.util.BubbleSort cannot be cast to ub4.util.BubbleSort

at ub4.gui.Visualiser.run(Visualiser.java:140)

at java.lang.Thread.run(Unknown Source)

How can this be? Why can't i cast a wrapped BubbleSort into BubbleSort?

And why does this work?

Thread t = (Thread) cons.newInstance(argumentsList);

If it is not possible to find a solution for this type-casting problem; is it somehow possible to get the array of the BubbleSort class every 300ms? (while BubbleSort is running as "Thread ", i have no direct access to its fields. so int[] arr = t.field; would not work, or would it?)

Does anyone have an explanation for this and can maybe help me?

Thanx..

tufi

[3870 byte] By [tufia] at [2007-11-27 8:53:21]
# 1
Identical classes loaded by different classloaders are considered to be different classes by the JVM
georgemca at 2007-7-12 21:10:31 > top of Java-index,Core,Core APIs...
# 2

thanx for the rapid answer.

> Identical classes loaded by different classloaders

> are considered to be different classes by the JVM

great :-/

is there any possibility to load my (SortModule)classes by path+name without my customClassLoader, using the default ClassLoader ?

tufia at 2007-7-12 21:10:31 > top of Java-index,Core,Core APIs...
# 3
Class.forName()
ejpa at 2007-7-12 21:10:31 > top of Java-index,Core,Core APIs...
# 4
it worked. thanx..
tufia at 2007-7-12 21:10:31 > top of Java-index,Core,Core APIs...