Java Code Examples for org.springframework.data.util.ClassTypeInformation#from()

The following examples show how to use org.springframework.data.util.ClassTypeInformation#from() . 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: DefaultArangoTypeMapper.java    From spring-data with Apache License 2.0 6 votes vote down vote up
@Override
public <T> TypeInformation<? extends T> readType(final VPackSlice source, final TypeInformation<T> basicType) {
	Assert.notNull(source, "Source must not be null!");
	Assert.notNull(basicType, "Basic type must not be null!");

	final TypeInformation<?> documentsTargetType = readType(source);

	if (documentsTargetType == null) {
		return basicType;
	}

	final Class<T> rawType = basicType.getType();

	final boolean isMoreConcreteCustomType = rawType == null
			|| rawType.isAssignableFrom(documentsTargetType.getType()) && !rawType.equals(documentsTargetType);

	if (!isMoreConcreteCustomType) {
		return basicType;
	}

	final ClassTypeInformation<?> targetType = ClassTypeInformation.from(documentsTargetType.getType());

	return (TypeInformation<? extends T>) basicType.specialize(targetType);
}
 
Example 2
Source File: MybatisPersistentPropertyImpl.java    From spring-data-mybatis with Apache License 2.0 6 votes vote down vote up
@Nullable
private TypeInformation<?> detectAssociationTargetType() {

	if (!isAssociation()) {
		return null;
	}

	for (Class<? extends Annotation> annotationType : ASSOCIATION_ANNOTATIONS) {

		Annotation annotation = findAnnotation(annotationType);

		if (annotation == null) {
			continue;
		}

		Object entityValue = AnnotationUtils.getValue(annotation, "targetEntity");

		if (entityValue == null || entityValue.equals(void.class)) {
			continue;
		}

		return ClassTypeInformation.from((Class<?>) entityValue);
	}

	return null;
}
 
Example 3
Source File: DefaultPredicateArgumentResolver.java    From java-platform with Apache License 2.0 6 votes vote down vote up
/**
 * Obtains the domain type information from the given method parameter. Will
 * favor an explicitly registered on through
 * {@link QuerydslPredicate#root()} but use the actual type of the method's
 * return type as fallback.
 * 
 * @param parameter
 *            must not be {@literal null}.
 * @return
 */
static TypeInformation<?> extractTypeInfo(MethodParameter parameter) {

	QuerydslPredicate annotation = parameter.getParameterAnnotation(QuerydslPredicate.class);

	if (annotation != null && !Object.class.equals(annotation.root())) {
		return ClassTypeInformation.from(annotation.root());
	}

	Class<?> containingClass = parameter.getContainingClass();
	if (ClassUtils.isAssignable(EntityController.class, containingClass)) {
		ResolvableType resolvableType = ResolvableType.forClass(containingClass);
		return ClassTypeInformation.from(resolvableType.as(EntityController.class).getGeneric(0).resolve());
	}

	return detectDomainType(ClassTypeInformation.fromReturnTypeOf(parameter.getMethod()));
}
 
Example 4
Source File: SpannerPersistentEntityImplTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpressionResolutionFromApplicationContext() {
	SpannerPersistentEntityImpl<EntityWithExpression> entity = new SpannerPersistentEntityImpl<>(
			ClassTypeInformation.from(EntityWithExpression.class));

	ApplicationContext applicationContext = mock(ApplicationContext.class);
	when(applicationContext.getBean("tablePostfix")).thenReturn("something");
	when(applicationContext.containsBean("tablePostfix")).thenReturn(true);

	entity.setApplicationContext(applicationContext);
	assertThat(entity.tableName()).isEqualTo("table_something");
}
 
Example 5
Source File: SpannerPersistentEntityImplTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpressionResolutionWithoutApplicationContext() {
	this.thrown.expect(SpannerDataException.class);
	this.thrown.expectMessage("Error getting table name for EntityWithExpression; " +
			"nested exception is org.springframework.expression.spel.SpelEvaluationException: " +
			"EL1007E: Property or field 'tablePostfix' cannot be found on null");
	SpannerPersistentEntityImpl<EntityWithExpression> entity = new SpannerPersistentEntityImpl<>(
			ClassTypeInformation.from(EntityWithExpression.class));

	entity.tableName();
}
 
