Search in sources :

Example 1 with Type

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

Example 2 with Type

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

Example 3 with Type

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

Example 4 with Type

use of java.lang.reflect.Type in project elasticsearch by elastic.

the class ModuleTestCase method assertMapMultiBinding.

/**
     * Configures the module and checks a Map&lt;String, Class&gt; 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);
}
Also used : Type(java.lang.reflect.Type) Element(org.elasticsearch.common.inject.spi.Element) ProviderInstanceBinding(org.elasticsearch.common.inject.spi.ProviderInstanceBinding) HashSet(java.util.HashSet) LinkedKeyBinding(org.elasticsearch.common.inject.spi.LinkedKeyBinding)

Example 5 with Type

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

Type (java.lang.reflect.Type)6423 ParameterizedType (java.lang.reflect.ParameterizedType)1761 ProgressRequestBody (io.kubernetes.client.ProgressRequestBody)722 ProgressResponseBody (io.kubernetes.client.ProgressResponseBody)722 GenericArrayType (java.lang.reflect.GenericArrayType)690 WildcardType (java.lang.reflect.WildcardType)571 Test (org.junit.Test)512 ArrayList (java.util.ArrayList)427 Method (java.lang.reflect.Method)416 TypeVariable (java.lang.reflect.TypeVariable)337 List (java.util.List)335 Map (java.util.Map)289 Gson (com.google.gson.Gson)231 V1Status (io.kubernetes.client.models.V1Status)228 V1Status (io.kubernetes.client.openapi.models.V1Status)224 HashMap (java.util.HashMap)204 Field (java.lang.reflect.Field)163 Annotation (java.lang.annotation.Annotation)160 IOException (java.io.IOException)140 Collection (java.util.Collection)111