Example usage for java.lang.reflect GenericArrayType toString

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

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:com.wavemaker.json.type.reflect.ReflectTypeUtils.java

/**
 * Returns information about array or collection types.
 * //w w w  .j  av  a2  s  .  com
 * @param type The type to introspect.
 * @param typeState The current TypeState.
 * @param strict True indicates that processing should stop on ambiguous entries; false indicates that null should
 *        be entered.
 * @return A Tuple.Two containing:
 *         <ol>
 *         <li>The enclosed nested Type; String[][] would return String.class, while List<Map<String,String>> will
 *         return the Type of Map<String,String>.</li>
 *         <li>The list of all enclosing classes. String[][] will return [String[][].class, String[].class].</li>
 *         </ol>
 */
protected static Tuple.Two<TypeDefinition, List<ListTypeDefinition>> getArrayDimensions(Type type,
        TypeState typeState, boolean strict) {

    if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;

        Type[] types = pt.getActualTypeArguments();
        if (1 == types.length) {
            Tuple.Two<TypeDefinition, List<ListTypeDefinition>> temp = getArrayDimensions(types[0], typeState,
                    strict);
            temp.v2.add(0, getListTypeDefinition(pt.getRawType(), typeState, strict));
            return temp;
        } else {
            return new Tuple.Two<TypeDefinition, List<ListTypeDefinition>>(
                    getTypeDefinition(pt, typeState, strict), new ArrayList<ListTypeDefinition>());
        }
    } else if (type instanceof GenericArrayType) {
        GenericArrayType gat = (GenericArrayType) type;

        Class<?> klass;
        try {
            klass = ClassUtils.forName(gat.toString());
        } catch (ClassNotFoundException e) {
            klass = null;
        } catch (LinkageError e) {
            klass = null;
        }
        if (klass == null && gat.getGenericComponentType() instanceof Class) {
            klass = Array.newInstance((Class<?>) gat.getGenericComponentType(), 0).getClass();
        }
        if (klass == null) {
            throw new WMRuntimeException(MessageResource.JSON_FAILED_GENERICARRAYTYPE, gat,
                    gat.getGenericComponentType());
        }

        Tuple.Two<TypeDefinition, List<ListTypeDefinition>> temp = getArrayDimensions(
                gat.getGenericComponentType(), typeState, strict);
        temp.v2.add(0, getListTypeDefinition(klass, typeState, strict));

        return temp;
    } else if (type instanceof Class && ((Class<?>) type).isArray()) {
        Tuple.Two<TypeDefinition, List<ListTypeDefinition>> temp = getArrayDimensions(
                ((Class<?>) type).getComponentType(), typeState, strict);
        temp.v2.add(0, getListTypeDefinition(type, typeState, strict));

        return temp;
    } else if (type instanceof Class) {
        return new Tuple.Two<TypeDefinition, List<ListTypeDefinition>>(
                getTypeDefinition(type, typeState, strict), new ArrayList<ListTypeDefinition>());
    } else {
        throw new WMRuntimeException(MessageResource.JSON_TYPE_UNKNOWNPARAMTYPE, type,
                type != null ? type.getClass() : null);
    }
}

From source file:org.assertj.assertions.generator.description.converter.ClassToClassDescriptionConverter.java

private TypeDescription buildIterableTypeDescription(Member member, final Class<?> type) {
    final TypeDescription typeDescription = new TypeDescription(new TypeName(type));
    typeDescription.setIterable(true);/*from   www .ja v a 2s .  c o  m*/
    if (methodReturnTypeHasNoParameterInfo(member)) {
        // not a ParameterizedType, i.e. no parameter information => use Object as element type.
        typeDescription.setElementTypeName(new TypeName(Object.class));
        return typeDescription;
    }
    ParameterizedType parameterizedType = getParameterizedTypeOf(member);
    if (parameterizedType.getActualTypeArguments()[0] instanceof GenericArrayType) {
        GenericArrayType genericArrayType = (GenericArrayType) parameterizedType.getActualTypeArguments()[0];
        typeDescription.setElementTypeName(new TypeName(genericArrayType.toString()));
        return typeDescription;
    }
    // Due to http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7151486,
    // java 7 is not able to detect GenericArrayType correctly => let's use a different way to detect array
    Class<?> internalClass = ClassUtil.getClass(parameterizedType.getActualTypeArguments()[0]);
    if (internalClass.isArray()) {
        String componentTypeWithoutClassPrefix = remove(internalClass.getComponentType().toString(), "class ");
        typeDescription.setElementTypeName(new TypeName(componentTypeWithoutClassPrefix + "[]"));
    } else {
        typeDescription.setElementTypeName(new TypeName(internalClass));
    }
    return typeDescription;
}