List of usage examples for java.lang.reflect GenericArrayType getGenericComponentType
Type getGenericComponentType();
From source file:org.evosuite.utils.generic.GenericUtils.java
public static Type replaceTypeVariable(Type targetType, TypeVariable<?> variable, Type variableType) { if (targetType instanceof Class<?>) return targetType; else if (targetType instanceof GenericArrayType) { GenericArrayType gType = (GenericArrayType) targetType; Type componentType = replaceTypeVariable(gType.getGenericComponentType(), variable, variableType); return GenericArrayTypeImpl.createArrayType(componentType); } else if (targetType instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) targetType; Type ownerType = null;//ww w . j av a 2 s . c o m if (pType.getOwnerType() != null) { ownerType = replaceTypeVariable(pType.getOwnerType(), variable, variableType); } Type[] originalParameterTypes = pType.getActualTypeArguments(); Type[] parameterTypes = new Type[originalParameterTypes.length]; for (int i = 0; i < originalParameterTypes.length; i++) { parameterTypes[i] = replaceTypeVariable(originalParameterTypes[i], variable, variableType); } /* if (variableType instanceof ParameterizedType) { ParameterizedType parameterizedVars = (ParameterizedType) variableType; Map<TypeVariable<?>, Type> subTypes = TypeUtils.getTypeArguments(parameterizedVars); for (Entry<TypeVariable<?>, Type> subTypeEntry : subTypes.entrySet()) { if (pType.getOwnerType() != null) { ownerType = replaceTypeVariable(pType.getOwnerType(), subTypeEntry.getKey(), subTypeEntry.getValue()); } for (int i = 0; i < originalParameterTypes.length; i++) { parameterTypes[i] = replaceTypeVariable(originalParameterTypes[i], subTypeEntry.getKey(), subTypeEntry.getValue()); } } } */ return new ParameterizedTypeImpl((Class<?>) pType.getRawType(), parameterTypes, ownerType); } else if (targetType instanceof WildcardType) { WildcardType wType = (WildcardType) targetType; Type[] originalUpperBounds = wType.getUpperBounds(); Type[] originalLowerBounds = wType.getLowerBounds(); Type[] upperBounds = new Type[originalUpperBounds.length]; Type[] lowerBounds = new Type[originalLowerBounds.length]; for (int i = 0; i < originalUpperBounds.length; i++) { upperBounds[i] = replaceTypeVariable(originalUpperBounds[i], variable, variableType); } for (int i = 0; i < originalLowerBounds.length; i++) { lowerBounds[i] = replaceTypeVariable(originalLowerBounds[i], variable, variableType); } return new WildcardTypeImpl(upperBounds, lowerBounds); } else if (targetType instanceof TypeVariable<?>) { if (targetType.equals(variable)) { //logger.debug("Do equal: " + variable + "/" + targetType); return variableType; } else { //logger.debug("Do not equal: " + variable + "/" + targetType); //logger.debug("Do not equal: " + variable.getGenericDeclaration() + "/" // + ((TypeVariable<?>) targetType).getGenericDeclaration()); return targetType; } } else { //logger.debug("Unknown type of class " + targetType.getClass() + ": " // + targetType); return targetType; } }
From source file:org.evosuite.utils.generic.GenericUtils.java
public Map<TypeVariable<?>, Type> getMatchingTypeParameters(GenericArrayType p1, GenericArrayType p2) { if (p1.getGenericComponentType() instanceof ParameterizedType && p2.getGenericComponentType() instanceof ParameterizedType) { return getMatchingTypeParameters((ParameterizedType) p1.getGenericComponentType(), (ParameterizedType) p2.getGenericComponentType()); } else {//w ww . j a v a2s.co m Map<TypeVariable<?>, Type> map = new HashMap<TypeVariable<?>, Type>(); return map; } }
From source file:org.itest.impl.ITestRandomObjectGeneratorImpl.java
public <T> T generateRandom(Type type, Map<String, Type> itestGenericMap, ITestContext iTestContext) { ITestParamState iTestState = iTestContext.getCurrentParam(); Class<?> clazz = ITestTypeUtil.getRawClass(type); Class<?> requestedClass = getClassFromParam(iTestState); if (null != iTestState && null != iTestState.getAttribute(ITestConstants.ATTRIBUTE_DEFINITION)) { ITestParamState iTestStateLoaded = iTestConfig.getITestParamLoader() .loadITestParam(null == requestedClass ? clazz : requestedClass, iTestState.getAttribute(ITestConstants.ATTRIBUTE_DEFINITION)) .getElement(ITestConstants.THIS); iTestState = iTestConfig.getITestParamsMerger().merge( new ITestParamAssignmentImpl("", iTestStateLoaded), new ITestParamAssignmentImpl("", iTestState)); iTestContext.replaceCurrentState(iTestState); }//from w ww. ja v a 2 s . c o m Object res; if (ITestParamState.class == clazz) { res = processITestState(iTestContext); } else if (null != iTestState && null == iTestState.getSizeParam() && null == iTestState.getValue()) { res = null; } else if (null != iTestState && null != iTestState.getAttribute(ITestConstants.REFERENCE_ATTRIBUTE)) { res = iTestContext.findGeneratedObject(iTestState.getAttribute(ITestConstants.REFERENCE_ATTRIBUTE)); } else if (PROXY_CLASS == requestedClass) { res = newDynamicProxy(type, itestGenericMap, iTestContext); } else if (Collection.class.isAssignableFrom(clazz)) { res = fillCollection(null, type, itestGenericMap, iTestContext); } else if (Map.class.isAssignableFrom(clazz)) { res = fillMap(null, type, itestGenericMap, iTestContext); } else if (null != requestedClass) { res = generateRandom(requestedClass, itestGenericMap, iTestContext); } else if (type instanceof GenericArrayType) { GenericArrayType arrayType = (GenericArrayType) type; Class<?> componentClass; int size = random.nextInt(RANDOM_MAX - RANDOM_MIN) + RANDOM_MIN; if (null != iTestState && iTestState.getSizeParam() != null) { size = iTestState.getSizeParam(); } Map<String, Type> map = new HashMap<String, Type>(itestGenericMap); if (arrayType.getGenericComponentType() instanceof ParameterizedType) { componentClass = (Class<?>) ((ParameterizedType) arrayType.getGenericComponentType()).getRawType(); } else { componentClass = (Class<?>) arrayType.getGenericComponentType(); } Object array = Array.newInstance(componentClass, size); for (int i = 0; i < size; i++) { ITestParamState elementITestState = iTestState == null ? null : iTestState.getElement(String.valueOf(i)); Array.set(array, i, generateRandom(arrayType.getGenericComponentType(), map, iTestContext)); } res = array; } else if (null != iTestState && null == iTestState.getNames()) { res = iTestConfig.getITestValueConverter().convert(clazz, iTestState.getValue()); } else if (clazz.isInterface()) { res = newDynamicProxy(type, itestGenericMap, iTestContext); } else if (type instanceof Class) { res = generateRandom(clazz, itestGenericMap, iTestContext); } else if (Enum.class == clazz) { Type enumType = ITestTypeUtil.getTypeProxy(((ParameterizedType) type).getActualTypeArguments()[0], itestGenericMap); res = generateRandom(enumType, itestGenericMap, iTestContext); } else if (Class.class == clazz) { // probably proxy will be required here res = ((ParameterizedType) type).getActualTypeArguments()[0]; } else { Map<String, Type> map = ITestTypeUtil.getTypeMap(type, itestGenericMap); res = newInstance(clazz, iTestContext, ITestTypeUtil.getTypeActualArguments(type)); fillFields(clazz, res, iTestState, map, iTestContext); } return (T) res; }
From source file:org.jiemamy.utils.reflect.GenericUtil.java
/** * {@code type}????/*from www . j a va2s .com*/ * <ul> * <li>{@code type}?{@code Class}?????????</li> * <li>{@code type}??????????</li> * <li>{@code type}????(??)??</li> * <li>{@code type}??????????</li> * <li>{@code type}??????????????</li> * <li>??????{@code null}?</li> * </ul> * * @param type * @param map ????????{@link Map} * @return {@code type}?? * @throws IllegalArgumentException ?{@code null}??? */ public static Class<?> getActualClass(Type type, Map<TypeVariable<?>, Type> map) { Validate.notNull(type); Validate.notNull(map); if (Class.class.isInstance(type)) { return Class.class.cast(type); } if (ParameterizedType.class.isInstance(type)) { return getActualClass(ParameterizedType.class.cast(type).getRawType(), map); } if (WildcardType.class.isInstance(type)) { return getActualClass(WildcardType.class.cast(type).getUpperBounds()[0], map); } if (TypeVariable.class.isInstance(type)) { return getActualClass(map.get(TypeVariable.class.cast(type)), map); } if (GenericArrayType.class.isInstance(type)) { GenericArrayType genericArrayType = GenericArrayType.class.cast(type); Class<?> componentClass = getActualClass(genericArrayType.getGenericComponentType(), map); return Array.newInstance(componentClass, 0).getClass(); } return null; }
From source file:org.jiemamy.utils.reflect.GenericUtil.java
/** * {@code type}???/*from w ww . j a va 2 s .c o m*/ * <ul> * <li>{@code type}?{@code Class}?????????</li> * <li>{@code type}??????????</li> * <li>{@code type}????(??)??</li> * <li>{@code type}??????????????</li> * <li>??????{@code null}?</li> * </ul> * * @param type * @return {@code type}? * @throws IllegalArgumentException ?{@code null}??? */ public static Class<?> getRawClass(Type type) { Validate.notNull(type); if (Class.class.isInstance(type)) { return Class.class.cast(type); } if (ParameterizedType.class.isInstance(type)) { ParameterizedType parameterizedType = ParameterizedType.class.cast(type); return getRawClass(parameterizedType.getRawType()); } if (WildcardType.class.isInstance(type)) { WildcardType wildcardType = WildcardType.class.cast(type); Type[] types = wildcardType.getUpperBounds(); return getRawClass(types[0]); } if (GenericArrayType.class.isInstance(type)) { GenericArrayType genericArrayType = GenericArrayType.class.cast(type); Class<?> rawClass = getRawClass(genericArrayType.getGenericComponentType()); return Array.newInstance(rawClass, 0).getClass(); } return null; }
From source file:org.mousepilots.es.maven.model.generator.model.attribute.GenericMethodInfo.java
/** * Resolves the generic {@code type}/* w w w.j a v a 2 s. c om*/ * @param typeVariable2Value * @param type * @return */ private String getGenericTypeString(Type type) { if (type instanceof Class) { //(raw) class return ((Class) type).getCanonicalName(); } else if (type instanceof ParameterizedType) { // e.g. Map<K,V> final ParameterizedType parameterizedType = (ParameterizedType) type; return getGenericTypeString(parameterizedType.getRawType()) + "<" + StringUtils.join(Arrays.asList(parameterizedType.getActualTypeArguments()), (Type actualTypeArgument) -> getGenericTypeString(actualTypeArgument), ",") + ">"; } else if (type instanceof TypeVariable) { //e.g. T final TypeVariable<?> typeVariable = (TypeVariable) type; final Type typeVariableValue = typeVariable2Value.get(typeVariable); if (typeVariableValue == null) { return typeVariable.getName(); } else { return getGenericTypeString(typeVariableValue); } } else if (type instanceof GenericArrayType) { //e.g. T[] final GenericArrayType genericArrayType = (GenericArrayType) type; return getGenericTypeString(genericArrayType.getGenericComponentType()) + "[]"; } else { throw new IllegalArgumentException("unsupported type " + type); } }
From source file:org.romaframework.core.schema.SchemaHelper.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public static Class<?> resolveClassFromType(Type type, ParameterizedType params) { if (type instanceof Class<?>) return (Class<?>) type; if (type instanceof ParameterizedType) { return resolveClassFromType(((ParameterizedType) type).getRawType(), (ParameterizedType) type); }/*from w w w . ja v a 2s . c o m*/ if (type instanceof GenericArrayType) { GenericArrayType gat = (GenericArrayType) type; Class<?> arrItemp = resolveClassFromType(gat.getGenericComponentType(), null); return Array.newInstance(arrItemp, 0).getClass(); } if (type instanceof TypeVariable<?>) { TypeVariable<?> t = (TypeVariable<?>) type; if (params != null) { Class<?> cl = resolveClassFromType(params.getRawType(), null); if (cl != null) { TypeVariable<Class<?>>[] var = ((Class) cl).getTypeParameters(); int i = 0; for (; i < var.length; i++) { if (var[i].getName().equals(t.getName())) { return resolveClassFromType(params.getActualTypeArguments()[i], resolveParameterizedType(params.getOwnerType())); } } } } Type[] bounds = t.getBounds(); if (bounds.length == 1) return resolveClassFromType(bounds[0], params); } if (type instanceof WildcardType) { // TODO: } return null; }
From source file:org.soybeanMilk.core.bean.DefaultGenericConverter.java
/** * ?{@linkplain GenericArrayType}/*from www. j a va2 s . c o m*/ * @param array * @param type * @return * @throws ConvertException * @date 2012-5-14 */ protected Object convertArrayToGenericArrayType(Object array, GenericArrayType type) throws ConvertException { Object result = null; Type ct = type.getGenericComponentType(); result = convertArrayToArray(array, ct); return result; }
From source file:org.soybeanMilk.core.bean.DefaultGenericConverter.java
/** * ?{@linkplain GenericArrayType}/*from w ww. ja v a 2 s. c om*/ * @param pvm * @param type * @return * @throws ConvertException * @date 2012-5-14 */ protected Object convertPropertyValueMapToGenericArrayType(PropertyValueMap pvm, GenericArrayType type) throws ConvertException { Object result = null; Type ct = type.getGenericComponentType(); result = convertPropertyValueMapToList(pvm, List.class, ct); result = listToArray((List<?>) result, ct); return result; }
From source file:org.soybeanMilk.SbmUtils.java
/** * //from w w w .ja v a 2 s. c o m * @param type * @param variableTypesMap * @return * @date 2012-5-14 */ private static Type reifyInner(Type type, Map<TypeVariable<?>, Type> variableTypesMap) { Type result = null; if (type instanceof Class<?>) { result = type; } else if (type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; Type[] at = pt.getActualTypeArguments(); Type[] cat = new Type[at.length]; //pt?pt?? boolean reified = true; for (int i = 0; i < at.length; i++) { cat[i] = reifyInner(at[i], variableTypesMap); if (cat[i] != at[i]) reified = false; } if (reified) result = pt; else result = new CustomParameterizedType(pt.getRawType(), pt.getOwnerType(), cat); } else if (type instanceof GenericArrayType) { GenericArrayType gap = (GenericArrayType) type; Type ct = gap.getGenericComponentType(); Type cct = reifyInner(ct, variableTypesMap); if (cct == ct) result = gap; else result = new CustomGenericArrayType(cct); } else if (type instanceof TypeVariable<?>) { TypeVariable<?> tv = (TypeVariable<?>) type; if (variableTypesMap != null) result = variableTypesMap.get(tv); if (result == null) { Type[] bounds = tv.getBounds(); if (bounds == null || bounds.length == 0) result = Object.class; else result = bounds[0]; } result = reifyInner(result, variableTypesMap); } else if (type instanceof WildcardType) { WildcardType wt = (WildcardType) type; Type[] upperBounds = wt.getUpperBounds(); Type upperType = (upperBounds != null && upperBounds.length > 0 ? upperBounds[0] : null); if (upperType == null) upperType = Object.class; result = reifyInner(upperType, variableTypesMap); } else result = type; return result; }