List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments
Type[] getActualTypeArguments();
From source file:com.sf.ddao.crud.param.CRUDParameterService.java
public static Class<?> getCRUDDaoBean(Context ctx, int idx) { final MethodCallCtx callCtx = CtxHelper.get(ctx, MethodCallCtx.class); final Method method = callCtx.getMethod(); if (idx != USE_GENERICS) { Type beanClass;/*from w w w . ja va 2 s. c om*/ if (idx == DefaultParameter.RETURN_ARG_IDX) { beanClass = method.getGenericReturnType(); } else { beanClass = method.getGenericParameterTypes()[idx]; } if (beanClass instanceof Class) { return (Class) beanClass; } } Class<?> iFace = callCtx.getSubjClass(); for (Type type : iFace.getGenericInterfaces()) { final ParameterizedType parameterizedType = (ParameterizedType) type; if (parameterizedType.getRawType().equals(method.getDeclaringClass())) { final Type[] typeArguments = parameterizedType.getActualTypeArguments(); return (Class<?>) typeArguments[GENERICS_ARG_NUM]; } } throw new RuntimeException(iFace + " expected to extend " + CRUDDao.class); }
From source file:com.jaspersoft.jasperserver.war.helper.GenericParametersHelper.java
public static Class<?> getGenericTypeArgument(Class<?> classToParse, Class<?> genericClassToFind, Integer argumentIndex) {/*w ww . ja v a 2 s . com*/ Class<?> result = null; ParameterizedType parameterizedType = null; Class<?> currentClass = classToParse; Map<String, Class<?>> currentParameterValues = new HashMap<String, Class<?>>(); Type[] previousTypeArguments = null; while (parameterizedType == null) { final TypeVariable<? extends Class<?>>[] typeParameters = currentClass.getTypeParameters(); currentParameterValues = getCurrentParameterValues(typeParameters, previousTypeArguments, currentParameterValues); parameterizedType = findParametrizedType(currentClass, genericClassToFind, currentParameterValues); if (parameterizedType == null) { // current class doesn't extend/implement searched class directly. Should parse superclass final Type genericSuperclassType = currentClass.getGenericSuperclass(); if (genericSuperclassType instanceof Class<?>) { log.debug(classToParse.getName() + " is raw subclass of " + genericClassToFind.getName()); return null; } final ParameterizedType genericSuperclass = (ParameterizedType) genericSuperclassType; previousTypeArguments = genericSuperclass.getActualTypeArguments(); currentClass = (Class<?>) genericSuperclass.getRawType(); } } final Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); if (actualTypeArguments[argumentIndex] instanceof Class<?>) { result = (Class<?>) actualTypeArguments[argumentIndex]; } else if (actualTypeArguments[argumentIndex] instanceof TypeVariable) { result = currentParameterValues.get(((TypeVariable<?>) actualTypeArguments[argumentIndex]).getName()); } if (result == null) { log.debug("Class " + classToParse.getName() + " has unsupported inheritance structure"); } return result; }
From source file:com.amazonaws.hal.client.ConversionUtil.java
static Type getCollectionType(Type type, int index, Class defaultClass) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; return parameterizedType.getActualTypeArguments()[index]; } else {//w w w.j ava 2 s .c o m return defaultClass; } }
From source file:com.jeroensteenbeeke.hyperion.events.DefaultEventDispatcher.java
@SuppressWarnings("unchecked") static Class<? extends Event<?>> getEventClass(Class<?> handlerClass) { for (Class<?> i : handlerClass.getInterfaces()) { if (EventHandler.class.equals(i)) { for (Type t : handlerClass.getGenericInterfaces()) { if (t instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) t; if (EventHandler.class.equals(pt.getRawType())) { return (Class<? extends Event<?>>) pt.getActualTypeArguments()[0]; }/*from w w w . j av a 2s . c o m*/ } } } else if (EventHandler.class.isAssignableFrom(i)) { return getEventClass((Class<? extends EventHandler<?>>) i); } } if (EventHandler.class.isAssignableFrom(handlerClass.getSuperclass())) { return getEventClass((Class<?>) handlerClass.getSuperclass()); } return null; }
From source file:com.netflix.paas.config.base.ConfigurationProxyUtils.java
static <T> Map<String, Supplier<?>> getMethodSuppliers(Class<T> configClass, DynamicPropertyFactory propertyFactory, AbstractConfiguration configuration) { final Map<String, Supplier<?>> properties = Maps.newHashMap(); for (Method method : configClass.getMethods()) { Configuration c = method.getAnnotation(Configuration.class); if (c == null) continue; String defaultValue = null; DefaultValue dv = method.getAnnotation(DefaultValue.class); if (dv != null) defaultValue = dv.value();/*from ww w. j a va 2 s.c o m*/ String name = getPropertyName(method, c); if (method.getReturnType().isAssignableFrom(Supplier.class)) { Type returnType = method.getGenericReturnType(); if (returnType instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) returnType; Class<?> actualType = (Class<?>) type.getActualTypeArguments()[0]; properties.put(method.getName(), method.getAnnotation(Dynamic.class) != null ? Suppliers.ofInstance( getDynamicSupplier(actualType, name, defaultValue, propertyFactory)) : Suppliers.ofInstance( getStaticSupplier(actualType, name, defaultValue, configuration))); } else { throw new RuntimeException("We'll get to this later"); } } else { properties.put(method.getName(), method.getAnnotation(Dynamic.class) != null ? getDynamicSupplier(method.getReturnType(), name, defaultValue, propertyFactory) : getStaticSupplier(method.getReturnType(), name, defaultValue, configuration)); } } return properties; }
From source file:com.liferay.apio.architect.internal.annotation.representor.processor.TypeProcessor.java
private static void _addOptionalType(Builder builder, Method method) { java.lang.reflect.Type type = _getGenericType(method); if (type instanceof ParameterizedType) { ParameterizedType innerParametrizedType = (ParameterizedType) type; if (innerParametrizedType.getRawType() == List.class) { _addListType(builder, method, (Class<?>) innerParametrizedType.getActualTypeArguments()[0]); }/*from w ww. j av a 2 s . co m*/ } else { Class<?> returnTypeClass = (Class<?>) type; _addFields(builder, method, returnTypeClass); } }
From source file:org.jcurl.core.ui.TaskExecutor.java
/** Find the presence of a generic type parameter. */ @SuppressWarnings("unchecked") static Class<Executor> findMessageTypeParam(final Class<? extends Task> clz) { if (Object.class.equals(clz.getGenericSuperclass())) { final ParameterizedType pt = (ParameterizedType) clz.getGenericInterfaces()[0]; return (Class<Executor>) pt.getActualTypeArguments()[0]; }//from ww w . j av a2 s.co m final ParameterizedType pt = (ParameterizedType) clz.getGenericSuperclass(); return (Class<Executor>) pt.getActualTypeArguments()[0]; }
From source file:jp.terasoluna.fw.util.GenericPropertyUtil.java
/** * ??????? <code>Generics</code>????? * @param genericClass <code>Generics</code>? ??? * @param clazz ???//w w w.j ava2 s . c om * @param type ????? <code>Type</code> * @param index ?? * @return <code>Generics</code>??? ???????<code>Object</code>??? * @throws IllegalArgumentException <code>genericClass</code>? <code>null</code>?? <code>clazz</code>? * <code>null</code>?? <code>index</code>?<code>0</code>???????? ???? * @throws IllegalStateException ?<code>WildCardType</code>??? */ @SuppressWarnings("unchecked") protected static Class<?> resolveType(Class<?> genericClass, @SuppressWarnings("rawtypes") Class clazz, Type type, int index) throws IllegalArgumentException, IllegalStateException { if (genericClass == null) { throw new IllegalArgumentException("Argument 'genericsClass' (" + Class.class.getName() + ") is null"); } if (clazz == null || !genericClass.isAssignableFrom(clazz)) { throw new IllegalStateException(genericClass + " is not assignable from " + clazz); } List<ParameterizedType> ancestorTypeList = null; try { ancestorTypeList = GenericsUtil.getAncestorTypeList(genericClass, clazz); } catch (IllegalStateException e) { if (log.isTraceEnabled()) { log.trace(e.getMessage()); } } if (ancestorTypeList == null) { ancestorTypeList = new ArrayList<ParameterizedType>(); } if (type instanceof ParameterizedType) { ancestorTypeList.add(0, (ParameterizedType) type); } if (ancestorTypeList.size() <= 0) { throw new IllegalStateException("No parameterizedType was detected."); } ParameterizedType parameterizedType = ancestorTypeList.get(ancestorTypeList.size() - 1); Type[] actualTypes = parameterizedType.getActualTypeArguments(); // ?????? if (index < 0 || index >= actualTypes.length) { throw new IllegalArgumentException("Argument 'index'(" + Integer.toString(index) + ") is out of bounds of" + " generics parameters"); } Class<?> resolved = Object.class; try { resolved = GenericsUtil.resolveTypeVariable(actualTypes[index], ancestorTypeList); } catch (IllegalStateException e) { if (log.isTraceEnabled()) { log.trace(e.getMessage()); } } return resolved; }
From source file:com.iterranux.droolsjbpmCore.internal.AbstractGenericFactory.java
/** * Get the actual type arguments a child class has used to extend a generic base class. * * @param baseClass the base class//from w ww .j a v a2s . c om * @param childClass the child class * @return a list of the raw classes for the actual type arguments. */ @SuppressWarnings("all") 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; }
From source file:TypeUtils.java
private static boolean isAssignable(ParameterizedType lhsType, ParameterizedType rhsType) { if (lhsType.equals(rhsType)) { return true; }/*from w ww . ja v a2 s . co m*/ Type[] lhsTypeArguments = lhsType.getActualTypeArguments(); Type[] rhsTypeArguments = rhsType.getActualTypeArguments(); if (lhsTypeArguments.length != rhsTypeArguments.length) { return false; } for (int size = lhsTypeArguments.length, i = 0; i < size; ++i) { Type lhsArg = lhsTypeArguments[i]; Type rhsArg = rhsTypeArguments[i]; if (!lhsArg.equals(rhsArg) && !(lhsArg instanceof WildcardType && isAssignable((WildcardType) lhsArg, rhsArg))) { return false; } } return true; }