commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Cooper <mfncoo...@gmail.com>
Subject Re: [lang] enhanced version of Class.forName
Date Tue, 06 Sep 2005 02:18:42 GMT
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 <james@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
> 
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message