Example usage for java.lang.reflect TypeVariable equals

List of usage examples for java.lang.reflect TypeVariable equals

Introduction

In this page you can find the example usage for java.lang.reflect TypeVariable equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:Main.java

private final static Type resolveTypeVariable(TypeVariable<? extends GenericDeclaration> type,
        Class<?> declaringClass, Class<?> inClass) {

    if (inClass == null)
        return null;

    Class<?> superClass = null;
    Type resolvedType = null;/*from  w  ww  .  j av  a 2s  .  co m*/
    Type genericSuperClass = null;

    if (!declaringClass.equals(inClass)) {
        if (declaringClass.isInterface()) {
            // the declaringClass is an interface
            Class<?>[] interfaces = inClass.getInterfaces();
            for (int i = 0; i < interfaces.length && resolvedType == null; i++) {
                superClass = interfaces[i];
                resolvedType = resolveTypeVariable(type, declaringClass, superClass);
                genericSuperClass = inClass.getGenericInterfaces()[i];
            }
        }

        if (resolvedType == null) {
            superClass = inClass.getSuperclass();
            resolvedType = resolveTypeVariable(type, declaringClass, superClass);
            genericSuperClass = inClass.getGenericSuperclass();
        }
    } else {
        resolvedType = type;
        genericSuperClass = superClass = inClass;

    }

    if (resolvedType != null) {
        // if its another type this means we have finished
        if (resolvedType instanceof TypeVariable<?>) {
            type = (TypeVariable<?>) resolvedType;
            TypeVariable<?>[] parameters = superClass.getTypeParameters();
            int positionInClass = 0;
            for (; positionInClass < parameters.length
                    && !type.equals(parameters[positionInClass]); positionInClass++) {
            }

            // we located the position of the typevariable in the superclass
            if (positionInClass < parameters.length) {
                // let's look if we have type specialization information in the current class
                if (genericSuperClass instanceof ParameterizedType) {
                    ParameterizedType pGenericType = (ParameterizedType) genericSuperClass;
                    Type[] args = pGenericType.getActualTypeArguments();
                    return positionInClass < args.length ? args[positionInClass] : null;
                }
            }

            // we didnt find typevariable specialization in the class, so it's the best we can
            // do, lets return the resolvedType...
        }
    }

    return resolvedType;
}

From source file:com.link_intersystems.lang.reflect.Class2.java

private Type doGetBoundType(ParameterizedType parameterizedType, TypeVariable<?> typeVariable) {
    Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
    Type rawType = parameterizedType.getRawType();
    if (rawType instanceof Class<?>) {
        Class<?> rawTypeClass = (Class<?>) rawType;
        TypeVariable<?>[] typeParameters = rawTypeClass.getTypeParameters();
        for (int i = 0; i < typeParameters.length; i++) {
            TypeVariable<?> typeParameter = typeParameters[i];
            if (typeParameter.equals(typeVariable)) {
                return actualTypeArguments[i];
            }//w  w  w  .j a  v a2 s  .c  o m
        }
    }
    return null;
}

From source file:com.clark.func.Functions.java

/**
 * <p>/*from w w  w .ja  va 2s . c  o m*/
 * Checks if the subject type may be implicitly cast to the target type
 * variable following the Java generics rules.
 * </p>
 * 
 * @param type
 *            the subject type to be assigned to the target type
 * @param toTypeVariable
 *            the target type variable
 * @return true if <code>type</code> is assignable to
 *         <code>toTypeVariable</code>.
 */
private static boolean isAssignable(Type type, TypeVariable<?> toTypeVariable,
        Map<TypeVariable<?>, Type> typeVarAssigns) {
    if (type == null) {
        return true;
    }

    // only a null type can be assigned to null type which
    // would have cause the previous to return true
    if (toTypeVariable == null) {
        return false;
    }

    // all types are assignable to themselves
    if (toTypeVariable.equals(type)) {
        return true;
    }

    if (type instanceof TypeVariable<?>) {
        // a type variable is assignable to another type variable, if
        // and only if the former is the latter, extends the latter, or
        // is otherwise a descendant of the latter.
        Type[] bounds = getImplicitBounds((TypeVariable<?>) type);

        for (Type bound : bounds) {
            if (isAssignable(bound, toTypeVariable, typeVarAssigns)) {
                return true;
            }
        }
    }

    if (type instanceof Class<?> || type instanceof ParameterizedType || type instanceof GenericArrayType
            || type instanceof WildcardType) {
        return false;
    }

    throw new IllegalStateException("found an unhandled type: " + type);
}

From source file:org.datalorax.populace.core.util.TypeResolver.java

private Type resolveTypeVariable(final TypeVariable<?> type, final TypeToken<?> assigningType) {
    Type resolved = typeTable.resolveTypeVariable(type);

    if (type.equals(resolved) && assigningType != null) {
        resolved = resolveTypeVariableFromSupers(type, assigningType);
    }//from   w w  w.  java  2 s  .c om

    if (type.equals(resolved)) {
        return type;
    }

    return resolve(resolved);
}

