KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > reflect > plugins > ValueConvertor


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.reflect.plugins;
23
24 import java.beans.PropertyEditor JavaDoc;
25 import java.beans.PropertyEditorManager JavaDoc;
26 import java.lang.reflect.Constructor JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.lang.reflect.Modifier JavaDoc;
29
30 import org.jboss.logging.Logger;
31 import org.jboss.reflect.plugins.introspection.ReflectionUtils;
32 import org.jboss.util.propertyeditor.PropertyEditors;
33
34 /**
35  * PropertyEditorHelper.
36  *
37  * @todo fix the introspection assumption
38  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
39  * @version $Revision: 58414 $
40  */

41 public class ValueConvertor
42 {
43    /** The log */
44    private static final Logger log = Logger.getLogger(ValueConvertor.class);
45    
46    static
47    {
48       try
49       {
50          PropertyEditors.init();
51       }
52       catch (Throwable JavaDoc t)
53       {
54          log.debug("Unable to initialise property editors", t);
55       }
56    }
57    
58    /**
59     * Convert a value
60     *
61     * @todo look at integer progression, e.g. Integer.longValue()
62     * @param clazz the class
63     * @param value the value
64     * @return the value or null if there is no editor
65     * @throws Throwable for any error
66     */

67    public static Object JavaDoc convertValue(Class JavaDoc<? extends Object JavaDoc> clazz, Object JavaDoc value) throws Throwable JavaDoc
68    {
69       if (clazz == null)
70          throw new IllegalArgumentException JavaDoc("Null class");
71       if (value == null)
72          return null;
73       
74       Class JavaDoc<? extends Object JavaDoc> valueClass = value.getClass();
75       if (clazz.isAssignableFrom(valueClass))
76          return value;
77
78       // First see if this is an Enum
79
if (clazz.isEnum())
80       {
81          Class JavaDoc<? extends Enum JavaDoc> eclazz = clazz.asSubclass(Enum JavaDoc.class);
82          return Enum.valueOf(eclazz, value.toString());
83       }
84
85       // Next look for a property editor
86
if (valueClass == String JavaDoc.class)
87       {
88          PropertyEditor JavaDoc editor = PropertyEditorManager.findEditor(clazz);
89          if (editor != null)
90          {
91             editor.setAsText((String JavaDoc) value);
92             return editor.getValue();
93          }
94       }
95
96       // Try a static clazz.valueOf(value)
97
try
98       {
99          Method JavaDoc method = clazz.getMethod("valueOf", new Class JavaDoc[] { valueClass });
100          int modifiers = method.getModifiers();
101          if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)
102                && clazz.isAssignableFrom(method.getReturnType()))
103             return ReflectionUtils.invoke(null, method, new Object JavaDoc[] { value });
104       }
105       catch (Exception JavaDoc ignored)
106       {
107       }
108       
109
110       // TODO improve <init>(String) might not be relevent?
111
if (valueClass == String JavaDoc.class)
112       {
113          try
114          {
115             Constructor JavaDoc constructor = clazz.getConstructor(new Class JavaDoc[] { String JavaDoc.class });
116             if (Modifier.isPublic(constructor.getModifiers()))
117                return ReflectionUtils.newInstance(constructor, new Object JavaDoc[] { value });
118          }
119          catch (Exception JavaDoc ignored)
120          {
121          }
122       }
123       
124       return value;
125    }
126 }
127
Popular Tags