Example usage for java.lang.reflect GenericArrayType getGenericComponentType

List of usage examples for java.lang.reflect GenericArrayType getGenericComponentType

Introduction

In this page you can find the example usage for java.lang.reflect GenericArrayType getGenericComponentType.

Prototype

Type getGenericComponentType();

Source Link

Document

Returns a Type object representing the component type of this array.

Usage

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

private static Type ensureConsistentGenericArrayType(final GenericArrayType type) {
    final Type componentType = ensureConsistentType(type.getGenericComponentType());
    return org.apache.commons.lang3.reflect.TypeUtils.genericArrayType(componentType);
}

From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java

/**
 * @param runtime/*from   www  .j a  va 2  s. c  om*/
 *          is <code>true</code> if we need name for class loading, <code>false</code> if we need
 *          name for source generation.
 * 
 * @return the fully qualified name of given {@link Type}.
 */
public static String getFullyQualifiedName(Type type, boolean runtime) {
    Assert.isNotNull(type);
    // Class
    if (type instanceof Class<?>) {
        Class<?> clazz = (Class<?>) type;
        // array
        if (clazz.isArray()) {
            return getFullyQualifiedName(clazz.getComponentType(), runtime) + "[]";
        }
        // object
        String name = clazz.getName();
        if (!runtime) {
            name = name.replace('$', '.');
        }
        return name;
    }
    // GenericArrayType
    if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = (GenericArrayType) type;
        return getFullyQualifiedName(genericArrayType.getGenericComponentType(), runtime) + "[]";
    }
    // ParameterizedType
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        Type rawType = parameterizedType.getRawType();
        // raw type
        StringBuilder sb = new StringBuilder();
        sb.append(getFullyQualifiedName(rawType, runtime));
        // type arguments
        sb.append("<");
        boolean firstTypeArgument = true;
        for (Type typeArgument : parameterizedType.getActualTypeArguments()) {
            if (!firstTypeArgument) {
                sb.append(",");
            }
            firstTypeArgument = false;
            sb.append(getFullyQualifiedName(typeArgument, runtime));
        }
        sb.append(">");
        // done
        return sb.toString();
    }
    // WildcardType
    if (type instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) type;
        return "? extends " + getFullyQualifiedName(wildcardType.getUpperBounds()[0], runtime);
    }
    // TypeVariable
    TypeVariable<?> typeVariable = (TypeVariable<?>) type;
    return typeVariable.getName();
}

From source file:org.eclipse.wb.internal.swing.databinding.model.generic.GenericUtils.java

public static IGenericType getObjectType(TypeVariable<?> superTypeParameter, Type superTypeParameterClass,
        PropertyDescriptor descriptor) {
    Method readMethod = descriptor.getReadMethod();
    Class<?> rawType = descriptor.getPropertyType();
    if (readMethod == null) {
        return new ClassGenericType(rawType, null, null);
    }//from   w w  w.  jav a 2s .  c o m
    Type type = readMethod.getGenericReturnType();
    if (type instanceof Class<?> || type instanceof TypeVariable<?>) {
        return new ClassGenericType(rawType, null, null);
    }
    if (type instanceof ParameterizedType) {
        GenericTypeContainer genericType = new GenericTypeContainer(rawType);
        ParameterizedType parameterizedType = (ParameterizedType) type;
        //
        if (superTypeParameter != null && parameterizedType.getActualTypeArguments().length == 1
                && superTypeParameter == parameterizedType.getActualTypeArguments()[0]) {
            genericType.getSubTypes().add(resolveType(superTypeParameterClass));
            return genericType;
        }
        for (Type subType : parameterizedType.getActualTypeArguments()) {
            genericType.getSubTypes().add(resolveType(subType));
        }
        return genericType;
    }
    if (type instanceof GenericArrayType) {
        int dimension = 0;
        Type elementType = null;
        GenericArrayType arrayType = (GenericArrayType) type;
        while (true) {
            dimension++;
            elementType = arrayType.getGenericComponentType();
            if (elementType instanceof GenericArrayType) {
                arrayType = (GenericArrayType) elementType;
                continue;
            }
            break;
        }
        GenericTypeContainer genericType = new GenericTypeContainer(rawType, dimension);
        genericType.getSubTypes().add(resolveType(elementType));
        return genericType;
    }
    Assert.fail(MessageFormat.format("Undefine type: {0} {1}", readMethod, rawType));
    return null;
}

From source file:org.eclipse.wb.internal.swing.databinding.model.generic.GenericUtils.java

