Skip to content

Commit

Permalink
Add getEffectiveReturnType into Types
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgitario committed May 11, 2022
1 parent 325a758 commit 2ee3256
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.jboss.resteasy.reactive.common.util.types;

import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
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.HashMap;
import java.util.Map;
import java.util.concurrent.CompletionStage;
import org.jboss.resteasy.reactive.RestResponse;

/**
* Type conversions and generic type manipulations
Expand Down Expand Up @@ -168,6 +172,35 @@ private static Type[] extractTypes(Map<TypeVariable<?>, Type> typeVarMap, Type g
}
}

public static Type getEffectiveReturnType(Type returnType) {
if (returnType instanceof Class)
return returnType;
if (returnType instanceof ParameterizedType) {
ParameterizedType type = (ParameterizedType) returnType;
Type firstTypeArgument = type.getActualTypeArguments()[0];
if (type.getRawType() == CompletionStage.class) {
return getEffectiveReturnType(firstTypeArgument);
}
if (type.getRawType() == Uni.class) {
return getEffectiveReturnType(firstTypeArgument);
}
if (type.getRawType() == Multi.class) {
return getEffectiveReturnType(firstTypeArgument);
}
if (type.getRawType() == RestResponse.class) {
return getEffectiveReturnType(firstTypeArgument);
}
return returnType;
}
if (returnType instanceof WildcardType) {
Type[] bounds = ((WildcardType) returnType).getLowerBounds();
if (bounds.length > 0)
return getRawType(bounds[0]);
return getRawType(((WildcardType) returnType).getUpperBounds()[0]);
}
throw new UnsupportedOperationException("Endpoint return type not supported yet: " + returnType);
}

public static Class<?> getRawType(Type type) {
if (type instanceof Class)
return (Class<?>) type;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package org.jboss.resteasy.reactive.server.core.startup;

import static org.jboss.resteasy.reactive.common.util.DeploymentUtils.loadClass;
import static org.jboss.resteasy.reactive.common.util.types.Types.getEffectiveReturnType;
import static org.jboss.resteasy.reactive.common.util.types.Types.getRawType;

import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -19,7 +17,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
import javax.ws.rs.RuntimeType;
Expand All @@ -28,7 +25,6 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.MessageBodyWriter;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.common.ResteasyReactiveConfig;
import org.jboss.resteasy.reactive.common.model.MethodParameter;
import org.jboss.resteasy.reactive.common.model.ParameterType;
Expand Down Expand Up @@ -617,43 +613,4 @@ public Map<String, Integer> buildParamIndexMap(URITemplate classPathTemplate, UR
return pathParameterIndexes;
}

private Class<?> getRawType(Type type) {
if (type instanceof Class)
return (Class<?>) type;
if (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
return (Class<?>) ptype.getRawType();
}
throw new UnsupportedOperationException("Endpoint return type not supported yet: " + type);
}

private Type getEffectiveReturnType(Type returnType) {
if (returnType instanceof Class)
return returnType;
if (returnType instanceof ParameterizedType) {
ParameterizedType type = (ParameterizedType) returnType;
Type firstTypeArgument = type.getActualTypeArguments()[0];
if (type.getRawType() == CompletionStage.class) {
return getEffectiveReturnType(firstTypeArgument);
}
if (type.getRawType() == Uni.class) {
return getEffectiveReturnType(firstTypeArgument);
}
if (type.getRawType() == Multi.class) {
return getEffectiveReturnType(firstTypeArgument);
}
if (type.getRawType() == RestResponse.class) {
return getEffectiveReturnType(firstTypeArgument);
}
return returnType;
}
if (returnType instanceof WildcardType) {
Type[] bounds = ((WildcardType) returnType).getLowerBounds();
if (bounds.length > 0)
return getRawType(bounds[0]);
return getRawType(((WildcardType) returnType).getUpperBounds()[0]);
}
throw new UnsupportedOperationException("Endpoint return type not supported yet: " + returnType);
}

}

0 comments on commit 2ee3256

Please sign in to comment.