List of usage examples for java.lang.reflect GenericArrayType getGenericComponentType
Type getGenericComponentType();
From source file:Main.java
protected static List<Type> getGenericParameterList(Type type) { if (ParameterizedType.class.isInstance(type)) { final ParameterizedType paramType = ParameterizedType.class.cast(type); return Arrays.asList(paramType.getActualTypeArguments()); }//w ww . j av a2 s .c o m if (GenericArrayType.class.isInstance(type)) { final GenericArrayType arrayType = GenericArrayType.class.cast(type); return getGenericParameterList(arrayType.getGenericComponentType()); } @SuppressWarnings("unchecked") List<Type> emptyList = Collections.EMPTY_LIST; return emptyList; }
From source file:GenericsUtil.java
private static Class<?> getActualClass(Type type, Map<TypeVariable<?>, Type> map) { if (Class.class.isInstance(type)) { return Class.class.cast(type); }// w ww . j a v a2 s.c o m if (ParameterizedType.class.isInstance(type)) { final Type actualType = getActualType(type, map); return getActualClass(actualType, map); } else if (TypeVariable.class.isInstance(type)) { final Type actualType = getActualType(type, map); return getActualClass(actualType, map); } else if (GenericArrayType.class.isInstance(type)) { GenericArrayType genericArrayType = GenericArrayType.class.cast(type); final Type genericComponentType = genericArrayType.getGenericComponentType(); Class<?> componentClass = getActualClass(genericComponentType, map); return Array.newInstance(componentClass, 0).getClass(); } else { return null; } }
From source file:com.agimatec.validation.jsr303.NestedMetaProperty.java
static Type getIndexedType(Type type) { Type indexedType = type;//from ww w. j av a 2s. c o m if (isCollection(type) && type instanceof ParameterizedType) { ParameterizedType paramType = (ParameterizedType) type; Class collectionClass = getCollectionClass(type); if (Collection.class.isAssignableFrom(collectionClass)) { indexedType = paramType.getActualTypeArguments()[0]; } else if (Map.class.isAssignableFrom(collectionClass)) { indexedType = paramType.getActualTypeArguments()[1]; } } else if (isArray(type) && type instanceof GenericArrayType) { GenericArrayType arrayTye = (GenericArrayType) type; indexedType = arrayTye.getGenericComponentType(); } return indexedType; }
From source file:com.autentia.common.util.ClassWithList.java
private static void print(GenericArrayType gat) { System.out.println("Generic array type"); System.out.println("Type of array: "); print(gat.getGenericComponentType()); }
From source file:antre.TypeResolver.java
/** * Resolves the raw class for the given {@code genericType}, using the type variable information * from the {@code targetType}.//ww w . j a va 2s .co m */ public static Class<?> resolveClass(Type genericType, Class<?> targetType) { if (genericType instanceof Class) { return (Class<?>) genericType; } else if (genericType instanceof ParameterizedType) { return resolveClass(((ParameterizedType) genericType).getRawType(), targetType); } else if (genericType instanceof GenericArrayType) { GenericArrayType arrayType = (GenericArrayType) genericType; Class<?> compoment = resolveClass(arrayType.getGenericComponentType(), targetType); return Array.newInstance(compoment, 0).getClass(); } else if (genericType instanceof TypeVariable) { TypeVariable<?> variable = (TypeVariable<?>) genericType; genericType = getTypeVariableMap(targetType).get(variable); genericType = genericType == null ? resolveBound(variable) : resolveClass(genericType, targetType); } return genericType instanceof Class ? (Class<?>) genericType : Unknown.class; }
From source file:org.jodah.typetools.TypeResolver.java
/** * Resolves the raw class for the {@code genericType}, using the type variable information from the {@code subType} * else {@link Unknown} if the raw class cannot be resolved. * /* w ww.ja v a 2 s . c om*/ * @param type * to resolve raw class for * @param subType * to extract type variable information from * @return raw class for the {@code genericType} else {@link Unknown} if it cannot be resolved */ public static Class<?> resolveRawClass(Type genericType, Class<?> subType) { if (genericType instanceof Class) { return (Class<?>) genericType; } else if (genericType instanceof ParameterizedType) { return resolveRawClass(((ParameterizedType) genericType).getRawType(), subType); } else if (genericType instanceof GenericArrayType) { GenericArrayType arrayType = (GenericArrayType) genericType; Class<?> compoment = resolveRawClass(arrayType.getGenericComponentType(), subType); return Array.newInstance(compoment, 0).getClass(); } else if (genericType instanceof TypeVariable) { TypeVariable<?> variable = (TypeVariable<?>) genericType; genericType = getTypeVariableMap(subType).get(variable); genericType = genericType == null ? resolveBound(variable) : resolveRawClass(genericType, subType); } return genericType instanceof Class ? (Class<?>) genericType : Unknown.class; }
From source file:net.radai.beanz.util.ReflectionUtil.java
public static Type getElementType(Type type) { if (isArray(type)) { if (type instanceof Class<?>) { Class<?> clazz = (Class<?>) type; return clazz.getComponentType(); }//from w ww. ja va2 s. com if (type instanceof GenericArrayType) { GenericArrayType arrayType = (GenericArrayType) type; return arrayType.getGenericComponentType(); } throw new UnsupportedOperationException(); } if (isCollection(type)) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type[] typeArguments = parameterizedType.getActualTypeArguments(); if (typeArguments.length != 1) { throw new UnsupportedOperationException(); } return typeArguments[0]; } if (type instanceof Class<?>) { //this is something like a plain List(), no generic information, so type unknown return null; } throw new UnsupportedOperationException(); } if (isMap(type)) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type[] typeArguments = parameterizedType.getActualTypeArguments(); if (typeArguments.length != 2) { throw new UnsupportedOperationException(); } return typeArguments[1]; } if (type instanceof Class<?>) { //this is something like a plain Map(), no generic information, so type unknown return null; } } throw new UnsupportedOperationException(); }
From source file:net.radai.beanz.util.ReflectionUtil.java
public static String prettyPrint(Type type) { if (type instanceof GenericArrayType) { GenericArrayType genericArrayType = (GenericArrayType) type; return prettyPrint(genericArrayType.getGenericComponentType()) + "[]"; }// ww w.j a v a2 s. c om if (type instanceof WildcardType) { WildcardType wildcardType = (WildcardType) type; return wildcardType.toString(); } if (type instanceof TypeVariable) { TypeVariable<?> typeVariable = (TypeVariable) type; return typeVariable.getName(); } if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; StringBuilder sb = new StringBuilder(); sb.append(prettyPrint(parameterizedType.getRawType())); Type[] typeArguments = parameterizedType.getActualTypeArguments(); if (typeArguments != null && typeArguments.length > 0) { sb.append("<"); for (Type typeArgument : typeArguments) { sb.append(prettyPrint(typeArgument)).append(", "); } sb.delete(sb.length() - 2, sb.length()); // last ", " sb.append(">"); } return sb.toString(); } Class<?> clazz = (Class<?>) type; if (clazz.isArray()) { return prettyPrint(clazz.getComponentType()) + "[]"; } return clazz.getSimpleName(); }
From source file:net.jodah.typetools.TypeResolver.java
private static Class<?> resolveRawClass(Type genericType, Class<?> subType, Class<?> functionalInterface) { if (genericType instanceof Class) { return (Class<?>) genericType; } else if (genericType instanceof ParameterizedType) { return resolveRawClass(((ParameterizedType) genericType).getRawType(), subType, functionalInterface); } else if (genericType instanceof GenericArrayType) { GenericArrayType arrayType = (GenericArrayType) genericType; Class<?> compoment = resolveRawClass(arrayType.getGenericComponentType(), subType, functionalInterface); return Array.newInstance(compoment, 0).getClass(); } else if (genericType instanceof TypeVariable) { TypeVariable<?> variable = (TypeVariable<?>) genericType; genericType = getTypeVariableMap(subType, functionalInterface).get(variable); genericType = genericType == null ? resolveBound(variable) : resolveRawClass(genericType, subType, functionalInterface); }// ww w . jav a 2 s .c om return genericType instanceof Class ? (Class<?>) genericType : Unknown.class; }
From source file:com.wavemaker.json.type.reflect.ReflectTypeUtils.java
/** * Returns information about array or collection types. * //w w w. jav a 2s .co m * @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); } }