commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jon Steelman" <JSteel...@incomm.com>
Subject RE: [Digester] enums in ConvertUtils.register
Date Wed, 15 Jun 2005 08:30:57 GMT
I wrote the following code that works on jdk1.5 *and* I expect to be
backwards compatible to jdk1.1:

public Object checkForEnum(String stringValue, Class clazz) {
    Class superclass = clazz.getSuperclass();
    System.out.println("superClass = " + superclass);
    if (superclass.getName().equals("java.lang.Enum")) {
        // Only makes it here if the jdk is 1.5+ and class is an enum
        Method valueOf = null;
        try {
            valueOf = clazz.getMethod("valueOf",new Class[]
{String.class});
        }
        catch (NoSuchMethodException e) {
            // Should never get here! All enums have this dynamic method
            e.printStackTrace();
        }
        Object enm = null;
        try {
            enm = valueOf.invoke(null,new Object[]{stringValue});
        }
        catch (IllegalAccessException e) {
            // Should never get here as the method is always public.
            e.printStackTrace();
        }
        catch (InvocationTargetException e) {
            // Should never get here as the target object is 
		// null thanks to method being static/
            e.printStackTrace();
        }
        // IllegalArgumentException is possible when the 
        return enm;
    }
}

Can it be incorporated?

Thanks,
Jon

-----Original Message-----
From: Simon Kitching [mailto:skitching@apache.org] 
Sent: Wednesday, June 15, 2005 3:45 AM
To: Jakarta Commons Users List
Subject: RE: [Digester] enums in ConvertUtils.register

On Wed, 2005-06-15 at 03:19 -0400, Jon Steelman wrote:
> However, it brings up the point that ConvertUtils.convert(String,
Class)
> should automatically handle the case of the Class is an enum by using
> the enum's built in valueOf(String) method so that a custom Converter
is
> not required. Is this the forum to make that suggestion?

Yes, this is definitely the right place to discuss things like this.

The problem is that if we add that code to beanutils then beanutils will
no longer work in pre-1.5 jvms. Offhand I can't think of a reasonable
way to support 1.5 enums without breaking earlier java versions. And
having separate versions of beanutils for the two java platforms would
be a major nuisance.

Any ideas how this could be implemented?

Regards,

Simon



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message