给定一个对象和一个 Class>,我可以判断该对象是否属于该类的子类型吗? (GWT,客户端)
Class<?> baseClass = ...
Object obj = ...
GWT中有没有办法检查obj
的类型是否实现/扩展baseClass
? (这是客户端代码)。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JRE 模拟参考表示 isInstance() 不受支持。
您可以使用 obj instanceOf SomeConcrete.class ,并且所有类都必须在编译时已知。 GWT 编译器需要在编译时了解所有类型(以便它可以生成适当的 JavaScript 代码),因此不可能实现真正的运行时反射。我认为 GWT 的方法是延迟绑定,这样您就不必检查。
以下是来自2008 年 Google IO 关于此主题:
似乎有一个项目可以解决这个问题,gwt-reflection,但我没有使用过它。
JRE emulation reference says isInstance() is not supported.
You can use
obj instanceOf SomeConcrete.class
, and all classes have to be known at compile time. GWT compiler needs to know all the types at compile time (so that it can generate appropriate JavaScript code) so true runtime reflection isn't possible. I think GWT's approach to that is deferred binding so that you don't have to check.Here's a presentation from the 2008 Google IO about this topic:
Seems like there's a project for this, gwt-reflection, but I haven't used it.