本文整理了Java中java.lang.Class.getGenericSuperclass()
方法的一些代码示例,展示了Class.getGenericSuperclass()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.getGenericSuperclass()
方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:getGenericSuperclass
[英]Returns the Type that represents the superclass of this class.
[中]返回表示此类的超类的类型。
代码示例来源:origin: prestodb/presto
/**
* @since 2.7
*/
public static Type getGenericSuperclass(Class<?> cls) {
return cls.getGenericSuperclass();
}
代码示例来源:origin: apache/incubator-dubbo
public static <T> Class<T> resolveGenericType(Class<?> declaredClass) {
ParameterizedType parameterizedType = (ParameterizedType) declaredClass.getGenericSuperclass();
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
return (Class<T>) actualTypeArguments[0];
}
}
代码示例来源:origin: apache/incubator-dubbo
public static <T> Class<T> resolveGenericType(Class<?> declaredClass) {
ParameterizedType parameterizedType = (ParameterizedType) declaredClass.getGenericSuperclass();
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
return (Class<T>) actualTypeArguments[0];
}
}
代码示例来源:origin: google/guava
/** Returns the captured type. */
final Type capture() {
Type superclass = getClass().getGenericSuperclass();
checkArgument(superclass instanceof ParameterizedType, "%s isn't parameterized", superclass);
return ((ParameterizedType) superclass).getActualTypeArguments()[0];
}
}
代码示例来源:origin: shuzheng/zheng
/**
* 获取类泛型class
* @return
*/
public Class<Mapper> getMapperClass() {
return (Class<Mapper>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
代码示例来源:origin: spring-projects/spring-framework
protected ParameterizedTypeReference() {
Class<?> parameterizedTypeReferenceSubclass = findParameterizedTypeReferenceSubclass(getClass());
Type type = parameterizedTypeReferenceSubclass.getGenericSuperclass();
Assert.isInstanceOf(ParameterizedType.class, type, "Type must be a parameterized type");
ParameterizedType parameterizedType = (ParameterizedType) type;
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
Assert.isTrue(actualTypeArguments.length == 1, "Number of type arguments must be 1");
this.type = actualTypeArguments[0];
}
代码示例来源:origin: google/guava
private static ClassOwnership detectJvmBehavior() {
class LocalClass<T> {}
Class<?> subclass = new LocalClass<String>() {}.getClass();
ParameterizedType parameterizedType = (ParameterizedType) subclass.getGenericSuperclass();
for (ClassOwnership behavior : ClassOwnership.values()) {
if (behavior.getOwnerType(LocalClass.class) == parameterizedType.getOwnerType()) {
return behavior;
}
}
throw new AssertionError();
}
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Return a {@link ResolvableType} representing the direct supertype of this type.
* If no supertype is available this method returns {@link #NONE}.
* <p>Note: The resulting {@link ResolvableType} instance may not be {@link Serializable}.
* @see #getInterfaces()
*/
public ResolvableType getSuperType() {
Class<?> resolved = resolve();
if (resolved == null || resolved.getGenericSuperclass() == null) {
return NONE;
}
ResolvableType superType = this.superType;
if (superType == null) {
superType = forType(resolved.getGenericSuperclass(), this);
this.superType = superType;
}
return superType;
}
代码示例来源:origin: prestodb/presto
/** Returns the captured type. */
final Type capture() {
Type superclass = getClass().getGenericSuperclass();
checkArgument(superclass instanceof ParameterizedType, "%s isn't parameterized", superclass);
return ((ParameterizedType) superclass).getActualTypeArguments()[0];
}
}
代码示例来源:origin: google/guava
@Override
void visitClass(Class<?> clazz) {
visit(clazz.getGenericSuperclass());
visit(clazz.getGenericInterfaces());
}
代码示例来源:origin: prestodb/presto
private static ClassOwnership detectJvmBehavior() {
class LocalClass<T> {}
Class<?> subclass = new LocalClass<String>() {}.getClass();
ParameterizedType parameterizedType = (ParameterizedType) subclass.getGenericSuperclass();
for (ClassOwnership behavior : ClassOwnership.values()) {
if (behavior.getOwnerType(LocalClass.class) == parameterizedType.getOwnerType()) {
return behavior;
}
}
throw new AssertionError();
}
}
代码示例来源:origin: prestodb/presto
@Override
void visitClass(Class<?> clazz) {
visit(clazz.getGenericSuperclass());
visit(clazz.getGenericInterfaces());
}
代码示例来源:origin: google/guava
private static void doTestNewParameterizedType_staticLocalClass() {
class LocalClass<T> {}
Type jvmType = new LocalClass<String>() {}.getClass().getGenericSuperclass();
Type ourType = Types.newParameterizedType(LocalClass.class, String.class);
assertEquals(jvmType, ourType);
}
代码示例来源:origin: google/guava
public void testNewParameterizedType_nonStaticLocalClass() {
class LocalClass<T> {}
Type jvmType = new LocalClass<String>() {}.getClass().getGenericSuperclass();
Type ourType = Types.newParameterizedType(LocalClass.class, String.class);
assertEquals(jvmType, ourType);
}
代码示例来源:origin: google/guava
public void testVisitNull() {
new BaseTypeVisitor()
.visit(((ParameterizedType) ArrayList.class.getGenericSuperclass()).getOwnerType());
}
代码示例来源: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
private static void doTestStaticLocalClass() {
class Local<T> {}
TypeToken<Local<String>> type = new TypeToken<Local<String>>() {};
assertEquals(Types.newParameterizedType(Local.class, String.class), type.getType());
assertEquals(new Local<String>() {}.getClass().getGenericSuperclass(), type.getType());
}
代码示例来源:origin: google/guava
public void testNonStaticLocalClass() {
class Local<T> {}
TypeToken<Local<String>> type = new TypeToken<Local<String>>() {};
assertEquals(Types.newParameterizedType(Local.class, String.class), type.getType());
assertEquals(new Local<String>() {}.getClass().getGenericSuperclass(), type.getType());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void getSource() throws Exception {
Class<?> classType = MySimpleInterfaceType.class;
Field basicField = Fields.class.getField("classType");
Field field = Fields.class.getField("charSequenceList");
Method method = Methods.class.getMethod("charSequenceParameter", List.class);
MethodParameter methodParameter = MethodParameter.forExecutable(method, 0);
assertThat(ResolvableType.forField(basicField).getSource(), equalTo((Object) basicField));
assertThat(ResolvableType.forField(field).getSource(), equalTo((Object) field));
assertThat(ResolvableType.forMethodParameter(methodParameter).getSource(), equalTo((Object) methodParameter));
assertThat(ResolvableType.forMethodParameter(method, 0).getSource(), equalTo((Object) methodParameter));
assertThat(ResolvableType.forClass(classType).getSource(), equalTo((Object) classType));
assertThat(ResolvableType.forClass(classType).getSuperType().getSource(), equalTo((Object) classType.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()));
}
内容来源于网络,如有侵权,请联系作者删除!