use of java.lang.reflect.ParameterizedType in project dropwizard by dropwizard.
the class OptionalMessageBodyWriter method writeTo.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void writeTo(Optional<?> entity, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
if (!entity.isPresent()) {
throw EmptyOptionalException.INSTANCE;
}
final Type innerGenericType = (genericType instanceof ParameterizedType) ? ((ParameterizedType) genericType).getActualTypeArguments()[0] : entity.get().getClass();
final MessageBodyWriter writer = mbw.get().getMessageBodyWriter(entity.get().getClass(), innerGenericType, annotations, mediaType);
writer.writeTo(entity.get(), entity.get().getClass(), innerGenericType, annotations, mediaType, httpHeaders, entityStream);
}
use of java.lang.reflect.ParameterizedType in project dropwizard by dropwizard.
the class PolymorphicAuthDynamicFeature method configure.
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
final AnnotatedMethod am = new AnnotatedMethod(resourceInfo.getResourceMethod());
final Annotation[][] parameterAnnotations = am.getParameterAnnotations();
final Class<?>[] parameterTypes = am.getParameterTypes();
final Type[] parameterGenericTypes = am.getGenericParameterTypes();
for (int i = 0; i < parameterAnnotations.length; i++) {
final Class<?> type = parameterTypes[i];
// If the parameter type is an Optional, extract its type
// parameter. Otherwise, use the parameter type itself.
final Type paramType = type == Optional.class ? ((ParameterizedType) parameterGenericTypes[i]).getActualTypeArguments()[0] : type;
for (final Annotation annotation : parameterAnnotations[i]) {
if (annotation instanceof Auth && authFilterMap.containsKey(paramType)) {
if (type == Optional.class) {
final ContainerRequestFilter filter = authFilterMap.get(paramType);
context.register(new WebApplicationExceptionCatchingFilter(filter));
return;
} else {
context.register(authFilterMap.get(type));
return;
}
}
}
}
}
use of java.lang.reflect.ParameterizedType in project jetty.project by eclipse.
the class ReflectUtils method toShortName.
public static String toShortName(Type type) {
if (type == null) {
return "<null>";
}
if (type instanceof Class) {
String name = ((Class<?>) type).getName();
return trimClassName(name);
}
if (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
StringBuilder str = new StringBuilder();
str.append(trimClassName(((Class<?>) ptype.getRawType()).getName()));
str.append("<");
Type[] args = ptype.getActualTypeArguments();
for (int i = 0; i < args.length; i++) {
if (i > 0) {
str.append(",");
}
str.append(args[i]);
}
str.append(">");
return str.toString();
}
return type.toString();
}
use of java.lang.reflect.ParameterizedType in project jetty.project by eclipse.
the class ReflectUtils method resolveGenericRef.
private static boolean resolveGenericRef(GenericRef ref, Class<?> clazz, Type type) {
if (type instanceof Class) {
if (type == ref.ifaceClass) {
// is this a straight ref or a TypeVariable?
// debug("Found ref (as class): %s",toShortName(type));
ref.setGenericFromType(type, 0);
return true;
} else {
// Keep digging
return resolveGenericRef(ref, type);
}
}
if (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
Type rawType = ptype.getRawType();
if (rawType == ref.ifaceClass) {
// debug("Found ref on [%s] as ParameterizedType [%s]",toShortName(clazz),toShortName(ptype));
// Always get the raw type parameter, let unwrap() solve for what it is
ref.setGenericFromType(ptype.getActualTypeArguments()[0], 0);
return true;
} else {
// Keep digging
return resolveGenericRef(ref, rawType);
}
}
return false;
}
use of java.lang.reflect.ParameterizedType in project elasticsearch by elastic.
the class InjectorImpl method createTypeLiteralBinding.
/**
* Converts a binding for a {@code Key<TypeLiteral<T>>} to the value {@code TypeLiteral<T>}. It's
* a bit awkward because we have to pull out the inner type in the type literal.
*/
private <T> BindingImpl<TypeLiteral<T>> createTypeLiteralBinding(Key<TypeLiteral<T>> key, Errors errors) throws ErrorsException {
Type typeLiteralType = key.getTypeLiteral().getType();
if (!(typeLiteralType instanceof ParameterizedType)) {
throw errors.cannotInjectRawTypeLiteral().toException();
}
ParameterizedType parameterizedType = (ParameterizedType) typeLiteralType;
Type innerType = parameterizedType.getActualTypeArguments()[0];
// this proves problematic, we can probably fix TypeLiteral to support type variables
if (!(innerType instanceof Class) && !(innerType instanceof GenericArrayType) && !(innerType instanceof ParameterizedType)) {
throw errors.cannotInjectTypeLiteralOf(innerType).toException();
}
// by definition, innerType == T, so this is safe
@SuppressWarnings("unchecked") TypeLiteral<T> value = (TypeLiteral<T>) TypeLiteral.get(innerType);
InternalFactory<TypeLiteral<T>> factory = new ConstantFactory<>(Initializables.of(value));
return new InstanceBindingImpl<>(this, key, SourceProvider.UNKNOWN_SOURCE, factory, emptySet(), value);
}
Aggregations