本文整理了Java中java.lang.Class.getGenericInterfaces()
方法的一些代码示例,展示了Class.getGenericInterfaces()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.getGenericInterfaces()
方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:getGenericInterfaces
[英]Returns the Types of the interfaces that this Class directly implements. If the Class represents a primitive type or void then an empty array is returned.
[中]返回此类直接实现的接口类型。如果类表示基元类型或void,则返回空数组。
代码示例来源:origin: prestodb/presto
/**
* @since 2.7
*/
public static Type[] getGenericInterfaces(Class<?> cls) {
return cls.getGenericInterfaces();
}
代码示例来源:origin: redisson/redisson
/**
* @since 2.7
*/
public static Type[] getGenericInterfaces(Class<?> cls) {
return cls.getGenericInterfaces();
}
代码示例来源:origin: apache/incubator-dubbo
public static Class<?> getGenericClass(Class<?> cls, int i) {
try {
ParameterizedType parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]);
Object genericClass = parameterizedType.getActualTypeArguments()[i];
if (genericClass instanceof ParameterizedType) { // handle nested generic type
return (Class<?>) ((ParameterizedType) genericClass).getRawType();
} else if (genericClass instanceof GenericArrayType) { // handle array generic type
return (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType();
} else if (((Class) genericClass).isArray()) {
// Requires JDK 7 or higher, Foo<int[]> is no longer GenericArrayType
return ((Class) genericClass).getComponentType();
} else {
return (Class<?>) genericClass;
}
} catch (Throwable e) {
throw new IllegalArgumentException(cls.getName()
+ " generic type undefined!", e);
}
}
代码示例来源:origin: apache/incubator-dubbo
public static Class<?> getGenericClass(Class<?> cls, int i) {
try {
ParameterizedType parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]);
Object genericClass = parameterizedType.getActualTypeArguments()[i];
if (genericClass instanceof ParameterizedType) { // handle nested generic type
return (Class<?>) ((ParameterizedType) genericClass).getRawType();
} else if (genericClass instanceof GenericArrayType) { // handle array generic type
return (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType();
} else if (((Class) genericClass).isArray()) {
// Requires JDK 7 or higher, Foo<int[]> is no longer GenericArrayType
return ((Class) genericClass).getComponentType();
} else {
return (Class<?>) genericClass;
}
} catch (Throwable e) {
throw new IllegalArgumentException(cls.getName()
+ " generic type undefined!", e);
}
}
代码示例来源:origin: google/guava
@Override
void visitClass(Class<?> clazz) {
visit(clazz.getGenericSuperclass());
visit(clazz.getGenericInterfaces());
}
代码示例来源:origin: org.mockito/mockito-core
/**
* Finds generic interface implementation based on the source class and the target interface.
* Returns null if not found. Recurses the interface hierarchy.
*/
private static Type findGenericInterface(Class<?> sourceClass, Class<?> targetBaseInterface) {
for (int i = 0; i < sourceClass.getInterfaces().length; i++) {
Class<?> inter = sourceClass.getInterfaces()[i];
if (inter == targetBaseInterface) {
return sourceClass.getGenericInterfaces()[0];
} else {
Type deeper = findGenericInterface(inter, targetBaseInterface);
if (deeper != null) {
return deeper;
}
}
}
return null;
}
代码示例来源:origin: apache/incubator-dubbo
public static Class<?> getGenericClass(Class<?> cls, int i) {
try {
ParameterizedType parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]);
Object genericClass = parameterizedType.getActualTypeArguments()[i];
if (genericClass instanceof ParameterizedType) {
return (Class<?>) ((ParameterizedType) genericClass).getRawType();
} else if (genericClass instanceof GenericArrayType) {
Type type = ((GenericArrayType) genericClass).getGenericComponentType();
if (type instanceof TypeVariable) {
return type.getClass();
}
return (((GenericArrayType) genericClass).getGenericComponentType() instanceof Class<?>)
? (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType()
: ((GenericArrayType) genericClass).getGenericComponentType().getClass();
} else if (genericClass != null) {
if (genericClass instanceof TypeVariable) {
return genericClass.getClass();
}
return (Class<?>) genericClass;
}
} catch (Throwable e) {
}
if (cls.getSuperclass() != null) {
return getGenericClass(cls.getSuperclass(), i);
} else {
throw new IllegalArgumentException(cls.getName() + " generic type undefined!");
}
}
代码示例来源:origin: apache/incubator-dubbo
public static Class<?> getGenericClass(Class<?> cls, int i) {
try {
ParameterizedType parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]);
Object genericClass = parameterizedType.getActualTypeArguments()[i];
if (genericClass instanceof ParameterizedType) {
return (Class<?>) ((ParameterizedType) genericClass).getRawType();
} else if (genericClass instanceof GenericArrayType) {
Type type = ((GenericArrayType) genericClass).getGenericComponentType();
if (type instanceof TypeVariable) {
return type.getClass();
}
return (((GenericArrayType) genericClass).getGenericComponentType() instanceof Class<?>)
? (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType()
: ((GenericArrayType) genericClass).getGenericComponentType().getClass();
} else if (genericClass != null) {
if (genericClass instanceof TypeVariable) {
return genericClass.getClass();
}
return (Class<?>) genericClass;
}
} catch (Throwable e) {
}
if (cls.getSuperclass() != null) {
return getGenericClass(cls.getSuperclass(), i);
} else {
throw new IllegalArgumentException(cls.getName() + " generic type undefined!");
}
}
代码示例来源:origin: square/retrofit
for (int i = 0, length = interfaces.length; i < length; i++) {
if (interfaces[i] == toResolve) {
return rawType.getGenericInterfaces()[i];
} else if (toResolve.isAssignableFrom(interfaces[i])) {
return getGenericSupertype(rawType.getGenericInterfaces()[i], interfaces[i], toResolve);
代码示例来源:origin: org.mockito/mockito-core
/**
* Registers the type variables for the given type and all of its superclasses and superinterfaces.
*/
protected void registerAllTypeVariables(Type classType) {
Queue<Type> typesToRegister = new LinkedList<Type>();
Set<Type> registeredTypes = new HashSet<Type>();
typesToRegister.add(classType);
while (!typesToRegister.isEmpty()) {
Type typeToRegister = typesToRegister.poll();
if (typeToRegister == null || registeredTypes.contains(typeToRegister)) {
continue;
}
registerTypeVariablesOn(typeToRegister);
registeredTypes.add(typeToRegister);
Class<?> rawType = extractRawTypeOf(typeToRegister);
typesToRegister.add(rawType.getGenericSuperclass());
typesToRegister.addAll(Arrays.asList(rawType.getGenericInterfaces()));
}
}
代码示例来源:origin: prestodb/presto
@Override
void visitClass(Class<?> clazz) {
visit(clazz.getGenericSuperclass());
visit(clazz.getGenericInterfaces());
}
代码示例来源:origin: google/guava
/**
* Returns the generic interfaces that this type directly {@code implements}. This method is
* similar but different from {@link Class#getGenericInterfaces()}. For example, {@code new
* TypeToken<List<String>>() {}.getGenericInterfaces()} will return a list that contains {@code
* new TypeToken<Iterable<String>>() {}}; while {@code List.class.getGenericInterfaces()} will
* return an array that contains {@code Iterable<T>}, where the {@code T} is the type variable
* declared by interface {@code Iterable}.
*
* <p>If this type is a type variable or wildcard, its upper bounds are examined and those that
* are either an interface or upper-bounded only by interfaces are returned. This means that the
* returned types could include type variables too.
*/
final ImmutableList<TypeToken<? super T>> getGenericInterfaces() {
if (runtimeType instanceof TypeVariable) {
return boundsAsInterfaces(((TypeVariable<?>) runtimeType).getBounds());
}
if (runtimeType instanceof WildcardType) {
return boundsAsInterfaces(((WildcardType) runtimeType).getUpperBounds());
}
ImmutableList.Builder<TypeToken<? super T>> builder = ImmutableList.builder();
for (Type interfaceType : getRawType().getGenericInterfaces()) {
@SuppressWarnings("unchecked") // interface of T
TypeToken<? super T> resolvedInterface =
(TypeToken<? super T>) resolveSupertype(interfaceType);
builder.add(resolvedInterface);
}
return builder.build();
}
代码示例来源:origin: google/j2objc
@Override
void visitClass(Class<?> clazz) {
visit(clazz.getGenericSuperclass());
visit(clazz.getGenericInterfaces());
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Return a {@link ResolvableType} array representing the direct interfaces
* implemented by this type. If this type does not implement any interfaces an
* empty array is returned.
* <p>Note: The resulting {@link ResolvableType} instances may not be {@link Serializable}.
* @see #getSuperType()
*/
public ResolvableType[] getInterfaces() {
Class<?> resolved = resolve();
if (resolved == null) {
return EMPTY_TYPES_ARRAY;
}
ResolvableType[] interfaces = this.interfaces;
if (interfaces == null) {
Type[] genericIfcs = resolved.getGenericInterfaces();
interfaces = new ResolvableType[genericIfcs.length];
for (int i = 0; i < genericIfcs.length; i++) {
interfaces[i] = forType(genericIfcs[i], this);
}
this.interfaces = interfaces;
}
return interfaces;
}
代码示例来源:origin: google/guava
public void testGetType() {
TypeToken<?> t = TypeToken.of(StringList.class.getGenericInterfaces()[0]);
assertEquals(new TypeToken<List<String>>() {}, t);
}
代码示例来源:origin: google/guava
public void testTypeIsCorrect() {
TypeToken<List<String>> token = new TypeToken<List<String>>() {};
assertEquals(StringList.class.getGenericInterfaces()[0], token.getType());
}
代码示例来源:origin: redisson/redisson
@Override
@CachedReturnPlugin.Enhance("resolved")
protected TypeDescription.Generic resolve() {
java.lang.reflect.Type[] type = this.type.getGenericInterfaces();
return erasure.length == type.length
? Sort.describe(type[index], getAnnotationReader())
: asRawType();
}
代码示例来源:origin: google/guava
public void testAssignableParameterizedTypeToType() {
TypeToken<List<String>> tokenL = new TypeToken<List<String>>() {};
assertTrue(tokenL.isSupertypeOf(StringList.class.getGenericInterfaces()[0]));
assertFalse(tokenL.isSupertypeOf(IntegerList.class.getGenericInterfaces()[0]));
TypeToken<First<String>> tokenF = new TypeToken<First<String>>() {};
assertTrue(tokenF.isSupertypeOf(ConcreteIS.class.getGenericSuperclass()));
assertFalse(tokenF.isSupertypeOf(ConcreteSI.class.getGenericSuperclass()));
}
代码示例来源:origin: google/guava
public void testAssignableParameterizedTypeToClass() {
@SuppressWarnings("rawtypes") // Trying to test raw class
TypeToken<List> tokL = new TypeToken<List>() {};
assertTrue(tokL.isSupertypeOf(StringList.class));
assertTrue(tokL.isSupertypeOf(StringList.class.getGenericInterfaces()[0]));
@SuppressWarnings("rawtypes") // Trying to test raw class
TypeToken<Second> tokS = new TypeToken<Second>() {};
assertTrue(tokS.isSupertypeOf(Second.class));
assertTrue(tokS.isSupertypeOf(Third.class.getGenericSuperclass()));
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Determine whether the underlying type has any unresolvable generics:
* either through an unresolvable type variable on the type itself
* or through implementing a generic interface in a raw fashion,
* i.e. without substituting that interface's type variables.
* The result will be {@code true} only in those two scenarios.
*/
public boolean hasUnresolvableGenerics() {
if (this == NONE) {
return false;
}
ResolvableType[] generics = getGenerics();
for (ResolvableType generic : generics) {
if (generic.isUnresolvableTypeVariable() || generic.isWildcardWithoutBounds()) {
return true;
}
}
Class<?> resolved = resolve();
if (resolved != null) {
for (Type genericInterface : resolved.getGenericInterfaces()) {
if (genericInterface instanceof Class) {
if (forClass((Class<?>) genericInterface).hasGenerics()) {
return true;
}
}
}
return getSuperType().hasUnresolvableGenerics();
}
return false;
}
内容来源于网络,如有侵权,请联系作者删除!