本文整理汇总了Java中java.lang.reflect.ParameterizedType.getActualTypeArguments方法的典型用法代码示例。如果您正苦于以下问题:Java ParameterizedType.getActualTypeArguments方法的具体用法?Java ParameterizedType.getActualTypeArguments怎么用?Java ParameterizedType.getActualTypeArguments使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.reflect.ParameterizedType
的用法示例。
在下文中一共展示了ParameterizedType.getActualTypeArguments方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: canonicalize
import java.lang.reflect.ParameterizedType; //导入方法依赖的package包/类
public static Type canonicalize(Type type) {
if (type instanceof Class) {
GenericArrayTypeImpl genericArrayTypeImpl;
Class<?> c = (Class) type;
if (c.isArray()) {
genericArrayTypeImpl = new GenericArrayTypeImpl(C$Gson$Types.canonicalize(c
.getComponentType()));
} else {
Object obj = c;
}
return genericArrayTypeImpl;
} else if (type instanceof ParameterizedType) {
ParameterizedType p = (ParameterizedType) type;
return new ParameterizedTypeImpl(p.getOwnerType(), p.getRawType(), p
.getActualTypeArguments());
} else if (type instanceof GenericArrayType) {
return new GenericArrayTypeImpl(((GenericArrayType) type).getGenericComponentType());
} else {
if (!(type instanceof WildcardType)) {
return type;
}
WildcardType w = (WildcardType) type;
return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds());
}
}
示例2: getGenericType
import java.lang.reflect.ParameterizedType; //导入方法依赖的package包/类
public static Class<?> getGenericType(Field f) {
try {
Type mapMainType = f.getGenericType();
if (mapMainType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType)mapMainType;
// 获取泛型类型的泛型参数
Type[] types = parameterizedType.getActualTypeArguments();
return (Class<?>) types[0];
} else {
throw new RuntimeException(String.format("在%s字段找不到泛型信息!!", f.getName()));
}
} catch (SecurityException e) {
e.printStackTrace();
return null;
}
}
示例3: resultList
import java.lang.reflect.ParameterizedType; //导入方法依赖的package包/类
/**
* 根据目标方法的类型,将请求中携带的参数封装成一个集合
* @param type httpBean中的方法参数集合的泛型类型
* @return
* @throws Exception
*/
private List<?> resultList(Type type) throws Exception {
List<Object> objs = new ArrayList<>();
ParameterizedType paramType = (ParameterizedType)type;
Class<?> generic = (Class<?>) paramType.getActualTypeArguments()[0];
Field[] fields = generic.getDeclaredFields();
int length = 0;
for (Field field : fields) {
String[] strings = paramMap.get(field.getName());
if (strings == null) continue;
// 判断有几个对象
length = length < strings.length ? strings.length : length;
}
for (int i = 0; i < length; i++) {
objs.add(resultSingle(generic, i));
}
return objs;
}
示例4: getGenericClass
import java.lang.reflect.ParameterizedType; //导入方法依赖的package包/类
public static Class getGenericClass(ParameterizedType parameterizedType, int i) {
Object genericClass = parameterizedType.getActualTypeArguments()[i];
if (genericClass instanceof ParameterizedType) {
// 处理多级泛型
System.out.println("111111");
return (Class) ((ParameterizedType) genericClass).getRawType();
} else if (genericClass instanceof GenericArrayType) {
// 处理数组泛型
return (Class) ((GenericArrayType) genericClass).getGenericComponentType();
} else if (genericClass instanceof TypeVariable) {
// 处理泛型擦拭对象
System.out.println("33333333");
return (Class) getClass(((TypeVariable) genericClass).getBounds()[0], 0);
} else {
System.out.println("444444");
return (Class) genericClass;
}
}
示例5: canonicalize
import java.lang.reflect.ParameterizedType; //导入方法依赖的package包/类
public static Type canonicalize(Type type) {
if (type instanceof Class) {
Class<?> c = (Class) type;
return c.isArray() ? new GenericArrayTypeImpl(C$Gson$Types.canonicalize(c.getComponentType())) : c;
} else if (type instanceof ParameterizedType) {
ParameterizedType p = (ParameterizedType) type;
return new ParameterizedTypeImpl(p.getOwnerType(), p.getRawType(), p.getActualTypeArguments());
} else if (type instanceof GenericArrayType) {
return new GenericArrayTypeImpl(((GenericArrayType) type).getGenericComponentType());
} else {
if (!(type instanceof WildcardType)) {
return type;
}
WildcardType w = (WildcardType) type;
return new WildcardTypeImpl(w.getUpperBounds(), w.getLowerBounds());
}
}
示例6: hasUnresolvableType
import java.lang.reflect.ParameterizedType; //导入方法依赖的package包/类
static boolean hasUnresolvableType(Type type) {
if (type instanceof Class<?>) {
return false;
}
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
for (Type typeArgument : parameterizedType.getActualTypeArguments()) {
if (hasUnresolvableType(typeArgument)) {
return true;
}
}
return false;
}
if (type instanceof GenericArrayType) {
return hasUnresolvableType(((GenericArrayType) type).getGenericComponentType());
}
if (type instanceof TypeVariable) {
return true;
}
if (type instanceof WildcardType) {
return true;
}
String className = type == null ? "null" : type.getClass().getName();
throw new IllegalArgumentException("Expected a Class, ParameterizedType, or "
+ "GenericArrayType, but <" + type + "> is of type " + className);
}
示例7: getCustomType
import java.lang.reflect.ParameterizedType; //导入方法依赖的package包/类
private static Object getCustomType(Parameter parameter, Signature signature) throws Exception {
Class<?> argType = parameter.getType();
if (argType == Signature.class) {
return signature;
} else if (argType == Request.class) {
return signature.request();
} else if (argType == Response.class) {
return signature.response();
} else if (argType == Session.class || argType == HttpSession.class) {
return signature.request().session();
} else if (argType == FileItem.class) {
return new ArrayList<>(signature.request().fileItems().values()).get(0);
} else if (argType == ModelAndView.class) {
return new ModelAndView();
} else if (argType == Map.class) {
return signature.request().parameters();
} else if (argType == Optional.class) {
ParameterizedType firstParam = (ParameterizedType) parameter.getParameterizedType();
Type paramsOfFirstGeneric = firstParam.getActualTypeArguments()[0];
Class<?> modelType = ReflectKit.form(paramsOfFirstGeneric.getTypeName());
return Optional.ofNullable(parseModel(modelType, signature.request(), null));
} else {
return parseModel(argType, signature.request(), null);
}
}
示例8: createDialog
import java.lang.reflect.ParameterizedType; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private D createDialog(Context context, Theme theme) throws Throwable {
ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
Class<D> dialogClass = (Class<D>) type.getActualTypeArguments()[0];
String clzName = ReflectHelper.importClass(dialogClass.getName());
return (D) ReflectHelper.newInstance(clzName, context, theme);
}
示例9: TypeReference
import java.lang.reflect.ParameterizedType; //导入方法依赖的package包/类
/**
* @since 1.2.9
* @param actualTypeArguments
*/
protected TypeReference(Type... actualTypeArguments){
Class<?> thisClass = this.getClass();
Type superClass = thisClass.getGenericSuperclass();
ParameterizedType argType = (ParameterizedType) ((ParameterizedType) superClass).getActualTypeArguments()[0];
Type rawType = argType.getRawType();
Type[] argTypes = argType.getActualTypeArguments();
int actualIndex = 0;
for (int i = 0; i < argTypes.length; ++i) {
if (argTypes[i] instanceof TypeVariable) {
argTypes[i] = actualTypeArguments[actualIndex++];
if (actualIndex >= actualTypeArguments.length) {
break;
}
}
}
Type key = new ParameterizedTypeImpl(argTypes, thisClass, rawType);
Type cachedType = classTypeCache.get(key);
if (cachedType == null) {
classTypeCache.putIfAbsent(key, key);
cachedType = classTypeCache.get(key);
}
type = cachedType;
}
示例10: gen
import java.lang.reflect.ParameterizedType; //导入方法依赖的package包/类
/**
* 创建解析器
*
* @param cacheKey
* @param type
* @param annotations
* @return
*/
private static Decoder gen(String cacheKey, Type type, Annotation[] annotations) {
Decoder decoder = getDecoder(cacheKey);
if (decoder != null) {
return decoder;
}
type = ReflectKit.chooseImpl(type);
Type[] typeArgs = new Type[0];
Class clazz;
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
clazz = (Class) pType.getRawType();
typeArgs = pType.getActualTypeArguments();
} else {
clazz = (Class) type;
}
decoder = NATIVE_DECODERS.get(clazz);//基本数据类型
if (decoder != null) {
return decoder;
}
decoder = registerDecoder.get(clazz); //自定义的解析器
if (decoder == null) {
decoder = ReflectionDecoderFactory.create(clazz, annotations, typeArgs); //注解解析器
}
cacheDecoder(cacheKey, decoder);
return decoder;
}
示例11: getMapValueOpenType
import java.lang.reflect.ParameterizedType; //导入方法依赖的package包/类
public static OpenType<?> getMapValueOpenType(Class<?> mapType) throws OpenDataException {
if(Map.class.isAssignableFrom(mapType)){
ParameterizedType parameterizedType = (ParameterizedType) mapType.getGenericSuperclass();
Class<?> valueType = (Class<?>) parameterizedType.getActualTypeArguments()[1];
return classMapToOpenType(valueType);
}
return null;
}
示例12: getHandler
import java.lang.reflect.ParameterizedType; //导入方法依赖的package包/类
Handler getHandler(Type type) {
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Class rawClass = (Class<?>) parameterizedType.getRawType();
if (!Collection.class.isAssignableFrom(rawClass)) {
throw new RuntimeException("cannot handle non-collection parameterized type " + type);
}
Type actualType = parameterizedType.getActualTypeArguments()[0];
if (!(actualType instanceof Class)) {
throw new RuntimeException("cannot handle nested parameterized type " + type);
}
return getHandler(actualType);
}
if (type instanceof Class) {
Class<?> classType = (Class) type;
if (Collection.class.isAssignableFrom(classType)) {
// could handle by just having a default of treating
// contents as String but consciously decided this
// should be an error
throw new RuntimeException(
"cannot handle non-parameterized collection " + type + ". " +
"use a generic Collection to specify a desired element type");
}
if (classType.isEnum()) {
return new EnumHandler(classType);
}
return handlers.get(classType);
}
throw new RuntimeException("cannot handle unknown field type " + type);
}
示例13: build
import java.lang.reflect.ParameterizedType; //导入方法依赖的package包/类
@Override
public TypeDefinition build(Type type, Class<?> clazz, Map<Class<?>, TypeDefinition> typeCache) {
if (!(type instanceof ParameterizedType)) {
throw new IllegalArgumentException(MessageFormat.format("[Jaket] Unexpected type {0}.",
new Object[]{type}));
}
ParameterizedType parameterizedType = (ParameterizedType) type;
Type[] actualTypeArgs = parameterizedType.getActualTypeArguments();
if (actualTypeArgs == null || actualTypeArgs.length != 2) {
throw new IllegalArgumentException(MessageFormat.format(
"[Jaket] Map type [{0}] with unexpected amount of arguments [{1}]." + actualTypeArgs, new Object[] {
type, actualTypeArgs }));
}
for (Type actualType : actualTypeArgs) {
if (actualType instanceof ParameterizedType) {
// Nested collection or map.
Class<?> rawType = (Class<?>) ((ParameterizedType) actualType).getRawType();
JaketTypeBuilder.build(actualType, rawType, typeCache);
} else if (actualType instanceof Class<?>) {
Class<?> actualClass = (Class<?>) actualType;
if (actualClass.isArray() || actualClass.isEnum()) {
JaketTypeBuilder.build(null, actualClass, typeCache);
} else {
DefaultTypeBuilder.build(actualClass, typeCache);
}
}
}
TypeDefinition td = new TypeDefinition(type.toString());
return td;
}
示例14: checkOneParameterType
import java.lang.reflect.ParameterizedType; //导入方法依赖的package包/类
private static void checkOneParameterType(ParameterizedType toCheck, Class<?> rawType,
Class<?>... bounds) {
System.out.println(((Class<?>) toCheck.getRawType()).getName()
.equals(rawType.getName()));
Type[] parameters = toCheck.getActualTypeArguments();
System.out.println(parameters.length);
TypeVariable<?> parameter = (TypeVariable<?>) parameters[0];
System.out.println(parameter.getName());
Type[] actualBounds = parameter.getBounds();
for (int i = 0; i < bounds.length; i++) {
System.out.println(((Class<?>) actualBounds[i]).getName().equals(bounds[i].getName()));
}
}
示例15: resolveParameterizedType
import java.lang.reflect.ParameterizedType; //导入方法依赖的package包/类
private ParameterizedType resolveParameterizedType(ParameterizedType type) {
Type owner = type.getOwnerType();
Type resolvedOwner = (owner == null) ? null : resolveType(owner);
Type resolvedRawType = resolveType(type.getRawType());
Type[] args = type.getActualTypeArguments();
Type[] resolvedArgs = resolveTypes(args);
return Types.newParameterizedTypeWithOwner(
resolvedOwner, (Class<?>) resolvedRawType, resolvedArgs);
}