commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From radone <debianu...@seznam.cz>
Subject Javaflow - cannot be cast
Date Tue, 15 Jul 2008 08:09:51 GMT

Greetings,
I have the following "HelloWorld"-like example of javaflow where the class
implements interface IRunnable:
#########################################
package my.test;
public interface IRunnable extends Runnable {
}
#########################################
package my.test;
import java.net.URL;
import org.apache.commons.javaflow.Continuation;
import org.apache.commons.javaflow.ContinuationClassLoader;

public class MyRunnable implements IRunnable {
	public void run() {
		System.out.println("started!");
		for (int i = 0; i < 10; i++) {
			echo(i);
		}
	}

	public void echo(int x) {
		System.out.println(x);
		Continuation.suspend();
	}

	public static void main(String[] args) {
		try {
			final ContinuationClassLoader cl = new ContinuationClassLoader(
					new URL[] { new URL("file://bin") }, MyRunnable.class
							.getClassLoader());

			cl.addLoaderPackageRoot("my.test");
			final Class clz = cl.loadClass("my.test.MyRunnable");
			Runnable o = (Runnable) clz.newInstance();

			Continuation c = Continuation.startWith(o);
			System.out.println("returned a continuation");
			while (c != null) {
				System.out.println("Pozastaveno");
				c = Continuation.continueWith(c);
			}
			System.out.println("Ukonceno");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
##################################################

When I create class such as:
    Runnable o = (Runnable) clz.newInstance();
everything goes right. But when the type is changed to IRunnable:
    IRunnable o = (Runnable) clz.newInstance();

I am getting:
java.lang.ClassCastException: my.test.MyRunnable cannot be cast to
my.test.IRunnable
	at my.test.MyRunnable.main(MyRunnable.java:29)

To see class rewritten by ClassLoader I added logger which prints the class
bytecode:

public class my/test/MyRunnable implements my/test/IRunnable
org/apache/commons/javaflow/bytecode/Continuable  {
  // compiled from: MyRunnable.java
}

public abstract interface my/test/IRunnable implements java/lang/Runnable
org/apache/commons/javaflow/bytecode/Continuable  {
  // compiled from: IRunnable.java
}


public abstract interface java/lang/Runnable {
  public abstract run()V
}

I guess it has something to do with class rewriting and thus
my.test.IRunnable is not possible to cast to rewriten my.test.IRunnable.
Has anyone any suggestion how to deal with it? 

-- 
View this message in context: http://www.nabble.com/Javaflow---cannot-be-cast-tp18460050p18460050.html
Sent from the Commons - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message