简体   繁体   中英

Given an object and a Class<?>, can I tell whether the object is of sub-type of that class? (GWT, client-side)

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM