Eclipse Community Forums - RDF feed
https://www.eclipse.org/forums/
Eclipse Community ForumsWhy reflection doesn't work in rap
https://www.eclipse.org/forums/index.php/mv/msg/160629/507596/#msg_507596
In rap, I try to use reflection to load a class by its classpath and classname. When I test it in junit, it work good. But when I call this method from UI, it always give me a ClassCastException
My code:
/** The reference to a data processing object, is loaded as an Algorithm (see RunAlgorithm) */
protected DataProcessing dataProcessing;
/** Reference to class of Algorithm used in this activity */
private Class<? extends DataProcessing> dpClass;
Exception is :
java.lang.ClassCastException: ianalyst.algorithm.dataprocessing.rowoperation.Sampling cannot be cast to ianalyst.algorithm.dataprocessing.DataProcessing
..............
............
algorithmLoader extend from URLClassLoader.
Sampling extend from DataProcessing.
Can some one give me some idea, thank you very much
]]>jason2010-01-14T00:59:03-00:00Re: Why reflection doesn't work in rap
https://www.eclipse.org/forums/index.php/mv/msg/160629/507611/#msg_507611
jason2010-01-14T04:27:32-00:00Re: Why reflection doesn't work in rap
https://www.eclipse.org/forums/index.php/mv/msg/160629/507635/#msg_507635
bundle has its own class loader, this is probably what causes the trouble.
If you are working with class loaders, make sure that you understand the
technologies that are involved [1]. Searching for "osgi class loading"
should bring up enough material.
The reason why the unit tests are working is that they are executed as a
plain Java application (no OSGi involved).
On 14.01.2010 01:59, chen_jiahang@hotmail.com wrote:
> Hi,
> In rap, I try to use reflection to load a class by its classpath and
> classname. When I test it in junit, it work good. But when I call this
> method from UI, it always give me a ClassCastException
>
> My code:
>
> /** The reference to a data processing object, is loaded as an Algorithm
> (see RunAlgorithm) */
> protected DataProcessing dataProcessing;
>
> /** Reference to class of Algorithm used in this activity */
> private Class<? extends DataProcessing> dpClass;
>
> AlgorithmLoader algorithmLoader = new AlgorithmLoader();
> algorithmLoader.addPath(dpClassPath);
> dpClass = (Class<? extends DataProcessing>)
> algorithmLoader.loadClass(dpClassName);
> dataProcessing = dpClass.newInstance();
>
> Exception is : java.lang.ClassCastException:
> ianalyst.algorithm.dataprocessing.rowoperation.Sampling cannot be cast
> to ianalyst.algorithm.dataprocessing.DataProcessing
> ..............
> ............
>
> algorithmLoader extend from URLClassLoader.
> Sampling extend from DataProcessing.
>
>
> Can some one give me some idea, thank you very much
>]]>Rüdiger Herrmann2010-01-14T08:23:52-00:00