commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Juozas Baliuka" <bali...@centras.lt>
Subject Re: [lang] reflect - Class.forName
Date Sun, 24 Nov 2002 07:48:33 GMT

This way is "evil" too:

1. MyClass implements MyInterface
2. MyClass is visible for Thread.currentThread().getContextClassLoader();
3. MyInterface.class.getClassLoader() is not "parent" of
Thread.currentThread().getContextClassLoader();
4. MyInterface.class.getClassLoader() !=
Thread.currentThread().getContextClassLoader();

MyInterface var = null;
Class cls = Thread.currentThread().getContextClassLoader.loadClass(
"MyClass" );
var = (MyInterface)cls.newInstance();

the last line will throw ClassCastExeption.

----- Original Message -----
From: "Henri Yandell" <bayard@generationjava.com>
To: "Jakarta Commons Developers List" <commons-dev@jakarta.apache.org>
Sent: Sunday, November 24, 2002 4:17 AM
Subject: [lang] reflect - Class.forName


>
> I've not really been paying attention to the reflect stuff, and had missed
> a question about it in a blog I usually read:
>
> http://radio.weblogs.com/0112098/2002/09/24.html#a114
>
> Rather than using:
>
> Class theClass = Class.forName( className );
>
> in reflect.ReflectionUtils, James suggests we should use:
>
> ****
> Class theClass = null;
>
> try {
>  theClass = Thread.currentThread().getContextClassLoader().loadClass(
> className );
> } catch (ClassNotFoundException e) {
>  theClass = getClass().getClassLoader().loadClass( className );
> }
> ****
>
> as "Class.forName is evil".
>
> Any views on this??
>
> Hen
>
>
> --
> To unsubscribe, e-mail:
<mailto:commons-dev-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
<mailto:commons-dev-help@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message