Example 6
Source File: FreemarkerTemplateQuery.java    From spring-data-jpa-extra with Apache License 2.0 5 votes vote down vote up
private Query createJpaQuery(String queryString) {
    Class<?> objectType = getQueryMethod().getReturnedObjectType();

    //get original proxy query.
    Query oriProxyQuery;

    //must be hibernate QueryImpl
    NativeQuery query;

    if (useJpaSpec && getQueryMethod().isQueryForEntity()) {
        oriProxyQuery = getEntityManager().createNativeQuery(queryString, objectType);
    } else {
        oriProxyQuery = getEntityManager().createNativeQuery(queryString);
        query = AopTargetUtils.getTarget(oriProxyQuery);
        Class<?> genericType;
        //find generic type
        if (objectType.isAssignableFrom(Map.class)) {
            genericType = objectType;
        } else {
            ClassTypeInformation<?> ctif = ClassTypeInformation.from(objectType);
            TypeInformation<?> actualType = ctif.getActualType();
            genericType = actualType.getType();
        }
        if (genericType != Void.class) {
            QueryBuilder.transform(query, genericType);
        }
    }
    //return the original proxy query, for a series of JPA actions, e.g.:close em.
    return oriProxyQuery;
}
 
Example 7
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 5 votes vote down vote up
private void writeProperty(final Object source, final VPackBuilder sink, final ArangoPersistentProperty property) {
	if (source == null) {
		return;
	}

	final TypeInformation<?> sourceType = ClassTypeInformation.from(source.getClass());
	final String fieldName = property.getFieldName();

	if (property.getRef().isPresent()) {
		if (sourceType.isCollectionLike()) {
			writeReferences(fieldName, source, sink);
		} else {
			writeReference(fieldName, source, sink);
		}
	}

	else if (property.getRelations().isPresent()) {
		// nothing to store
	}

	else if (property.getFrom().isPresent() || property.getTo().isPresent()) {
		if (!sourceType.isCollectionLike()) {
			writeReference(fieldName, source, sink);
		}
	}

	else {
		final Object entity = source instanceof LazyLoadingProxy ? ((LazyLoadingProxy) source).getEntity() : source;
		writeInternal(fieldName, entity, sink, property.getTypeInformation());
	}
}
 
Example 8
Source File: MappingSolrConverter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public <S, R> List<R> read(SolrDocumentList source, Class<R> type) {
	if (source == null) {
		return Collections.emptyList();
	}

	List<R> resultList = new ArrayList<R>(source.size());
	TypeInformation<R> typeInformation = ClassTypeInformation.from(type);
	for (Map<String, ?> item : source) {
		resultList.add(read(typeInformation, item));
	}

	return resultList;
}
 
Example 9
Source File: SpannerPersistentEntityImplTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidTableName() {
	this.thrown.expect(SpannerDataException.class);
	this.thrown.expectMessage(
			"Error getting table name for EntityBadName; nested exception is " +
					"org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException: Only " +
					"letters, numbers, and underscores are allowed in table names: ;DROP TABLE your_table;");

	SpannerPersistentEntityImpl<EntityBadName> entity = new SpannerPersistentEntityImpl<>(
			ClassTypeInformation.from(EntityBadName.class));
	entity.tableName();
}
 
Example 10
Source File: SpannerPersistentEntityImplTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testRawTableName() {
	SpannerPersistentEntityImpl<EntityNoCustomName> entity = new SpannerPersistentEntityImpl<>(
			ClassTypeInformation.from(EntityNoCustomName.class));

	assertThat(entity.tableName()).isEqualTo("entityNoCustomName");
}
 
Example 11
Source File: DatastorePersistentEntityImplTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpressionResolutionWithoutApplicationContext() {
	this.expectedException.expect(SpelEvaluationException.class);
	this.expectedException.expectMessage("Property or field 'kindPostfix' cannot be found on null");
	DatastorePersistentEntityImpl<EntityWithExpression> entity = new DatastorePersistentEntityImpl<>(
			ClassTypeInformation.from(EntityWithExpression.class), null);

	entity.kindName();
}
 
Example 12
Source File: DatastorePersistentEntityImplTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpressionResolutionFromApplicationContext() {
	DatastorePersistentEntityImpl<EntityWithExpression> entity = new DatastorePersistentEntityImpl<>(
			ClassTypeInformation.from(EntityWithExpression.class), null);

	ApplicationContext applicationContext = mock(ApplicationContext.class);
	when(applicationContext.getBean("kindPostfix")).thenReturn("something");
	when(applicationContext.containsBean("kindPostfix")).thenReturn(true);

	entity.setApplicationContext(applicationContext);
	assertThat(entity.kindName()).isEqualTo("kind_something");
}
 
Example 13
Source File: FirestorePersistentEntityImplTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Test
public void testInferCollectionName() {
	FirestorePersistentEntity<Employee> firestorePersistentEntity = new FirestorePersistentEntityImpl<>(
			ClassTypeInformation.from(Employee.class));
	assertThat(firestorePersistentEntity.collectionName()).isEqualTo("employee_table");
}
 
