This content has been marked as final.
Show 3 replies
-
1. Re: JBMICROCONT-132 todo?
adrian.brock Dec 21, 2006 7:55 AM (in response to alesj)Rather than using
public MyType(String) { ... }
It should first check for a static methodpublic static MyType valueOf(String) { ... }
In fact, I'd like to remove to use of the constructor since it will be incorrect
in some circumstances. -
2. Re: JBMICROCONT-132 todo?
alesj Dec 21, 2006 8:08 AM (in response to alesj)As I can see
public static Object convertValue(Class<? extends Object> clazz, Object value) throws Throwable { if (clazz == null) throw new IllegalArgumentException("Null class"); if (value == null) return null; Class<? extends Object> valueClass = value.getClass(); 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(); } } Object result = null; // 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())) result = ReflectionUtils.invoke(null, method, new Object[]{value}); } catch (Exception ignored) { } if (result == null) { // TODO JBMICROCONT-132 improve <init>(String) might not be relevent? if (valueClass == String.class) { try { Constructor constructor = clazz.getConstructor(valueClass); if (Modifier.isPublic(constructor.getModifiers())) result = ReflectionUtils.newInstance(constructor, new Object[]{value}); } catch (Exception ignored) { } } } // try progression result = progressValue(clazz, result); // return result if progression supported, else the old value return result != null ? result : value; }
it already checks first for valueOf method.
So I remove the usage of constructor (and test for backcompatibility)? -
3. Re: JBMICROCONT-132 todo?
adrian.brock Dec 21, 2006 8:56 AM (in response to alesj)Ah ok.
I must have already done it and forgetten about it. :-)