本文整理了Java中java.lang.Class.getTypeParameters()
方法的一些代码示例,展示了Class.getTypeParameters()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.getTypeParameters()
方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:getTypeParameters
[英]Returns an array containing TypeVariable objects for type variables declared by the generic class represented by this Class. Returns an empty array if the class is not generic.
[中]返回一个数组,该数组包含由此类表示的泛型类声明的类型变量的TypeVariable对象。如果类不是泛型的,则返回空数组。
代码示例来源:origin: google/guava
private static ParameterizedType canonicalizeWildcardsInParameterizedType(
ParameterizedType type) {
Class<?> rawType = (Class<?>) type.getRawType();
TypeVariable<?>[] typeVars = rawType.getTypeParameters();
Type[] typeArgs = type.getActualTypeArguments();
for (int i = 0; i < typeArgs.length; i++) {
typeArgs[i] = canonicalizeTypeArg(typeVars[i], typeArgs[i]);
}
return Types.newParameterizedTypeWithOwner(type.getOwnerType(), rawType, typeArgs);
}
代码示例来源:origin: google/guava
ParameterizedTypeImpl(@Nullable Type ownerType, Class<?> rawType, Type[] typeArguments) {
checkNotNull(rawType);
checkArgument(typeArguments.length == rawType.getTypeParameters().length);
disallowPrimitiveType(typeArguments, "type parameter");
this.ownerType = ownerType;
this.rawType = rawType;
this.argumentsList = JavaVersion.CURRENT.usedInGenericType(typeArguments);
}
代码示例来源:origin: google/guava
@Override
void visitParameterizedType(ParameterizedType parameterizedType) {
Class<?> rawClass = (Class<?>) parameterizedType.getRawType();
TypeVariable<?>[] vars = rawClass.getTypeParameters();
Type[] typeArgs = parameterizedType.getActualTypeArguments();
checkState(vars.length == typeArgs.length);
for (int i = 0; i < vars.length; i++) {
map(new TypeVariableKey(vars[i]), typeArgs[i]);
}
visit(rawClass);
visit(parameterizedType.getOwnerType());
}
代码示例来源:origin: google/guava
/**
* If the class is parameterized, such as {@link java.util.ArrayList ArrayList}, this returns
* {@code ArrayList<E>}.
*/
@Override
Type getGenericReturnType() {
Class<?> declaringClass = getDeclaringClass();
TypeVariable<?>[] typeParams = declaringClass.getTypeParameters();
if (typeParams.length > 0) {
return Types.newParameterizedType(declaringClass, typeParams);
} else {
return declaringClass;
}
}
代码示例来源:origin: square/retrofit
private static Type resolveTypeVariable(
Type context, Class<?> contextRawType, TypeVariable<?> unknown) {
Class<?> declaredByRaw = declaringClassOf(unknown);
// We can't reduce this further.
if (declaredByRaw == null) return unknown;
Type declaredBy = getGenericSupertype(context, contextRawType, declaredByRaw);
if (declaredBy instanceof ParameterizedType) {
int index = indexOf(declaredByRaw.getTypeParameters(), unknown);
return ((ParameterizedType) declaredBy).getActualTypeArguments()[index];
}
return unknown;
}
代码示例来源:origin: google/guava
public void testGenericInterface() {
// test the 1st generic interface on the class
Type fType = Supplier.class.getTypeParameters()[0];
assertEquals(
Integer.class, TypeToken.of(IntegerStringFunction.class).resolveType(fType).getRawType());
// test the 2nd generic interface on the class
Type predicateParameterType = Predicate.class.getTypeParameters()[0];
assertEquals(
new TypeToken<List<String>>() {}.getType(),
TypeToken.of(IntegerStringFunction.class).resolveType(predicateParameterType).getType());
}
代码示例来源:origin: google/guava
public void testGetSupertype_fullyGenericType() {
ParameterizedType expectedType =
Types.newParameterizedType(
Map.class,
ListMap.class.getTypeParameters()[0],
Types.newParameterizedType(List.class, ListMap.class.getTypeParameters()[1]));
assertEquals(expectedType, TypeToken.of(ListMap.class).getSupertype(Map.class).getType());
}
代码示例来源:origin: google/guava
public void testFalseRecursiveType_mappingOnTheSameDeclarationNotUsed() {
Type returnType =
genericReturnType(WithFalseRecursiveType.class, "keyShouldNotResolveToStringList");
TypeToken<?> keyType =
TypeToken.of(returnType).resolveType(WithFalseRecursiveType.class.getTypeParameters()[0]);
assertEquals("java.util.List<V>", keyType.getType().toString());
}
代码示例来源:origin: google/guava
public void testFalseRecursiveType_referenceOfSubtypeDoesNotConfuseMe() {
Type returnType = genericReturnType(WithFalseRecursiveType.class, "evenSubtypeWorks");
TypeToken<?> keyType =
TypeToken.of(returnType).resolveType(WithFalseRecursiveType.class.getTypeParameters()[0]);
assertEquals("java.util.List<java.util.List<V>>", keyType.getType().toString());
}
代码示例来源:origin: google/guava
public void testFalseRecursiveType_intermediaryTypeMappingDoesNotConfuseMe() {
Type returnType =
genericReturnType(SubtypeOfWithFalseRecursiveType.class, "revertKeyAndValueTypes");
TypeToken<?> keyType =
TypeToken.of(returnType).resolveType(WithFalseRecursiveType.class.getTypeParameters()[0]);
assertEquals("java.util.List<K1>", keyType.getType().toString());
}
代码示例来源:origin: google/guava
public void testFalseRecursiveType_notRealRecursiveMapping() {
Type returnType = genericReturnType(WithFalseRecursiveType.class, "shouldNotCauseInfiniteLoop");
TypeToken<?> keyType =
TypeToken.of(returnType).resolveType(WithFalseRecursiveType.class.getTypeParameters()[0]);
assertEquals("java.util.List<K>", keyType.getType().toString());
}
代码示例来源:origin: google/guava
public void testResolveType_fromWildcard() {
ParameterizedType withWildcardType =
(ParameterizedType) new TypeCapture<Comparable<? extends Iterable<String>>>() {}.capture();
TypeToken<?> wildcardType = TypeToken.of(withWildcardType.getActualTypeArguments()[0]);
assertEquals(
String.class, wildcardType.resolveType(Iterable.class.getTypeParameters()[0]).getType());
}
代码示例来源:origin: google/guava
public void testInterfaceTypeParameterResolution() throws Exception {
assertEquals(
String.class,
TypeToken.of(new TypeToken<ArrayList<String>>() {}.getType())
.resolveType(List.class.getTypeParameters()[0])
.getType());
}
代码示例来源:origin: google/guava
public <F extends Enum<F> & Function<String, Integer> & Iterable<Long>>
void testResolveType_fromTypeVariable() {
TypeToken<?> f = TypeToken.of(new TypeCapture<F>() {}.capture());
assertEquals(String.class, f.resolveType(Function.class.getTypeParameters()[0]).getType());
assertEquals(Integer.class, f.resolveType(Function.class.getTypeParameters()[1]).getType());
assertEquals(Long.class, f.resolveType(Iterable.class.getTypeParameters()[0]).getType());
}
代码示例来源:origin: google/guava
public <E extends Comparable<Iterable<String>> & Iterable<Integer>>
void testResolveType_fromTypeVariable_onlyDirectBoundsAreUsed() {
TypeToken<?> e = TypeToken.of(new TypeCapture<E>() {}.capture());
assertEquals(Integer.class, e.resolveType(Iterable.class.getTypeParameters()[0]).getType());
}
代码示例来源:origin: google/guava
public void testToString() {
assertEquals(int[].class.getName(), Types.toString(int[].class));
assertEquals(int[][].class.getName(), Types.toString(int[][].class));
assertEquals(String[].class.getName(), Types.toString(String[].class));
Type elementType = List.class.getTypeParameters()[0];
assertEquals(elementType.toString(), Types.toString(elementType));
}
代码示例来源:origin: google/guava
public void testGetSupertype_withTypeVariable() {
ParameterizedType expectedType =
Types.newParameterizedType(
Iterable.class,
Types.newParameterizedType(List.class, ListIterable.class.getTypeParameters()[0]));
assertEquals(
expectedType, TypeToken.of(ListIterable.class).getSupertype(Iterable.class).getType());
}
代码示例来源:origin: google/guava
public void testGenericArrayType() {
GenericArray<?> genericArray = new GenericArray<>();
assertEquals(GenericArray.class.getTypeParameters()[0], genericArray.t);
assertEquals(Types.newArrayType(genericArray.t), genericArray.array);
}
代码示例来源:origin: google/guava
@SuppressWarnings("rawtypes") // purpose is to test raw type
public void testGetSupertype_fromRawClass() {
assertEquals(
Types.newParameterizedType(Iterable.class, List.class.getTypeParameters()[0]),
new TypeToken<List>() {}.getSupertype(Iterable.class).getType());
}
代码示例来源:origin: google/guava
public void testToGenericType() {
assertEquals(TypeToken.of(String.class), TypeToken.toGenericType(String.class));
assertEquals(new TypeToken<int[]>() {}, TypeToken.toGenericType(int[].class));
@SuppressWarnings("rawtypes") // Iterable.class
TypeToken<? extends Iterable> genericType = TypeToken.toGenericType(Iterable.class);
assertEquals(Iterable.class, genericType.getRawType());
assertEquals(
Types.newParameterizedType(Iterable.class, Iterable.class.getTypeParameters()[0]),
genericType.getType());
}
内容来源于网络,如有侵权,请联系作者删除!