Problem loading a class from plugin code; ClassCastException

Hello,

i'm new in developing a plugin and i try to take over the scrappy plugin. After adapt the API changes this plugin runs with IDEA 4.0. But with 4.1 (Pallada) it doesn't. The log is telling me that the generated scrap class can not be casted and i receive a ClassCastException.

I wrote a little test:

public static Class loadClassFromFile() {
File file = new File("C:
work
strug
develop
examples
scrappyAlt
classes
");

try {
URL url = file.toURL();
URL[] urls = new URL[]{url};

// Create a new class loader with the directory
ClassLoader cl = new URLClassLoader(urls);

Class cls = cl.loadClass("bytecrafters.ScrapPiece");
return cls;
}
catch (Exception e) {
System.err.println( "Exception" + e);
}
return null;
}


When using this in a main class the message is printed:

public static void main(String[] args) throws Exception {
Class cls = loadClassFromFile();
ScrapPiece obj = (ScrapPiece) cls.newInstance();
System.out.println( "loadClassFromFile - message = " + obj.getMessage() );
}

When i'm loading this class from the plugin code and try the cast it, then i receive a ClassCastException. Did anyone have a idea what my problem is? Here is the exception and the ScrapPiece class:

2004-04-26 09:59:27,997 INFO - bytecrafters.scrappy.Scrappy -
java.lang.ClassCastException
at bytecrafters.scrappy.ExecuteIt.createScrap(ExecuteIt.java:114)
at bytecrafters.scrappy.ExecuteIt.executeIt(ExecuteIt.java:61)
at bytecrafters.scrappy.Scrappy.keyPressedEvent(Scrappy.java:226)
at bytecrafters.scrappy.Scrappy.access$600(Scrappy.java:12)
at bytecrafters.scrappy.Scrappy$11.keyPressed(Scrappy.java:167)
at java.awt.Component.processKeyEvent(Component.java:5058)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2388)
at java.awt.Component.processEvent(Component.java:4909)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1713)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:627)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:831)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:741)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:592)
at java.awt.Component.dispatchEventImpl(Component.java:3506)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at com.intellij.ide.p.b(p.java:38)
at com.intellij.ide.p.a(p.java:47)
at com.intellij.ide.p.dispatchEvent(p.java:134)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)


ScrapPiece:

package bytecrafters;

public class ScrapPiece {
public String getMessage() {
return "getMessage - message";
}
}

Any help would be appreciated. Thanks in advance, Leif

2 comments
Comment actions Permalink

Looks like this ScrapPiece class is available to both PluginClassLoader (created by IDEA for your plugin) and the URLClassLoader
created by your code. So there are 2 classes with the same name "ScrapPiece" loaded by 2 different classloaders. The classes are not
castable because they have different runtime types. In order to fix the problem the loader should be created like this:

ClassLoader cl = new URLClassLoader(urls, MyClass.class.getClassLoader());

where "MyClass" is the class where the "loadClassFromFile()" method is declared.

Alternative is to make the ScrapPiece class available to only one of the loaders.

--
Best regards,
Eugene Zhuravlev
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"


"Hanack" <no_mail@jetbrains.com> wrote in message news:14458488.1082968205866.JavaMail.itn@is.intellij.net...

Hello,

>

i'm new in developing a plugin and i try to take over the scrappy plugin. After adapt the API changes this plugin runs with IDEA

4.0. But with 4.1 (Pallada) it doesn't. The log is telling me that the generated scrap class can not be casted and i receive a
ClassCastException.
>

I wrote a little test:

>

