Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ClassCastException: Class to Interface casting 1

Status
Not open for further replies.

russland

Programmer
Jan 9, 2003
315
CH
Hi,

I tried to use an interface to enforce the plugin developers to implement the function "initialize(Composite parent, int style)".

Interface:
Code:
import org.eclipse.swt.widgets.Composite;

interface IPlugin {
	void initialize(Composite parent, int style);
}

a Test-Plugin is this:
Code:
public class Loader extends Object implements IPlugin{

	//a nullary constructor. mandatory for casting!
	public Loader() {	
	}	
	public void initialize(Composite parent, int style){
		FlashCardsPlugin fcp = new FlashCardsPlugin(parent, style);	
	}
}

Now, the main application, which intends to load the plugin, needs to cast the successfully loaded class (fqcn). I wanted to do this with an interface. From what I've read, this is recommendable and good practice.
Code:
...
LocalClassLoader lcl=new LocalClassLoader(jar);		
String strClass = "com.brayan.projects.icda.apps.flashcards.Loader";
Class<?> c = lcl.loadClass(strClass);
[red]IPlugin plugin = (IPlugin)c.newInstance();[/red]

Class loading passes fine (no exceptions). Then the casting with IPlugin fails with a java.lang.ClassCastException. No additional error text/hints. Why does that casting request fail?

Thanks heaps for any hints!


 
ClassLoader problems, not nice. Why don't you just instatiate the class with the new operator?

Cheers,
Dian
 
Because with Plugins you don't know the class names (caution!!! I hardcoded the fqcn ONLY for test purpose. I want to fetsch it later dynamically.

Thanks anyway for your response.
 
Is that LocalClassLoader included on the hierarchy? I think the problem is you're loading a class with a classloader and casting it to another one

Cheers,
Dian
 
Hm, I didn't think of that. I mean the IPlugin of the Plugin is, OF COURSE, in a different package path than the main application. Also it has its own IPlugin interface in that very same package path. The other IPlugin, the one I use for casting, is stored in the main application. However, they are syntactically identical (copied and pasted the file). Does that really matter? If yes, how do I get around this 2 identical but distributed files? (of course, I'd love to have it just once, but how else could I enforce the plugin developer to apply the methods I need (contracting)?

Any clue and experience with that?
 
Humm, that's a different problem then.

You have two different IPlugin interfaces. You can't use one to implement and another one to cast unless the first one inherits from the second

Cheers,
Dian
 
ow. Well, if that is a definite answer, than I say thanks! hm... jjj I've got no clue how the main and plugin application should read from the same Interface in order to cast. That seems to oppose the idea of reusability. If you can think of an approach, I'm happy to hear.

Thanks for your responses.
 
Should be something like

Code:
import org.eclipse.swt.widgets.Composite;

interface IPlugin extends main.application.IPlugin {
    void initialize(Composite parent, int style);
}

Btw, why isn't jar plugin included in the main application?

Cheers,
Dian
 
ow. given the entire fqcn. I think I should try to dynamicly load the IPlugin from the plugin jar.

The plugin jar included in the main application??? It's a plugin. I want people to develope plugins for my main application. I don't care what they programm, as long as they give me a name (that I will display in the application head), which I want to enforce (later) with the IPlugin interface.

hm, tricky. I'll try to checkout some development patterns.
 
But you can try the Eclipse way. The main application starts and loads all jar plugins into its classpath, so there's no problem when trying to invoke classes

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top