-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE] Provide option to use getters for @With
er
#3128
Comments
I get the need. I've been thinking about this one for a few days. There are 2 big problems with what you want:
Your example doesn't make sense to meJust looking at your example without further context, I think what you want is just flat out wrong, and wrong in a nasty way (that is: A way that is really hard to test or observe, but is wrong and will therefore eventually lead to an expensive bug, as in, hits production, costs hours of dev time, literally more expensive than fixing 100 more normal bugs or so). The behaviour of your These are all 'values'. However, in your code snippet, if I use The point isn't: This code is buggy. Who knows - it's clearly an example, not real code written for real dollars or eyeballs. The point is instead: It is equally, if not more, imaginable that the reverse behaviour is desired: That I have some POJO where I want that Hence, I can only interpret this request as: "Whilst lomboks default choice in this regard is usually irrelevant, and in the rare cases where it is relevant, the current choice of doing direct field access is almost always correct, but in the cornercase of the cornercase, lombok's choice is wrong (it IS functionallity different, and I want to invoke the getter)". I want a way to tell lombok explicitly that it needs to make the other choice. If your request instead is: Can this work exactly like The choice is fundamentally differentIn particular for this example it feels like the 'choice' of using getter or direct field access needs to be made for every property. I can easily imagine a scenario where you want e.g. One trivial way to solve that problem is to make the 'argument' to the annotation parameter Feature proposalIntroduce annotation
Your snippetTo get the desired behaviour, you'd write: @With
@Value
public class Pojo {
Long id;
@With.CallGetter Long timestamp;
public long getTimestamp() {
return timestamp != null ? timestamp : System.currentTimeMillis();
}
} I'm introducing a new tag "discuss", as I'm not sure this is worth adding just yet. But if the answer is yes, the above proposal seems like the right way to do it. Feedback welcome! |
Describe the feature
There is a case where frameworks like, for example, Micronaut, trying using wither methods to create a copy of near-immutable instance to, for example, set DB assigned value (like id) to property. Unfortunately in case of "lazy" getter ("lazy" getter in common sense, not relevant to Lombok's feature), which ever only executed once and subsequent invocation simply check if field non-null. So, initially, backing field is null until getter first called.
Unlike
@EqualsAndHashCode
and@ToString
-@With
uses fields, which makes "lazy" getter not being executed, so copied state is incomplete from what is supposed to be.Hope/seems it's easy enough to create maybe config an option
@EqualsAndHashCode
and@ToString
alike to force using getters.Describe the target audience
Anyone with something like this in pojo
The text was updated successfully, but these errors were encountered: