Search in sources :

Example 21 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project jodd by oblac.

the class ReflectUtil method getRawType.

/**
	 * Returns raw class for given <code>type</code> when implementation class is known
	 * and it makes difference.
	 * @see #resolveVariable(java.lang.reflect.TypeVariable, Class)
	 */
public static Class<?> getRawType(Type type, Class implClass) {
    if (type instanceof Class) {
        return (Class) type;
    }
    if (type instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType) type;
        return getRawType(pType.getRawType(), implClass);
    }
    if (type instanceof WildcardType) {
        WildcardType wType = (WildcardType) type;
        Type[] lowerTypes = wType.getLowerBounds();
        if (lowerTypes.length > 0) {
            return getRawType(lowerTypes[0], implClass);
        }
        Type[] upperTypes = wType.getUpperBounds();
        if (upperTypes.length != 0) {
            return getRawType(upperTypes[0], implClass);
        }
        return Object.class;
    }
    if (type instanceof GenericArrayType) {
        Type genericComponentType = ((GenericArrayType) type).getGenericComponentType();
        Class<?> rawType = getRawType(genericComponentType, implClass);
        // this is sort of stupid, but there seems no other way (consider don't creating new instances each time)...
        return Array.newInstance(rawType, 0).getClass();
    }
    if (type instanceof TypeVariable) {
        TypeVariable<?> varType = (TypeVariable<?>) type;
        if (implClass != null) {
            Type resolvedType = resolveVariable(varType, implClass);
            if (resolvedType != null) {
                return getRawType(resolvedType, null);
            }
        }
        Type[] boundsTypes = varType.getBounds();
        if (boundsTypes.length == 0) {
            return Object.class;
        }
        return getRawType(boundsTypes[0], implClass);
    }
    return null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) AccessibleObject(java.lang.reflect.AccessibleObject) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 22 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project jodd by oblac.

the class ReflectUtil method getComponentTypes.

/**
	 * Returns all component types of the given type.
	 * For example the following types all have the
	 * component-type MyClass:
	 * <ul>
	 * <li>MyClass[]</li>
	 * <li>List&lt;MyClass&gt;</li>
	 * <li>Foo&lt;? extends MyClass&gt;</li>
	 * <li>Bar&lt;? super MyClass&gt;</li>
	 * <li>&lt;T extends MyClass&gt; T[]</li>
	 * </ul>
	 */
public static Class[] getComponentTypes(Type type, Class implClass) {
    if (type instanceof Class) {
        Class clazz = (Class) type;
        if (clazz.isArray()) {
            return new Class[] { clazz.getComponentType() };
        }
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        Type[] generics = pt.getActualTypeArguments();
        if (generics.length == 0) {
            return null;
        }
        Class[] types = new Class[generics.length];
        for (int i = 0; i < generics.length; i++) {
            types[i] = getRawType(generics[i], implClass);
        }
        return types;
    } else if (type instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) type;
        Class rawType = getRawType(gat.getGenericComponentType(), implClass);
        if (rawType == null) {
            return null;
        }
        return new Class[] { rawType };
    }
    return null;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 23 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project Genius-Android by qiujuer.

the class Reflector method getClass.

/**
     * Get the underlying class for a type, or null if the type is a variable
     * type.
     *
     * @param type the type
     * @return the underlying class
     */
public static Class<?> getClass(Type type) {
    if (type instanceof Class) {
        return (Class) type;
    } else if (type instanceof ParameterizedType) {
        return getClass(((ParameterizedType) type).getRawType());
    } else if (type instanceof GenericArrayType) {
        Type componentType = ((GenericArrayType) type).getGenericComponentType();
        Class<?> componentClass = getClass(componentType);
        if (componentClass != null) {
            return Array.newInstance(componentClass, 0).getClass();
        } else {
            return null;
        }
    } else {
        return null;
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 24 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project roboguice by roboguice.

the class TypesTest method testEqualsAndHashcode.

public void testEqualsAndHashcode() {
    ParameterizedType parameterizedType = Types.newParameterizedType(Map.class, String.class, Integer.class);
    assertEqualsBothWays(mapStringInteger, parameterizedType);
    assertEquals(mapStringInteger.toString(), parameterizedType.toString());
    GenericArrayType genericArrayType = Types.arrayOf(Types.arrayOf(Types.newParameterizedType(Set.class, String.class)));
    assertEqualsBothWays(setStringArray, genericArrayType);
    assertEquals(setStringArray.toString(), genericArrayType.toString());
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType)

Example 25 with GenericArrayType

use of java.lang.reflect.GenericArrayType in project robovm by robovm.

the class Types method appendGenericType.

public static void appendGenericType(StringBuilder out, Type type) {
    if (type instanceof TypeVariable) {
        out.append(((TypeVariable) type).getName());
    } else if (type instanceof ParameterizedType) {
        out.append(type.toString());
    } else if (type instanceof GenericArrayType) {
        Type simplified = ((GenericArrayType) type).getGenericComponentType();
        appendGenericType(out, simplified);
        out.append("[]");
    } else if (type instanceof Class) {
        Class c = (Class<?>) type;
        if (c.isArray()) {
            String[] as = c.getName().split("\\[");
            int len = as.length - 1;
            if (as[len].length() > 1) {
                out.append(as[len].substring(1, as[len].length() - 1));
            } else {
                char ch = as[len].charAt(0);
                if (ch == 'I') {
                    out.append("int");
                } else if (ch == 'B') {
                    out.append("byte");
                } else if (ch == 'J') {
                    out.append("long");
                } else if (ch == 'F') {
                    out.append("float");
                } else if (ch == 'D') {
                    out.append("double");
                } else if (ch == 'S') {
                    out.append("short");
                } else if (ch == 'C') {
                    out.append("char");
                } else if (ch == 'Z') {
                    out.append("boolean");
                } else if (ch == 'V') {
                    out.append("void");
                }
            }
            for (int i = 0; i < len; i++) {
                out.append("[]");
            }
        } else {
            out.append(c.getName());
        }
    }
}
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) GenericArrayType(java.lang.reflect.GenericArrayType)

Aggregations

GenericArrayType (java.lang.reflect.GenericArrayType)126 ParameterizedType (java.lang.reflect.ParameterizedType)120 Type (java.lang.reflect.Type)97 TypeVariable (java.lang.reflect.TypeVariable)75 WildcardType (java.lang.reflect.WildcardType)72 Field (java.lang.reflect.Field)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Method (java.lang.reflect.Method)4 Test (org.junit.Test)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 ElementType (java.lang.annotation.ElementType)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 GenericType (javax.ws.rs.core.GenericType)3 CompositeType (org.apache.flink.api.common.typeutils.CompositeType)3 EqualsTester (com.google.common.testing.EqualsTester)2 TypeLiteral (com.google.inject.TypeLiteral)2 DBObject (com.mongodb.DBObject)2