DATACMNS-1738 - Expose method to obtain the repository query method return type.

We now expose RepositoryMetadata.getReturnType(…) to obtain the declared method return type. While pure Java code can rely on Method.getReturnType(), suspended Kotlin methods (Coroutines) require additional lookups. This is because the Kotlin compiler messes around with the return type in the bytecode. It changes the return type to java.lang.Object while synthesizing an additional method argument:

interface MyCoroutineRepository : Repository<Person, String> {
	suspend fun suspendedQueryMethod(): Flow<Person>
}

compiles to

interface MyCoroutineRepository extends Repository<Person, String> {
	Object suspendedQueryMethod(Continuation<Flow<Person>> arg0);
}

Therefore, the triviality of obtaining a return type becomes an inspection of the actual method arguments.

Related ticket: https://jira.spring.io/browse/DATAMONGO-2562
This commit is contained in:
Mark Paluch
2020-06-09 11:09:31 +02:00
parent 210ab249e3
commit 40e7e89e27
5 changed files with 99 additions and 5 deletions

View File

@@ -20,11 +20,13 @@ import java.util.Collection;
import java.util.Set;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.util.TypeInformation;
/**
* Metadata for repository interfaces.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
public interface RepositoryMetadata {
@@ -50,11 +52,22 @@ public interface RepositoryMetadata {
Class<?> getRepositoryInterface();
/**
* Returns the domain class returned by the given {@link Method}. Will extract the type from {@link Collection}s and
* {@link org.springframework.data.domain.Page} as well.
* Returns the type {@link Method} return type as it is declared in the repository. Considers suspended methods and
* does not unwrap component types but leaves those for further inspection.
*
* @param method
* @return
* @since 2.4
*/
TypeInformation<?> getReturnType(Method method);
/**
* Returns the domain class returned by the given {@link Method}. In contrast to {@link #getReturnType(Method)}, this
* method extracts the type from {@link Collection}s and {@link org.springframework.data.domain.Page} as well.
*
* @param method
* @return
* @see #getReturnType(Method)
*/
Class<?> getReturnedDomainClass(Method method);

View File

@@ -40,6 +40,7 @@ import org.springframework.util.Assert;
* @author Oliver Gierke
* @author Thomas Darimont
* @author Jens Schauder
* @author Mark Paluch
*/
public abstract class AbstractRepositoryMetadata implements RepositoryMetadata {
@@ -79,9 +80,10 @@ public abstract class AbstractRepositoryMetadata implements RepositoryMetadata {
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.RepositoryMetadata#getReturnedDomainClass(java.lang.reflect.Method)
* @see org.springframework.data.repository.core.RepositoryMetadata#getReturnType(java.lang.reflect.Method)
*/
public Class<?> getReturnedDomainClass(Method method) {
@Override
public TypeInformation<?> getReturnType(Method method) {
TypeInformation<?> returnType = null;
if (KotlinDetector.isKotlinType(method.getDeclaringClass()) && KotlinReflectionUtils.isSuspend(method)) {
@@ -95,7 +97,15 @@ public abstract class AbstractRepositoryMetadata implements RepositoryMetadata {
returnType = typeInformation.getReturnType(method);
}
return QueryExecutionConverters.unwrapWrapperTypes(returnType).getType();
return returnType;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.RepositoryMetadata#getReturnedDomainClass(java.lang.reflect.Method)
*/
public Class<?> getReturnedDomainClass(Method method) {
return QueryExecutionConverters.unwrapWrapperTypes(getReturnType(method)).getType();
}
/*

View File

@@ -32,6 +32,7 @@ import org.springframework.data.repository.core.CrudMethods;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.util.Streamable;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@@ -244,6 +245,15 @@ class DefaultRepositoryInformation implements RepositoryInformation {
return metadata.getReturnedDomainClass(method);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.RepositoryMetadata#getReturnType(java.lang.reflect.Method)
*/
@Override
public TypeInformation<?> getReturnType(Method method) {
return metadata.getReturnType(method);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.RepositoryMetadata#getCrudMethods()