3 Replies Latest reply on Dec 21, 2006 8:56 AM by Adrian Brock

    JBMICROCONT-132 todo?

    Ales Justin Master

       


      When the ValueConvertor can't find a property editor it falls back to looking for
      a constructor with a single String parameter.

      This is probably not correct in some cases.

      It should be looking for a static toValue(String) before looking for the constructor.


      What's there to do?
      This is already in the ValueConvertor class.

      improve <init>(String) might not be relevent?
      


      Should we relax the String.class constraint for the value class, and just use whatever value's class type is.
      Or using the same String.class contraint with constructor on the value.toString()

        • 1. Re: JBMICROCONT-132 todo?
          Adrian Brock Master

          Rather than using

          public MyType(String) { ... }
          


          It should first check for a static method
          public 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?
            Ales Justin Master

            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 Master

              Ah ok.

              I must have already done it and forgetten about it. :-)