@ SuppressWarnings ( "unchecked" ) public static Object convertValue ( Class < ? > clazz , Object value , boolean replaceProperties , boolean trim ) throws Throwable { if ( clazz == null ) return value ; if ( value == null ) return null ; Class < ? > valueClass = value . getClass ( ) ; // If we have a string, trim and replace any system properties when requested if ( valueClass == String . class ) { String string = ( String ) value ; if ( trim ) string = string . trim ( ) ; if ( replaceProperties ) value = PropertiesValueResolver . replaceProperties ( string ) ; } if ( clazz . isAssignableFrom ( valueClass ) ) return value ; // First see if this is an Enum if ( clazz . isEnum ( ) ) { Class < ? extends Enum > eclazz = clazz . asSubclass ( Enum . class ) ; return Enum . valueOf ( eclazz , value . toString ( ) ) ; } // Next look for a property editor if ( valueClass == String . class ) { PropertyEditor editor = PropertyEditorManager . findEditor ( clazz ) ; if ( editor != null ) { editor . setAsText ( ( String ) value ) ; return editor . getValue ( ) ; } } // Try a static clazz.valueOf(value) try { Method method = clazz . getMethod ( "valueOf" , valueClass ) ; int modifiers = method . getModifiers ( ) ; if ( Modifier . isPublic ( modifiers ) && Modifier . isStatic ( modifiers ) && clazz . isAssignableFrom ( method . getReturnType ( ) ) ) return method . invoke ( null , value ) ; } catch ( Exception ignored ) { } if ( valueClass == String . class ) { try { Constructor constructor = clazz . getConstructor ( valueClass ) ; if ( Modifier . isPublic ( constructor . getModifiers ( ) ) ) return constructor . newInstance ( value ) ; } catch ( Exception ignored ) { } } return value ; }
Convert a value