- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想申请一个项目类别的集合(具体反射(reflect))。但是关于类型删除似乎是不可能的,而且关于我在堆栈上阅读过的一些主题。有一些解决方法(here),但我很好奇是否有人知道它是如何完成的,例如 DWR:
http://directwebremoting.org/dwr/documentation/server/configuration/dwrxml/signatures.html
或者如果有更好的解决方法,那就太好了。
假设我们有这样的东西:
public String foo(List<String> possibleFoos) {
我需要做的就是找出参数 possibleFoos 是字符串列表,而不仅仅是列表
最佳答案
虽然 Java 确实会在运行时删除类型(从而将 List<String>
变成 List
),但在许多情况下,它实际上会在运行时存储泛型类型,从而允许您恢复因删除而丢失的信息。您可以检索这些的实际通用类型:
这意味着如果您只有一个类型为 List 的对象,您将无法获得它的泛型类型(object.getClass()
将获得 List
,仅此而已)- 它已永久丢失。但是,如果您试图找出方法参数或上述任何参数的泛型类型,您通常可以使用反射。由于您的案例不涉及类型变量或其他并发症,因此获取实际类型非常简单:
ParameterizedType listArgument = (ParameterizedType) ClassDeclaringFoo.class.getMethod("foo", List.class).getGenericParameterTypes()[0];
Type listType = listArgument.getActualTypeArguments()[0]; //This will be a Class representing String, the type of the List
如果您有更多参数和 map :
public String foo(Object bar, Map<String, Number> possibleFoos) { ... }
代码类似:
ParameterizedType mapArgument = (ParameterizedType) ClassDeclaringFoo.class.getMethod("foo", Object.class, Map.class).getGenericParameterTypes()[1]; //1 is the index of the map argument
Type keyType = mapArgument.getActualTypeArguments()[0]; //This will be a Class representing String, the type of the Map key
Type valType = mapArgument.getActualTypeArguments()[1]; //This will be a Class representing Number, the type of the Map value
可以安全地假设这也是 DWR 正在使用的,因为类型是方法参数。
其他列出的案例也有类似的方法:
Class.getMethod(...).getGenericReturnType()
会给你真正的返回类型Class.getField(fieldName).getGenericType()
将为您提供该字段的真实类型Class.getGenericSuperClass()
会让你成为真正的 super 类型Class.getGenericInterfaces()
将为您提供真正的接口(interface)类型存在允许访问 AnnotatedType
的等效方法Java 8 中引入的(泛型加上类型用法的注解):
Class.getMethod(...).getAnnotatedParameterTypes()
Class.getMethod(...).getAnnotatedReturnType()
Class.getField(fieldName).getAnnotatedType()
Class.getAnnotatedSuperClass()
Class.getAnnotatedInterfaces()
现在,当您的情况像示例中一样简单时,这一切都很好。但是,想象一下如果您的示例如下所示:
public T foo(List<T> possibleFoos) {...}
在这种情况下,getGenericParameterTypes()[0].getActualTypeArguments()[0]
会给你T
这是没用的。解决什么T
代表,您必须查看类定义,也许还有父类(super class),同时跟踪类型变量在每个类中的命名方式,因为每个类中的名称可能不同。
为了更轻松地使用泛型类型反射,您可以使用一个名为 GenTyRef 的很棒的库这对你来说很辛苦,如果你需要支持 AnnotatedType
s,你可以使用我的 fork GeAnTyRef (两者都在 Maven Central 中)。它们还包括一个类型工厂,允许您构造 (Annotated)Type
的实例。 ,使用普通的 Java API 无法轻松做到这一点。还有一个方便的 super type token实现允许您获得 (Annotated)Type
字面意思。
有了这些,您就可以使用 Java 允许的泛型类型来完成所有事情,而无需我上面解释的麻烦:
GenericTypeReflector#getExactParameterTypes( ... )
GenericTypeReflector#getExactReturnType( ... )
GenericTypeReflector#getExactFieldType( ... )
GenericTypeReflector#getExactSuperType( ... )
还有更多的操作,比如判断一个 Type
是另一个的父类(super class)型(类似于 Class#isAssignableFrom
但对于泛型类型),解析特定类型变量等。
关于java - DWR 如何转换传入数据并规避类型删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40038691/
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: How do I copy an object in Java? 我的类中有一个函数 Claus它调用一个函数来接受
任何人都可以给我一个 linux 内核驱动程序手册或示例的链接,它可以修改来自/到指定 ip 端口的传入/传出数据包吗? 谢谢! 最佳答案 Kprobes 是一种可以做到这一点的方法。这是一个使用具有
我有一个 3rd 方网站,该网站具有发送到特定 url 的 webhook。我将其设置为发送到我网站上的空白页面(例如:www.mysite.com/webhook.php) 我在 webhook.p
我对传递的约定感到困惑*args在 super().__init__()在python继承中。 我了解需要使用关键字参数 **kwargs因此,如果需要,CRO 中的类可以采用所需的参数,但为什么还有
我有一个 webapi (.NET core 3.1) 方法,它正在获取 XML 并反序列化为对象。但是一些标签的开头和结尾是不一样的。例如,如果以 打开然后它以 关闭.因此 XML 格式不正确。
是否有一个 hg 命令可以组合 hg传入 + hg传出 + hg status? 这会告诉您是否有任何远程内容需要传入,是否有任何本地提交的内容需要出去,或者是否有任何本地更改需要提交。 最佳答案 虽
据我了解,你无法在两部 iPhone 之间建立连接(如果我错了,请纠正我)。因此,我想做的是在客户端应用程序之间放置一台服务器,该服务器接受消息并将其重新分发给适当的人员。本质上,该应用程序将允许人们
我正在使用 Apache Camel 向我的 Java 服务发送消息。我在消费者路线上保留了 transacted=true。我还需要发送有关成功处理 JMS 消息的电子邮件。 我正在使用以下代码来注
这个问题在这里已经有了答案: What does "this" point to? (5 个答案) 关闭 4 年前。 public class CommandForm extends Form im
我是一名 Swift 开发人员,也是 Dart 的新手。我正在尝试编写一些通用方法。 我想知道我是否可以在 Dart 中实现类似的事情。 //Swift version public func mod
基本上有人问我是否有任何方法可以捕获和下载通过邮件服务器的所有传入和传出电子邮件(最好是文本格式)。这种下载方法需要使用PHP或类似的服务器端语言来完成。 如果我能通过上述阶段,那么最终目标就很容易实
我正在学习 Flask 并且有一个关于动态路由的问题:是否可以传入一个可接受的路由列表?我注意到 any 转换器有潜力,但很难找到它的使用示例。基本上我有不同的端点组,它们应该在它们之间触发相同的 A
我正在学习如何使用 pthread 函数。在这里我想使用 pthread_create 创建一个线程并用它计算一些数字的平均值。(数字序列末尾的0表示已经完成) 这是我到目前为止写的: #includ
我需要传递一个带有字段“nombre”作为参数的条件 目前我这样做: @xcondicion= {:nombre.like => "%#{params[:qyBusqueda]}%"} cliente
我有以下两个 parseInt(),我不太清楚为什么它们会给我不同的结果: alert(parseInt(0.00001)) 显示 0; alert(parseInt(0.00000001)) 显示
我有一个使用 SODA 包来操作 JSON 数据库的 PL/SQL 处理程序。我想: 读取有效负载中键 id 的值 将负载 JSON 写入数据库中的新文档。 要执行第 1 步,处理程序将 :body
我正在使用 Oracle 试用 Dapper,我试图运行多结果集查询,但 Oracle 需要 dbtype 的 refcursor。 StringBuilder query = new StringB
我在将“工作表”选项卡重命名为任何文件名时遇到问题。这样做的问题是,如果用户意外下载工作表两次,Windows 会将 template(1).csv 附加到文件中,以保持其下载目录中的唯一性。在 Ex
我将 id 作为字符串传递到函数中。我想使用该 ID 来更改背景颜色。我用过: $("#"+address).css("background-color", "#3399FF"); 但它没有改变背景颜
我有一个 ListSourceAdapter.java 类, class ListSourceViewHolder extends RecyclerView.ViewHolder implements
我是一名优秀的程序员,十分优秀!