Search in sources :

Example 66 with WildcardType

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

the class AClass method testUnboundedWildcardType.

@Test
public void testUnboundedWildcardType() {
    final WildcardType unbounded = TypeUtils.wildcardType().withLowerBounds((Type) null).withUpperBounds().build();
    assertTrue(TypeUtils.equals(TypeUtils.WILDCARD_ALL, unbounded));
    assertArrayEquals(new Type[] { Object.class }, TypeUtils.getImplicitUpperBounds(unbounded));
    assertArrayEquals(new Type[] { null }, TypeUtils.getImplicitLowerBounds(unbounded));
    assertEquals("?", TypeUtils.toString(unbounded));
    assertEquals("?", unbounded.toString());
}
Also used : WildcardType(java.lang.reflect.WildcardType) Test(org.junit.Test)

Example 67 with WildcardType

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

the class AClass method testWildcardType.

@Test
public void testWildcardType() throws Exception {
    final WildcardType simpleWildcard = TypeUtils.wildcardType().withUpperBounds(String.class).build();
    final Field cClass = AClass.class.getField("cClass");
    assertTrue(TypeUtils.equals(((ParameterizedType) cClass.getGenericType()).getActualTypeArguments()[0], simpleWildcard));
    assertEquals(String.format("? extends %s", String.class.getName()), TypeUtils.toString(simpleWildcard));
    assertEquals(String.format("? extends %s", String.class.getName()), simpleWildcard.toString());
}
Also used : Field(java.lang.reflect.Field) WildcardType(java.lang.reflect.WildcardType) Test(org.junit.Test)

Example 68 with WildcardType

use of java.lang.reflect.WildcardType in project google-gin by gwtplus.

the class ReflectUtil method getSourceName.

/**
 * Returns a string representation of the passed type's name while ensuring
 * that all type names (base and parameters) are converted to source type
 * names.
 *
 * @param type type for which string will be returned
 * @return String representation of type
 * @throws NoSourceNameException if source name is not available for type
 */
public static String getSourceName(Type type) throws NoSourceNameException {
    if (type instanceof Class<?>) {
        Class<?> clazz = (Class<?>) type;
        if (clazz.isPrimitive()) {
            // Returns "int" for integer etc.
            return clazz.getName();
        }
        String name = clazz.getCanonicalName();
        // have source names.
        if (name == null) {
            throw new NoSourceNameException(type);
        }
        return name;
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        Type[] arguments = parameterizedType.getActualTypeArguments();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(getSourceName(parameterizedType.getRawType()));
        if (arguments.length == 0) {
            return stringBuilder.toString();
        }
        stringBuilder.append("<").append(getSourceName(arguments[0]));
        for (int i = 1; i < arguments.length; i++) {
            stringBuilder.append(", ").append(getSourceName(arguments[i]));
        }
        return stringBuilder.append(">").toString();
    }
    if (type instanceof GenericArrayType) {
        return getSourceName(((GenericArrayType) type).getGenericComponentType()) + "[]";
    }
    if (type instanceof WildcardType) {
        Type[] lowerBounds = ((WildcardType) type).getLowerBounds();
        Type[] upperBounds = ((WildcardType) type).getUpperBounds();
        if (lowerBounds.length > 0 && upperBounds.length > 0 && lowerBounds.length != 1 && lowerBounds[0] != Object.class) {
            throw new NoSourceNameException(type);
        }
        if (lowerBounds.length > 0) {
            return getBoundedSourceName("?", lowerBounds, "super");
        } else if (upperBounds.length == 1 && upperBounds[0] == Object.class) {
            return "?";
        } else {
            return getBoundedSourceName("?", upperBounds, "extends");
        }
    }
    // definition thereof. For the latter see #getTypeVariableDefinition(..).
    if (type instanceof TypeVariable) {
        return ((TypeVariable) type).getName();
    }
    throw new NoSourceNameException(type);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable)

Example 69 with WildcardType

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

the class MoreTypes method equals.

/**
 * Returns true if {@code a} and {@code b} are equal.
 */
public static boolean equals(Type a, Type b) {
    if (a == b) {
        // also handles (a == null && b == null)
        return true;
    } else if (a instanceof Class) {
        // Class already specifies equals().
        return a.equals(b);
    } else if (a instanceof ParameterizedType) {
        if (!(b instanceof ParameterizedType)) {
            return false;
        }
        ParameterizedType pa = (ParameterizedType) a;
        ParameterizedType pb = (ParameterizedType) b;
        return Objects.equal(pa.getOwnerType(), pb.getOwnerType()) && pa.getRawType().equals(pb.getRawType()) && Arrays.equals(getSharedTypeArguments(pa), getSharedTypeArguments(pb));
    } else if (a instanceof GenericArrayType) {
        if (!(b instanceof GenericArrayType)) {
            return false;
        }
        GenericArrayType ga = (GenericArrayType) a;
        GenericArrayType gb = (GenericArrayType) b;
        return equals(ga.getGenericComponentType(), gb.getGenericComponentType());
    } else if (a instanceof WildcardType) {
        if (!(b instanceof WildcardType)) {
            return false;
        }
        WildcardType wa = (WildcardType) a;
        WildcardType wb = (WildcardType) b;
        return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds()) && Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds());
    } else if (a instanceof TypeVariable) {
        if (!(b instanceof TypeVariable)) {
            return false;
        }
        TypeVariable<?> va = (TypeVariable) a;
        TypeVariable<?> vb = (TypeVariable) b;
        return va.getGenericDeclaration().equals(vb.getGenericDeclaration()) && va.getName().equals(vb.getName());
    } else {
        // This isn't a type we support. Could be a generic array type, wildcard type, etc.
        return false;
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 70 with WildcardType

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

the class MoreTypesTest method testGetRawType_wildcard.

public <T> void testGetRawType_wildcard() throws Exception {
    WildcardType wildcard = (WildcardType) ((ParameterizedType) new TypeLiteral<List<?>>() {
    }.getType()).getActualTypeArguments()[0];
    assertEquals(Object.class, MoreTypes.getRawType(wildcard));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) TypeLiteral(com.google.inject.TypeLiteral)

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