Retrieving values from fields in GWT generators

168 views
Skip to first unread message

Adam Gordon

unread,
Sep 10, 2012, 12:36:50 PM9/10/12
to google-we...@googlegroups.com
I'm building a GWT generator and I'm parsing a class.  I've gotten to the point where i have a JField[] and am iterating over the class fields.  I need to know how to get the value from a field and the GWT documentation is a bit lacking (i.e., non-existent, at least from a javadoc perspective).  Anyone have any insight?  Thanks.

--adam

Thomas Broyer

unread,
Sep 10, 2012, 12:44:12 PM9/10/12
to google-we...@googlegroups.com

On Monday, September 10, 2012 6:36:50 PM UTC+2, Adam Gordon wrote:
I'm building a GWT generator and I'm parsing a class.  I've gotten to the point where i have a JField[] and am iterating over the class fields.  I need to know how to get the value from a field and the GWT documentation is a bit lacking (i.e., non-existent, at least from a javadoc perspective).  Anyone have any insight?  Thanks.

Getting the value of a field is a runtime operation, and a generator runs at compile-time. The only thing you can do is generate code that will read the field's value and act upon it at runtime, and then hope for the compiler to possibly detect that the value is actually constant and thus optimize out all the code you generated that would never be reached.

Put differently, imagine the generator runs right within Eclipse (or your IDE of choice) and generates *.java files. This is more or less what happens actually, except internally during the compilation.

Adam Gordon

unread,
Sep 10, 2012, 1:16:05 PM9/10/12
to google-we...@googlegroups.com
They Thomas, thanks for the quick reply.

Yea.  Sorry about that.  I should have been more clear.  I realized that, at least for my implementation, reading the field isn't exactly what I wanted.

I'm writing writing generators to generate bean validator classes, only the bean fields are annotated with custom annotations that dictate the requirements of the field (which come from some industry specs).  We cannot use a JSR-303 solution (which probably would have been the easiest) because we cannot introduce a 3rd party dependency on our beans (even though we already using JAXB, so this requirement seems a bit silly).

The path I went down was to generate a validate(FooBean bean) method and using GWT reflection iterate over the fields of the bean looking for my annotation.  If present, get the details and then attempt to validate that the bean method parameter validated according to the annotations.  What I realized is that I don't need access to the field value, rather, I need to execute a getter method on the bean and validate that value - only, there's no way to map the bean methods to whatever field on which I'm iterating.

So ultimately, I was going down the wrong path to begin with in that I didn't need the JField value, rather, I needed a way to do the mapping as described above, which essentially cannot be done...or at least, not easily.

Thanks for the quick reply.

Thomas Broyer

unread,
Sep 10, 2012, 5:14:40 PM9/10/12
to google-we...@googlegroups.com


On Monday, September 10, 2012 7:16:05 PM UTC+2, Adam Gordon wrote:
They Thomas, thanks for the quick reply.

Yea.  Sorry about that.  I should have been more clear.  I realized that, at least for my implementation, reading the field isn't exactly what I wanted.

I'm writing writing generators to generate bean validator classes, only the bean fields are annotated with custom annotations that dictate the requirements of the field (which come from some industry specs).  We cannot use a JSR-303 solution (which probably would have been the easiest) because we cannot introduce a 3rd party dependency on our beans (even though we already using JAXB, so this requirement seems a bit silly).

The path I went down was to generate a validate(FooBean bean) method and using GWT reflection iterate over the fields of the bean looking for my annotation.  If present, get the details and then attempt to validate that the bean method parameter validated according to the annotations.  What I realized is that I don't need access to the field value, rather, I need to execute a getter method on the bean and validate that value - only, there's no way to map the bean methods to whatever field on which I'm iterating.

What's wrong with accessing the field directly? (using JSNI to bypass the Java access rules)
Maybe have a look at the kind of code generated by the JSR303 built-in support.

Adam Gordon

unread,
Sep 11, 2012, 4:53:14 PM9/11/12
to google-we...@googlegroups.com
On Monday, September 10, 2012 3:14:40 PM UTC-6, Thomas Broyer wrote:

What's wrong with accessing the field directly? (using JSNI to bypass the Java access rules)
Maybe have a look at the kind of code generated by the JSR303 built-in support.

I was under the impression that JSNI was more of a "last resort" technique however based on your answer, it sounds like this is not the case. 
Reply all
Reply to author
Forward
0 new messages