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

The following examples show how to use com.fasterxml.jackson.databind.JavaType#isPrimitive() . 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: DeserializerSchemaManager.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Override
protected <T> SchemaEx<T> newMessageSchema(Message message, JavaType javaType) {
  if (ProtoUtils.isWrapProperty(message) && javaType.getRawClass() != PropertyWrapper.class) {
    Field protoField = message.getField(1);
    if (javaType.isJavaLangObject()) {
      javaType =
          protoField.isRepeated() && !protoField.isMap() ? ProtoConst.LIST_TYPE
              : ProtoConst.MAP_TYPE;
    }

    if (javaType.isPrimitive()) {
      javaType = TypeFactory.defaultInstance()
          .constructParametricType(PropertyWrapper.class, TypesUtil.primitiveJavaTypeToWrapper(javaType));
    } else {
      javaType = TypeFactory.defaultInstance().constructParametricType(PropertyWrapper.class, javaType);
    }
  }

  if (javaType.isJavaLangObject()) {
    javaType = ProtoConst.MAP_TYPE;
  }

  return new MessageReadSchema<>(protoMapper, message, javaType);
}
 
Example 2
Source File: SchemaManager.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public FieldMapEx<Map<Object, Object>> createMapFields(Message message, Map<String, Type> types) {
  List<FieldSchema<Map<Object, Object>>> fieldSchemas = new ArrayList<>();
  for (Field protoField : message.getFields()) {
    PropertyDescriptor propertyDescriptor = new PropertyDescriptor();

    JavaType javaType = getParameterType(types, protoField.getName());
    if (javaType.isPrimitive()) {
      javaType = TypeFactory.defaultInstance().constructType(ClassUtils.primitiveToWrapper(javaType.getRawClass()));
    }
    propertyDescriptor.setJavaType(javaType);
    propertyDescriptor.setGetter(new MapGetter<>(protoField.getName()));
    propertyDescriptor.setSetter(new MapSetter<>(protoField.getName()));

    FieldSchema<Map<Object, Object>> fieldSchema = createSchemaField(protoField, propertyDescriptor);
    fieldSchemas.add(fieldSchema);
  }

  return FieldMapEx.createFieldMap(fieldSchemas);
}
 
Example 3
Source File: ResponseRootDeserializer.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public static boolean needConvert(Object obj, JavaType invocationTimeType) {
  if (obj == null || ClassUtils.isPrimitiveOrWrapper(obj.getClass()) || invocationTimeType.isPrimitive()
      || ProtoConst.OBJECT_TYPE.equals(invocationTimeType)) {
    return false;
  }

  if (obj.getClass() == invocationTimeType.getRawClass()) {
    return false;
  }

  if (invocationTimeType.getRawClass().isAssignableFrom(obj.getClass())) {
    if (invocationTimeType.getContentType() == null) {
      return false;
    }
  }

  return true;
}
 
Example 4
Source File: MarshallerService.java    From dawnsci with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean useForType(JavaType type) {
	Class<?> clazz = type.getRawClass();
	// Note: This does not work well with generics defined at or above the same scope as the call
	// to marshal or unmarshal. As a result, only use such generics there when dealing with a primitive
	// type or registered class, as these will cope with the idResolver.

	// We can lookup the class in the registry, for marshalling and unmarshalling.
	Boolean registryHasClass = registry.isClass(clazz);

	// We only ever declare as object if we intend to use one of our own classes (or a primitive).
	Boolean isObject = (Object.class.equals(clazz));

	// Also include abstract classes and interfaces as these are always defined with a type id. This
	// is not the case for container types, however, so these are excluded.
	Boolean isAbstract = type.isAbstract();
	Boolean isInterface = type.isInterface();
	Boolean isNotContainer = !type.isContainerType();

	// Primitive types are considered abstract, so exclude these as well.
	Boolean isNotPrimitive = !type.isPrimitive();

	return registryHasClass || ((isObject || isAbstract || isInterface) && isNotContainer && isNotPrimitive);
}
 