public static Class loadClassFromFile() {
File file = new File("C:
work
strug
develop
examples
scrappyAlt
classes
");

>

try {
URL url = file.toURL();
URL[] urls = new URL[]{url};

>

// Create a new class loader with the directory
ClassLoader cl = new URLClassLoader(urls);

>

Class cls = cl.loadClass("bytecrafters.ScrapPiece");
return cls;
}
catch (Exception e) {
System.err.println( "Exception" + e);
}
return null;
}

>
>

When using this in a main class the message is printed:

>

public static void main(String[] args) throws Exception {
Class cls = loadClassFromFile();
ScrapPiece obj = (ScrapPiece) cls.newInstance();
System.out.println( "loadClassFromFile - message = " + obj.getMessage() );
}

>

When i'm loading this class from the plugin code and try the cast it, then i receive a ClassCastException. Did anyone have a idea

what my problem is? Here is the exception and the ScrapPiece class:
>

2004-04-26 09:59:27,997 INFO - bytecrafters.scrappy.Scrappy -
java.lang.ClassCastException
at bytecrafters.scrappy.ExecuteIt.createScrap(ExecuteIt.java:114)
at bytecrafters.scrappy.ExecuteIt.executeIt(ExecuteIt.java:61)
at bytecrafters.scrappy.Scrappy.keyPressedEvent(Scrappy.java:226)
at bytecrafters.scrappy.Scrappy.access$600(Scrappy.java:12)
at bytecrafters.scrappy.Scrappy$11.keyPressed(Scrappy.java:167)
at java.awt.Component.processKeyEvent(Component.java:5058)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2388)
at java.awt.Component.processEvent(Component.java:4909)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1713)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:627)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:831)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:741)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:592)
at java.awt.Component.dispatchEventImpl(Component.java:3506)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at com.intellij.ide.p.b(p.java:38)
at com.intellij.ide.p.a(p.java:47)
at com.intellij.ide.p.dispatchEvent(p.java:134)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

>
>

ScrapPiece:

>

package bytecrafters;

>

public class ScrapPiece {
public String getMessage() {
return "getMessage - message";
}
}

>

Any help would be appreciated. Thanks in advance, Leif



0
Comment actions Permalink

I think the problem with your code is that you have to class instances of the same class (ScrapPiece) - one loaded the
current ClassLoader and the other loaded with created URLClassLoader.
You should pass the current ClassLoader into the URLClassLoader constructor.

Regards,
Stefan.

Hanack wrote:

Hello,

i'm new in developing a plugin and i try to take over the scrappy plugin. After adapt the API changes this plugin runs with IDEA 4.0. But with 4.1 (Pallada) it doesn't. The log is telling me that the generated scrap class can not be casted and i receive a ClassCastException.

I wrote a little test:

public static Class loadClassFromFile() {
File file = new File("C:
work
strug
develop
examples
scrappyAlt
classes
");

try {
URL url = file.toURL();
URL[] urls = new URL[]{url};

// Create a new class loader with the directory
ClassLoader cl = new URLClassLoader(urls);

Class cls = cl.loadClass("bytecrafters.ScrapPiece");
return cls;
}
catch (Exception e) {
System.err.println( "Exception" + e);
}
return null;
}


When using this in a main class the message is printed:

public static void main(String[] args) throws Exception {
Class cls = loadClassFromFile();
ScrapPiece obj = (ScrapPiece) cls.newInstance();
System.out.println( "loadClassFromFile - message = " + obj.getMessage() );
}

When i'm loading this class from the plugin code and try the cast it, then i receive a ClassCastException. Did anyone have a idea what my problem is? Here is the exception and the ScrapPiece class:

2004-04-26 09:59:27,997 INFO - bytecrafters.scrappy.Scrappy -
java.lang.ClassCastException
at bytecrafters.scrappy.ExecuteIt.createScrap(ExecuteIt.java:114)
at bytecrafters.scrappy.ExecuteIt.executeIt(ExecuteIt.java:61)
at bytecrafters.scrappy.Scrappy.keyPressedEvent(Scrappy.java:226)
at bytecrafters.scrappy.Scrappy.access$600(Scrappy.java:12)
at bytecrafters.scrappy.Scrappy$11.keyPressed(Scrappy.java:167)
at java.awt.Component.processKeyEvent(Component.java:5058)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2388)
at java.awt.Component.processEvent(Component.java:4909)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1713)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:627)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:831)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:741)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:592)
at java.awt.Component.dispatchEventImpl(Component.java:3506)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at com.intellij.ide.p.b(p.java:38)
at com.intellij.ide.p.a(p.java:47)
at com.intellij.ide.p.dispatchEvent(p.java:134)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)


ScrapPiece:

package bytecrafters;

public class ScrapPiece {
public String getMessage() {
return "getMessage - message";
}
}

Any help would be appreciated. Thanks in advance, Leif

0

Please sign in to leave a comment.