com.fasterxml.jackson.databind.JavaType#isMapLikeType ( )源码实例Demo

下面列出了com.fasterxml.jackson.databind.JavaType#isMapLikeType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

private void checkType(JavaType type) {
  // 原子类型/string在java中是abstract的
  if (type.getRawClass().isPrimitive()
      || propertyCreatorMap.containsKey(type.getRawClass())
      || String.class.equals(type.getRawClass())
      || concreteInterfaces.contains(type.getRawClass())) {
    return;
  }

  String msg = "Must be a concrete type.";
  if (type.isMapLikeType()) {
    Class<?> keyTypeClass = type.getKeyType().getRawClass();
    if (!String.class.equals(keyTypeClass)) {
      // swagger中map的key只允许为string
      throw new Error("Type of key in map must be string, but got " + keyTypeClass.getName());
    }
  }

  if (type.isContainerType()) {
    checkType(type.getContentType());
    return;
  }

  if (type.getRawClass().isInterface()) {
    throw new ServiceCombException(type.getTypeName() + " is interface. " + msg);
  }

  if (Modifier.isAbstract(type.getRawClass().getModifiers())) {
    throw new ServiceCombException(type.getTypeName() + " is abstract class. " + msg);
  }
}
 
protected void validateType(JavaType type, DeserializationTypeValidator validator, List<String> invalidTypes) {
  if (!type.isPrimitive()) {
    if (!type.isArrayType()) {
      validateTypeInternal(type, validator, invalidTypes);
    }
    if (type.isMapLikeType()) {
      validateType(type.getKeyType(), validator, invalidTypes);
    }
    if (type.isContainerType() || type.hasContentType()) {
      validateType(type.getContentType(), validator, invalidTypes);
    }
  }
}
 
protected void validateType(JavaType type, DeserializationTypeValidator validator, List<String> invalidTypes) {
  if (!type.isPrimitive()) {
    if (!type.isArrayType()) {
      validateTypeInternal(type, validator, invalidTypes);
    }
    if (type.isMapLikeType()) {
      validateType(type.getKeyType(), validator, invalidTypes);
    }
    if (type.isContainerType() || type.hasContentType()) {
      validateType(type.getContentType(), validator, invalidTypes);
    }
  }
}
 
源代码4 项目: lams   文件: TypeFactory.java
/**
 * Factory method for creating a subtype of given base type, as defined
 * by specified subclass; but retaining generic type information if any.
 * Can be used, for example, to get equivalent of "HashMap&lt;String,Integer&gt;"
 * from "Map&lt;String,Integer&gt;" by giving <code>HashMap.class</code>
 * as subclass.
 */
public JavaType constructSpecializedType(JavaType baseType, Class<?> subclass)
{
    // simple optimization to avoid costly introspection if type-erased type does NOT differ
    final Class<?> rawBase = baseType.getRawClass();
    if (rawBase == subclass) {
        return baseType;
    }

    JavaType newType;

    // also: if we start from untyped, not much to save
    do { // bogus loop to be able to break
        if (rawBase == Object.class) {
            newType = _fromClass(null, subclass, EMPTY_BINDINGS);
            break;
        }
        if (!rawBase.isAssignableFrom(subclass)) {
            throw new IllegalArgumentException(String.format(
                    "Class %s not subtype of %s", subclass.getName(), baseType));
        }
        // A few special cases where we can simplify handling:

        // (1) Original target type has no generics -- just resolve subtype
        if (baseType.getBindings().isEmpty()) {
            newType = _fromClass(null, subclass, EMPTY_BINDINGS);
            break;
        }
        // (2) A small set of "well-known" List/Map subtypes where can take a short-cut
        if (baseType.isContainerType()) {
            if (baseType.isMapLikeType()) {
                if ((subclass == HashMap.class)
                        || (subclass == LinkedHashMap.class)
                        || (subclass == EnumMap.class)
                        || (subclass == TreeMap.class)) {
                    newType = _fromClass(null, subclass,
                            TypeBindings.create(subclass, baseType.getKeyType(), baseType.getContentType()));
                    break;
                }
            } else if (baseType.isCollectionLikeType()) {
                if ((subclass == ArrayList.class)
                        || (subclass == LinkedList.class)
                        || (subclass == HashSet.class)
                        || (subclass == TreeSet.class)) {
                    newType = _fromClass(null, subclass,
                            TypeBindings.create(subclass, baseType.getContentType()));
                    break;
                }
                // 29-Oct-2015, tatu: One further shortcut: there are variants of `EnumSet`,
                //    but they are impl details and we basically do not care...
                if (rawBase == EnumSet.class) {
                    return baseType;
                }
            }
        }
        // (3) Sub-class does not take type parameters -- just resolve subtype
        int typeParamCount = subclass.getTypeParameters().length;
        if (typeParamCount == 0) {
            newType = _fromClass(null, subclass, EMPTY_BINDINGS);
            break;
        }
        // (4) If all else fails, do the full traversal using placeholders
        TypeBindings tb = _bindingsForSubtype(baseType, typeParamCount, subclass);
        newType = _fromClass(null, subclass, tb);

    } while (false);

    // 25-Sep-2016, tatu: As per [databind#1384] also need to ensure handlers get
    //   copied as well
    newType = newType.withHandlersFrom(baseType);
    return newType;
}