Example 5
Source File: FieldSchema.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
public FieldSchema(Field protoField, JavaType javaType) {
  this.protoField = protoField;
  this.name = protoField.getName();
  this.fieldNumber = protoField.getTag();
  this.packed = ProtoUtils.isPacked(protoField);

  int wireType = packed && protoField.isRepeated() ? WireFormat.WIRETYPE_LENGTH_DELIMITED
      : FieldTypeUtils.convert(protoField.getType()).wireType;
  this.tag = WireFormat.makeTag(fieldNumber, wireType);
  this.tagSize = ProtobufOutputEx.computeRawVarint32Size(tag);

  this.javaType = javaType;
  this.primitive = javaType.isPrimitive();
}
 
Example 6
Source File: AbstractTypeMaterializer.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
protected boolean _suitableType(JavaType type)
{
    // Future plans may include calling of this method for all kinds of abstract types.
    // So as simple precaution, let's limit kinds of types we will try materialize
    // implementations for.
    if (type.isContainerType() || type.isReferenceType()
            || type.isEnumType() || type.isPrimitive()) {
        return false;
    }
    Class<?> cls = type.getRawClass();
    if ((cls == Number.class)
            // 22-Jun-2016, tatu: As per [#12], avoid these too
            || (cls == Date.class) || (cls == Calendar.class)
            || (cls == CharSequence.class) || (cls == Iterable.class) || (cls == Iterator.class)
            // 06-Feb-2019, tatu: [modules-base#74] and:
            || (cls == java.io.Serializable.class)
    ) {
        return false;
    }

    // Fail on non-public classes, since we can't easily force  access to such
    // classes (unless we tried to generate impl classes in same package)
    if (!Modifier.isPublic(cls.getModifiers())) {
        if (isEnabled(Feature.FAIL_ON_NON_PUBLIC_TYPES)) {
            throw new IllegalArgumentException("Can not materialize implementation of "+cls+" since it is not public ");
        }
        return false;
    }
    return true;
}
 
Example 7
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 8
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 9
Source File: JacksonMessageSerializer.java    From loom with MIT License 4 votes vote down vote up
@Override
public boolean useForType(JavaType t) {
    return !t.isPrimitive() && t.getRawClass() != ZonedDateTime.class;
}
 
Example 10
Source File: ServerModelResolver.java    From proteus with Apache License 2.0 4 votes vote down vote up
@Override
public Schema resolve(AnnotatedType annotatedType, ModelConverterContext context, Iterator<ModelConverter> next)
{
    JavaType classType = TypeFactory.defaultInstance().constructType(annotatedType.getType());
    Class<?> rawClass = classType.getRawClass();
    JavaType resolvedType = classType;

    if ((rawClass != null) &&!resolvedType.isPrimitive())
    {
        if (rawClass.isAssignableFrom(ServerResponse.class))
        {
            resolvedType = classType.containedType(0);
        }
        else if (rawClass.isAssignableFrom(CompletableFuture.class))
        {
            Class<?> futureCls = classType.containedType(0).getRawClass();

            if (futureCls.isAssignableFrom(ServerResponse.class))
            {
                final JavaType futureType = TypeFactory.defaultInstance().constructType(classType.containedType(0));

                resolvedType = futureType.containedType(0);
            }
            else
            {
                resolvedType = classType.containedType(0);
            }
        }

        if (resolvedType != null)
        {
            if (resolvedType.getTypeName().contains("java.lang.Void"))
            {
                resolvedType = TypeFactory.defaultInstance().constructFromCanonical(Void.class.getName());
            }
            else if (resolvedType.getTypeName().contains("Optional"))
            {
                if (resolvedType.getTypeName().contains("java.nio.file.Path"))
                {
                    resolvedType = TypeFactory.defaultInstance().constructParametricType(Optional.class, File.class);
                }

                if (resolvedType.getTypeName().contains("ByteBuffer"))
                {
                    resolvedType = TypeFactory.defaultInstance().constructParametricType(Optional.class, File.class);
                }
            }
            else
            {
                if (resolvedType.getTypeName().contains("java.nio.file.Path"))
                {
                    resolvedType = TypeFactory.defaultInstance().constructFromCanonical(File.class.getName());
                }

                if (resolvedType.getTypeName().contains("ByteBuffer"))
                {
                    resolvedType = TypeFactory.defaultInstance().constructFromCanonical(File.class.getName());
                }
            }

            annotatedType.setType(resolvedType);

        }
    }

    try {

        return super.resolve(annotatedType, context, next);

    } catch (Exception e) {

        log.error("Error processing " + annotatedType + " " + classType + " " + annotatedType.getName(), e);

        return null;
    }
}