Search in sources :

Example 96 with Type

use of java.lang.reflect.Type in project Entitas-Java by Rubentxu.

the class TypeResolver method getTypeVariableMap.

private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> targetType, Class<?> functionalInterface) {
    Reference<Map<TypeVariable<?>, Type>> ref = TYPE_VARIABLE_CACHE.get(targetType);
    Map<TypeVariable<?>, Type> map = ref != null ? ref.get() : null;
    if (map == null) {
        map = new HashMap<TypeVariable<?>, Type>();
        // Populate lambdas
        if (functionalInterface != null)
            populateLambdaArgs(functionalInterface, targetType, map);
        // Populate interfaces
        populateSuperTypeArgs(targetType.getGenericInterfaces(), map, functionalInterface != null);
        // Populate super classes and interfaces
        Type genericType = targetType.getGenericSuperclass();
        Class<?> type = targetType.getSuperclass();
        while (type != null && !Object.class.equals(type)) {
            if (genericType instanceof ParameterizedType)
                populateTypeArgs((ParameterizedType) genericType, map, false);
            populateSuperTypeArgs(type.getGenericInterfaces(), map, false);
            genericType = type.getGenericSuperclass();
            type = type.getSuperclass();
        }
        // Populate enclosing classes
        type = targetType;
        while (type.isMemberClass()) {
            genericType = type.getGenericSuperclass();
            if (genericType instanceof ParameterizedType)
                populateTypeArgs((ParameterizedType) genericType, map, functionalInterface != null);
            type = type.getEnclosingClass();
        }
        if (CACHE_ENABLED)
            TYPE_VARIABLE_CACHE.put(targetType, new WeakReference<Map<TypeVariable<?>, Type>>(map));
    }
    return map;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) WeakReference(java.lang.ref.WeakReference) HashMap(java.util.HashMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap)

Example 97 with Type

use of java.lang.reflect.Type in project Entitas-Java by Rubentxu.

the class TypeResolver method populateLambdaArgs.

/**
   * Populates the {@code map} with variable/argument pairs for the {@code functionalInterface}.
   */
