dev@glassfish.java.net

Re: Classloading strategy for OSGi <was> Fwd: [Jersey] Jersey in Felix

From: Craig L Russell <Craig.Russell_at_Sun.COM>
Date: Tue, 06 Jan 2009 10:18:21 -0800

And don't forget that Class.forName is a secure method and depending
on what class you're trying to load, might need to be wrapped in a
doPrivileged block.

Craig

On Jan 6, 2009, at 8:15 AM, Paul Sandoz wrote:

>
> On Dec 8, 2008, at 4:36 PM, Eamonn McManus wrote:
>
>> Hi,
>>
>> Disclaimer: I know much more about class loading than I would like
>> to.
>>
>> The boolean parameter in question is usually unimportant. If the
>> class has already been loaded, the boolean makes no difference. If
>> this is the first time the class is being loaded, then its static
>> initializer will be run only if this parameter is true. But
>> otherwise the static initializer will be run anyway as soon as you
>> try to do pretty much anything with the Class that you have obtained.
>>
>
> Yes, i just ran into this which cause me a couple of hours head
> scratching.
>
> Given that Class.forName(className) defaults to
> Class.forName(className, true, currentLoader) then should not the
> code used to load the class be:
>
> ClassLoader cl = getContextClassLoader();
> if (cl != null) {
> try {
> clazz = Class.forName(name, true, cl);
> } catch (ClassNotFoundException ex) {
> clazz = Class.forName(name);
> }
> } else {
> clazz = Class.forName(name);
> }
>
> ?
>
> Paul.
>
>> I'm not sure I see the relation to 6500212, which is more about why
>> you should always use Class.forName(name, false, cl) rather than
>> cl.loadClass(name). The latter is not guaranteed to work for names
>> of array types, such as "[Ljava.lang.String;" (which is
>> String[].class.getName()). Recognizing array types is not part of
>> the contract of ClassLoader.loadClass, and in JDK 6 URLClassLoader
>> stopped recognizing them.
>>
>> Regards,
>> --
>> Éamonn McManus · JMX Spec Lead · http://weblogs.java.net/blog/emcmanus/
>>
>>
>> Roberto Chinnici wrote:
>>> Paul Sandoz wrote:
>>>> On Dec 4, 2008, at 9:23 AM, Roberto Chinnici wrote:
>>>>> clazz = Class.forName(name, false, cl);
>>>> ^
>>>> |
>>>> Why is this
>>>> "false" ?
>>> In short, true vs. false won't make a difference except in the
>>> case of a serialization framework trying to deserialize an array
>>> of a new type:
>>> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6500212
>>> And no, I do not understand all the details.
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe_at_glassfish.dev.java.net
>>> For additional commands, e-mail: dev-help_at_glassfish.dev.java.net
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe_at_glassfish.dev.java.net
>> For additional commands, e-mail: dev-help_at_glassfish.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: dev-help_at_glassfish.dev.java.net
>

Craig L Russell
Architect, Sun Java Enterprise System http://db.apache.org/jdo
408 276-5638 mailto:Craig.Russell_at_sun.com
P.S. A good JDO? O, Gasp!