Example usage for java.lang.reflect ParameterizedType getRawType

List of usage examples for java.lang.reflect ParameterizedType getRawType

Introduction

In this page you can find the example usage for java.lang.reflect ParameterizedType getRawType.

Prototype

Type getRawType();

Source Link

Document

Returns the Type object representing the class or interface that declared this type.

Usage

From source file:jef.tools.collection.CollectionUtil.java

/**
 * ?Collection//from  www  .ja  va  2s . c o  m
 * 
 * @param type
 * @return
 */
public static boolean isCollection(Type type) {
    if (type instanceof GenericArrayType) {
        return false;
    } else if (type instanceof Class) {
        Class<?> rawType = (Class<?>) type;
        return Collection.class.isAssignableFrom(rawType);
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType) type;
        return isArrayOrCollection(pType.getRawType());
    }
    return false;
}

From source file:org.openflexo.antar.binding.TypeUtils.java

public static String simpleRepresentation(Type aType) {
    if (aType == null) {
        return "null";
    }//from w ww .ja  v a  2s .c  o  m
    if (aType instanceof CustomType) {
        return ((CustomType) aType).simpleRepresentation();
    }
    if (aType instanceof Class) {
        return ((Class) aType).getSimpleName();
    } else if (aType instanceof ParameterizedType) {
        ParameterizedType t = (ParameterizedType) aType;
        StringBuilder sb = new StringBuilder();
        sb.append(simpleRepresentation(t.getRawType())).append("<");
        boolean isFirst = true;
        for (Type st : t.getActualTypeArguments()) {
            sb.append(isFirst ? "" : ",").append(simpleRepresentation(st));
            isFirst = false;
        }
        sb.append(">");
        return sb.toString();
    }
    return aType.toString();
}

From source file:org.openflexo.antar.binding.TypeUtils.java

public static String fullQualifiedRepresentation(Type aType) {
    if (aType == null) {
        return null;
    }//ww w  . j  a v  a  2  s.  c  om
    if (aType instanceof CustomType) {
        return ((CustomType) aType).fullQualifiedRepresentation();
    }
    if (aType instanceof Class) {
        return ((Class) aType).getName();
    } else if (aType instanceof ParameterizedType) {
        ParameterizedType t = (ParameterizedType) aType;
        StringBuilder sb = new StringBuilder();
        sb.append(fullQualifiedRepresentation(t.getRawType())).append("<");
        boolean isFirst = true;
        for (Type st : t.getActualTypeArguments()) {
            sb.append(isFirst ? "" : ",").append(fullQualifiedRepresentation(st));
            isFirst = false;
        }
        sb.append(">");
        return sb.toString();
    }
    return aType.toString();
}

From source file:com.bstek.dorado.data.method.MethodAutoMatchingUtils.java

private static Class<?> getElementType(Type type) {
    if (type instanceof ParameterizedType) {
        ParameterizedType paramedType = (ParameterizedType) type;
        if (Collection.class.isAssignableFrom(toClass(paramedType.getRawType()))) {
            return toClass(paramedType.getActualTypeArguments()[0]);
        }/*from w  w w  .j a v a  2 s .  co m*/
    } else if (type instanceof ParameterizedCollectionType) {
        return ((ParameterizedCollectionType) type).getElementType();
    }
    return null;
}

From source file:org.restlet.ext.jaxrs.internal.wrappers.params.ParameterList.java

/**
 * @return the collection type for the given {@link ParameterizedType
 *         parametrized Type}.<br>
 *         If the given type do not represent an collection, null is
 *         returned.// w w  w .  j av a  2 s .  c om
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Class<Collection<?>> collType(ParameterizedType type) {
    final Type rawType = type.getRawType();
    if (rawType.equals(List.class)) {
        return (Class) ArrayList.class;
    } else if (rawType.equals(Set.class)) {
        return (Class) HashSet.class;
    } else if (rawType.equals(SortedSet.class)) {
        return (Class) TreeSet.class;
    } else if (rawType.equals(Collection.class)) {
        localLogger.config(ParameterList.COLL_PARAM_NOT_DEFAULT);
        return (Class) ArrayList.class;
    }
    return null;
}

From source file:jp.terasoluna.fw.util.GenericsUtil.java

/**
 * <code>Type</code>???<code>ParameterizedType</code> ????????????
 * @param <T> ???/*from  ww w. jav a  2  s.  com*/
 * @param type ?
 * @param genericClass ??
 * @param ancestorTypeList <code>ParameterizedType</code> ?
 * @return <code>type</code>????????
 */
protected static <T> boolean checkParameterizedType(Type type, Class<T> genericClass,
        List<ParameterizedType> ancestorTypeList) {
    if (!(type instanceof ParameterizedType)) {
        return false;
    }

    // ParameterizedType?????ParameterizedType
    // ??
    ParameterizedType parameterlizedType = (ParameterizedType) type;

    // ?Generics????ParameterizedType?
    // ????????
    // ???????????????????
    // ??????????
    // ???Generics?API?????????????
    // ?????????
    if (!genericClass.isAssignableFrom((Class<?>) parameterlizedType.getRawType())) {
        return false;
    }
    ancestorTypeList.add(parameterlizedType);

    // #getRawType???Type??
    if (parameterlizedType.getRawType().equals(genericClass)) {
        return true;
    }
    return false;
}

