Tuesday, December 1, 2015

Re: Is there any GWT equivalent to Class.getDeclaredField?

That's about what I thought, thanks. This particular bit of pain isn't large enough to merit a generator or annotation processor, and goes away w/ Java 8 anyway, so I'll just put up with it for now. :)

On Tuesday, December 1, 2015 at 1:31:46 AM UTC-8, Thomas Broyer wrote:
It is effectively not possible to use reflection in GWT (well, not entirely true, some people have made libraries to make it somehow possible); see notes in http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsCompatibility.html and http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsDeferred.html#benefits (returned by a search on "reflection" in the website).
Note however that GWT Generators are likely to go away in the so-called "GWT 3.0" and you're invited to use annotation processors instead. In any case, the idea is the same: to cut down on boilerplate, have it generated for you rather than relying on reflection; in other words, do your meta-programming at compilation-time rather than at runtime.

On Tuesday, December 1, 2015 at 10:25:23 AM UTC+1, Jonathan Fischer wrote:
Basically, I want to cut down on a bunch of boilerplate code (needing to check if a list of boolean fields are true/false across a collection of objects). In normal Java, I can use Class.getDeclaredField and Field.getBoolean to do that, something like:


public static boolean allTrue(Class<? extends Object> cls, List<? extends Object> things, String fieldName) {
 
try {
 
Field field = cls.getDeclaredField(fieldName);
 
boolean val = true;
 
for (Object o : things) {
   val
= val && field.getBoolean(o);
 
}

 
return val;
 
} catch (Exception e) {
 
return false;
 
}
}


I don't see any way to accomplish the same thing in GWT code, at least not in the GWT site. Is this just not an option under GWT?

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment