[jboss-dev-forums] [Design the new POJO MicroContainer] - Re: JBMICROCONT-132 todo?

alesj do-not-reply at jboss.com
Thu Dec 21 08:08:31 EST 2006


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)?

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3995582#3995582

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3995582



More information about the jboss-dev-forums mailing list