From source file:org.vulpe.commons.util.VulpeReflectUtil.java

/**
 * Returns class of Type.//from  ww  w.j  a v a  2  s  .c o  m
 *
 * @param clazz
 * @param type
 * @return
 */
public static DeclaredType getDeclaredType(final Class<?> clazz, final Type type) {
    if (type == null) {
        return null;
    }

    DeclaredType declaredType = null;
    if (type instanceof Class) {
        declaredType = new DeclaredType();
        declaredType.setType((Class<?>) type);
    } else if (type instanceof ParameterizedType) {
        declaredType = new DeclaredType();
        final ParameterizedType parameterizedType = (ParameterizedType) type;
        final DeclaredType rawType = getDeclaredType(clazz, parameterizedType.getRawType());
        declaredType.setType(rawType.getType());
        for (int i = 0; i < parameterizedType.getActualTypeArguments().length; i++) {
            declaredType.getItems().add(getDeclaredType(clazz, parameterizedType.getActualTypeArguments()[i]));
        }
    } else if (type instanceof TypeVariable) {
        return getDeclaredTypeVariable(clazz, (TypeVariable<?>) type);
    } else if (type instanceof WildcardType) {
        declaredType = new DeclaredType();
        final WildcardType wildcardType = (WildcardType) type;
        if (wildcardType.getLowerBounds().length > 0) {
            declaredType.setType(getDeclaredType(clazz, wildcardType.getLowerBounds()[0]).getType());
        } else {
            declaredType.setType(null);
        }

        for (int i = 0; i < wildcardType.getUpperBounds().length; i++) {
            declaredType.getTypeItems().add(getDeclaredType(clazz, wildcardType.getUpperBounds()[i]));
        }

        return declaredType;
    }
    return declaredType;
}

From source file:ca.oson.json.util.ObjectUtil.java

public static <E> Class<E> getTypeClass(java.lang.reflect.Type type) {
    Class cl = type.getClass();/*w  w w.jav  a  2 s  .  c  o m*/

    if (ComponentType.class.isAssignableFrom(cl)) {
        ComponentType componentType = (ComponentType) type;
        return componentType.getClassType();
    }

    //java.util.List<ca.oson.json.test.Dataset>
    String className = type.getTypeName();
    try {
        int idx = className.indexOf('<');
        if (idx > 0) {
            className = className.substring(0, idx);
        }

        return (Class<E>) Class.forName(className);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    // Collection<String>, return String.class
    if (ParameterizedType.class.isAssignableFrom(cl)) {
        java.lang.reflect.ParameterizedType pt = (java.lang.reflect.ParameterizedType) type;

        return (Class<E>) pt.getRawType().getClass();

        // GenericArrayType represents an array type whose component
        // type is either a parameterized type or a type variable.
    } else if (java.lang.reflect.GenericArrayType.class.isAssignableFrom(cl)) {
        java.lang.reflect.GenericArrayType pt = (java.lang.reflect.GenericArrayType) type;

        return (Class<E>) pt.getClass();

    } else if (java.lang.reflect.TypeVariable.class.isAssignableFrom(cl)) {
        java.lang.reflect.TypeVariable pt = (java.lang.reflect.TypeVariable) type;

        return (Class<E>) pt.getClass();
    }

    return cl;
}

From source file:richtercloud.reflection.form.builder.typehandler.AbstractListTypeHandler.java

@Override
public Pair<JComponent, ComponentHandler<?>> handle(Type type, T fieldValue, String fieldName,
        Class<?> declaringClass, FieldUpdateListener<E> updateListener, R reflectionFormBuilder)
        throws IllegalArgumentException, IllegalAccessException, FieldHandlingException {
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        if (!parameterizedType.getRawType().equals(List.class)) {
            throw new IllegalArgumentException(String.format(
                    "list field handlers are only allowed to be used with types with raw type %s (type is %s)",
                    List.class, type));
        }/*from   w  w  w  .ja  v a 2 s  .  co  m*/
    }
    Pair<JComponent, ComponentHandler<?>> retValue = handle0(type, fieldValue, updateListener,
            reflectionFormBuilder);
    return retValue;
}

From source file:org.assertj.assertions.generator.util.ClassUtil.java

public static Set<Class<?>> getClassesRelatedTo(Type type) {
    Set<Class<?>> classes = new HashSet<Class<?>>();

    // non generic type : just add current type.
    if (type instanceof Class) {
        classes.add((Class<?>) type);
        return classes;
    }//w w w  .j a v  a  2  s .  com

    // generic type : add current type and its parameter types
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        for (Type actualTypeArgument : parameterizedType.getActualTypeArguments()) {
            if (actualTypeArgument instanceof ParameterizedType) {
                classes.addAll(getClassesRelatedTo(actualTypeArgument));
            } else if (actualTypeArgument instanceof Class) {
                classes.add((Class<?>) actualTypeArgument);
            } else if (actualTypeArgument instanceof GenericArrayType) {
                classes.addAll(getClassesRelatedTo(actualTypeArgument));
            }
        }
        Type rawType = parameterizedType.getRawType();
        if (rawType instanceof Class) {
            classes.add((Class<?>) rawType);
        }
    }
    return classes;
}