Java Code Examples for com.fasterxml.jackson.databind.JavaType#isMapLikeType()

The following examples show how to use com.fasterxml.jackson.databind.JavaType#isMapLikeType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModelResolverExt.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 2
Source File: JacksonJsonDataFormatMapper.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
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);
    }
  }
}
 
Example 3
Source File: AbstractVariablesResource.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
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);
    }
  }
}
 
Example 4
Source File: TypeFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;
}