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

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

@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);
}
 
源代码2 项目: servicecomb-java-chassis   文件: SchemaManager.java
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);
}
 
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;
}
 
源代码4 项目: dawnsci   文件: MarshallerService.java
@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);
}
 
源代码5 项目: servicecomb-java-chassis   文件: FieldSchema.java
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();
}
 
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;
}
 
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);
    }
  }
}
 
源代码9 项目: loom   文件: JacksonMessageSerializer.java
@Override
public boolean useForType(JavaType t) {
    return !t.isPrimitive() && t.getRawClass() != ZonedDateTime.class;
}
 
源代码10 项目: proteus   文件: ServerModelResolver.java
@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;
    }
}