You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by radone <de...@seznam.cz> on 2008/07/15 10:09:51 UTC

Javaflow - cannot be cast

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


Re: Javaflow - cannot be cast

Posted by radone <de...@seznam.cz>.

tcurdt wrote:
> 
>>
>> I am getting:
>> java.lang.ClassCastException: my.test.MyRunnable cannot be cast to
>> my.test.IRunnable
>> 	at my.test.MyRunnable.main(MyRunnable.java:29)
> 
> 
>> 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?
> 
> I guess it's more likely to be a class loading problem.
> 
> Print out the classloaders involved. That should give you more insights.
> 
> Who loaded IRunnable? Who loaded the instance?
> 
>   Object o = clz.newInstance();
>   System.out.println(o.getClass().getClassLoader());
>   IRunnable r = (IRunnable) o;
>   System.out.println(r.getClass().getClassLoader());
> 
> 
> cheers
> --
> Torsten
> 
> 

For anyone interested in the same problem - after some debugging and reading
the Javaflow ContinuationClassLoader code I found a solution. The only thing
what is neccessary to do is to exclude the base class from JavaFlow class
loader and force the loading by "parent" class loader. This can be done by
moving the IRunnable to different package and adding this package (e.g.
"my.excluded") as a system root: 

			final ContinuationClassLoader cl = new ContinuationClassLoader(
					new URL[] { new URL("file://bin") }, MyRunnable.class
							.getClassLoader());

			cl.addSystemPackageRoot("my.excluded");
			cl.addLoaderPackageRoot("my.test");


Thanks for help.
-- 
View this message in context: http://www.nabble.com/Javaflow---cannot-be-cast-tp18460050p18462363.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


Re: Javaflow - cannot be cast

Posted by Torsten Curdt <tc...@apache.org>.
>
> I am getting:
> java.lang.ClassCastException: my.test.MyRunnable cannot be cast to
> my.test.IRunnable
> 	at my.test.MyRunnable.main(MyRunnable.java:29)


> 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?

I guess it's more likely to be a class loading problem.

Print out the classloaders involved. That should give you more insights.

Who loaded IRunnable? Who loaded the instance?

  Object o = clz.newInstance();
  System.out.println(o.getClass().getClassLoader());
  IRunnable r = (IRunnable) o;
  System.out.println(r.getClass().getClassLoader());


cheers
--
Torsten


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