private static void populateLambdaArgs(Class<?> functionalInterface, final Class<?> lambdaType, Map<TypeVariable<?>, Type> map) {
    if (RESOLVES_LAMBDAS) {
        // Find SAM
        for (Method m : functionalInterface.getMethods()) {
            if (!isDefaultMethod(m) && !Modifier.isStatic(m.getModifiers()) && !m.isBridge()) {
                // Skip methods that override Object.class
                Method objectMethod = OBJECT_METHODS.get(m.getName());
                if (objectMethod != null && Arrays.equals(m.getTypeParameters(), objectMethod.getTypeParameters()))
                    continue;
                // Get functional interface's type params
                Type returnTypeVar = m.getGenericReturnType();
                Type[] paramTypeVars = m.getGenericParameterTypes();
                Member member = getMemberRef(lambdaType);
                if (member == null)
                    return;
                // Populate return type argument
                if (returnTypeVar instanceof TypeVariable) {
                    Class<?> returnType = member instanceof Method ? ((Method) member).getReturnType() : ((Constructor<?>) member).getDeclaringClass();
                    returnType = wrapPrimitives(returnType);
                    if (!returnType.equals(Void.class))
                        map.put((TypeVariable<?>) returnTypeVar, returnType);
                }
                Class<?>[] arguments = member instanceof Method ? ((Method) member).getParameterTypes() : ((Constructor<?>) member).getParameterTypes();
                // Populate object type from arbitrary object method reference
                int paramOffset = 0;
                if (paramTypeVars.length > 0 && paramTypeVars[0] instanceof TypeVariable && paramTypeVars.length == arguments.length + 1) {
                    Class<?> instanceType = member.getDeclaringClass();
                    map.put((TypeVariable<?>) paramTypeVars[0], instanceType);
                    paramOffset = 1;
                }
                // Handle additional arguments that are captured from the lambda's enclosing scope
                int argOffset = 0;
                if (paramTypeVars.length < arguments.length) {
                    argOffset = arguments.length - paramTypeVars.length;
                }
                // Populate type arguments
                for (int i = 0; i + argOffset < arguments.length; i++) {
                    if (paramTypeVars[i] instanceof TypeVariable)
                        map.put((TypeVariable<?>) paramTypeVars[i + paramOffset], wrapPrimitives(arguments[i + argOffset]));
                }
                return;
            }
        }
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) Method(java.lang.reflect.Method) Member(java.lang.reflect.Member)

Example 98 with Type

use of java.lang.reflect.Type in project Entitas-Java by Rubentxu.

the class TypeResolver method resolveRawArguments.

/**
   * Returns an array of raw classes representing arguments for the {@code genericType} using type variable information
   * from the {@code subType}. Arguments for {@code genericType} that cannot be resolved are returned as
   * {@code Unknown.class}. If no arguments can be resolved then {@code null} is returned.
   *
   * @param genericType to resolve arguments for
   * @param subType to extract type variable information from
   * @return array of raw classes representing arguments for the {@code genericType} else {@code null} if no type
   *         arguments are declared
   */
public static Class<?>[] resolveRawArguments(Type genericType, Class<?> subType) {
    Class<?>[] result = null;
    Class<?> functionalInterface = null;
    // Handle lambdas
    if (RESOLVES_LAMBDAS && subType.isSynthetic()) {
        Class<?> fi = genericType instanceof ParameterizedType && ((ParameterizedType) genericType).getRawType() instanceof Class ? (Class<?>) ((ParameterizedType) genericType).getRawType() : genericType instanceof Class ? (Class<?>) genericType : null;
        if (fi != null && fi.isInterface())
            functionalInterface = fi;
    }
    if (genericType instanceof ParameterizedType) {
        ParameterizedType paramType = (ParameterizedType) genericType;
        Type[] arguments = paramType.getActualTypeArguments();
        result = new Class[arguments.length];
        for (int i = 0; i < arguments.length; i++) result[i] = resolveRawClass(arguments[i], subType, functionalInterface);
    } else if (genericType instanceof TypeVariable) {
        result = new Class[1];
        result[0] = resolveRawClass(genericType, subType, functionalInterface);
    } else if (genericType instanceof Class) {
        TypeVariable<?>[] typeParams = ((Class<?>) genericType).getTypeParameters();
        result = new Class[typeParams.length];
        for (int i = 0; i < typeParams.length; i++) result[i] = resolveRawClass(typeParams[i], subType, functionalInterface);
    }
    return result;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable)

Example 99 with Type

use of java.lang.reflect.Type in project core-java by SpineEventEngine.

the class Identifiers method idMessageToString.

// OK to cast to String as output type of Stringifier.
@SuppressWarnings("unchecked")
static String idMessageToString(Message message) {
    checkNotNull(message);
    final String result;
    final StringifierRegistry registry = StringifierRegistry.getInstance();
    final Class<? extends Message> msgClass = message.getClass();
    final TypeToken<? extends Message> msgToken = TypeToken.of(msgClass);
    final Type msgType = msgToken.getType();
    final Optional<Stringifier<Object>> optional = registry.get(msgType);
    if (optional.isPresent()) {
        final Stringifier converter = optional.get();
        result = (String) converter.convert(message);
    } else {
        result = convert(message);
    }
    return result;
}
Also used : Type(java.lang.reflect.Type) Stringifier(io.spine.string.Stringifier) StringifierRegistry(io.spine.string.StringifierRegistry) TextFormat.shortDebugString(com.google.protobuf.TextFormat.shortDebugString)

Example 100 with Type

use of java.lang.reflect.Type in project core-java by SpineEventEngine.

the class Types method listTypeOf.

/**
     * Creates the parametrized {@code Type} of the list.
     *
     * @param elementClass the class of the list elements
     * @param <T>          the type of the elements in this list
     * @return the type of the list
     */
public static <T> Type listTypeOf(Class<T> elementClass) {
    checkNotNull(elementClass);
    final Type type = new TypeToken<List<T>>() {
    }.where(new TypeParameter<T>() {
    }, elementClass).getType();
    return type;
}
Also used : Type(java.lang.reflect.Type) TypeParameter(com.google.common.reflect.TypeParameter) TypeToken(com.google.common.reflect.TypeToken)

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