List of usage examples for java.lang.reflect ParameterizedType getRawType
Type getRawType();
From source file:com.jaspersoft.jasperserver.war.helper.GenericParametersHelper.java
private static ParameterizedType findParametrizedType(Class<?> classToParse, Class<?> genericClassToFind, Map<String, Class<?>> inputParameterValues) { ParameterizedType type = null; if (genericClassToFind.isInterface()) { final Type[] genericInterfaces = classToParse.getGenericInterfaces(); if (genericInterfaces != null && genericInterfaces.length > 0) { for (Type genericInterface : genericInterfaces) { if (genericInterface == genericClassToFind) { throw new IllegalArgumentException(classToParse.getName() + " is raw implementation of " + genericClassToFind.getName()); }/*from w w w. j a va 2 s.c om*/ if (genericInterface instanceof ParameterizedType) { ParameterizedType currentParametrizedType = (ParameterizedType) genericInterface; Map<String, Class<?>> currentParameterValues = new HashMap<String, Class<?>>( inputParameterValues); if (currentParametrizedType.getRawType() == genericClassToFind) { type = (ParameterizedType) genericInterface; } else { currentParameterValues = getCurrentParameterValues( ((Class<?>) currentParametrizedType.getRawType()).getTypeParameters(), currentParametrizedType.getActualTypeArguments(), new HashMap<String, Class<?>>(inputParameterValues)); type = findParametrizedType((Class<?>) currentParametrizedType.getRawType(), genericClassToFind, currentParameterValues); } if (type != null) { inputParameterValues.clear(); inputParameterValues.putAll(currentParameterValues); break; } } } } } else { final Type genericSuperclass = classToParse.getGenericSuperclass(); if (genericSuperclass == genericClassToFind) { log.debug(classToParse.getName() + " is raw subclass of " + genericClassToFind.getName()); } else if (genericSuperclass instanceof ParameterizedType && ((ParameterizedType) genericSuperclass).getRawType() == genericClassToFind) { type = (ParameterizedType) genericSuperclass; } } return type; }
From source file:org.alfresco.module.org_alfresco_module_rm.api.PublicAPITestUtil.java
/** * Find all classes that are within the supplied type. For example a {@code Pair<Set<String>, Integer>} contains * references to four classes./* w w w . java2s . c o m*/ * * @param type The type to examine. * @param processedTypes The set of types which have already been processed. If {@code type} is within this set then * the method returns an empty set, to prevent analysis of the same type multiple times, and to guard * against circular references. The underlying set is updated with the given type. * @return The set of classes used to form the given type. */ private static Set<Class<?>> getClassesFromType(Type type, Set<Type> processedTypes) { Set<Class<?>> returnClasses = new HashSet<>(); if (processedTypes.add(type)) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; returnClasses.add((Class<?>) parameterizedType.getRawType()); for (Type t : parameterizedType.getActualTypeArguments()) { returnClasses.addAll(getClassesFromType(t, processedTypes)); } } else if (type instanceof Class) { Class<?> clazz = (Class<?>) type; if (clazz.isArray()) { returnClasses.add(clazz.getComponentType()); } returnClasses.add(clazz); } else if (type instanceof WildcardType) { // No-op - Caller can choose what type to use. } else if (type instanceof TypeVariable<?>) { TypeVariable<?> typeVariable = (TypeVariable<?>) type; for (Type bound : typeVariable.getBounds()) { returnClasses.addAll(getClassesFromType(bound, processedTypes)); } } else if (type instanceof GenericArrayType) { GenericArrayType genericArrayType = (GenericArrayType) type; returnClasses .addAll(getClassesFromType(genericArrayType.getGenericComponentType(), processedTypes)); } else { throw new IllegalStateException("This test was not written to work with type " + type); } } return returnClasses; }
From source file:com.oltpbenchmark.util.ClassUtil.java
private static void getGenericTypesImpl(ParameterizedType ptype, List<Class<?>> classes) { // list the actual type arguments for (Type t : ptype.getActualTypeArguments()) { if (t instanceof Class) { // System.err.println("C: " + t); classes.add((Class<?>) t); } else if (t instanceof ParameterizedType) { ParameterizedType next = (ParameterizedType) t; // System.err.println("PT: " + next); classes.add((Class<?>) next.getRawType()); getGenericTypesImpl(next, classes); }//from ww w.ja v a 2s. c om } // FOR return; }
From source file:org.jodah.typetools.TypeResolver.java
/** * Populates the {@code map} with with variable/argument pairs for the given {@code types}. *///ww w. j a va 2 s . co m private static void buildTypeVariableMap(final Type[] types, final Map<TypeVariable<?>, Type> map) { for (Type type : types) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; buildTypeVariableMap(parameterizedType, map); Type rawType = parameterizedType.getRawType(); if (rawType instanceof Class) buildTypeVariableMap(((Class<?>) rawType).getGenericInterfaces(), map); } else if (type instanceof Class) { buildTypeVariableMap(((Class<?>) type).getGenericInterfaces(), map); } } }
From source file:com.project.framework.util.ReflectionUtils.java
/** * ??, Class??.// ww w .j a v a 2 s.c o m * , null Class Array. * * @param clazz * @param interfaze * @param index * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static Class getInterfaceGenricType(final Class clazz, final Class interfaze, final int index) { if (!interfaze.isInterface()) throw new IllegalArgumentException("interfaze must be a interface."); if (!interfaze.isAssignableFrom(clazz)) throw new IllegalArgumentException("clazz must be implemnet form interfaze"); Type[] genTypes = clazz.getGenericInterfaces(); for (Type genType : genTypes) { if (genType instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) genType; if (interfaze.equals(pt.getRawType())) { Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); if (index >= params.length || index < 0) { logger.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: " + params.length); return Object.class; } if (!(params[index] instanceof Class)) { logger.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter"); return Object.class; } return (Class) params[index]; } } } return null; }
From source file:com.sf.ddao.orm.RSMapperFactoryRegistry.java
public static RowMapperFactory getRowMapperFactory(Type itemType, int startIdx) { // see if return type is simple so that we should map just startIdx column RowMapperFactory scalarMapperFactory = getScalarMapper(itemType, startIdx, false); if (scalarMapperFactory != null) { return scalarMapperFactory; }/*from w w w . ja v a 2 s. c om*/ if (itemType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) itemType; final Type rawType = parameterizedType.getRawType(); if (rawType instanceof Class && Map.class.isAssignableFrom((Class<?>) rawType)) { return new RowMapperFactory() { public RowMapper get() { return new MapRowMapper(); } }; } } if (itemType instanceof Class) { final Class itemClass = (Class) itemType; if (RowMapper.class.isAssignableFrom(itemClass)) { //noinspection unchecked return new SelfRowMapperFactory(itemClass); } if (Map.class.isAssignableFrom(itemClass)) { return new RowMapperFactory() { public RowMapper get() { return new MapRowMapper(); } }; } return new BeanRowMapperFactory(itemClass); } throw new DaoException("No mapping defined for type " + itemType); }
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)); }/*www.j a v a 2 s . 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:edu.brown.utils.ClassUtil.java
private static void getGenericTypesImpl(ParameterizedType ptype, List<Class<?>> classes) { // list the actual type arguments for (Type t : ptype.getActualTypeArguments()) { if (t instanceof Class<?>) { // System.err.println("C: " + t); classes.add((Class<?>) t); } else if (t instanceof ParameterizedType) { ParameterizedType next = (ParameterizedType) t; // System.err.println("PT: " + next); classes.add((Class<?>) next.getRawType()); getGenericTypesImpl(next, classes); }/*from w w w .j a va2 s.c o m*/ } // FOR return; }
From source file:io.werval.runtime.util.TypeResolver.java
/** * Populates the {@code map} with with variable/argument pairs for the given {@code types}. *//* w w w . ja v a 2 s . co m*/ static void buildTypeVariableMap(final Type[] types, final Map<TypeVariable<?>, Type> map) { for (Type type : types) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; buildTypeVariableMap(parameterizedType, map); Type rawType = parameterizedType.getRawType(); if (rawType instanceof Class) { buildTypeVariableMap(((Class<?>) rawType).getGenericInterfaces(), map); } } else if (type instanceof Class) { buildTypeVariableMap(((Class<?>) type).getGenericInterfaces(), map); } } }
From source file:com.yx.baseframe.util.ReflectionUtils.java
/** * Get the actual type arguments a child class has used to extend a generic * base class. (Taken from http://www.artima.com/weblogs/viewpost.jsp?thread=208860. Thanks * mathieu.grenonville for finding this solution!) * /*from ww w. ja v a2 s . c o m*/ * @param baseClass * the base class * @param childClass * the child class * @return a list of the raw classes for the actual type arguments. */ public static <T> List<Class<?>> getTypeArguments(Class<T> baseClass, Class<? extends T> childClass) { Map<Type, Type> resolvedTypes = new HashMap<Type, Type>(); Type type = childClass; // start walking up the inheritance hierarchy until we hit baseClass while (!getClass(type).equals(baseClass)) { if (type instanceof Class) { // there is no useful information for us in raw types, so just // keep going. type = ((Class) type).getGenericSuperclass(); } else { ParameterizedType parameterizedType = (ParameterizedType) type; Class<?> rawType = (Class) parameterizedType.getRawType(); Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); TypeVariable<?>[] typeParameters = rawType.getTypeParameters(); for (int i = 0; i < actualTypeArguments.length; i++) { resolvedTypes.put(typeParameters[i], actualTypeArguments[i]); } if (!rawType.equals(baseClass)) { type = rawType.getGenericSuperclass(); } } } // finally, for each actual type argument provided to baseClass, // determine (if possible) // the raw class for that type argument. Type[] actualTypeArguments; if (type instanceof Class) { actualTypeArguments = ((Class) type).getTypeParameters(); } else { actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments(); } List<Class<?>> typeArgumentsAsClasses = new ArrayList<Class<?>>(); // resolve types by chasing down type variables. for (Type baseType : actualTypeArguments) { while (resolvedTypes.containsKey(baseType)) { baseType = resolvedTypes.get(baseType); } typeArgumentsAsClasses.add(getClass(baseType)); } return typeArgumentsAsClasses; }