给定一个对象和一个 Class,我可以判断该对象是否属于该类的子类型吗? (GWT,客户端)

发布于 2024-12-23 09:25:03 字数 152 浏览 5 评论 0 原文

Class<?> baseClass = ...
Object obj = ...

GWT中有没有办法检查obj的类型是否实现/扩展baseClass? (这是客户端代码)。

Class<?> baseClass = ...
Object obj = ...

Is there a way in GWT to check whether obj's type implements/extends baseClass? (that's client-side code).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忱杏 2024-12-30 09:25:03

JRE 模拟参考表示 isInstance() 不受支持。

您可以使用 obj instanceOf SomeConcrete.class ,并且所有类都必须在编译时已知。 GWT 编译器需要在编译时了解所有类型(以便它可以生成适当的 JavaScript 代码),因此不可能实现真正的运行时反射。我认为 GWT 的方法是延迟绑定,这样您就不必检查。

以下是来自2008 年 Google IO 关于此主题:

延迟绑定是一种可插入的编译时类型替换和代码生成机制。这种独特的程序模块化方法提供了 Java 反射和动态类加载的许多优点,而不会影响 GWT 编译器优化代码的能力。

似乎有一个项目可以解决这个问题,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:

Deferred binding is a pluggable compile-time type substitution and code generation mechanism. This unique approach to program modularization provides many of the benefits of Java reflection and dynamic class loading without compromising the GWT compiler's ability to optimize the heck out of your code.

Seems like there's a project for this, gwt-reflection, but I haven't used it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文