Example 14
Source File: MappingVaultConverter.java    From spring-vault with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "unchecked" })
protected void writePropertyInternal(@Nullable Object obj, SecretDocumentAccessor accessor,
		VaultPersistentProperty prop) {

	if (obj == null) {
		return;
	}

	TypeInformation<?> valueType = ClassTypeInformation.from(obj.getClass());
	TypeInformation<?> type = prop.getTypeInformation();

	if (valueType.isCollectionLike()) {
		List<Object> collectionInternal = createCollection(asCollection(obj), prop);
		accessor.put(prop, collectionInternal);
		return;
	}

	if (valueType.isMap()) {
		Map<String, Object> mapDbObj = createMap((Map<Object, Object>) obj, prop);
		accessor.put(prop, mapDbObj);
		return;
	}

	// Lookup potential custom target type
	Optional<Class<?>> basicTargetType = this.conversions.getCustomWriteTarget(obj.getClass());

	if (basicTargetType.isPresent()) {

		accessor.put(prop, this.conversionService.convert(obj, basicTargetType.get()));
		return;
	}

	VaultPersistentEntity<?> entity = isSubtype(prop.getType(), obj.getClass())
			? this.mappingContext.getRequiredPersistentEntity(obj.getClass())
			: this.mappingContext.getRequiredPersistentEntity(type);

	SecretDocumentAccessor nested = accessor.writeNested(prop);

	writeInternal(obj, nested, entity);
	addCustomTypeKeyIfNecessary(ClassTypeInformation.from(prop.getRawType()), obj, nested);
}
 
Example 15
Source File: FirestorePersistentEntityImplTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Test
public void testSetCollectionName() {
	FirestorePersistentEntity<Student> firestorePersistentEntity = new FirestorePersistentEntityImpl<>(
			ClassTypeInformation.from(Student.class));
	assertThat(firestorePersistentEntity.collectionName()).isEqualTo("student");
}
 
Example 16
Source File: QuerydslPredicateOperationCustomizer.java    From springdoc-openapi with Apache License 2.0 4 votes vote down vote up
/**
 * Extract qdsl bindings querydsl bindings.
 *
 * @param predicate the predicate
 * @return the querydsl bindings
 */
private QuerydslBindings extractQdslBindings(QuerydslPredicate predicate) {
	ClassTypeInformation<?> classTypeInformation = ClassTypeInformation.from(predicate.root());
	TypeInformation<?> domainType = classTypeInformation.getRequiredActualType();

	Optional<Class<? extends QuerydslBinderCustomizer<?>>> bindingsAnnotation = Optional.of(predicate)
			.map(QuerydslPredicate::bindings)
			.map(CastUtils::cast);

	return bindingsAnnotation
			.map(it -> querydslBindingsFactory.createBindingsFor(domainType, it))
			.orElseGet(() -> querydslBindingsFactory.createBindingsFor(domainType));
}
 
Example 17
Source File: DatastorePersistentEntityImplTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Test
public void testTableName() {
	DatastorePersistentEntityImpl<TestEntity> entity = new DatastorePersistentEntityImpl<>(
			ClassTypeInformation.from(TestEntity.class), null);
	assertThat(entity.kindName()).isEqualTo("custom_test_kind");
}
 
Example 18
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void writeInternal(
	final String attribute,
	final Object source,
	final VPackBuilder sink,
	final TypeInformation<?> definedType) {

	final Class<?> rawType = source.getClass();
	final TypeInformation<?> type = ClassTypeInformation.from(rawType);

	if (conversions.isSimpleType(rawType)) {
		final Optional<Class<?>> customWriteTarget = conversions.getCustomWriteTarget(rawType);
		final Class<?> targetType = customWriteTarget.orElse(rawType);
		writeSimple(attribute, conversionService.convert(source, targetType), sink);
	}

	else if (BaseDocument.class.equals(rawType)) {
		writeBaseDocument(attribute, (BaseDocument) source, sink, definedType);
	}

	else if (BaseEdgeDocument.class.equals(rawType)) {
		writeBaseEdgeDocument(attribute, (BaseEdgeDocument) source, sink, definedType);
	}

	else if (type.isMap()) {
		writeMap(attribute, (Map<Object, Object>) source, sink, definedType);
	}

	else if (type.getType().isArray()) {
		writeArray(attribute, source, sink, definedType);
	}

	else if (type.isCollectionLike()) {
		writeCollection(attribute, source, sink, definedType);
	}

	else {
		final ArangoPersistentEntity<?> entity = context.getRequiredPersistentEntity(source.getClass());
		writeEntity(attribute, source, sink, entity, definedType);
	}
}
 
Example 19
Source File: ArangoRepositoryFactory.java    From spring-data with Apache License 2.0 4 votes vote down vote up
public AnnotationArangoRepositoryMetadata(final Class<?> repositoryInterface) {
	super(repositoryInterface);
	typeInformation = ClassTypeInformation.from(repositoryInterface);
}
 
Example 20
Source File: ArangoRepositoryFactory.java    From spring-data with Apache License 2.0 4 votes vote down vote up
public DefaultArangoRepositoryMetadata(final Class<?> repositoryInterface) {
	super(repositoryInterface);
	typeInformation = ClassTypeInformation.from(repositoryInterface);
}