Search in sources :

Example 76 with WildcardType

use of java.lang.reflect.WildcardType in project logging-log4j2 by apache.

the class TypeUtil method isAssignable.

/**
 * Indicates if two {@link Type}s are assignment compatible.
 *
 * @param lhs the left hand side to check assignability to
 * @param rhs the right hand side to check assignability from
 * @return {@code true} if it is legal to assign a variable of type {@code rhs} to a variable of type {@code lhs}
 * @see Class#isAssignableFrom(Class)
 */
public static boolean isAssignable(final Type lhs, final Type rhs) {
    Objects.requireNonNull(lhs, "No left hand side type provided");
    Objects.requireNonNull(rhs, "No right hand side type provided");
    if (lhs.equals(rhs)) {
        return true;
    }
    if (Object.class.equals(lhs)) {
        // everything is assignable to Object
        return true;
    }
    // raw type on left
    if (lhs instanceof Class<?>) {
        final Class<?> lhsClass = (Class<?>) lhs;
        if (rhs instanceof Class<?>) {
            // no generics involved
            final Class<?> rhsClass = (Class<?>) rhs;
            // possible for primitive types here
            return isAssignable(lhsClass, rhsClass);
        }
        if (rhs instanceof ParameterizedType) {
            return isAssignable(lhsClass, getRawType(rhs));
        } else if (lhsClass.isArray() && rhs instanceof GenericArrayType) {
            // check for compatible array component types
            return isAssignable(lhsClass.getComponentType(), ((GenericArrayType) rhs).getGenericComponentType());
        } else if (rhs instanceof TypeVariable<?>) {
            for (final Type bound : ((TypeVariable<?>) rhs).getBounds()) {
                if (isAssignable(lhs, bound)) {
                    return true;
                }
            }
        } else {
            return false;
        }
    }
    // parameterized type on left
    if (lhs instanceof ParameterizedType) {
        final ParameterizedType lhsType = (ParameterizedType) lhs;
        if (rhs instanceof Class<?>) {
            return isAssignable(getRawType(lhs), (Class<?>) rhs);
        } else if (rhs instanceof ParameterizedType) {
            final ParameterizedType rhsType = (ParameterizedType) rhs;
            return isParameterizedAssignable(lhsType, rhsType);
        } else if (rhs instanceof TypeVariable<?>) {
            for (final Type bound : ((TypeVariable<?>) rhs).getBounds()) {
                if (isAssignable(lhsType, bound)) {
                    return true;
                }
            }
        } else {
            return false;
        }
    }
    // generic array type on left
    if (lhs instanceof GenericArrayType) {
        final Type lhsComponentType = ((GenericArrayType) lhs).getGenericComponentType();
        if (rhs instanceof Class<?>) {
            // raw type on right
            final Class<?> rhsClass = (Class<?>) rhs;
            if (rhsClass.isArray()) {
                return isAssignable(lhsComponentType, rhsClass.getComponentType());
            }
        } else if (rhs instanceof GenericArrayType) {
            return isAssignable(lhsComponentType, ((GenericArrayType) rhs).getGenericComponentType());
        }
    }
    // wildcard type on left
    if (lhs instanceof WildcardType) {
        return isWildcardAssignable((WildcardType) lhs, rhs);
    }
    // strange...
    return false;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 77 with WildcardType

use of java.lang.reflect.WildcardType in project logging-log4j2 by apache.

the class TypeUtil method isParameterizedAssignable.

private static boolean isParameterizedAssignable(final ParameterizedType lhs, final ParameterizedType rhs) {
    if (lhs.equals(rhs)) {
        // that was easy
        return true;
    }
    final Type[] lhsTypeArguments = lhs.getActualTypeArguments();
    final Type[] rhsTypeArguments = rhs.getActualTypeArguments();
    final int size = lhsTypeArguments.length;
    if (rhsTypeArguments.length != size) {
        // clearly incompatible types
        return false;
    }
    for (int i = 0; i < size; i++) {
        // verify all type arguments are assignable
        final Type lhsArgument = lhsTypeArguments[i];
        final Type rhsArgument = rhsTypeArguments[i];
        if (!lhsArgument.equals(rhsArgument) && !(lhsArgument instanceof WildcardType && isWildcardAssignable((WildcardType) lhsArgument, rhsArgument))) {
            return false;
        }
    }
    return true;
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) WildcardType(java.lang.reflect.WildcardType)

Example 78 with WildcardType

use of java.lang.reflect.WildcardType in project groovy-core by groovy.

the class Java5 method configureTypeArguments.

private GenericsType[] configureTypeArguments(Type[] ta) {
    if (ta.length == 0)
        return null;
    GenericsType[] gts = new GenericsType[ta.length];
    for (int i = 0; i < ta.length; i++) {
        ClassNode t = configureType(ta[i]);
        if (ta[i] instanceof WildcardType) {
            GenericsType[] gen = t.getGenericsTypes();
            gts[i] = gen[0];
        } else {
            gts[i] = new GenericsType(t);
        }
    }
    return gts;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) WildcardType(java.lang.reflect.WildcardType) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 79 with WildcardType

use of java.lang.reflect.WildcardType in project beam by apache.

the class ApiSurface method addExposedTypes.

/**
 * Adds any types exposed to this set. These will come from the (possibly absent) bounds on the
 * wildcard.
 */
private void addExposedTypes(WildcardType type, Class<?> cause) {
    visit(type);
    for (Type lowerBound : type.getLowerBounds()) {
        LOG.debug("Adding exposed types from {}, which is a type lower bound on wildcard type {}", lowerBound, type);
        addExposedTypes(lowerBound, cause);
    }
    for (Type upperBound : type.getUpperBounds()) {
        LOG.debug("Adding exposed types from {}, which is a type upper bound on wildcard type {}", upperBound, type);
        addExposedTypes(upperBound, cause);
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type)

Example 80 with WildcardType

use of java.lang.reflect.WildcardType in project guava by google.

the class TypeTokenResolutionTest method testWithGenericUpperBoundInWildcard.

public void testWithGenericUpperBoundInWildcard() throws Exception {
    WildcardType wildcardType = (WildcardType) new WithGenericBound<String>() {
    }.getTargetType("withWildcardUpperBound");
    assertEquals(String.class, wildcardType.getUpperBounds()[0]);
}
Also used : WildcardType(java.lang.reflect.WildcardType)

Aggregations

WildcardType (java.lang.reflect.WildcardType)236 ParameterizedType (java.lang.reflect.ParameterizedType)208 Type (java.lang.reflect.Type)180 GenericArrayType (java.lang.reflect.GenericArrayType)160 TypeVariable (java.lang.reflect.TypeVariable)134 Test (org.junit.Test)24 GenericClass (org.evosuite.utils.generic.GenericClass)14 Method (java.lang.reflect.Method)10 WildcardTypeImpl (org.evosuite.utils.generic.WildcardTypeImpl)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)8 Test (org.junit.jupiter.api.Test)8 CaptureType (com.googlecode.gentyref.CaptureType)7 JSONException (com.alibaba.fastjson.JSONException)5 JSONType (com.alibaba.fastjson.annotation.JSONType)5 CatalogType (org.spongepowered.api.CatalogType)4 GenericDeclaration (java.lang.reflect.GenericDeclaration)3 HashSet (java.util.HashSet)3 List (java.util.List)3 MediaType (javax.ws.rs.core.MediaType)3