private static String resolveTypeName(Type type) {
    if (type instanceof Class<?>) {
        Class<?> rawType = (Class<?>) type;
        return convertPrimitiveType(ReflectionUtils.getFullyQualifiedName(rawType, false));
    }/*ww w  . j a va 2s . c o  m*/
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        Class<?> rawType = (Class<?>) parameterizedType.getRawType();
        StringBuffer fullName = new StringBuffer();
        fullName.append(CoreUtils.getClassName(rawType));
        fullName.append("<");
        Type[] types = parameterizedType.getActualTypeArguments();
        for (int i = 0; i < types.length; i++) {
            if (i > 0) {
                fullName.append(", ");
            }
            fullName.append(resolveTypeName(types[i]));
        }
        fullName.append(">");
        return fullName.toString();
    }
    if (type instanceof GenericArrayType) {
        StringBuffer fullName = new StringBuffer();
        Type elementType = null;
        GenericArrayType arrayType = (GenericArrayType) type;
        while (true) {
            fullName.append("[]");
            elementType = arrayType.getGenericComponentType();
            if (elementType instanceof GenericArrayType) {
                arrayType = (GenericArrayType) elementType;
                continue;
            }
            break;
        }
        fullName.insert(0, resolveTypeName(elementType));
        return fullName.toString();
    }
    if (type instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) type;
        Type[] upperBounds = wildcardType.getUpperBounds();
        Type[] lowerBounds = wildcardType.getLowerBounds();
        if (!ArrayUtils.isEmpty(upperBounds)) {
            Type upperBound = upperBounds[0];
            boolean isWildcard = upperBound instanceof Class<?>
                    && ((Class<?>) upperBound).getName().equals("java.lang.Object");
            if (!isWildcard) {
                return "? extends " + resolveTypeName(upperBound);
            }
        } else if (!ArrayUtils.isEmpty(lowerBounds)) {
            return "? super " + resolveTypeName(lowerBounds[0]);
        }
        return "?";
    }
    if (type instanceof TypeVariable<?>) {
        return "?";
    }
    Assert.fail("Undefine type: " + type);
    return null;
}

From source file:org.eiichiro.bootleg.Types.java

/**
 * Returns the element type of the specified collection type. 
 * The specified type must be collection or array. To make it sure, use 
 * {@code #isCollection(Type)} method or {@code #isArray(Type)} method.
 * //from w  w w  . j  a  v a 2s . co m
 * @param type Collection or array type. 
 * @return The element type of the specified collection or array.
 * @throws IllegalArgumentException If the specified 'type' is not a 
 * collection or array.
 */
public static Class<?> getElementType(Type type) {
    if (isCollection(type)) {
        if (type instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            return (Class<?>) parameterizedType.getActualTypeArguments()[0];
        } else {
            return Object.class;
        }

    } else if (isArray(type)) {
        if (type instanceof GenericArrayType) {
            GenericArrayType genericArrayType = (GenericArrayType) type;
            return (Class<?>) genericArrayType.getGenericComponentType();
        } else {
            Class<?> clazz = (Class<?>) type;
            return clazz.getComponentType();
        }

    } else {
        throw new IllegalArgumentException("'type' must be a collection or array");
    }
}

From source file:org.evosuite.utils.generic.GenericAccessibleObject.java

protected static Type getTypeFromExactReturnType(GenericArrayType returnType, GenericArrayType type) {
    return GenericArrayTypeImpl.createArrayType(
            getTypeFromExactReturnType(returnType.getGenericComponentType(), type.getGenericComponentType()));
}

From source file:org.evosuite.utils.generic.GenericAccessibleObject.java

protected static Type getTypeFromExactReturnType(GenericArrayType returnType, ParameterizedType type) {
    return GenericArrayTypeImpl
            .createArrayType(getTypeFromExactReturnType(returnType.getGenericComponentType(), type));
}

From source file:org.evosuite.utils.generic.GenericAccessibleObject.java

protected static Type getTypeFromExactReturnType(ParameterizedType returnType, GenericArrayType type) {
    return GenericArrayTypeImpl
            .createArrayType(getTypeFromExactReturnType(returnType, type.getGenericComponentType()));
}

From source file:org.evosuite.utils.generic.GenericClass.java

/**
 * Returns the erasure of the given type.
 *///  w ww  .  j a  v a  2s.c o  m
private static Class<?> erase(Type type) {
    if (type instanceof Class) {
        return (Class<?>) type;
    } else if (type instanceof ParameterizedType) {
        return (Class<?>) ((ParameterizedType) type).getRawType();
    } else if (type instanceof TypeVariable) {
        TypeVariable<?> tv = (TypeVariable<?>) type;
        if (tv.getBounds().length == 0)
            return Object.class;
        else
            return erase(tv.getBounds()[0]);
    } else if (type instanceof GenericArrayType) {
        GenericArrayType aType = (GenericArrayType) type;
        return GenericArrayTypeImpl.createArrayType(erase(aType.getGenericComponentType()));
    } else if (type instanceof CaptureType) {
        CaptureType captureType = (CaptureType) type;
        if (captureType.getUpperBounds().length == 0)
            return Object.class;
        else
            return erase(captureType.getUpperBounds()[0]);
    } else {
        // TODO at least support CaptureType here
        throw new RuntimeException("not supported: " + type.getClass());
    }
}

From source file:org.evosuite.utils.generic.GenericClass.java

public GenericClass getComponentClass() {
    if (type instanceof GenericArrayType) {
        GenericArrayType arrayType = (GenericArrayType) type;
        Type componentType = arrayType.getGenericComponentType();
        Class<?> rawComponentType = rawClass.getComponentType();
        return new GenericClass(componentType, rawComponentType);
    } else {//from   w  w  w . j a v a2  s  .  c o m
        return new GenericClass(rawClass.getComponentType());
    }
}