You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Thomas Dudziak <to...@gmail.com> on 2005/09/05 23:05:26 UTC
[lang] enhanced version of Class.forName
Hi folks,
I had this problem that I needed to create class objects for things
like "int" and "boolean", eg. the type specification that you would
use for a variable declaration or method parameter.
Since the normal Class.forName does not handle this really well, and
commons-lang does not provide such a function (please correct me if
I'm wrong here), I implemented it myself.
Now I wonder, would this be useful enhancement to ClassUtils ? If so,
I could provide a patch in BugZilla.
regards,
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [lang] enhanced version of Class.forName
Posted by Thomas Dudziak <to...@gmail.com>.
On 9/6/05, James Carman <ja...@carmanconsulting.com> wrote:
> Do you think we need to go through the trouble of throwing an
> IllegalArgumentException if it's not a well-formed class name (starts with
> character, blah blah blah)? Or, can we just let it throw a
> ClassNotFoundException after doing as much as we can with the string that's
> passed in (i.e. transforming it into the proper format)?
I usually prefer to use different exceptions for different causes,
because makes handling them a lot easier.
Ie. a NullPointerException is perfectly fine if the supplied type spec
is null. Likewise a IllegalArgumentException seems fine if the type
spec is not a valid type declaration (ie. only brackets, different
number of opening and closing brackets, etc.). If it is syntactically
correct, then a ClassNotFoundException seems fine though.
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
RE: [lang] enhanced version of Class.forName
Posted by James Carman <ja...@carmanconsulting.com>.
So, you're saying that you'd rather return a null Class object from
getClass() when you pass in a null? I can do that, no problem, but
Class.forName() actually throws an NPE for that scenario. Not that we
should try to mimic exactly what they did (otherwise we wouldn't have to
write this method), but we might want to be consistent. I don't know.
-----Original Message-----
From: Stephen Colebourne [mailto:scolebourne@btopenworld.com]
Sent: Tuesday, September 06, 2005 4:25 PM
To: Jakarta Commons Developers List
Subject: Re: [lang] enhanced version of Class.forName
James Carman wrote:
> Well, what does the [lang] "team" think about this approach (just letting
> Class.forName() throw the ClassNotFoundException)? Does this work for you
> guys? I have added a patch to issue 36512 which includes code to
implement
> it this way (my latest patch). Are there any votes against this method
> being implemented this way? If not, do you guys care if I go ahead and
> commit it (I'll wait a while for votes)? Since I'm not a "normal" [lang]
> committer, I don't want to step on anyone's toes.
You are welcome to check it in, but you will need to add your name to
the [lang] committers in project.xml. Other committers will then review
it :-)
For reference, lang strives to avoid NPE, so typically I would want a
null input to yield a null output.
Stephen
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [lang] enhanced version of Class.forName
Posted by Stephen Colebourne <sc...@btopenworld.com>.
James Carman wrote:
> Well, what does the [lang] "team" think about this approach (just letting
> Class.forName() throw the ClassNotFoundException)? Does this work for you
> guys? I have added a patch to issue 36512 which includes code to implement
> it this way (my latest patch). Are there any votes against this method
> being implemented this way? If not, do you guys care if I go ahead and
> commit it (I'll wait a while for votes)? Since I'm not a "normal" [lang]
> committer, I don't want to step on anyone's toes.
You are welcome to check it in, but you will need to add your name to
the [lang] committers in project.xml. Other committers will then review
it :-)
For reference, lang strives to avoid NPE, so typically I would want a
null input to yield a null output.
Stephen
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
RE: [lang] enhanced version of Class.forName
Posted by James Carman <ja...@carmanconsulting.com>.
Well, what does the [lang] "team" think about this approach (just letting
Class.forName() throw the ClassNotFoundException)? Does this work for you
guys? I have added a patch to issue 36512 which includes code to implement
it this way (my latest patch). Are there any votes against this method
being implemented this way? If not, do you guys care if I go ahead and
commit it (I'll wait a while for votes)? Since I'm not a "normal" [lang]
committer, I don't want to step on anyone's toes.
-----Original Message-----
From: Emmanuel Bourg [mailto:ebourg@apache.org]
Sent: Tuesday, September 06, 2005 11:04 AM
To: Jakarta Commons Developers List
Subject: Re: [lang] enhanced version of Class.forName
James Carman wrote:
> Do you think we need to go through the trouble of throwing an
> IllegalArgumentException if it's not a well-formed class name (starts with
> character, blah blah blah)? Or, can we just let it throw a
> ClassNotFoundException after doing as much as we can with the string
that's
> passed in (i.e. transforming it into the proper format)?
I was going to make this suggestion, that seems good enough to me.
Emmanuel Bourg
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [lang] enhanced version of Class.forName
Posted by Emmanuel Bourg <eb...@apache.org>.
James Carman wrote:
> Do you think we need to go through the trouble of throwing an
> IllegalArgumentException if it's not a well-formed class name (starts with
> character, blah blah blah)? Or, can we just let it throw a
> ClassNotFoundException after doing as much as we can with the string that's
> passed in (i.e. transforming it into the proper format)?
I was going to make this suggestion, that seems good enough to me.
Emmanuel Bourg
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
RE: [lang] enhanced version of Class.forName
Posted by James Carman <ja...@carmanconsulting.com>.
Do you think we need to go through the trouble of throwing an
IllegalArgumentException if it's not a well-formed class name (starts with
character, blah blah blah)? Or, can we just let it throw a
ClassNotFoundException after doing as much as we can with the string that's
passed in (i.e. transforming it into the proper format)?
-----Original Message-----
From: Thomas Dudziak [mailto:tomdzk@gmail.com]
Sent: Tuesday, September 06, 2005 10:39 AM
To: Jakarta Commons Developers List
Subject: Re: [lang] enhanced version of Class.forName
On 9/6/05, James Carman <ja...@carmanconsulting.com> wrote:
> 1. Well, it's not exactly the same thing. I'm talking about when they
pass
> in just "boolean" or "int". In that case, we construct the name "[B" or
> "[I" and pass that to Class.forName() with the "initialize" parameter,
> thereby telling it whether to initialize the array class and not
> (necessarily) the component type class.
If I understand the VM spec correctly, the initialization is
recursively applied to the component type:
http://java.sun.com/docs/books/vmspec/2nd-edition/html/ConstantPool.doc.html
#79473
> 2. Your parsing code didn't support the "proper" type format for arrays.
> We want this method to do so.
If you want me to, I can enhance it accordingly this evening.
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [lang] enhanced version of Class.forName
Posted by Thomas Dudziak <to...@gmail.com>.
On 9/6/05, James Carman <ja...@carmanconsulting.com> wrote:
> 1. Well, it's not exactly the same thing. I'm talking about when they pass
> in just "boolean" or "int". In that case, we construct the name "[B" or
> "[I" and pass that to Class.forName() with the "initialize" parameter,
> thereby telling it whether to initialize the array class and not
> (necessarily) the component type class.
If I understand the VM spec correctly, the initialization is
recursively applied to the component type:
http://java.sun.com/docs/books/vmspec/2nd-edition/html/ConstantPool.doc.html#79473
> 2. Your parsing code didn't support the "proper" type format for arrays.
> We want this method to do so.
If you want me to, I can enhance it accordingly this evening.
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
RE: [lang] enhanced version of Class.forName
Posted by James Carman <ja...@carmanconsulting.com>.
1. Well, it's not exactly the same thing. I'm talking about when they pass
in just "boolean" or "int". In that case, we construct the name "[B" or
"[I" and pass that to Class.forName() with the "initialize" parameter,
thereby telling it whether to initialize the array class and not
(necessarily) the component type class.
2. Your parsing code didn't support the "proper" type format for arrays.
We want this method to do so.
-----Original Message-----
From: Thomas Dudziak [mailto:tomdzk@gmail.com]
Sent: Tuesday, September 06, 2005 10:12 AM
To: Jakarta Commons Developers List
Subject: Re: [lang] enhanced version of Class.forName
On 9/6/05, James Carman <ja...@carmanconsulting.com> wrote:
> Okay, I caved (or compromised)! :-) I re-wrote the method to use your
> little trick of loading the array version of the class and taking the
> component type. The only problem I have with this is that I'm passing the
> "initialize" parameter in when I call forName() for the array version. I
> guess it doesn't really hurt to initialize the array version, but does
that
> cause the component class to get initialized? Of course, this only
happens
> for primitives, so it's probably not a big issue, since the primitive
> classes will most likely be loaded by the bootstrap classloader. I'm
> attaching the code as a patch for others to review.
Since all we're doing is creating the "[I" / "[L..." version and pass
it to the classloader, we would get the same effect as by using the
bytecode version directly. Isn't that desired ?
> I still have the problem that my code uses a regex to check the validity
of
> the class name that's passed in. Darn backward compatibility!
Why don't you merge our versions ? I think my stringtokenizer usage is
reasonably well behaved and its 1.2 compatible. It should not be too
difficult to adapt it so that the bytecode specifications
("[I"/"L...;") can also be parsed.
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [lang] enhanced version of Class.forName
Posted by Thomas Dudziak <to...@gmail.com>.
On 9/6/05, James Carman <ja...@carmanconsulting.com> wrote:
> Okay, I caved (or compromised)! :-) I re-wrote the method to use your
> little trick of loading the array version of the class and taking the
> component type. The only problem I have with this is that I'm passing the
> "initialize" parameter in when I call forName() for the array version. I
> guess it doesn't really hurt to initialize the array version, but does that
> cause the component class to get initialized? Of course, this only happens
> for primitives, so it's probably not a big issue, since the primitive
> classes will most likely be loaded by the bootstrap classloader. I'm
> attaching the code as a patch for others to review.
Since all we're doing is creating the "[I" / "[L..." version and pass
it to the classloader, we would get the same effect as by using the
bytecode version directly. Isn't that desired ?
> I still have the problem that my code uses a regex to check the validity of
> the class name that's passed in. Darn backward compatibility!
Why don't you merge our versions ? I think my stringtokenizer usage is
reasonably well behaved and its 1.2 compatible. It should not be too
difficult to adapt it so that the bytecode specifications
("[I"/"L...;") can also be parsed.
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [lang] enhanced version of Class.forName
Posted by Thomas Dudziak <to...@gmail.com>.
On 9/6/05, James Carman <ja...@carmanconsulting.com> wrote:
> Well, I just tried your code against my test cases and it fails in two
> ways...
>
> 1. It doesn't throw an IllegalArgumentException when you pass in null.
> 2. It doesn't hand the actual java class names for array types (i.e.
> [[Ljava.lang.String;).
>
> Now, it could be argued that an NPE is just as good as
> IllegalArgumentException in the first case, but the second case is
> definitely something that it should support.
Mhm, ok, it wasn't intended to support the old style but if it is
important, it can easily be added.
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
RE: [lang] enhanced version of Class.forName
Posted by James Carman <ja...@carmanconsulting.com>.
Well, I just tried your code against my test cases and it fails in two
ways...
1. It doesn't throw an IllegalArgumentException when you pass in null.
2. It doesn't hand the actual java class names for array types (i.e.
[[Ljava.lang.String;).
Now, it could be argued that an NPE is just as good as
IllegalArgumentException in the first case, but the second case is
definitely something that it should support.
-----Original Message-----
From: Thomas Dudziak [mailto:tomdzk@gmail.com]
Sent: Tuesday, September 06, 2005 9:24 AM
To: Jakarta Commons Developers List
Subject: Re: [lang] enhanced version of Class.forName
On 9/6/05, James Carman <ja...@carmanconsulting.com> wrote:
> 1. If you're going to use that logic, then who is to say that the
> String.equals() or String.hashCode() method is implemented correctly?
> You're using a HashMap (so am I) that has Strings as keys. In your
scenario
> (EvilClassLoader), the String class could be replaced with another class
> that totally messes things up. I think this point is a non issue and not
> worth coding around. Very unlikely to occur and if it does, then shame on
> whoever wrote the code.
Erm, I think that is a non-argument. I was not talking about evil
classloaders, only about class loaders that also handle core classes -
nothing evil about that.
My argument was that it is not necessary to perform such special logic
that hardcodes against the *.TYPE constants (loaded by the system
class loader). In fact not doing this is slightly easier as the same
mechanism is used for every class (no coding around primitive
classes), and only one hashmap is used instead of two.
Don't get me wrong, I do not want to push my implementation or
something. However IMHO the usage of *.TYPE is not only not necessary
(it does not bring any benefits) but also somewhat dangerous in regard
to custom class loaders. And "very unlikely" are these things that
make hard-to-find errors that drive us nuts when debugging.
> 3. I changed my code to use the classloader's loadClass() method, also.
My
> vote would be +1 to using the classloader if it really is safer.
I wonder whether the long version Class.forName(String name, boolean
initialize, ClassLoader loader) or ClassLoader.loadClass would be
better ?
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
RE: [lang] enhanced version of Class.forName
Posted by James Carman <ja...@carmanconsulting.com>.
Okay, I caved (or compromised)! :-) I re-wrote the method to use your
little trick of loading the array version of the class and taking the
component type. The only problem I have with this is that I'm passing the
"initialize" parameter in when I call forName() for the array version. I
guess it doesn't really hurt to initialize the array version, but does that
cause the component class to get initialized? Of course, this only happens
for primitives, so it's probably not a big issue, since the primitive
classes will most likely be loaded by the bootstrap classloader. I'm
attaching the code as a patch for others to review.
I still have the problem that my code uses a regex to check the validity of
the class name that's passed in. Darn backward compatibility!
-----Original Message-----
From: Thomas Dudziak [mailto:tomdzk@gmail.com]
Sent: Tuesday, September 06, 2005 9:24 AM
To: Jakarta Commons Developers List
Subject: Re: [lang] enhanced version of Class.forName
On 9/6/05, James Carman <ja...@carmanconsulting.com> wrote:
> 1. If you're going to use that logic, then who is to say that the
> String.equals() or String.hashCode() method is implemented correctly?
> You're using a HashMap (so am I) that has Strings as keys. In your
scenario
> (EvilClassLoader), the String class could be replaced with another class
> that totally messes things up. I think this point is a non issue and not
> worth coding around. Very unlikely to occur and if it does, then shame on
> whoever wrote the code.
Erm, I think that is a non-argument. I was not talking about evil
classloaders, only about class loaders that also handle core classes -
nothing evil about that.
My argument was that it is not necessary to perform such special logic
that hardcodes against the *.TYPE constants (loaded by the system
class loader). In fact not doing this is slightly easier as the same
mechanism is used for every class (no coding around primitive
classes), and only one hashmap is used instead of two.
Don't get me wrong, I do not want to push my implementation or
something. However IMHO the usage of *.TYPE is not only not necessary
(it does not bring any benefits) but also somewhat dangerous in regard
to custom class loaders. And "very unlikely" are these things that
make hard-to-find errors that drive us nuts when debugging.
> 3. I changed my code to use the classloader's loadClass() method, also.
My
> vote would be +1 to using the classloader if it really is safer.
I wonder whether the long version Class.forName(String name, boolean
initialize, ClassLoader loader) or ClassLoader.loadClass would be
better ?
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [lang] enhanced version of Class.forName
Posted by Thomas Dudziak <to...@gmail.com>.
On 9/6/05, James Carman <ja...@carmanconsulting.com> wrote:
> 1. If you're going to use that logic, then who is to say that the
> String.equals() or String.hashCode() method is implemented correctly?
> You're using a HashMap (so am I) that has Strings as keys. In your scenario
> (EvilClassLoader), the String class could be replaced with another class
> that totally messes things up. I think this point is a non issue and not
> worth coding around. Very unlikely to occur and if it does, then shame on
> whoever wrote the code.
Erm, I think that is a non-argument. I was not talking about evil
classloaders, only about class loaders that also handle core classes -
nothing evil about that.
My argument was that it is not necessary to perform such special logic
that hardcodes against the *.TYPE constants (loaded by the system
class loader). In fact not doing this is slightly easier as the same
mechanism is used for every class (no coding around primitive
classes), and only one hashmap is used instead of two.
Don't get me wrong, I do not want to push my implementation or
something. However IMHO the usage of *.TYPE is not only not necessary
(it does not bring any benefits) but also somewhat dangerous in regard
to custom class loaders. And "very unlikely" are these things that
make hard-to-find errors that drive us nuts when debugging.
> 3. I changed my code to use the classloader's loadClass() method, also. My
> vote would be +1 to using the classloader if it really is safer.
I wonder whether the long version Class.forName(String name, boolean
initialize, ClassLoader loader) or ClassLoader.loadClass would be
better ?
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
RE: [lang] enhanced version of Class.forName
Posted by James Carman <ja...@carmanconsulting.com>.
1. If you're going to use that logic, then who is to say that the
String.equals() or String.hashCode() method is implemented correctly?
You're using a HashMap (so am I) that has Strings as keys. In your scenario
(EvilClassLoader), the String class could be replaced with another class
that totally messes things up. I think this point is a non issue and not
worth coding around. Very unlikely to occur and if it does, then shame on
whoever wrote the code.
2. See above.
3. I changed my code to use the classloader's loadClass() method, also. My
vote would be +1 to using the classloader if it really is safer.
-----Original Message-----
From: Thomas Dudziak [mailto:tomdzk@gmail.com]
Sent: Tuesday, September 06, 2005 8:59 AM
To: Jakarta Commons Developers List
Subject: Re: [lang] enhanced version of Class.forName
On 9/6/05, James Carman <ja...@carmanconsulting.com> wrote:
> 1. In an environment where a SecurityManager is installed (if you're
> running code that is going to try to replace system classes, I'd suggest
> it), code has to be given the permission to instantiate a ClassLoader.
> Also, that ClassLoader has to be given the rights to define classes in
> certain packages. So, it's highly unlikely that Integer.TYPE will be
> different in different threads within the same VM.
Yep, but not impossible. And this dependency is not necessary anyway
(see the impl that I attached to the issue; it only needs the string
map), so please, do not use Integer.TYPE or int.class directly.
> 2. I did not assert that int.class = Integer.class. I asserted that
> Integer.TYPE = int.class. I have changed my code to use int.class,
though,
> as it's shorter and more obvious as to what we're doing.
see above
> 3. Was unaware of the problems with Class.forName(). Is everyone in
> agreement that we should use
> Thread.currentThread().getContextClassLoader().loadClass() rather than
> Class.forName()?
+1
In a normal app these are the same, and in a scenario with multiple
class loaders (eg. a web app), the context class loader is safer IMO.
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [lang] enhanced version of Class.forName
Posted by Thomas Dudziak <to...@gmail.com>.
On 9/6/05, James Carman <ja...@carmanconsulting.com> wrote:
> 1. In an environment where a SecurityManager is installed (if you're
> running code that is going to try to replace system classes, I'd suggest
> it), code has to be given the permission to instantiate a ClassLoader.
> Also, that ClassLoader has to be given the rights to define classes in
> certain packages. So, it's highly unlikely that Integer.TYPE will be
> different in different threads within the same VM.
Yep, but not impossible. And this dependency is not necessary anyway
(see the impl that I attached to the issue; it only needs the string
map), so please, do not use Integer.TYPE or int.class directly.
> 2. I did not assert that int.class = Integer.class. I asserted that
> Integer.TYPE = int.class. I have changed my code to use int.class, though,
> as it's shorter and more obvious as to what we're doing.
see above
> 3. Was unaware of the problems with Class.forName(). Is everyone in
> agreement that we should use
> Thread.currentThread().getContextClassLoader().loadClass() rather than
> Class.forName()?
+1
In a normal app these are the same, and in a scenario with multiple
class loaders (eg. a web app), the context class loader is safer IMO.
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [lang] enhanced version of Class.forName
Posted by Martin Cooper <mf...@gmail.com>.
On 9/6/05, James Carman <ja...@carmanconsulting.com> wrote:
>
> 1. In an environment where a SecurityManager is installed (if you're
> running code that is going to try to replace system classes, I'd suggest
> it), code has to be given the permission to instantiate a ClassLoader.
> Also, that ClassLoader has to be given the rights to define classes in
> certain packages. So, it's highly unlikely that Integer.TYPE will be
> different in different threads within the same VM.
>
> 2. I did not assert that int.class = Integer.class. I asserted that
> Integer.TYPE = int.class. I have changed my code to use int.class, though,
> as it's shorter and more obvious as to what we're doing.
>
> 3. Was unaware of the problems with Class.forName(). Is everyone in
> agreement that we should use
> Thread.currentThread().getContextClassLoader().loadClass() rather than
> Class.forName()?
To work correctly in a J2EE environment, you need to check the context class
loader first, use it if it is non-null, and use the implementing class's
class loader if it is not. You can't assume that there will always be a
context class loader.
--
Martin Cooper
-----Original Message-----
> From: Thomas Dudziak [mailto:tomdzk@gmail.com]
> Sent: Tuesday, September 06, 2005 3:09 AM
> To: Jakarta Commons Developers List
> Subject: Re: [lang] enhanced version of Class.forName
>
> On 9/6/05, James Carman <ja...@carmanconsulting.com> wrote:
>
> > I would say that this is something that would be very useful. We did
> > something similar in HiveMind. I can imagine an implementation like:
> >
> > private static Map typeMap = new HashMap();
> > private static Map abbreviationMap = new HashMap();
> >
> > static
> > {
> > typeMap.put( "int", Integer.TYPE );
> > typeMap.put( "boolean", Boolean.TYPE );
> > typeMap.put( "float", Float.TYPE );
> > typeMap.put( "long", Long.TYPE );
> > typeMap.put( "short", Short.TYPE );
> > typeMap.put( "byte", Byte.TYPE );
> > typeMap.put( "double", Double.TYPE );
> > typeMap.put( "char", Character.TYPE );
> >
> > abbreviationMap.put( "int", "I" );
> > abbreviationMap.put( "boolean", "Z" );
> > abbreviationMap.put( "float", "F" );
> > abbreviationMap.put( "long", "J" );
> > abbreviationMap.put( "short", "S" );
> > abbreviationMap.put( "byte", "B" );
> > abbreviationMap.put( "double", "D" );
> > abbreviationMap.put( "char", "C" );
> > }
>
> <snip>
>
> This is similar to my implementation with three important differences:
>
> * the direct usage of types (Integer.TYPE etc.) has obviously problems
> in environments with multiple class loaders (eg. Class.forName(String
> name, boolean initialize, ClassLoader classLoader); likewise there may
> or may not be issues with using static in this context
>
> * int.class is not guaranteed to be equal to Integer.class
>
> * Class.forName is known to be problematic in certain
> circumstances/environments; using
> Thread.currentThread().getContextClassLoader() might be more useful
> (as Henri/Stephen suggested).
>
> I'll add an issue for this in BugZilla with my code attached, and you
> guys can decide whether it is useful to include in commons-lang, ok ?
>
> regards,
> Tom
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
RE: [lang] enhanced version of Class.forName
Posted by James Carman <ja...@carmanconsulting.com>.
1. In an environment where a SecurityManager is installed (if you're
running code that is going to try to replace system classes, I'd suggest
it), code has to be given the permission to instantiate a ClassLoader.
Also, that ClassLoader has to be given the rights to define classes in
certain packages. So, it's highly unlikely that Integer.TYPE will be
different in different threads within the same VM.
2. I did not assert that int.class = Integer.class. I asserted that
Integer.TYPE = int.class. I have changed my code to use int.class, though,
as it's shorter and more obvious as to what we're doing.
3. Was unaware of the problems with Class.forName(). Is everyone in
agreement that we should use
Thread.currentThread().getContextClassLoader().loadClass() rather than
Class.forName()?
-----Original Message-----
From: Thomas Dudziak [mailto:tomdzk@gmail.com]
Sent: Tuesday, September 06, 2005 3:09 AM
To: Jakarta Commons Developers List
Subject: Re: [lang] enhanced version of Class.forName
On 9/6/05, James Carman <ja...@carmanconsulting.com> wrote:
> I would say that this is something that would be very useful. We did
> something similar in HiveMind. I can imagine an implementation like:
>
> private static Map typeMap = new HashMap();
> private static Map abbreviationMap = new HashMap();
>
> static
> {
> typeMap.put( "int", Integer.TYPE );
> typeMap.put( "boolean", Boolean.TYPE );
> typeMap.put( "float", Float.TYPE );
> typeMap.put( "long", Long.TYPE );
> typeMap.put( "short", Short.TYPE );
> typeMap.put( "byte", Byte.TYPE );
> typeMap.put( "double", Double.TYPE );
> typeMap.put( "char", Character.TYPE );
>
> abbreviationMap.put( "int", "I" );
> abbreviationMap.put( "boolean", "Z" );
> abbreviationMap.put( "float", "F" );
> abbreviationMap.put( "long", "J" );
> abbreviationMap.put( "short", "S" );
> abbreviationMap.put( "byte", "B" );
> abbreviationMap.put( "double", "D" );
> abbreviationMap.put( "char", "C" );
> }
<snip>
This is similar to my implementation with three important differences:
* the direct usage of types (Integer.TYPE etc.) has obviously problems
in environments with multiple class loaders (eg. Class.forName(String
name, boolean initialize, ClassLoader classLoader); likewise there may
or may not be issues with using static in this context
* int.class is not guaranteed to be equal to Integer.class
* Class.forName is known to be problematic in certain
circumstances/environments; using
Thread.currentThread().getContextClassLoader() might be more useful
(as Henri/Stephen suggested).
I'll add an issue for this in BugZilla with my code attached, and you
guys can decide whether it is useful to include in commons-lang, ok ?
regards,
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [lang] enhanced version of Class.forName
Posted by Thomas Dudziak <to...@gmail.com>.
On 9/6/05, James Carman <ja...@carmanconsulting.com> wrote:
> I would say that this is something that would be very useful. We did
> something similar in HiveMind. I can imagine an implementation like:
>
> private static Map typeMap = new HashMap();
> private static Map abbreviationMap = new HashMap();
>
> static
> {
> typeMap.put( "int", Integer.TYPE );
> typeMap.put( "boolean", Boolean.TYPE );
> typeMap.put( "float", Float.TYPE );
> typeMap.put( "long", Long.TYPE );
> typeMap.put( "short", Short.TYPE );
> typeMap.put( "byte", Byte.TYPE );
> typeMap.put( "double", Double.TYPE );
> typeMap.put( "char", Character.TYPE );
>
> abbreviationMap.put( "int", "I" );
> abbreviationMap.put( "boolean", "Z" );
> abbreviationMap.put( "float", "F" );
> abbreviationMap.put( "long", "J" );
> abbreviationMap.put( "short", "S" );
> abbreviationMap.put( "byte", "B" );
> abbreviationMap.put( "double", "D" );
> abbreviationMap.put( "char", "C" );
> }
<snip>
This is similar to my implementation with three important differences:
* the direct usage of types (Integer.TYPE etc.) has obviously problems
in environments with multiple class loaders (eg. Class.forName(String
name, boolean initialize, ClassLoader classLoader); likewise there may
or may not be issues with using static in this context
* int.class is not guaranteed to be equal to Integer.class
* Class.forName is known to be problematic in certain
circumstances/environments; using
Thread.currentThread().getContextClassLoader() might be more useful
(as Henri/Stephen suggested).
I'll add an issue for this in BugZilla with my code attached, and you
guys can decide whether it is useful to include in commons-lang, ok ?
regards,
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
RE: [lang] enhanced version of Class.forName
Posted by James Carman <ja...@carmanconsulting.com>.
This was a first stab, but good catches on the NPE and the "[]" and "["
cases. I suppose a quick null/syntax check at the beginning would help.
As far as your optimization goes, I was not aware that the endsWith()
operations were so expensive. I think the endsWith() version of the code is
much more readable/maintainable, but yours might be faster. I don't know
that speed is an extremely important requirement for this code, though. I
just forgot to change the append()/'+' statement to chained append() calls.
Anyway, thanks for the tips.
-----Original Message-----
From: Henning P. Schmiedehausen [mailto:hps@intermeta.de]
Sent: Tuesday, September 06, 2005 3:30 AM
To: commons-dev@jakarta.apache.org
Subject: Re: [lang] enhanced version of Class.forName
"James Carman" <ja...@carmanconsulting.com> writes:
>I would say that this is something that would be very useful. We did
>something similar in HiveMind. I can imagine an implementation like:
I always wondered over the obsession with startsWith() and endsWith()
in string-related commons code. These operations are hugely expensive,
especially with long strings to check!
They are implemented using a loop and char compares. I very much
prefer using charAt and length() for this; both are constant time
operations.
Same goes to the mixture of StringBuffer operations and '+'. Finally
append()'ing a char is (at least in the Sun 1.4.x JSDK) more effective
than append()'ing a String.
Your implementation also throws NPE for className == null BTW. There
is also the pathological case of ClassUtils.enhancedForName("[]").
Currently this throws CNF for class '['.
How about this:
--- cut ---
private static Map typeMap = new HashMap();
private static Map abbreviationMap = new HashMap();
static
{
typeMap.put( "int", Integer.TYPE );
typeMap.put( "boolean", Boolean.TYPE );
typeMap.put( "float", Float.TYPE );
typeMap.put( "long", Long.TYPE );
typeMap.put( "short", Short.TYPE );
typeMap.put( "byte", Byte.TYPE );
typeMap.put( "double", Double.TYPE );
typeMap.put( "char", Character.TYPE );
abbreviationMap.put( "int", "I" );
abbreviationMap.put( "boolean", "Z" );
abbreviationMap.put( "float", "F" );
abbreviationMap.put( "long", "J" );
abbreviationMap.put( "short", "S" );
abbreviationMap.put( "byte", "B" );
abbreviationMap.put( "double", "D" );
abbreviationMap.put( "char", "C" );
}
public static Class enhancedForName(String className)
throws ClassNotFoundException
{
Class clazz = null;
if (className != null)
{
clazz = typeMap.get(className);
if (clazz == null)
{
int nameLen = className.length();
StringBuffer actualNameBuffer = new StringBuffer();
boolean foundArray = false;
while(namelen > 2
&& className.charAt(nameLen - 2) == '['
&& className.charAt(nameLen - 1) == ']')
{
actualNameBuffer.append('[');
nameLen -= 2;
foundArray = true;
}
if (!foundArray)
{
clazz = Class.forName(className);
}
else
{
final String abbreviation = (String)
abbreviationMap.get(className);
if (abbreviation != null)
{
actualNameBuffer.append(abbreviation);
}
else
{
actualNameBuffer.append('L')
.append(className)
.append(';');
}
clazz = Class.forName(actualNameBuffer.toString());
}
}
}
return clazz;
}
--- cut ---
Best regards
Henning
>public static Class enhancedForName( String className ) throws
>ClassNotFoundException
>{
> Class clazz = ( Class )typeMap.get( className );
> if( clazz == null )
> {
> if( className.endsWith( "[]" ) )
> {
> final StringBuffer actualNameBuffer = new StringBuffer();
> while( className.endsWith( "[]" ) )
> {
> className = className.substring( 0, className.length() - 2 );
> actualNameBuffer.append( "[" );
> }
> final String abbreviation = ( String )abbreviationMap.get( className
>);
> if( abbreviation != null )
> {
> actualNameBuffer.append( abbreviation );
> }
> else
> {
> actualNameBuffer.append( "L" + className + ";" );
> }
> clazz = Class.forName( actualNameBuffer.toString() );
> }
> else
> {
> clazz = Class.forName( className );
> }
> }
> return clazz;
>}
>-----Original Message-----
>From: Thomas Dudziak [mailto:tomdzk@gmail.com]
>Sent: Monday, September 05, 2005 5:05 PM
>To: Jakarta Commons Developers List
>Subject: [lang] enhanced version of Class.forName
>Hi folks,
>I had this problem that I needed to create class objects for things
>like "int" and "boolean", eg. the type specification that you would
>use for a variable declaration or method parameter.
>Since the normal Class.forName does not handle this really well, and
>commons-lang does not provide such a function (please correct me if
>I'm wrong here), I implemented it myself.
>Now I wonder, would this be useful enhancement to ClassUtils ? If so,
>I could provide a patch in BugZilla.
>regards,
>Tom
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-dev-help@jakarta.apache.org
--
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen INTERMETA GmbH
hps@intermeta.de +49 9131 50 654 0 http://www.intermeta.de/
RedHat Certified Engineer -- Jakarta Turbine Development -- hero for hire
Linux, Java, perl, Solaris -- Consulting, Training, Development
4 - 8 - 15 - 16 - 23 - 42
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [lang] enhanced version of Class.forName
Posted by "Henning P. Schmiedehausen" <hp...@intermeta.de>.
"James Carman" <ja...@carmanconsulting.com> writes:
>I would say that this is something that would be very useful. We did
>something similar in HiveMind. I can imagine an implementation like:
I always wondered over the obsession with startsWith() and endsWith()
in string-related commons code. These operations are hugely expensive,
especially with long strings to check!
They are implemented using a loop and char compares. I very much
prefer using charAt and length() for this; both are constant time
operations.
Same goes to the mixture of StringBuffer operations and '+'. Finally
append()'ing a char is (at least in the Sun 1.4.x JSDK) more effective
than append()'ing a String.
Your implementation also throws NPE for className == null BTW. There
is also the pathological case of ClassUtils.enhancedForName("[]").
Currently this throws CNF for class '['.
How about this:
--- cut ---
private static Map typeMap = new HashMap();
private static Map abbreviationMap = new HashMap();
static
{
typeMap.put( "int", Integer.TYPE );
typeMap.put( "boolean", Boolean.TYPE );
typeMap.put( "float", Float.TYPE );
typeMap.put( "long", Long.TYPE );
typeMap.put( "short", Short.TYPE );
typeMap.put( "byte", Byte.TYPE );
typeMap.put( "double", Double.TYPE );
typeMap.put( "char", Character.TYPE );
abbreviationMap.put( "int", "I" );
abbreviationMap.put( "boolean", "Z" );
abbreviationMap.put( "float", "F" );
abbreviationMap.put( "long", "J" );
abbreviationMap.put( "short", "S" );
abbreviationMap.put( "byte", "B" );
abbreviationMap.put( "double", "D" );
abbreviationMap.put( "char", "C" );
}
public static Class enhancedForName(String className)
throws ClassNotFoundException
{
Class clazz = null;
if (className != null)
{
clazz = typeMap.get(className);
if (clazz == null)
{
int nameLen = className.length();
StringBuffer actualNameBuffer = new StringBuffer();
boolean foundArray = false;
while(namelen > 2
&& className.charAt(nameLen - 2) == '['
&& className.charAt(nameLen - 1) == ']')
{
actualNameBuffer.append('[');
nameLen -= 2;
foundArray = true;
}
if (!foundArray)
{
clazz = Class.forName(className);
}
else
{
final String abbreviation = (String) abbreviationMap.get(className);
if (abbreviation != null)
{
actualNameBuffer.append(abbreviation);
}
else
{
actualNameBuffer.append('L')
.append(className)
.append(';');
}
clazz = Class.forName(actualNameBuffer.toString());
}
}
}
return clazz;
}
--- cut ---
Best regards
Henning
>public static Class enhancedForName( String className ) throws
>ClassNotFoundException
>{
> Class clazz = ( Class )typeMap.get( className );
> if( clazz == null )
> {
> if( className.endsWith( "[]" ) )
> {
> final StringBuffer actualNameBuffer = new StringBuffer();
> while( className.endsWith( "[]" ) )
> {
> className = className.substring( 0, className.length() - 2 );
> actualNameBuffer.append( "[" );
> }
> final String abbreviation = ( String )abbreviationMap.get( className
>);
> if( abbreviation != null )
> {
> actualNameBuffer.append( abbreviation );
> }
> else
> {
> actualNameBuffer.append( "L" + className + ";" );
> }
> clazz = Class.forName( actualNameBuffer.toString() );
> }
> else
> {
> clazz = Class.forName( className );
> }
> }
> return clazz;
>}
>-----Original Message-----
>From: Thomas Dudziak [mailto:tomdzk@gmail.com]
>Sent: Monday, September 05, 2005 5:05 PM
>To: Jakarta Commons Developers List
>Subject: [lang] enhanced version of Class.forName
>Hi folks,
>I had this problem that I needed to create class objects for things
>like "int" and "boolean", eg. the type specification that you would
>use for a variable declaration or method parameter.
>Since the normal Class.forName does not handle this really well, and
>commons-lang does not provide such a function (please correct me if
>I'm wrong here), I implemented it myself.
>Now I wonder, would this be useful enhancement to ClassUtils ? If so,
>I could provide a patch in BugZilla.
>regards,
>Tom
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-dev-help@jakarta.apache.org
--
Dipl.-Inf. (Univ.) Henning P. Schmiedehausen INTERMETA GmbH
hps@intermeta.de +49 9131 50 654 0 http://www.intermeta.de/
RedHat Certified Engineer -- Jakarta Turbine Development -- hero for hire
Linux, Java, perl, Solaris -- Consulting, Training, Development
4 - 8 - 15 - 16 - 23 - 42
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
RE: [lang] enhanced version of Class.forName
Posted by James Carman <ja...@carmanconsulting.com>.
The code in "clazz" looks somewhat similar to what I have, but they used a
bunch of if/else statements rather than maps to do what I did. I personally
like my implementation a bit better (well, DUH), though. I couldn't find
any code in "reflect" which did what we are looking to do. The obvious
place for it would be in ClassUtils, but I didn't see anything.
-----Original Message-----
From: Martin Cooper [mailto:mfncooper@gmail.com]
Sent: Monday, September 05, 2005 10:19 PM
To: Jakarta Commons Developers List
Cc: tomdzk@gmail.com
Subject: Re: [lang] enhanced version of Class.forName
This might be a good time to mention two dormant sandbox components that
(tried to) address similar needs. One, at least, has a stated relationship
with Lang.
http://svn.apache.org/viewcvs.cgi/jakarta/commons/dormant/clazz/trunk/
http://svn.apache.org/viewcvs.cgi/jakarta/commons/dormant/reflect/trunk/
--
Martin Cooper
On 9/5/05, James Carman <ja...@carmanconsulting.com> wrote:
>
> I would say that this is something that would be very useful. We did
> something similar in HiveMind. I can imagine an implementation like:
>
> private static Map typeMap = new HashMap();
> private static Map abbreviationMap = new HashMap();
>
> static
> {
> typeMap.put( "int", Integer.TYPE );
> typeMap.put( "boolean", Boolean.TYPE );
> typeMap.put( "float", Float.TYPE );
> typeMap.put( "long", Long.TYPE );
> typeMap.put( "short", Short.TYPE );
> typeMap.put( "byte", Byte.TYPE );
> typeMap.put( "double", Double.TYPE );
> typeMap.put( "char", Character.TYPE );
>
> abbreviationMap.put( "int", "I" );
> abbreviationMap.put( "boolean", "Z" );
> abbreviationMap.put( "float", "F" );
> abbreviationMap.put( "long", "J" );
> abbreviationMap.put( "short", "S" );
> abbreviationMap.put( "byte", "B" );
> abbreviationMap.put( "double", "D" );
> abbreviationMap.put( "char", "C" );
> }
>
> public static Class enhancedForName( String className ) throws
> ClassNotFoundException
> {
> Class clazz = ( Class )typeMap.get( className );
> if( clazz == null )
> {
> if( className.endsWith( "[]" ) )
> {
> final StringBuffer actualNameBuffer = new StringBuffer();
> while( className.endsWith( "[]" ) )
> {
> className = className.substring( 0, className.length() - 2 );
> actualNameBuffer.append( "[" );
> }
> final String abbreviation = ( String )abbreviationMap.get( className
> );
> if( abbreviation != null )
> {
> actualNameBuffer.append( abbreviation );
> }
> else
> {
> actualNameBuffer.append( "L" + className + ";" );
> }
> clazz = Class.forName( actualNameBuffer.toString() );
> }
> else
> {
> clazz = Class.forName( className );
> }
> }
> return clazz;
> }
>
> -----Original Message-----
> From: Thomas Dudziak [mailto:tomdzk@gmail.com]
> Sent: Monday, September 05, 2005 5:05 PM
> To: Jakarta Commons Developers List
> Subject: [lang] enhanced version of Class.forName
>
> Hi folks,
>
> I had this problem that I needed to create class objects for things
> like "int" and "boolean", eg. the type specification that you would
> use for a variable declaration or method parameter.
> Since the normal Class.forName does not handle this really well, and
> commons-lang does not provide such a function (please correct me if
> I'm wrong here), I implemented it myself.
> Now I wonder, would this be useful enhancement to ClassUtils ? If so,
> I could provide a patch in BugZilla.
>
> regards,
> Tom
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [lang] enhanced version of Class.forName
Posted by Martin Cooper <mf...@gmail.com>.
This might be a good time to mention two dormant sandbox components that
(tried to) address similar needs. One, at least, has a stated relationship
with Lang.
http://svn.apache.org/viewcvs.cgi/jakarta/commons/dormant/clazz/trunk/
http://svn.apache.org/viewcvs.cgi/jakarta/commons/dormant/reflect/trunk/
--
Martin Cooper
On 9/5/05, James Carman <ja...@carmanconsulting.com> wrote:
>
> I would say that this is something that would be very useful. We did
> something similar in HiveMind. I can imagine an implementation like:
>
> private static Map typeMap = new HashMap();
> private static Map abbreviationMap = new HashMap();
>
> static
> {
> typeMap.put( "int", Integer.TYPE );
> typeMap.put( "boolean", Boolean.TYPE );
> typeMap.put( "float", Float.TYPE );
> typeMap.put( "long", Long.TYPE );
> typeMap.put( "short", Short.TYPE );
> typeMap.put( "byte", Byte.TYPE );
> typeMap.put( "double", Double.TYPE );
> typeMap.put( "char", Character.TYPE );
>
> abbreviationMap.put( "int", "I" );
> abbreviationMap.put( "boolean", "Z" );
> abbreviationMap.put( "float", "F" );
> abbreviationMap.put( "long", "J" );
> abbreviationMap.put( "short", "S" );
> abbreviationMap.put( "byte", "B" );
> abbreviationMap.put( "double", "D" );
> abbreviationMap.put( "char", "C" );
> }
>
> public static Class enhancedForName( String className ) throws
> ClassNotFoundException
> {
> Class clazz = ( Class )typeMap.get( className );
> if( clazz == null )
> {
> if( className.endsWith( "[]" ) )
> {
> final StringBuffer actualNameBuffer = new StringBuffer();
> while( className.endsWith( "[]" ) )
> {
> className = className.substring( 0, className.length() - 2 );
> actualNameBuffer.append( "[" );
> }
> final String abbreviation = ( String )abbreviationMap.get( className
> );
> if( abbreviation != null )
> {
> actualNameBuffer.append( abbreviation );
> }
> else
> {
> actualNameBuffer.append( "L" + className + ";" );
> }
> clazz = Class.forName( actualNameBuffer.toString() );
> }
> else
> {
> clazz = Class.forName( className );
> }
> }
> return clazz;
> }
>
> -----Original Message-----
> From: Thomas Dudziak [mailto:tomdzk@gmail.com]
> Sent: Monday, September 05, 2005 5:05 PM
> To: Jakarta Commons Developers List
> Subject: [lang] enhanced version of Class.forName
>
> Hi folks,
>
> I had this problem that I needed to create class objects for things
> like "int" and "boolean", eg. the type specification that you would
> use for a variable declaration or method parameter.
> Since the normal Class.forName does not handle this really well, and
> commons-lang does not provide such a function (please correct me if
> I'm wrong here), I implemented it myself.
> Now I wonder, would this be useful enhancement to ClassUtils ? If so,
> I could provide a patch in BugZilla.
>
> regards,
> Tom
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
RE: [lang] enhanced version of Class.forName
Posted by James Carman <ja...@carmanconsulting.com>.
Sorry, I meant "forCanonicalName" of course.
-----Original Message-----
From: James Carman [mailto:james@carmanconsulting.com]
Sent: Monday, September 05, 2005 11:26 PM
To: 'Jakarta Commons Developers List'; tomdzk@gmail.com
Subject: RE: [lang] enhanced version of Class.forName
I have the code added to ClassUtils in "lang" right now in my IDE. What
would be a good name for the method, though? I don't really like
"extendedForName." What about "getCanonicalName"? Any other suggestions?
We could just use "forName", but the distinction between the Class.forName()
and the ClassUtils.forName() wouldn't be that obvious.
Would the "lang" folks care if I checked in this code (with test cases of
course) once we decide on what the name of the method should be (or whether
or not it should be in there in the first place)?
-----Original Message-----
From: Henri Yandell [mailto:flamefew@gmail.com]
Sent: Monday, September 05, 2005 8:26 PM
To: Jakarta Commons Developers List; tomdzk@gmail.com
Subject: Re: [lang] enhanced version of Class.forName
Deja Vu :) +1
Jan-2005:http://mail-archives.apache.org/mod_mbox/jakarta-commons-dev/200501
.mbox/%3c31cc37360501261434acc4d4b@mail.gmail.com%3e
Feb-2005:http://mail-archives.apache.org/mod_mbox/jakarta-commons-dev/200502
.mbox/%3c004101c508bc$d21eb9b0$b5739951@SCOLEBOURNE%3e
Various ClassLoader issues raised by Stephen, we agreed to hold off
until after 2.1; so now is a good time.
Hen
On 9/5/05, Thomas Dudziak <to...@gmail.com> wrote:
> (Ups, only replied to Wade, not the list)
>
> On 9/5/05, Wade Chandler <hw...@yahoo.com> wrote:
>
> > What's a use case for it Tom? curious.
>
> I just checked-in an enhancement for OJB that allows to specify which
> constructor/factory-method to utilize for constructing objects read
> from the database. These can have parameters which are described by
> corresponding parameter element:
>
> <creation-descriptor
> type="factory"
> factory-class="org.apache.ojb.broker.ObjectTestFactory"
> factory-method="createInstance"
> >
> <parameter field-ref="name"/>
> <parameter field-ref="value" type="int[]"/>
> </creation-descriptor>
>
> The optional type attribute allows the user to explicitly specify the
> parameter type thus helping OJB finding the correct constructor/method
> (in case of overload).
>
> Now if I'd use plain Class.forName, the user would have to specify:
>
> [LI
>
> for this int array which is hardly user-friendly. The solution that I
> added, allows for the normal type specification as is used in variable
> declarations or for method parameters.
>
> Tom
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
RE: [lang] enhanced version of Class.forName
Posted by James Carman <ja...@carmanconsulting.com>.
I have the code added to ClassUtils in "lang" right now in my IDE. What
would be a good name for the method, though? I don't really like
"extendedForName." What about "getCanonicalName"? Any other suggestions?
We could just use "forName", but the distinction between the Class.forName()
and the ClassUtils.forName() wouldn't be that obvious.
Would the "lang" folks care if I checked in this code (with test cases of
course) once we decide on what the name of the method should be (or whether
or not it should be in there in the first place)?
-----Original Message-----
From: Henri Yandell [mailto:flamefew@gmail.com]
Sent: Monday, September 05, 2005 8:26 PM
To: Jakarta Commons Developers List; tomdzk@gmail.com
Subject: Re: [lang] enhanced version of Class.forName
Deja Vu :) +1
Jan-2005:http://mail-archives.apache.org/mod_mbox/jakarta-commons-dev/200501
.mbox/%3c31cc37360501261434acc4d4b@mail.gmail.com%3e
Feb-2005:http://mail-archives.apache.org/mod_mbox/jakarta-commons-dev/200502
.mbox/%3c004101c508bc$d21eb9b0$b5739951@SCOLEBOURNE%3e
Various ClassLoader issues raised by Stephen, we agreed to hold off
until after 2.1; so now is a good time.
Hen
On 9/5/05, Thomas Dudziak <to...@gmail.com> wrote:
> (Ups, only replied to Wade, not the list)
>
> On 9/5/05, Wade Chandler <hw...@yahoo.com> wrote:
>
> > What's a use case for it Tom? curious.
>
> I just checked-in an enhancement for OJB that allows to specify which
> constructor/factory-method to utilize for constructing objects read
> from the database. These can have parameters which are described by
> corresponding parameter element:
>
> <creation-descriptor
> type="factory"
> factory-class="org.apache.ojb.broker.ObjectTestFactory"
> factory-method="createInstance"
> >
> <parameter field-ref="name"/>
> <parameter field-ref="value" type="int[]"/>
> </creation-descriptor>
>
> The optional type attribute allows the user to explicitly specify the
> parameter type thus helping OJB finding the correct constructor/method
> (in case of overload).
>
> Now if I'd use plain Class.forName, the user would have to specify:
>
> [LI
>
> for this int array which is hardly user-friendly. The solution that I
> added, allows for the normal type specification as is used in variable
> declarations or for method parameters.
>
> Tom
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [lang] enhanced version of Class.forName
Posted by Henri Yandell <fl...@gmail.com>.
Deja Vu :) +1
Jan-2005:http://mail-archives.apache.org/mod_mbox/jakarta-commons-dev/200501.mbox/%3c31cc37360501261434acc4d4b@mail.gmail.com%3e
Feb-2005:http://mail-archives.apache.org/mod_mbox/jakarta-commons-dev/200502.mbox/%3c004101c508bc$d21eb9b0$b5739951@SCOLEBOURNE%3e
Various ClassLoader issues raised by Stephen, we agreed to hold off
until after 2.1; so now is a good time.
Hen
On 9/5/05, Thomas Dudziak <to...@gmail.com> wrote:
> (Ups, only replied to Wade, not the list)
>
> On 9/5/05, Wade Chandler <hw...@yahoo.com> wrote:
>
> > What's a use case for it Tom? curious.
>
> I just checked-in an enhancement for OJB that allows to specify which
> constructor/factory-method to utilize for constructing objects read
> from the database. These can have parameters which are described by
> corresponding parameter element:
>
> <creation-descriptor
> type="factory"
> factory-class="org.apache.ojb.broker.ObjectTestFactory"
> factory-method="createInstance"
> >
> <parameter field-ref="name"/>
> <parameter field-ref="value" type="int[]"/>
> </creation-descriptor>
>
> The optional type attribute allows the user to explicitly specify the
> parameter type thus helping OJB finding the correct constructor/method
> (in case of overload).
>
> Now if I'd use plain Class.forName, the user would have to specify:
>
> [LI
>
> for this int array which is hardly user-friendly. The solution that I
> added, allows for the normal type specification as is used in variable
> declarations or for method parameters.
>
> Tom
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [lang] enhanced version of Class.forName
Posted by Thomas Dudziak <to...@gmail.com>.
(Ups, only replied to Wade, not the list)
On 9/5/05, Wade Chandler <hw...@yahoo.com> wrote:
> What's a use case for it Tom? curious.
I just checked-in an enhancement for OJB that allows to specify which
constructor/factory-method to utilize for constructing objects read
from the database. These can have parameters which are described by
corresponding parameter element:
<creation-descriptor
type="factory"
factory-class="org.apache.ojb.broker.ObjectTestFactory"
factory-method="createInstance"
>
<parameter field-ref="name"/>
<parameter field-ref="value" type="int[]"/>
</creation-descriptor>
The optional type attribute allows the user to explicitly specify the
parameter type thus helping OJB finding the correct constructor/method
(in case of overload).
Now if I'd use plain Class.forName, the user would have to specify:
[LI
for this int array which is hardly user-friendly. The solution that I
added, allows for the normal type specification as is used in variable
declarations or for method parameters.
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [lang] enhanced version of Class.forName
Posted by Wade Chandler <hw...@yahoo.com>.
--- Thomas Dudziak <to...@gmail.com> wrote:
> Hi folks,
>
> I had this problem that I needed to create class
> objects for things
> like "int" and "boolean", eg. the type specification
> that you would
> use for a variable declaration or method parameter.
> Since the normal Class.forName does not handle this
> really well, and
> commons-lang does not provide such a function
> (please correct me if
> I'm wrong here), I implemented it myself.
> Now I wonder, would this be useful enhancement to
> ClassUtils ? If so,
> I could provide a patch in BugZilla.
>
> regards,
> Tom
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> commons-dev-help@jakarta.apache.org
>
>
What's a use case for it Tom? curious.
Wade
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
RE: [lang] enhanced version of Class.forName
Posted by James Carman <ja...@carmanconsulting.com>.
I would say that this is something that would be very useful. We did
something similar in HiveMind. I can imagine an implementation like:
private static Map typeMap = new HashMap();
private static Map abbreviationMap = new HashMap();
static
{
typeMap.put( "int", Integer.TYPE );
typeMap.put( "boolean", Boolean.TYPE );
typeMap.put( "float", Float.TYPE );
typeMap.put( "long", Long.TYPE );
typeMap.put( "short", Short.TYPE );
typeMap.put( "byte", Byte.TYPE );
typeMap.put( "double", Double.TYPE );
typeMap.put( "char", Character.TYPE );
abbreviationMap.put( "int", "I" );
abbreviationMap.put( "boolean", "Z" );
abbreviationMap.put( "float", "F" );
abbreviationMap.put( "long", "J" );
abbreviationMap.put( "short", "S" );
abbreviationMap.put( "byte", "B" );
abbreviationMap.put( "double", "D" );
abbreviationMap.put( "char", "C" );
}
public static Class enhancedForName( String className ) throws
ClassNotFoundException
{
Class clazz = ( Class )typeMap.get( className );
if( clazz == null )
{
if( className.endsWith( "[]" ) )
{
final StringBuffer actualNameBuffer = new StringBuffer();
while( className.endsWith( "[]" ) )
{
className = className.substring( 0, className.length() - 2 );
actualNameBuffer.append( "[" );
}
final String abbreviation = ( String )abbreviationMap.get( className
);
if( abbreviation != null )
{
actualNameBuffer.append( abbreviation );
}
else
{
actualNameBuffer.append( "L" + className + ";" );
}
clazz = Class.forName( actualNameBuffer.toString() );
}
else
{
clazz = Class.forName( className );
}
}
return clazz;
}
-----Original Message-----
From: Thomas Dudziak [mailto:tomdzk@gmail.com]
Sent: Monday, September 05, 2005 5:05 PM
To: Jakarta Commons Developers List
Subject: [lang] enhanced version of Class.forName
Hi folks,
I had this problem that I needed to create class objects for things
like "int" and "boolean", eg. the type specification that you would
use for a variable declaration or method parameter.
Since the normal Class.forName does not handle this really well, and
commons-lang does not provide such a function (please correct me if
I'm wrong here), I implemented it myself.
Now I wonder, would this be useful enhancement to ClassUtils ? If so,
I could provide a patch in BugZilla.
regards,
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org