-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add Dynamic Accessors #386
Add Dynamic Accessors #386
Conversation
@@ -325,6 +327,7 @@ private void addHashCode(JDefinedClass jclass) { | |||
} | |||
|
|||
for (JFieldVar fieldVar : fields.values()) { | |||
if( (fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) continue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change, and the others related to filtering out static fields, should probably come in as a separate PR, since this is technically broken on the 0.4.X line of development.
+1 |
Great feature, I currently have use cases in which this would definitely be useful. Have you considered expanding this feature allowing get$() and set$() to work on json pointers, or paths to a specific field? |
@cminardi I had not planned on getting into pathing with this change, since that would probably lead to |
5e9382b
to
ab04b75
Compare
@joelittlejohn Is the intention of |
Yes exactly that, public fields.
|
Great. I will add in checks for that flag and some more testing, since I am generating methods now and the test suite is passing. |
887b8e1
to
f138277
Compare
The |
public static final String GETTER_NAME = "get"; | ||
public static final String BUILDER_NAME = "with"; | ||
public static final String DEFINED_SETTER_NAME = "declaredProperty"; | ||
public static final String DEFINED_GETTER_NAME = "declaredPropertyOrDefault"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be declaredPropertyOrNotFound
. The current name implies that the provided value is returned for null.
Once #454 is on master, this change this change should be rebased and squashed. Otherwise, it is complete. |
@@ -337,6 +339,7 @@ private void addHashCode(JDefinedClass jclass) { | |||
} | |||
|
|||
for (JFieldVar fieldVar : fields.values()) { | |||
if( (fieldVar.mods().getValue() & JMod.STATIC) == JMod.STATIC) continue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When squashing this PR, bring all of the changes like this into a single commit, before the actual feature.
Nice work, thanks for continuing with this. Is 1.6 support something you need for your own project @ctrimble? It's nearly three years since public updates stopped for that version of Java so I'm not sure we still need to support it. I'll leave that up to you though (it seems to add a lot of code and complexity to this PR). The population of users that use this feature, and the population of users that are still on Java 6 are two sets that may never intersect :) The last thing we need to here is put this behind a config flag. People can get very particular about the code generated and there's now a lot added when this feature is active. |
@joelittlejohn I didn't know if the android people were all ready to move up to 1.7, so I left in support. If you are comfortable with moving the version up, then perhaps we should just drop the 1.6 support, so the project doesn't have to lug it around. In either case, I would like to get the For instance, methods like this would be very handy for me:
As for adding a flag, that is not a problem. |
@ctrimble Very good point, I hadn't considered Android. I also agree having a target version will be useful for Java 8. That flag sounds fine (maybe includeDynamicAccessors?), it's a bit cryptic but the docs will help 😄 |
I like includeDynamicAccessors better. I will add that in and start preparing for a merge. I could really use input on the PR I opened specifically for the target option. |
Added the |
Took me a while to understand that comment :) I don't think that's a problem. |
f97e72b
to
f1bf672
Compare
…ng static fields.
f1bf672
to
d084f1a
Compare
I have rebased this on master. Some naming alignment and clean up should be done before a merge happens. |
import static com.sun.codemodel.JMod.*; | ||
import static org.jsonschema2pojo.util.LanguageFeatures.*; | ||
|
||
public class PropertiesByNameRule implements Rule<JDefinedClass, JDefinedClass> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This rule should be called DynamicPropertiesRule, to align naming with the flag.
import static com.sun.codemodel.JMod.*; | ||
import static com.sun.codemodel.JExpr.*; | ||
|
||
public class PropertiesByNameRuleTest { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be DynamicPropertiesRuleTest, to align name with flag.
51997ab
to
82557c6
Compare
82557c6
to
37a60ec
Compare
@joelittlejohn I think this PR is ready to go, unless you see anything that should be cleaned up. |
This PR adds methods for getting and setting properties by name, either in one of the declared fields or in the additional properties map. This code generates getter methods like this:
and setter methods like this:
when
generateBuilders
== true, then a builder method is also generated, with the signaturewhen the
target
==1.6
,if/elseif
blocks are used, instead of String switch statements.This feature can be turned off by setting
includeDynamicAccessors
to false.