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 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));
}
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);
}
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);
}
Aggregations