Skip to content

Commit

Permalink
Refine matching functionality
Browse files Browse the repository at this point in the history
-Add usages of googlecode.gentyref i.o. coekie.gentyref
-Adjust isCollectionOfExpectedType() to actual check if the exact super
type is a iterable
-Rename isCollectionOfExpectedType() to isIterableOfExpectedType()
-Rename isCollectionOfExpectedType() to isIterableOfExpectedType()
-Make hasOneTypeArgumentWhichMatches() part of
isParameterizedTypeOfExpectedType()
-Drop usages of GenericTypeReflector.getArrayComponentType()
-Add protected match functions for all instance of calls
-Add support for isStreamOfExpectedType()

[#463]
  • Loading branch information
smcvb committed Feb 5, 2018
1 parent 6716eea commit 7c812d1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
4 changes: 2 additions & 2 deletions core/pom.xml
Expand Up @@ -200,9 +200,9 @@
</exclusions>
</dependency>
<dependency>
<groupId>com.coekie.gentyref</groupId>
<groupId>com.googlecode.gentyref</groupId>
<artifactId>gentyref</artifactId>
<version>1.3.0</version>
<version>1.2.0</version>
</dependency>

<!-- Optional dependencies -->
Expand Down
@@ -1,13 +1,14 @@
package org.axonframework.queryhandling.responsetypes;

import com.coekie.gentyref.GenericTypeReflector;
import com.googlecode.gentyref.GenericTypeReflector;

import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.Arrays;
import java.util.stream.Stream;

/**
* Abstract implementation of the {@link org.axonframework.queryhandling.responsetypes.ResponseType} which contains
Expand Down Expand Up @@ -37,29 +38,35 @@ protected AbstractResponseType(Class<?> expectedResponseType) {
}

protected boolean isCollectionOfExpectedType(Type responseType) {
Type collectionType = GenericTypeReflector.getExactSuperType(responseType, Iterable.class);
return collectionType != null && isParameterizedTypeOfExpectedType(collectionType);
}

protected boolean isStreamOfExpectedType(Type responseType) {
Type streamType = GenericTypeReflector.getExactSuperType(responseType, Stream.class);
return streamType != null && isParameterizedTypeOfExpectedType(streamType);
}

protected boolean isParameterizedTypeOfExpectedType(Type responseType) {
boolean isParameterizedType = isParameterizedType(responseType);
if (!isParameterizedType) {
return false;
}

Type[] actualTypeArguments = ((ParameterizedType) responseType).getActualTypeArguments();
return hasOneTypeArgumentWhichMatches(actualTypeArguments);
}

protected boolean isParameterizedType(Type responseType) {
return responseType instanceof ParameterizedType;
}

protected boolean hasOneTypeArgumentWhichMatches(Type[] typeArguments) {
boolean hasOneTypeArgument = typeArguments.length == 1;
boolean hasOneTypeArgument = actualTypeArguments.length == 1;
if (!hasOneTypeArgument) {
return false;
}

Type responseType = typeArguments[0];
return isAssignableFrom(responseType) ||
isGenericAssignableFrom(responseType) ||
isWildcardTypeWithMatchingUpperBound(responseType);
Type actualTypeArgument = actualTypeArguments[0];
return isAssignableFrom(actualTypeArgument) ||
isGenericAssignableFrom(actualTypeArgument) ||
isWildcardTypeWithMatchingUpperBound(actualTypeArgument);
}

protected boolean isParameterizedType(Type responseType) {
return responseType instanceof ParameterizedType;
}

protected boolean isWildcardTypeWithMatchingUpperBound(Type responseType) {
Expand All @@ -78,24 +85,32 @@ protected boolean isWildcardType(Type responseType) {
}

protected boolean isArrayOfExpectedType(Type responseType) {
return isArray(responseType) && isAssignableFrom(GenericTypeReflector.getArrayComponentType(responseType));
return isArray(responseType) && isAssignableFrom(((Class) responseType).getComponentType());
}

protected boolean isArray(Type responseType) {
return responseType instanceof Class && ((Class) responseType).isArray();
}

protected boolean isGenericArrayOfExpectedType(Type responseType) {
return responseType instanceof GenericArrayType &&
return isGenericArrayType(responseType) &&
isGenericAssignableFrom(((GenericArrayType) responseType).getGenericComponentType());
}

protected boolean isGenericArrayType(Type responseType) {
return responseType instanceof GenericArrayType;
}

protected boolean isGenericAssignableFrom(Type responseType) {
return responseType instanceof TypeVariable &&
return isTypeVariable(responseType) &&
Arrays.stream(((TypeVariable) responseType).getBounds())
.anyMatch(this::isAssignableFrom);
}

protected boolean isTypeVariable(Type responseType) {
return responseType instanceof TypeVariable;
}

protected boolean isAssignableFrom(Type responseType) {
return responseType instanceof Class && expectedResponseType.isAssignableFrom((Class) responseType);
}
Expand Down

0 comments on commit 7c812d1

Please sign in to comment.