Search in sources :

Example 11 with ParameterizedType

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);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) MediaType(javax.ws.rs.core.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter)

Example 12 with ParameterizedType

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;
                }
            }
        }
    }
}
Also used : AnnotatedMethod(org.glassfish.jersey.server.model.AnnotatedMethod) Optional(java.util.Optional) Annotation(java.lang.annotation.Annotation) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter)

Example 13 with ParameterizedType

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();
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 14 with ParameterizedType

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 15 with ParameterizedType

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);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) InstanceBindingImpl(org.elasticsearch.common.inject.internal.InstanceBindingImpl) GenericArrayType(java.lang.reflect.GenericArrayType)

Aggregations

ParameterizedType (java.lang.reflect.ParameterizedType)605 Type (java.lang.reflect.Type)456 GenericArrayType (java.lang.reflect.GenericArrayType)203 WildcardType (java.lang.reflect.WildcardType)162 TypeVariable (java.lang.reflect.TypeVariable)126 ArrayList (java.util.ArrayList)79 Method (java.lang.reflect.Method)60 Test (org.junit.Test)55 List (java.util.List)45 Field (java.lang.reflect.Field)43 Map (java.util.Map)39 HashMap (java.util.HashMap)32 Collection (java.util.Collection)18 Annotation (java.lang.annotation.Annotation)13 ImmutableList (com.google.common.collect.ImmutableList)12 TypeLiteral (com.google.inject.TypeLiteral)12 HashSet (java.util.HashSet)11 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)10 GenericType (javax.ws.rs.core.GenericType)10 MediaType (javax.ws.rs.core.MediaType)10