Search in sources :

Example 86 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 87 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 88 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project druid by druid-io.

the class JsonConfigProvider method bindInstance.

@SuppressWarnings("unchecked")
public static <T> void bindInstance(Binder binder, Key<T> bindKey, T instance) {
    binder.bind(bindKey).toInstance(instance);
    final ParameterizedType supType = Types.newParameterizedType(Supplier.class, bindKey.getTypeLiteral().getType());
    final Key supplierKey;
    if (bindKey.getAnnotationType() != null) {
        supplierKey = Key.get(supType, bindKey.getAnnotationType());
    } else if (bindKey.getAnnotation() != null) {
        supplierKey = Key.get(supType, bindKey.getAnnotation());
    } else {
        supplierKey = Key.get(supType);
    }
    binder.bind(supplierKey).toInstance(Suppliers.<T>ofInstance(instance));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Key(com.google.inject.Key)

Example 89 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 ParameterizedType actualGenericType = (ParameterizedType) genericType;
    final Type actualGenericTypeArgument = actualGenericType.getActualTypeArguments()[0];
    final MessageBodyWriter writer = mbw.get().getMessageBodyWriter(entity.get().getClass(), actualGenericTypeArgument, annotations, mediaType);
    writer.writeTo(entity.get(), entity.get().getClass(), actualGenericTypeArgument, 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 90 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)

Aggregations

ParameterizedType (java.lang.reflect.ParameterizedType)704 Type (java.lang.reflect.Type)532 GenericArrayType (java.lang.reflect.GenericArrayType)226 WildcardType (java.lang.reflect.WildcardType)182 TypeVariable (java.lang.reflect.TypeVariable)137 ArrayList (java.util.ArrayList)94 Method (java.lang.reflect.Method)71 Test (org.junit.Test)59 List (java.util.List)55 Field (java.lang.reflect.Field)49 Map (java.util.Map)47 HashMap (java.util.HashMap)42 Collection (java.util.Collection)26 MediaType (javax.ws.rs.core.MediaType)23 Annotation (java.lang.annotation.Annotation)18 HashSet (java.util.HashSet)13 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)13 ImmutableList (com.google.common.collect.ImmutableList)12 TypeLiteral (com.google.inject.TypeLiteral)12 IOException (java.io.IOException)11