java - 是否有 Class.isAssignableFrom 与 Type 对象一起使用的替代方法?

标签 java reflection

在 Java 中, Class 有一个 isAssignableFrom method定义如下:

public boolean isAssignableFrom(Class<?> cls)

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.

Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.

Parameters:

cls - the Class object to be checked

Returns:

the boolean value indicating whether objects of the type cls can be assigned to objects of this class

Class实现 Type界面。有没有等价的 isAssignableFrom适用于 Type 的方法s 而不仅仅是 Class是吗?例如,是否有一种方法可以确定类型为 List<String> 的变量是否存在? (将通过 ParameterizedType 的实例表示)可以分配给 List<? extends Object> 类型的变量?

最佳答案

看看javaRuntype -- 我发现它很有用。它可以根据 java.lang.reflect.Type 的实例生成类型表示,并测试它们之间的可分配性。

关于java - 是否有 Class.isAssignableFrom 与 Type 对象一起使用的替代方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7920023/

相关文章:

java - Integer 类型引用 String 类型值

c# - 如何在C#中获取对象的所有简单属性

java - SQL、Hibernate - 从指定 ID 获取所有新行

java - 使用java从SQL表中获取主键值

java - 从 Java 执行 Hive 查询

java - 搜索数组中的重复项

java - newInstance() 与新的

java - java从服务器发送文件到客户端

inheritance - 如何在 Golang 中为多种类型编写一个函数?

Android如何检测拨出电话是否被接听