示例#1
0
  /**
   * Finds the map type for the specified type mirror, if it exists.
   *
   * @param typeMirror The type mirror.
   * @param context The context
   * @return The map type or null.
   */
  public static MapType findMapType(TypeMirror typeMirror, EnunciateJacksonContext context) {
    if (!(typeMirror instanceof DeclaredType)) {
      return null;
    }

    DeclaredType declaredType = (DeclaredType) typeMirror;
    TypeElement element = (TypeElement) declaredType.asElement();
    if (element == null) {
      return null;
    } else {
      String fqn = element.getQualifiedName().toString();
      @SuppressWarnings("unchecked")
      Map<String, MapType> mapTypes =
          (Map<String, MapType>) context.getContext().getProperty(PROPERTY_MAP_TYPES);
      if (mapTypes == null) {
        mapTypes = new HashMap<String, MapType>();
        context.getContext().setProperty(PROPERTY_MAP_TYPES, mapTypes);
      }

      MapType mapType = mapTypes.get(fqn);
      if (mapType != null) {
        return mapType;
      } else {
        DeclaredType declaredMapType = findMapTypeDeclaration(declaredType, context);
        if (declaredMapType == null) {
          return null;
        }

        MapType newMapType =
            new MapType(declaredType, context.getContext().getProcessingEnvironment());
        mapTypes.put(fqn, newMapType);

        TypeMirror keyType = null;
        TypeMirror valueType = null;

        List<? extends TypeMirror> typeArgs = declaredMapType.getTypeArguments();
        if ((typeArgs != null) && (typeArgs.size() == 2)) {
          Iterator<? extends TypeMirror> argIt = typeArgs.iterator();
          keyType = argIt.next();
          valueType = argIt.next();
        }

        if ((keyType == null) || (valueType == null)) {
          TypeMirror objectType =
              TypeMirrorUtils.objectType(context.getContext().getProcessingEnvironment());
          keyType = objectType;
          valueType = objectType;
        }

        TypeMirror mapKeyType = findMapType(keyType, context);
        newMapType.keyType = mapKeyType == null ? keyType : mapKeyType;

        TypeMirror mapValueType = findMapType(valueType, context);
        newMapType.valueType = mapValueType == null ? valueType : mapValueType;

        return newMapType;
      }
    }
  }
示例#2
0
  private static DeclaredType findMapTypeDeclaration(
      TypeMirror typeMirror, EnunciateJacksonContext context) {
    if (!(typeMirror instanceof DeclaredType)) {
      return null;
    }

    DeclaredType declaredType = (DeclaredType) typeMirror;
    TypeElement element = (TypeElement) declaredType.asElement();
    String fqn = element.getQualifiedName().toString();
    if (Map.class.getName().equals(fqn)) {
      return declaredType;
    }

    AdapterType adapterType = JacksonUtil.findAdapterType(element, context);
    if (adapterType != null) {
      return findMapTypeDeclaration(adapterType.getAdaptingType(), context);
    }

    DeclaredType mapType = null;
    Types typeUtils = context.getContext().getProcessingEnvironment().getTypeUtils();
    List<? extends TypeMirror> supers = typeUtils.directSupertypes(declaredType);
    for (TypeMirror superInterface : supers) {
      mapType = findMapTypeDeclaration(superInterface, context);
      if (mapType != null) {
        break;
      }
    }

    return mapType;
  }