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          return info;
    68      }
    69
    70      @Override
    71      public Type createCollectionType(TypeClassInfo info)
    72      {
    73          Object genericType = info.getGenericType();
    74          Class paramClass = Object.class;
    75          if (genericType instanceof ParameterizedType)
    76          {
    77              ParameterizedType type = (ParameterizedType) genericType;
    78
    79              if (type.getActualTypeArguments()[0] instanceof Class)
    80              {
    81                  paramClass = (Class) type.getActualTypeArguments()[0];
    82              }
    83          }
    84
    85          return super.createCollectionType(info, paramClass);
    86      }
    87
    88      @Override
    89      public Type createDefaultType(TypeClassInfo info)
    90      {
    91          BeanType type = new BeanType(new AnnotatedTypeInfo(getTypeMapping(), info.getTypeClass()));
    92          type.setTypeMapping(getTypeMapping());
    93          type.setSchemaType(createQName(info.getTypeClass()));
    94
    95          return type;
    96      }
    97
    98      @Override
    99      public Type createEnumType(TypeClassInfo info)
    100     {
    101         EnumType type = new EnumType();
    102
    103         type.setSchemaType(createQName(info.getTypeClass()));
    104         type.setTypeClass(info.getTypeClass());
    105         type.setTypeMapping(getTypeMapping());
    106
    107         return type;
    108     }
    109
    110     @Override
    111     public QName createQName(Class typeClass)
    112     {
    113         String name = null;
    114         String ns = null;
    115
    116         XmlType xtype = (XmlType) typeClass.getAnnotation(XmlType.class);
    117         if (xtype != null)
    118         {
    119             name = xtype.name();
    120             ns = xtype.namespace();
    121         }
    122
    123         String clsName = typeClass.getName();
    124         if (name == null || name.length() == 0)
    125             name = ServiceUtils.makeServiceNameFromClassName(typeClass);
    126
    127         if (ns == null || ns.length() == 0)
    128             ns = NamespaceHelper.makeNamespaceFromClassName(clsName, "http");
    129
    130         return new QName(ns, name);
    131     }
    132
    133     @Override
    134     protected boolean isEnum(Class javaType)
    135     {
    136         return javaType.isEnum();
    137     }
    138 }