View Javadoc

    1   package org.codehaus.xfire.aegis.type.java5;
    2
    3   import java.beans.PropertyDescriptor;
    4   import java.lang.reflect.Method;
    5   import java.lang.reflect.ParameterizedType;
    6
    7   import javax.xml.namespace.QName;
    8
    9   import org.codehaus.xfire.aegis.type.AbstractTypeCreator;
    10  import org.codehaus.xfire.aegis.type.Type;
    11  import org.codehaus.xfire.aegis.type.basic.BeanType;
    12  import org.codehaus.xfire.util.NamespaceHelper;
    13  import org.codehaus.xfire.util.ServiceUtils;
    14
    15  public class Java5TypeCreator
    16      extends AbstractTypeCreator
    17  {
    18
    19      @Override
    20      public TypeClassInfo createClassInfo(Method m, int index)
    21      {
    22          if (index >= 0)
    23          {
    24              TypeClassInfo info;
    25              java.lang.reflect.Type genericType = m.getGenericParameterTypes()[index];
    26              if(genericType instanceof Class)
    27              {
    28                  info = nextCreator.createClassInfo(m, index);
    29              }
    30              else
    31              {
    32                  info = new TypeClassInfo();
    33                  info.setGenericType(genericType);
    34              }
    35              info.setTypeClass(m.getParameterTypes()[index]);
    36              if(m.getParameterAnnotations()[index].length > 0)
    37                  info.setAnnotations(m.getParameterAnnotations()[index]);
    38              return info;
    39          }
    40          else
    41          {
    42              java.lang.reflect.Type genericReturnType = m.getGenericReturnType();
    43              TypeClassInfo info;
    44              if(genericReturnType instanceof Class)
    45              {
    46                  info = nextCreator.createClassInfo(m, index);
    47              }
    48              else
    49              {
    50                  info = new TypeClassInfo();
    51                  info.setGenericType(genericReturnType);
    52              }
    53              info.setTypeClass(m.getReturnType());
    54              if(m.getAnnotations().length > 0)
    55                  info.setAnnotations(m.getAnnotations());
    56              return info;
    57          }
    58      }
    59
    60      @Override
    61      public TypeClassInfo createClassInfo(PropertyDescriptor pd)
    62      {
    63          TypeClassInfo info = createBasicClassInfo(pd.getPropertyType());
    64          info.setGenericType(pd.getReadMethod().getGenericReturnType());
    65          info.setAnnotations(pd.getReadMethod().getAnnotations());
    66
    67          XmlElement el = pd.getReadMethod().getAnnotation(XmlElement.class);
    68          if (el != null && !el.type().equals(Type.class))
    69          {
    70              info.setType(el.type());
    71          }
    72
    73          XmlAttribute att = pd.getReadMethod().getAnnotation(XmlAttribute.class);
    74          if (att != null && !att.type().equals(Type.class))
    75          {
    76              info.setType(att.type());
    77          }
    78
    79          return info;
    80      }
    81
    82      @Override
    83      public Type createCollectionType(TypeClassInfo info)
    84      {
    85          Object genericType = info.getGenericType();
    86          Class paramClass = Object.class;
    87          if (genericType instanceof ParameterizedType)
    88          {
    89              ParameterizedType type = (ParameterizedType) genericType;
    90
    91              if (type.getActualTypeArguments()[0] instanceof Class)
    92              {
    93                  paramClass = (Class) type.getActualTypeArguments()[0];
    94              }
    95          }
    96
    97          return super.createCollectionType(info, paramClass);
    98      }
    99
    100     @Override
    101     public Type createDefaultType(TypeClassInfo info)
    102     {
    103         BeanType type = new BeanType(new AnnotatedTypeInfo(getTypeMapping(), info.getTypeClass()));
    104         type.setTypeMapping(getTypeMapping());
    105         type.setSchemaType(createQName(info.getTypeClass()));
    106
    107         return type;
    108     }
    109
    110     @Override
    111     public Type createEnumType(TypeClassInfo info)
    112     {
    113         EnumType type = new EnumType();
    114
    115         type.setSchemaType(createQName(info.getTypeClass()));
    116         type.setTypeClass(info.getTypeClass());
    117         type.setTypeMapping(getTypeMapping());
    118
    119         return type;
    120     }
    121
    122     @Override
    123     public QName createQName(Class typeClass)
    124     {
    125         String name = null;
    126         String ns = null;
    127
    128         XmlType xtype = (XmlType) typeClass.getAnnotation(XmlType.class);
    129         if (xtype != null)
    130         {
    131             name = xtype.name();
    132             ns = xtype.namespace();
    133         }
    134
    135         String clsName = typeClass.getName();
    136         if (name == null || name.length() == 0)
    137             name = ServiceUtils.makeServiceNameFromClassName(typeClass);
    138
    139         if (ns == null || ns.length() == 0)
    140             ns = NamespaceHelper.makeNamespaceFromClassName(clsName, "http");
    141
    142         return new QName(ns, name);
    143     }
    144
    145     @Override
    146     protected boolean isEnum(Class javaType)
    147     {
    148         return javaType.isEnum();
    149     }
    150 }