Example usage for java.lang.reflect Type equals

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

Introduction

In this page you can find the example usage for java.lang.reflect Type 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

final static Type expand(Type type, Class<?> inClass) {
    Type expandedType = null;//from  w w w .jav  a  2 s .  c  o m
    if (type instanceof TypeVariable) {
        @SuppressWarnings("unchecked")
        // for the moment we assume it is a class, we can later handle ctr and methods
        TypeVariable<GenericDeclaration> tvType = (TypeVariable<GenericDeclaration>) type;
        if (inClass == null)
            inClass = genericDeclarationToClass(tvType.getGenericDeclaration());
        expandedType = resolveTypeVariable(tvType, inClass);
        if (type.equals(expandedType))
            expandedType = tvType.getBounds()[0];
    } else if (type instanceof WildcardType) {
        WildcardType wType = (WildcardType) type;
        expandedType = wType.getUpperBounds().length > 0 ? expand(wType.getUpperBounds()[0], inClass)
                : Object.class;
    } else
        return type;

    return expandedType == null || type.equals(expandedType) ? Object.class : expandedType;
}

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.// w  w w .  ja  va  2s.c o  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();
}

From source file:com.expressui.core.util.ReflectionUtil.java

/**
 * Gets a generic argument of the given class, based on arg index.
 *
 * @param clazz    class from which to extract generic argument
 * @param argIndex index of the declared argument type
 * @return type of generic argument/*  w  w w .j  a va 2  s .  c  o m*/
 */
public static Class getGenericArgumentType(Class clazz, int argIndex) {
    Type type = clazz.getGenericSuperclass();

    if (type != null && type instanceof ParameterizedType) {
        if (((ParameterizedType) type).getActualTypeArguments()[argIndex] instanceof Class) {
            return (Class) ((ParameterizedType) type).getActualTypeArguments()[argIndex];
        } else {
            return null;
        }
    } else {
        if (!(type instanceof Class) || type.equals(Object.class)) {
            return null;
        } else {
            return getGenericArgumentType((Class) type, argIndex);
        }
    }
}

From source file:org.talend.dataprep.conversions.BeanConversionService.java

/**
 * The {@link BeanUtils#copyProperties(java.lang.Object, java.lang.Object)} method does <b>NOT</b> check if parametrized type
 * are compatible when copying values, this helper method performs this additional check and ignore copy of those values.
 *
 * @param source The source bean (from which values are read).
 * @param converted The target bean (to which values are written).
 *///w ww .ja v a  2  s.  com
private static void copyBean(Object source, Object converted) {
    // Find property(ies) to ignore during copy.
    List<String> discardedProperties = new LinkedList<>();
    final BeanWrapper sourceBean = new BeanWrapperImpl(source);
    final BeanWrapper targetBean = new BeanWrapperImpl(converted);
    final PropertyDescriptor[] sourceProperties = sourceBean.getPropertyDescriptors();
    for (PropertyDescriptor sourceProperty : sourceProperties) {
        if (targetBean.isWritableProperty(sourceProperty.getName())) {
            final PropertyDescriptor targetProperty = targetBean
                    .getPropertyDescriptor(sourceProperty.getName());
            final Class<?> sourcePropertyType = sourceProperty.getPropertyType();
            final Class<?> targetPropertyType = targetProperty.getPropertyType();
            final Method readMethod = sourceProperty.getReadMethod();
            if (readMethod != null) {
                final Type sourceReturnType = readMethod.getGenericReturnType();
                final Method targetPropertyWriteMethod = targetProperty.getWriteMethod();
                if (targetPropertyWriteMethod != null) {
                    final Type targetReturnType = targetPropertyWriteMethod.getParameters()[0]
                            .getParameterizedType();
                    boolean valid = Object.class.equals(targetPropertyType)
                            || sourcePropertyType.equals(targetPropertyType)
                                    && sourceReturnType.equals(targetReturnType);
                    if (!valid) {
                        discardedProperties.add(sourceProperty.getName());
                    }
                }
            } else {
                discardedProperties.add(sourceProperty.getName());
            }
        }
    }

    // Perform copy
    BeanUtils.copyProperties(source, converted,
            discardedProperties.toArray(new String[discardedProperties.size()]));
}

From source file:org.openflexo.antar.binding.TypeUtils.java

public static boolean isDouble(Type type) {
    if (type == null) {
        return false;
    }//from w  ww. ja v  a2 s . c om
    return type.equals(Double.class) || type.equals(Double.TYPE);
}

From source file:org.openflexo.antar.binding.TypeUtils.java

public static boolean isFloat(Type type) {
    if (type == null) {
        return false;
    }/*from   w  ww . jav  a2s  .  c  o  m*/
    return type.equals(Float.class) || type.equals(Float.TYPE);
}

From source file:org.openflexo.antar.binding.TypeUtils.java

public static boolean isInteger(Type type) {
    if (type == null) {
        return false;
    }/*from ww w.j a  v a2s. c  o  m*/
    return type.equals(Integer.class) || type.equals(Integer.TYPE);
}

From source file:org.openflexo.antar.binding.TypeUtils.java

public static boolean isLong(Type type) {
    if (type == null) {
        return false;
    }/*  ww w  .j  a va2  s  . c om*/
    return type.equals(Long.class) || type.equals(Long.TYPE);
}

From source file:org.openflexo.antar.binding.TypeUtils.java

public static boolean isObject(Type type) {
    if (type == null) {
        return false;
    }/*w  w w  .j av  a  2s  . c o  m*/
    return type.equals(Object.class);
}

From source file:org.openflexo.antar.binding.TypeUtils.java

public static boolean isShort(Type type) {
    if (type == null) {
        return false;
    }//  w  ww. j  av  a  2s.c  o m
    return type.equals(Short.class) || type.equals(Short.TYPE);
}