Example usage for org.springframework.data.util TypeInformation getActualType

List of usage examples for org.springframework.data.util TypeInformation getActualType

Introduction

In this page you can find the example usage for org.springframework.data.util TypeInformation getActualType.

Prototype

@Nullable
TypeInformation<?> getActualType();

Source Link

Document

Transparently returns the java.util.Map value type if the type is a java.util.Map , returns the component type if the type #isCollectionLike() or the simple type if none of this applies.

Usage

From source file:org.springframework.data.rest.webmvc.json.PersistentEntityToJsonSchemaConverter.java

private List<AbstractJsonSchemaProperty<?>> getPropertiesFor(Class<?> type, final ResourceMetadata metadata,
        final Definitions definitions) {

    final PersistentEntity<?, ?> entity = entities.getPersistentEntity(type);
    final JacksonMetadata jackson = new JacksonMetadata(objectMapper, type);

    if (entity == null) {
        return Collections.<AbstractJsonSchemaProperty<?>>emptyList();
    }//w w  w  .  ja va  2  s.co  m

    JsonSchemaPropertyRegistrar registrar = new JsonSchemaPropertyRegistrar(jackson);

    for (BeanPropertyDefinition definition : jackson) {

        PersistentProperty<?> persistentProperty = entity.getPersistentProperty(definition.getInternalName());

        // First pass, early drops to avoid unnecessary calculation
        if (persistentProperty != null) {

            if (persistentProperty.isIdProperty() && !configuration.isIdExposedFor(type)) {
                continue;
            }

            if (persistentProperty.isVersionProperty()) {
                continue;
            }

            if (!definition.couldSerialize()) {
                continue;
            }
        }

        AnnotatedMember primaryMember = definition.getPrimaryMember();

        if (primaryMember == null) {
            continue;
        }

        TypeInformation<?> propertyType = persistentProperty == null
                ? ClassTypeInformation.from(primaryMember.getRawType())
                : persistentProperty.getTypeInformation();
        TypeInformation<?> actualPropertyType = propertyType.getActualType();
        Class<?> rawPropertyType = propertyType.getType();

        JsonSchemaFormat format = configuration.getMetadataConfiguration().getSchemaFormatFor(rawPropertyType);
        ResourceDescription description = persistentProperty == null
                ? jackson.getFallbackDescription(metadata, definition)
                : getDescriptionFor(persistentProperty, metadata);
        JsonSchemaProperty property = getSchemaProperty(definition, propertyType, description);

        boolean isSyntheticProperty = persistentProperty == null;
        boolean isNotWritable = !isSyntheticProperty && !persistentProperty.isWritable();
        boolean isJacksonReadOnly = !isSyntheticProperty && jackson.isReadOnly(persistentProperty);

        if (isSyntheticProperty || isNotWritable || isJacksonReadOnly) {
            property = property.withReadOnly();
        }

        if (format != null) {

            // Types with explicitly registered format -> value object with format
            registrar.register(property.withFormat(format), actualPropertyType);
            continue;
        }

        Pattern pattern = configuration.getMetadataConfiguration().getPatternFor(rawPropertyType);

        if (pattern != null) {
            registrar.register(property.withPattern(pattern), actualPropertyType);
            continue;
        }

        if (jackson.isValueType()) {
            registrar.register(property.with(STRING_TYPE_INFORMATION), actualPropertyType);
            continue;
        }

        if (persistentProperty == null) {
            registrar.register(property, actualPropertyType);
            continue;
        }

        if (configuration.isLookupType(persistentProperty.getActualType())) {
            registrar.register(property.with(propertyType), actualPropertyType);
        } else if (associations.isLinkableAssociation(persistentProperty)) {
            registrar.register(property.asAssociation(), null);
        } else {

            if (persistentProperty.isEntity()) {

                if (!definitions.hasDefinitionFor(propertyType)) {
                    definitions.addDefinition(propertyType,
                            new Item(propertyType, getNestedPropertiesFor(persistentProperty, definitions)));
                }

                registrar.register(property.with(propertyType, Definitions.getReference(propertyType)),
                        actualPropertyType);

            } else {

                registrar.register(property.with(propertyType), actualPropertyType);
            }
        }
    }

    return registrar.getProperties();
}

