use of java.lang.reflect.Type in project jetty.project by eclipse.
the class ReflectUtils method toString.
public static String toString(Class<?> pojo, Method method) {
StringBuilder str = new StringBuilder();
// method modifiers
int mod = method.getModifiers() & Modifier.methodModifiers();
if (mod != 0) {
str.append(Modifier.toString(mod)).append(' ');
}
// return type
Type retType = method.getGenericReturnType();
appendTypeName(str, retType, false).append(' ');
// class name
str.append(pojo.getName());
str.append("#");
// method name
str.append(method.getName());
// method parameters
str.append('(');
Type[] params = method.getGenericParameterTypes();
for (int j = 0; j < params.length; j++) {
boolean ellipses = method.isVarArgs() && (j == (params.length - 1));
appendTypeName(str, params[j], ellipses);
if (j < (params.length - 1)) {
str.append(", ");
}
}
str.append(')');
// TODO: show exceptions?
return str.toString();
}
use of java.lang.reflect.Type 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.Type 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.Type in project elasticsearch by elastic.
the class ModuleTestCase method assertMapMultiBinding.
/**
* Configures the module and checks a Map<String, Class> of the "to" class
* is bound to "theClass".
*/
public void assertMapMultiBinding(Module module, Class to, Class theClass) {
List<Element> elements = Elements.getElements(module);
Set<Type> bindings = new HashSet<>();
boolean providerFound = false;
for (Element element : elements) {
if (element instanceof LinkedKeyBinding) {
LinkedKeyBinding binding = (LinkedKeyBinding) element;
if (to.equals(binding.getKey().getTypeLiteral().getType())) {
bindings.add(binding.getLinkedKey().getTypeLiteral().getType());
}
} else if (element instanceof ProviderInstanceBinding) {
ProviderInstanceBinding binding = (ProviderInstanceBinding) element;
String setType = binding.getKey().getTypeLiteral().getType().toString();
if (setType.equals("java.util.Map<java.lang.String, " + to.getName() + ">")) {
providerFound = true;
}
}
}
if (bindings.contains(theClass) == false) {
fail("Expected to find " + theClass.getName() + " as binding to " + to.getName() + ", found these classes:\n" + bindings);
}
assertTrue("Did not find provider for map of " + to.getName(), providerFound);
}
use of java.lang.reflect.Type 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