Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » how to dynamically create a instance of a class in eclipse plugin
how to dynamically create a instance of a class in eclipse plugin [message #333805] Tue, 06 January 2009 20:40 Go to next message
Hao Missing name is currently offline Hao Missing nameFriend
Messages: 115
Registered: July 2009
Senior Member
I have some code which runs in J2SE well for dynamically loading an
instance of a class but it does not work in eclipse plugin. What I can do
to make the code work in eclipse too.
In J2SE, the code is as

ClassLoader loader = getClass().getClassLoader();

Class c = null;
try {
c = loader.loadClass(className);
} catch (ClassNotFoundException cnfe) {
throw new ParsingException("couldn't load class " + className,
cnfe);
}
try {
instance = c.newInstance();
} catch (InstantiationException ie) {
throw new ParsingException("couldn't instantiate " + className
+ " with empty constructor", ie);
} catch (IllegalAccessException iae) {
throw new ParsingException("couldn't get access to instance " +
"of " + className, iae);
}

Can I still use newInstance() method?

thanks
hao
Re: how to dynamically create a instance of a class in eclipse plugin [message #333806 is a reply to message #333805] Tue, 06 January 2009 20:45 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Is newInstance or loadClass failing?

It's very likely the the OSGi-ClassLoader in your plug-in is not seeing
the class you want to load. Once loaded newInstance should work.

Tom

Hao schrieb:
> I have some code which runs in J2SE well for dynamically loading an
> instance of a class but it does not work in eclipse plugin. What I can
> do to make the code work in eclipse too.
> In J2SE, the code is as
>
> ClassLoader loader = getClass().getClassLoader();
>
> Class c = null;
> try {
> c = loader.loadClass(className);
> } catch (ClassNotFoundException cnfe) {
> throw new ParsingException("couldn't load class " + className,
> cnfe);
> }
> try {
> instance = c.newInstance();
> } catch (InstantiationException ie) {
> throw new ParsingException("couldn't instantiate " + className
> + " with empty constructor", ie);
> } catch (IllegalAccessException iae) {
> throw new ParsingException("couldn't get access to instance " +
> "of " + className, iae);
> }
>
> Can I still use newInstance() method?
>
> thanks
> hao
>


--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Re: how to dynamically create a instance of a class in eclipse plugin [message #333811 is a reply to message #333806] Wed, 07 January 2009 01:40 Go to previous messageGo to next message
Hao Missing name is currently offline Hao Missing nameFriend
Messages: 115
Registered: July 2009
Senior Member
>Is newInstance or loadClass failing?

>It's very likely the the OSGi-ClassLoader in your plug-in is not seeing
>the class you want to load. Once loaded newInstance should work.

class loading failed. How could I make sure OSGi-ClassLoader finds the
class?

thanks
hao
Re: how to dynamically create a instance of a class in eclipse plugin [message #333812 is a reply to message #333811] Wed, 07 January 2009 01:51 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
You need to add the plugin / bundle which holds it as a required-bundle
or add an import-package declaration to your MANIFEST.MF.

Tom

Hao schrieb:
>> Is newInstance or loadClass failing?
>
>> It's very likely the the OSGi-ClassLoader in your plug-in is not seeing
>> the class you want to load. Once loaded newInstance should work.
>
> class loading failed. How could I make sure OSGi-ClassLoader finds the
> class?
>
> thanks
> hao
>


--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Previous Topic:Feature-based product and specifying what plug-ins to include
Next Topic:TextEditor (sourceViewer) in a view
Goto Forum:
  


Current Time: Thu Apr 25 16:10:43 GMT 2024

Powered by FUDForum. Page generated in 0.03402 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top