View Javadoc
1   package org.andromda.cartridges.support.webservice.client;
2   
3   import java.lang.reflect.Method;
4   import org.apache.axis2.databinding.typemapping.SimpleTypeMapper;
5   
6   /**
7    * The default {@link TypeMapper} implementation.
8    *
9    * @author Chad Brandon
10   */
11  public class DefaultTypeMapper
12      implements TypeMapper
13  {
14      /**
15       * @see org.andromda.cartridges.support.webservice.client.TypeMapper#getObject(Class)
16       */
17      public Object getObject(Class type)
18      {
19          Object object = null;
20          if (type != null)
21          {
22              try
23              {
24                  object = type.newInstance();
25              }
26              catch (Exception exception)
27              {
28                  throw new TypeMapperException(exception);
29              }
30          }
31          return object;
32      }
33  
34      private static final String VALUE_OF = "valueOf";
35  
36      /**
37       * @see org.andromda.cartridges.support.webservice.client.TypeMapper#getObject(Class, String)
38       */
39      @SuppressWarnings("unchecked")
40      public Object getObject(Class type, String value)
41      {
42          Object object = null;
43          if (type != null && value != null)
44          {
45              try
46              {
47                  if (type.isEnum())
48                  {
49                      object = type.getMethod(
50                          VALUE_OF, new Class[]{String.class}).invoke(
51                              type, value);
52                  }
53                  else
54                  {
55                      final Method fromMethod = getEnumerationFromMethod(type);
56                      if (fromMethod != null)
57                      {
58                          object = fromMethod.invoke(type, value);
59                      }
60                  }
61              }
62              catch (Exception exception)
63              {
64                  throw new TypeMapperException(exception);
65              }
66          }
67          return object;
68      }
69  
70      /**
71       * @see org.andromda.cartridges.support.webservice.client.TypeMapper#getStringValue(Object)
72       */
73      public String getStringValue(Object object)
74      {
75          return SimpleTypeMapper.getStringValue(object);
76      }
77  
78      /**
79       * @see org.andromda.cartridges.support.webservice.client.TypeMapper#isSimpleType(Class)
80       */
81      public boolean isSimpleType(Class type)
82      {
83          return java.util.Calendar.class.isAssignableFrom(type) ||
84              java.util.Date.class.isAssignableFrom(type) ||
85              SimpleTypeMapper.isSimpleType(type) ||
86              isEnumeration(type);
87      }
88  
89      /**
90       * Indicates whether or not the given type represents an enumeration.
91       *
92       * @param type the type to check.
93       * @return true/false
94       */
95      private boolean isEnumeration(final Class type)
96      {
97          return isEnumeration(type, getEnumerationFromMethod(type));
98      }
99  
100     /**
101      * Indicates whether or not the given type represents an enumeration by checking
102      * whether the type is an actual "enum" class or the "fromMethod" is not null.
103      *
104      * @param type the type to check.
105      * @param the "from" method used to construct a typesafe enumeration from it's simple type.
106      * @return true/false
107      */
108     private boolean isEnumeration(final Class type, final Method fromMethod)
109     {
110         boolean enumeration = false;
111         if (type != null)
112         {
113             enumeration = type.isEnum();
114             if (!enumeration)
115             {
116                 enumeration = fromMethod != null;
117             }
118         }
119         return enumeration;
120     }
121 
122     private static final String FROM = "from";
123 
124     /**
125      * Gets the "from" method for a type safe enumeration.
126      *
127      * @param type the type.
128      * @return the "from" method (i.e. fromString, etc).
129      */
130     private Method getEnumerationFromMethod(final Class type)
131     {
132         Method fromMethod = null;
133         if (type != null)
134         {
135             // - check for the typesafe enum pattern
136             for (final Method method : type.getMethods())
137             {
138                 if (method.getName().startsWith(FROM))
139                 {
140                     final Class[] parameterTypes = method.getParameterTypes();
141                     if (parameterTypes.length == 1)
142                     {
143                         final Class parameterType = parameterTypes[0];
144                         if (method.getName().equals(FROM + parameterType.getSimpleName()))
145                         {
146                             fromMethod = method;
147                             break;
148                         }
149                     }
150                 }
151             }
152         }
153         return fromMethod;
154     }
155 
156 }