content: package com.alibaba.android.arouter.compiler.utils;
  
  import com.alibaba.android.arouter.facade.enums.TypeKind;
  
  import javax.lang.model.element.Element;
  import javax.lang.model.type.TypeMirror;
  import javax.lang.model.util.Elements;
  import javax.lang.model.util.Types;
  
  import static com.alibaba.android.arouter.compiler.utils.Consts.BOOLEAN;
  import static com.alibaba.android.arouter.compiler.utils.Consts.BYTE;
  import static com.alibaba.android.arouter.compiler.utils.Consts.DOUBEL;
  import static com.alibaba.android.arouter.compiler.utils.Consts.FLOAT;
  import static com.alibaba.android.arouter.compiler.utils.Consts.INTEGER;
  import static com.alibaba.android.arouter.compiler.utils.Consts.LONG;
  import static com.alibaba.android.arouter.compiler.utils.Consts.PARCELABLE;
  import static com.alibaba.android.arouter.compiler.utils.Consts.SERIALIZABLE;
  import static com.alibaba.android.arouter.compiler.utils.Consts.SHORT;
  import static com.alibaba.android.arouter.compiler.utils.Consts.STRING;
  import static com.alibaba.android.arouter.compiler.utils.Consts.CHAR;
  
  /**
   * Utils for type exchange
   *
   * @author zhilong <a href="mailto:zhilong.lzl@alibaba-inc.com">Contact me.</a>
   * @version 1.0
   * @since 2017/2/21 下午1:06
   */
  public class TypeUtils {
  
      private Types types;
      private TypeMirror parcelableType;
      private TypeMirror serializableType;
  
      public TypeUtils(Types types, Elements elements) {
          this.types = types;
  
          parcelableType = elements.getTypeElement(PARCELABLE).asType();
          serializableType = elements.getTypeElement(SERIALIZABLE).asType();
      }
  
      /**
       * Diagnostics out the true java type
       *
       * @param element Raw type
       * @return Type class of java
       */
      public int typeExchange(Element element) {
          TypeMirror typeMirror = element.asType();
  
          // Primitive
          if (typeMirror.getKind().isPrimitive()) {
              return element.asType().getKind().ordinal();
          }
  
          switch (typeMirror.toString()) {
              case BYTE:
                  return TypeKind.BYTE.ordinal();
              case SHORT:
                  return TypeKind.SHORT.ordinal();
              case INTEGER:
                  return TypeKind.INT.ordinal();
              case LONG:
                  return TypeKind.LONG.ordinal();
              case FLOAT:
                  return TypeKind.FLOAT.ordinal();
              case DOUBEL:
                  return TypeKind.DOUBLE.ordinal();
              case BOOLEAN:
                  return TypeKind.BOOLEAN.ordinal();
              case CHAR:
                  return TypeKind.CHAR.ordinal();
              case STRING:
                  return TypeKind.STRING.ordinal();
              default:
                  // Other side, maybe the PARCELABLE or SERIALIZABLE or OBJECT.
                  if (types.isSubtype(typeMirror, parcelableType)) {
                      // PARCELABLE
                      return TypeKind.PARCELABLE.ordinal();
                  } else if (types.isSubtype(typeMirror, serializableType)) {
                      // SERIALIZABLE
                      return TypeKind.SERIALIZABLE.ordinal();
                  } else {
                      return TypeKind.OBJECT.ordinal();
                  }
          }
      }
  }
  