You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by "Bryan Duxbury (JIRA)" <ji...@apache.org> on 2008/09/19 20:18:44 UTC
[jira] Created: (THRIFT-139) Add reflection-like features to
generated classes
Add reflection-like features to generated classes
-------------------------------------------------
Key: THRIFT-139
URL: https://issues.apache.org/jira/browse/THRIFT-139
Project: Thrift
Issue Type: New Feature
Components: Compiler (Java)
Reporter: Bryan Duxbury
There are a few things I think would increase the usability of generated classes:
* Contain named constants for each field name with its field ID. This would allow you to do stuff like MyClass.SOME_FIELD_NAME and get the field ID and use it elsewhere. (We use this as a selector for which field in a union-like structure is selected.) You could also use the named constants in the generated code's read and write methods to more clearly indicate which fields are being read or written.
* Setter and getter methods by field ID. You could have a signature like "public object getFieldValue(int fieldID)" and "public void setFieldValue(int fieldID, Object value)". This would allow you to write code that was generic over different fields of the same type and only took the field ID as a parameter, for instance.
Would other people be interested in capabilities like this? I'm sure we can think of other things we'd like to add as well.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (THRIFT-139) Add reflection-like features to
generated classes
Posted by "David Reiss (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/THRIFT-139?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12637769#action_12637769 ]
David Reiss commented on THRIFT-139:
------------------------------------
These are pretty cool features.
1/ There are two other places where get_key() is used. Might as well replace those also.
2/ Is this going to generate some sort of unchecked cast warning because it casts containers to the plain type instead of a generic type (sorry, I'm not a Java generics expert.
3/ It seems like there is some repeated code due to the fact that there are two separate loops over the fields that do the same setup. Do you think it would be possible to buffer up the getter and setter text in strings so that you could build them up concurrently before writing both of them to the output in order? See render_const_value for the ostringstream technique.
4/ Most importantly, I think this generator assumes that beans mode is in use and won't compile without it (setFoobar is not defined in non-beans mode).
> Add reflection-like features to generated classes
> -------------------------------------------------
>
> Key: THRIFT-139
> URL: https://issues.apache.org/jira/browse/THRIFT-139
> Project: Thrift
> Issue Type: New Feature
> Components: Compiler (Java)
> Reporter: Bryan Duxbury
> Assignee: Bryan Duxbury
> Attachments: thrift-139.patch
>
>
> There are a few things I think would increase the usability of generated classes:
> * Contain named constants for each field name with its field ID. This would allow you to do stuff like MyClass.SOME_FIELD_NAME and get the field ID and use it elsewhere. (We use this as a selector for which field in a union-like structure is selected.) You could also use the named constants in the generated code's read and write methods to more clearly indicate which fields are being read or written.
> * Setter and getter methods by field ID. You could have a signature like "public object getFieldValue(int fieldID)" and "public void setFieldValue(int fieldID, Object value)". This would allow you to write code that was generic over different fields of the same type and only took the field ID as a parameter, for instance.
> Would other people be interested in capabilities like this? I'm sure we can think of other things we'd like to add as well.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (THRIFT-139) Add reflection-like features to
generated classes
Posted by "David Reiss (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/THRIFT-139?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12638126#action_12638126 ]
David Reiss commented on THRIFT-139:
------------------------------------
I think the indentation is a little off in the new version, because the indents for the stringstreams are determined when they are constructed, not output. It looks like you also left in some commented-out code. What do you think of these small changes?
http://gitweb.thrift-rpc.org/?p=thrift.git;a=log;h=refs/heads/pri/dreiss/t139;hb=HEAD
I just realized that this will cause a compile error if someone uses an all-caps field, but I think I'm okay with that because the failure mode is safe.
I definitely get warnings with this. When compiling Hello.java (from SmallTest.thrift), I get
{noformat}
Note: Hello.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
{noformat}
When I add that option, I get
{noformat}
Hello.java:173: warning: [unchecked] unchecked cast
found : java.lang.Object
required: java.util.Map<java.lang.Integer,java.lang.Integer>
setComplex((Map<Integer,Integer>)value);
^
Hello.java:177: warning: [unchecked] unchecked cast
found : java.lang.Object
required: java.util.Map<java.lang.Integer,java.util.Map<java.lang.Integer,java.lang.Integer>>
setComplexer((Map<Integer,Map<Integer,Integer>>)value);
^
2 warnings
{noformat}
I'm not really sure if this is solvable, so I'm willing to commit without a fix.
> Add reflection-like features to generated classes
> -------------------------------------------------
>
> Key: THRIFT-139
> URL: https://issues.apache.org/jira/browse/THRIFT-139
> Project: Thrift
> Issue Type: New Feature
> Components: Compiler (Java)
> Reporter: Bryan Duxbury
> Assignee: Bryan Duxbury
> Attachments: thrift-139-v2.patch, thrift-139.patch
>
>
> There are a few things I think would increase the usability of generated classes:
> * Contain named constants for each field name with its field ID. This would allow you to do stuff like MyClass.SOME_FIELD_NAME and get the field ID and use it elsewhere. (We use this as a selector for which field in a union-like structure is selected.) You could also use the named constants in the generated code's read and write methods to more clearly indicate which fields are being read or written.
> * Setter and getter methods by field ID. You could have a signature like "public object getFieldValue(int fieldID)" and "public void setFieldValue(int fieldID, Object value)". This would allow you to write code that was generic over different fields of the same type and only took the field ID as a parameter, for instance.
> Would other people be interested in capabilities like this? I'm sure we can think of other things we'd like to add as well.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Updated: (THRIFT-139) Add reflection-like features to
generated classes
Posted by "Bryan Duxbury (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/THRIFT-139?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Bryan Duxbury updated THRIFT-139:
---------------------------------
Patch Info: [Patch Available]
> Add reflection-like features to generated classes
> -------------------------------------------------
>
> Key: THRIFT-139
> URL: https://issues.apache.org/jira/browse/THRIFT-139
> Project: Thrift
> Issue Type: New Feature
> Components: Compiler (Java)
> Reporter: Bryan Duxbury
> Assignee: Bryan Duxbury
> Attachments: thrift-139.patch
>
>
> There are a few things I think would increase the usability of generated classes:
> * Contain named constants for each field name with its field ID. This would allow you to do stuff like MyClass.SOME_FIELD_NAME and get the field ID and use it elsewhere. (We use this as a selector for which field in a union-like structure is selected.) You could also use the named constants in the generated code's read and write methods to more clearly indicate which fields are being read or written.
> * Setter and getter methods by field ID. You could have a signature like "public object getFieldValue(int fieldID)" and "public void setFieldValue(int fieldID, Object value)". This would allow you to write code that was generic over different fields of the same type and only took the field ID as a parameter, for instance.
> Would other people be interested in capabilities like this? I'm sure we can think of other things we'd like to add as well.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Resolved: (THRIFT-139) Add reflection-like features to
generated classes
Posted by "David Reiss (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/THRIFT-139?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
David Reiss resolved THRIFT-139.
--------------------------------
Resolution: Fixed
> Add reflection-like features to generated classes
> -------------------------------------------------
>
> Key: THRIFT-139
> URL: https://issues.apache.org/jira/browse/THRIFT-139
> Project: Thrift
> Issue Type: New Feature
> Components: Compiler (Java)
> Reporter: Bryan Duxbury
> Assignee: Bryan Duxbury
> Attachments: thrift-139-v2.patch, thrift-139.patch
>
>
> There are a few things I think would increase the usability of generated classes:
> * Contain named constants for each field name with its field ID. This would allow you to do stuff like MyClass.SOME_FIELD_NAME and get the field ID and use it elsewhere. (We use this as a selector for which field in a union-like structure is selected.) You could also use the named constants in the generated code's read and write methods to more clearly indicate which fields are being read or written.
> * Setter and getter methods by field ID. You could have a signature like "public object getFieldValue(int fieldID)" and "public void setFieldValue(int fieldID, Object value)". This would allow you to write code that was generic over different fields of the same type and only took the field ID as a parameter, for instance.
> Would other people be interested in capabilities like this? I'm sure we can think of other things we'd like to add as well.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (THRIFT-139) Add reflection-like features to
generated classes
Posted by "Bryan Duxbury (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/THRIFT-139?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12637784#action_12637784 ]
Bryan Duxbury commented on THRIFT-139:
--------------------------------------
1/ yes, makes sense, will look into it.
2/ it won't generate warnings, because a Set<Integer> is also a Set, but I'll fix this to use type_name() correctly anyways, since the information is there. (I'm still learning all the existing features of the Java generator... :)
3/ will check this out
4/ Not to totally hijack this issue, but perhaps there shouldn't be a non-beans Java generator. The reason that I'm so into the beans generator is that, frankly, the non-beans generator did not work as it needed to. I understand the argument that there was a desire for especially lightweight generated code, but it sacrifices a lot of other elements, like __isset consistency and thus wire density, which seems to be a critical element. We could simplify the generator if we didn't need to support two different modes at all times, which would be nice, and then this wouldn't be an issue at all. I can see what it would take for this patch to work with the generator remaining the same, though.
> Add reflection-like features to generated classes
> -------------------------------------------------
>
> Key: THRIFT-139
> URL: https://issues.apache.org/jira/browse/THRIFT-139
> Project: Thrift
> Issue Type: New Feature
> Components: Compiler (Java)
> Reporter: Bryan Duxbury
> Assignee: Bryan Duxbury
> Attachments: thrift-139.patch
>
>
> There are a few things I think would increase the usability of generated classes:
> * Contain named constants for each field name with its field ID. This would allow you to do stuff like MyClass.SOME_FIELD_NAME and get the field ID and use it elsewhere. (We use this as a selector for which field in a union-like structure is selected.) You could also use the named constants in the generated code's read and write methods to more clearly indicate which fields are being read or written.
> * Setter and getter methods by field ID. You could have a signature like "public object getFieldValue(int fieldID)" and "public void setFieldValue(int fieldID, Object value)". This would allow you to write code that was generic over different fields of the same type and only took the field ID as a parameter, for instance.
> Would other people be interested in capabilities like this? I'm sure we can think of other things we'd like to add as well.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (THRIFT-139) Add reflection-like features to
generated classes
Posted by "Bryan Duxbury (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/THRIFT-139?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12638130#action_12638130 ]
Bryan Duxbury commented on THRIFT-139:
--------------------------------------
The proposed changes seem ok. As far as those warnings, I don't know if there's a way to resolve them. If there's anyone more knowledgeable about Java casts, they can chime in, but I think it will do the right thing.
> Add reflection-like features to generated classes
> -------------------------------------------------
>
> Key: THRIFT-139
> URL: https://issues.apache.org/jira/browse/THRIFT-139
> Project: Thrift
> Issue Type: New Feature
> Components: Compiler (Java)
> Reporter: Bryan Duxbury
> Assignee: Bryan Duxbury
> Attachments: thrift-139-v2.patch, thrift-139.patch
>
>
> There are a few things I think would increase the usability of generated classes:
> * Contain named constants for each field name with its field ID. This would allow you to do stuff like MyClass.SOME_FIELD_NAME and get the field ID and use it elsewhere. (We use this as a selector for which field in a union-like structure is selected.) You could also use the named constants in the generated code's read and write methods to more clearly indicate which fields are being read or written.
> * Setter and getter methods by field ID. You could have a signature like "public object getFieldValue(int fieldID)" and "public void setFieldValue(int fieldID, Object value)". This would allow you to write code that was generic over different fields of the same type and only took the field ID as a parameter, for instance.
> Would other people be interested in capabilities like this? I'm sure we can think of other things we'd like to add as well.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Updated: (THRIFT-139) Add reflection-like features to
generated classes
Posted by "Bryan Duxbury (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/THRIFT-139?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Bryan Duxbury updated THRIFT-139:
---------------------------------
Attachment: thrift-139.patch
Here's a patch for some of this functionality. I've added constants for the each field id to the class as static members. It also has a setFieldValue that works on all object types (structs, lists, sets, maps, strings) and a getFieldValue that works on all types by boxing primitives into their Java object wrappers. Notably absent are primitive setFieldValue and getFieldValue overloads, something like getFieldValueInt, etc. I just wanted to get some initial feedback on this.
> Add reflection-like features to generated classes
> -------------------------------------------------
>
> Key: THRIFT-139
> URL: https://issues.apache.org/jira/browse/THRIFT-139
> Project: Thrift
> Issue Type: New Feature
> Components: Compiler (Java)
> Reporter: Bryan Duxbury
> Assignee: Bryan Duxbury
> Attachments: thrift-139.patch
>
>
> There are a few things I think would increase the usability of generated classes:
> * Contain named constants for each field name with its field ID. This would allow you to do stuff like MyClass.SOME_FIELD_NAME and get the field ID and use it elsewhere. (We use this as a selector for which field in a union-like structure is selected.) You could also use the named constants in the generated code's read and write methods to more clearly indicate which fields are being read or written.
> * Setter and getter methods by field ID. You could have a signature like "public object getFieldValue(int fieldID)" and "public void setFieldValue(int fieldID, Object value)". This would allow you to write code that was generic over different fields of the same type and only took the field ID as a parameter, for instance.
> Would other people be interested in capabilities like this? I'm sure we can think of other things we'd like to add as well.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Assigned: (THRIFT-139) Add reflection-like features to
generated classes
Posted by "Bryan Duxbury (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/THRIFT-139?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Bryan Duxbury reassigned THRIFT-139:
------------------------------------
Assignee: Bryan Duxbury
> Add reflection-like features to generated classes
> -------------------------------------------------
>
> Key: THRIFT-139
> URL: https://issues.apache.org/jira/browse/THRIFT-139
> Project: Thrift
> Issue Type: New Feature
> Components: Compiler (Java)
> Reporter: Bryan Duxbury
> Assignee: Bryan Duxbury
>
> There are a few things I think would increase the usability of generated classes:
> * Contain named constants for each field name with its field ID. This would allow you to do stuff like MyClass.SOME_FIELD_NAME and get the field ID and use it elsewhere. (We use this as a selector for which field in a union-like structure is selected.) You could also use the named constants in the generated code's read and write methods to more clearly indicate which fields are being read or written.
> * Setter and getter methods by field ID. You could have a signature like "public object getFieldValue(int fieldID)" and "public void setFieldValue(int fieldID, Object value)". This would allow you to write code that was generic over different fields of the same type and only took the field ID as a parameter, for instance.
> Would other people be interested in capabilities like this? I'm sure we can think of other things we'd like to add as well.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Updated: (THRIFT-139) Add reflection-like features to
generated classes
Posted by "Bryan Duxbury (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/THRIFT-139?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Bryan Duxbury updated THRIFT-139:
---------------------------------
Attachment: thrift-139-v2.patch
Ok, here's another version.
This addresses 1 and 2 above by using type_name, which just figures stuff out. It addresses 3 by using a single loop and two separate functions for generating the getter and setter bodies. It addresses 4 by only generating the reflecting getter and setter methods when you are in bean style :)
In addition, this patch resolves the issue of primitive types not being gettable/settable by converting them to their boxed (object) versions and letting autoboxing manage the parameter conversions.
> Add reflection-like features to generated classes
> -------------------------------------------------
>
> Key: THRIFT-139
> URL: https://issues.apache.org/jira/browse/THRIFT-139
> Project: Thrift
> Issue Type: New Feature
> Components: Compiler (Java)
> Reporter: Bryan Duxbury
> Assignee: Bryan Duxbury
> Attachments: thrift-139-v2.patch, thrift-139.patch
>
>
> There are a few things I think would increase the usability of generated classes:
> * Contain named constants for each field name with its field ID. This would allow you to do stuff like MyClass.SOME_FIELD_NAME and get the field ID and use it elsewhere. (We use this as a selector for which field in a union-like structure is selected.) You could also use the named constants in the generated code's read and write methods to more clearly indicate which fields are being read or written.
> * Setter and getter methods by field ID. You could have a signature like "public object getFieldValue(int fieldID)" and "public void setFieldValue(int fieldID, Object value)". This would allow you to write code that was generic over different fields of the same type and only took the field ID as a parameter, for instance.
> Would other people be interested in capabilities like this? I'm sure we can think of other things we'd like to add as well.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (THRIFT-139) Add reflection-like features to
generated classes
Posted by "Bryan Duxbury (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/THRIFT-139?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12636454#action_12636454 ]
Bryan Duxbury commented on THRIFT-139:
--------------------------------------
Has anyone looked at this patch? I'd really appreciate thoughts/comments.
> Add reflection-like features to generated classes
> -------------------------------------------------
>
> Key: THRIFT-139
> URL: https://issues.apache.org/jira/browse/THRIFT-139
> Project: Thrift
> Issue Type: New Feature
> Components: Compiler (Java)
> Reporter: Bryan Duxbury
> Assignee: Bryan Duxbury
> Attachments: thrift-139.patch
>
>
> There are a few things I think would increase the usability of generated classes:
> * Contain named constants for each field name with its field ID. This would allow you to do stuff like MyClass.SOME_FIELD_NAME and get the field ID and use it elsewhere. (We use this as a selector for which field in a union-like structure is selected.) You could also use the named constants in the generated code's read and write methods to more clearly indicate which fields are being read or written.
> * Setter and getter methods by field ID. You could have a signature like "public object getFieldValue(int fieldID)" and "public void setFieldValue(int fieldID, Object value)". This would allow you to write code that was generic over different fields of the same type and only took the field ID as a parameter, for instance.
> Would other people be interested in capabilities like this? I'm sure we can think of other things we'd like to add as well.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
[jira] Commented: (THRIFT-139) Add reflection-like features to
generated classes
Posted by "David Reiss (JIRA)" <ji...@apache.org>.
[ https://issues.apache.org/jira/browse/THRIFT-139?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12637798#action_12637798 ]
David Reiss commented on THRIFT-139:
------------------------------------
If you want to seriously discuss making the beans option mandatory, please create a separate issue. (I'm sure it won't surprise you to learn that I don't think it is a good idea.) One possible shortcut would be to add all of the setters and getters even in the non-beans case. This would not hurt the speed or flexibility of the non-beans generated code, and would allow this patch to work properly in that case.
> Add reflection-like features to generated classes
> -------------------------------------------------
>
> Key: THRIFT-139
> URL: https://issues.apache.org/jira/browse/THRIFT-139
> Project: Thrift
> Issue Type: New Feature
> Components: Compiler (Java)
> Reporter: Bryan Duxbury
> Assignee: Bryan Duxbury
> Attachments: thrift-139.patch
>
>
> There are a few things I think would increase the usability of generated classes:
> * Contain named constants for each field name with its field ID. This would allow you to do stuff like MyClass.SOME_FIELD_NAME and get the field ID and use it elsewhere. (We use this as a selector for which field in a union-like structure is selected.) You could also use the named constants in the generated code's read and write methods to more clearly indicate which fields are being read or written.
> * Setter and getter methods by field ID. You could have a signature like "public object getFieldValue(int fieldID)" and "public void setFieldValue(int fieldID, Object value)". This would allow you to write code that was generic over different fields of the same type and only took the field ID as a parameter, for instance.
> Would other people be interested in capabilities like this? I'm sure we can think of other things we'd like to add as well.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.