From source file:org.springframework.data.elasticsearch.core.MappingBuilder.java

private static boolean isEntity(java.lang.reflect.Field field) {
    TypeInformation typeInformation = ClassTypeInformation.from(field.getType());
    TypeInformation<?> actualType = typeInformation.getActualType();
    boolean isComplexType = actualType == null ? false : !SIMPLE_TYPE_HOLDER.isSimpleType(actualType.getType());
    return isComplexType && !actualType.isCollectionLike()
            && !Map.class.isAssignableFrom(typeInformation.getType());
}

From source file:org.springframework.data.mapping.PropertyPath.java

/**
 * Creates a leaf {@link PropertyPath} (no nested ones with the given name and owning type.
 * // ww w .  j av a  2  s. c o m
 * @param name must not be {@literal null} or empty.
 * @param owningType must not be {@literal null}.
 * @param base the {@link PropertyPath} previously found.
 */
PropertyPath(String name, TypeInformation<?> owningType, List<PropertyPath> base) {

    Assert.hasText(name);
    Assert.notNull(owningType);

    String propertyName = name.matches(ALL_UPPERCASE) ? name : StringUtils.uncapitalize(name);
    boolean isSpecli = org.apache.commons.lang3.StringUtils.contains(propertyName, "*");

    String _propertyName = isSpecli
            ? propertyName.replaceAll("\\*(.*)", org.apache.commons.lang3.StringUtils.EMPTY)
            : propertyName;

    if (propertyName.startsWith("iF")) {
        _propertyName = "name";
    }
    TypeInformation<?> propertyType = owningType.getProperty(_propertyName);

    if (!isSpecli) {
        if (propertyType == null) {
            throw new PropertyReferenceException(propertyName, owningType, base);
        }
    } else {

        if (true) {
            //for debug
        }

    }

    this.owningType = owningType;
    this.isCollection = propertyType == null ? false : propertyType.isCollectionLike();

    if (propertyType == null) {
        throw new PropertyReferenceException(_propertyName, owningType, base);
    }
    this.type = propertyType.getActualType();
    this.name = propertyName;
}

From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java

/**
 * Adds custom type information to the given {@link DBObject} if necessary. That is if the value is not the same as
 * the one given. This is usually the case if you store a subtype of the actual declared type of the property.
 * //from   w  ww .j a va 2  s .  co  m
 * @param type
 * @param value must not be {@literal null}.
 * @param dbObject must not be {@literal null}.
 */
protected void addCustomTypeKeyIfNecessary(TypeInformation<?> type, Object value, DBObject dbObject) {

    TypeInformation<?> actualType = type != null ? type.getActualType() : type;
    Class<?> reference = actualType == null ? Object.class : actualType.getType();

    boolean notTheSameClass = !value.getClass().equals(reference);
    if (notTheSameClass) {
        typeMapper.writeType(value.getClass(), dbObject);
    }
}

From source file:org.springframework.data.neo4j.support.Neo4jTemplate.java

@SuppressWarnings("unchecked")
public Object query(String statement, Map<String, Object> params, final TypeInformation<?> typeInformation) {
    final TypeInformation<?> actualType = typeInformation.getActualType();
    final Class<Object> targetType = (Class<Object>) actualType.getType();
    final Result<Object> result = queryEngineFor(QueryType.Cypher).query(statement, params);
    final Class<? extends Iterable<Object>> containerType = (Class<? extends Iterable<Object>>) typeInformation
            .getType();/*from   w  w  w.  j av a 2  s.  c o m*/
    if (EndResult.class.isAssignableFrom(containerType)) {
        return result;
    }
    if (actualType.isMap()) {
        return result;
    }
    if (typeInformation.isCollectionLike()) {
        return result.to(targetType).as(containerType);
    }
    return result.to(targetType).single();
}