From source file:org.springframework.core.GenericTypeResolver.java

/**
 * Determine the target type for the generic return type of the given
 * <em>generic method</em>, where formal type variables are declared on
 * the given method itself.//  ww w  .j a va  2  s . co m
 *
 * <p>For example, given a factory method with the following signature,
 * if {@code resolveReturnTypeForGenericMethod()} is invoked with the reflected
 * method for {@code creatProxy()} and an {@code Object[]} array containing
 * {@code MyService.class}, {@code resolveReturnTypeForGenericMethod()} will
 * infer that the target return type is {@code MyService}.
 *
 * <pre>{@code public static <T> T createProxy(Class<T> clazz)}</pre>
 *
 * <h4>Possible Return Values</h4>
 * <ul>
 * <li>the target return type, if it can be inferred</li>
 * <li>the {@linkplain Method#getReturnType() standard return type}, if
 * the given {@code method} does not declare any {@linkplain
 * Method#getTypeParameters() formal type variables}</li>
 * <li>the {@linkplain Method#getReturnType() standard return type}, if the
 * target return type cannot be inferred (e.g., due to type erasure)</li>
 * <li>{@code null}, if the length of the given arguments array is shorter
 * than the length of the {@linkplain
 * Method#getGenericParameterTypes() formal argument list} for the given
 * method</li>
 * </ul>
 *
 * @param method the method to introspect, never {@code null}
 * @param args the arguments that will be supplied to the method when it is
 * invoked, never {@code null}
 * @return the resolved target return type, the standard return type, or
 * {@code null}
 * @since 3.2
 * @see #resolveReturnType
 */
public static Class<?> resolveReturnTypeForGenericMethod(Method method, Object[] args) {
    Assert.notNull(method, "method must not be null");
    Assert.notNull(args, "args must not be null");

    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Resolving return type for [%s] with concrete method arguments [%s].",
                method.toGenericString(), ObjectUtils.nullSafeToString(args)));
    }

    final TypeVariable<Method>[] declaredTypeVariables = method.getTypeParameters();
    final Type genericReturnType = method.getGenericReturnType();
    final Type[] methodArgumentTypes = method.getGenericParameterTypes();

    // No declared type variables to inspect, so just return the standard return type.
    if (declaredTypeVariables.length == 0) {
        return method.getReturnType();
    }

    // The supplied argument list is too short for the method's signature, so
    // return null, since such a method invocation would fail.
    if (args.length < methodArgumentTypes.length) {
        return null;
    }

    // Ensure that the type variable (e.g., T) is declared directly on the method
    // itself (e.g., via <T>), not on the enclosing class or interface.
    boolean locallyDeclaredTypeVariableMatchesReturnType = false;
    for (TypeVariable<Method> currentTypeVariable : declaredTypeVariables) {
        if (currentTypeVariable.equals(genericReturnType)) {
            if (logger.isDebugEnabled()) {
                logger.debug(String.format(
                        "Found declared type variable [%s] that matches the target return type [%s].",
                        currentTypeVariable, genericReturnType));
            }
            locallyDeclaredTypeVariableMatchesReturnType = true;
            break;
        }
    }

    if (locallyDeclaredTypeVariableMatchesReturnType) {
        for (int i = 0; i < methodArgumentTypes.length; i++) {
            final Type currentMethodArgumentType = methodArgumentTypes[i];

            if (currentMethodArgumentType.equals(genericReturnType)) {
                if (logger.isDebugEnabled()) {
                    logger.debug(String.format(
                            "Found method argument type at index [%s] that matches the target return type.",
                            i));
                }
                return args[i].getClass();
            }

            if (currentMethodArgumentType instanceof ParameterizedType) {
                ParameterizedType parameterizedType = (ParameterizedType) currentMethodArgumentType;
                Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();

                for (int j = 0; j < actualTypeArguments.length; j++) {
                    final Type typeArg = actualTypeArguments[j];

                    if (typeArg.equals(genericReturnType)) {
                        if (logger.isDebugEnabled()) {
                            logger.debug(String.format(
                                    "Found method argument type at index [%s] that is parameterized with a type argument that matches the target return type.",
                                    i));
                        }

                        if (args[i] instanceof Class) {
                            return (Class<?>) args[i];
                        } else {
                            // Consider adding logic to determine the class of the
                            // J'th typeArg, if possible.
                            logger.info(String.format(
                                    "Could not determine the target type for type argument [%s] for method [%s].",
                                    typeArg, method.toGenericString()));

                            // For now, just fall back...
                            return method.getReturnType();
                        }
                    }
                }
            }
        }
    }

    // Fall back...
    return method.getReturnType();
}