-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
@FieldNameConstants Fields classes should use inheritance #2090
Comments
Note that I see you can define your own lomboked@FieldNameConstants
public class FieldNameConstantsBase {
private final String iAmAField;
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public static class Fields {}
}
@FieldNameConstants
public class FieldNameConstantsExample extends FieldNameConstantsBase {
private final int andSoAmI;
public static class Fields extends FieldNameConstantsBase.Fields {}
} |
Disclaimer: Not a lombok team member here.
I find things like
IMHO your workaround is fine. It is some boilerplate and I can imagine Lombok supporting it in a simpler way, but the cost is just one line, while you get all the fields of the superclass. Moreover, while you have to write this single line, you don't have to change it, when you add fields or whatever. Concerning the simpler way, you'd have to specify if the class should be final and if it should inherit from super Fields. Together, it isn't much shorter than your line and it's harder to understand (sure, it's trivial, but your line is much simpler, nonetheless). @rzwitserloot IMHO, it'd be nice to officially support this workaround (assuming, you don't want to support inheritance more), i.e., to add a sentence like "You can also use |
Original implementation of |
@eximius313 we changed it on request. Seems a bit daft to change it on request again, no? What's bad about the current version? Note that the old way |
@brianlenz This is a well-written feature request; thank you! Unfortunately, the problem with the request is that we can't do it without resolution which is probably a bit too heavy for this feature. We could introduce something like Top of the list if we ever tackle resolution in a way that it's not so pricey in performance and support. |
No, because current version is bad. Why didn't you respect the original version that community wanted? It had
Just look how many issues it generated on github and how many people have problem with that. Sometimes "better is the enemy of good". @brianlenz or I described it in details in our issues.
Because it worked just fine before! That's why it didn't have to address this issue explicitly! Current version is much worse! |
@rzwitserloot Would it be possible to do `Bar.Fields._Super.Fields? If I understand the resolution wiki page correctly, then this would generate the right references without need for resolution. Looking at HandleFieldNameConstants.java, I don't think a PR would be too difficult to do here. It might need |
Another workaround could be to allow us to add (or to generate):
buy making the generated Fields class non-final |
Describe the feature
We are upgrading to the 0.18.4 and switching over
@FieldNameConstants
to the new approach. One downside to the newFields
class approach is that you lose references of fields in the superclass hierarchy. Previously, you could use theFIELD_
constants from the current class and all of its superclasses. I didn't need to know or care where the fields were defined, which as a consumer of the class, I shouldn't really need to know anyway. With the newFields
approach, you have to explicitly reference the specific superclass that contains the field in order to reference the properFields
class that the constant lives on.Current Approach
lomboked:
delomboked:
If I'm a consumer of
FieldNameConstantsExample
, I know that it has two fields:iAmAField
andandSoAmI
. I don't care where those fields are defined, nor should I need to know. But, if I want to reference the field name constant foriAmAField
, I have to dig into the class hierarchy to see that it is defined onFieldNameConstantsBase
and therefore must referenceFieldNameConstantsBase.Fields.iAmAField
.Worse, the
FieldNameConstantsBase
could have limited access and may not even be exposed publicly (e.g. protected or package-protected). In this case, I have access to the class properties, but not the field name constants 😩Suggested Approach
To solve this problem, the
Fields
class should not befinal
and subclasses should extend the nearest superclass'Fields
class.delomboked:
With this approach, as a consumer of
FieldNameConstantsExample
, I don't need to know or care about the class structure. I know that theFieldNameConstantsExample
object I am consuming containsiAmAField
, so it references the field name constant throughFieldNameConstantsExample.Fields.iAmAField
.Describe the target audience
Anybody using object-oriented design w/ inheritance +
@FieldNameConstants
😉Additional context
Enum
-based approach is incompatible with this design due to the fact thatEnum
s do not support inheritance. I personally don't see the need/value to useEnum
s for these types of constants, and I think this is a much bigger problem than supportingEnum
s is. As a result, theEnum
approach would probably have to be abandoned in order to solve this problem.@FieldNameConstants
is defined on superclasses or not. The proposed solution here is definitely the ideal. If it's unfeasible, a couple of alternative solutions might be:@SuperBuilder
-like approach where all superclasses must have@FieldNameConstants
if the current class wants to use it. This could make some good sense when you control the entire class hierarchy, but it is limiting if you are using third-party libraries that you don't control.Fields
class, define all constants from not only the current class, but also all superclasses. This would result in a lot of duplication in theFields
classes, but it at least ensures that you can access all of the field name constants for all fields of the class. It would also allow you to use@FieldNameConstants
on classes that extend third-party libraries (and would include those fields).This is my first interaction with the Lombok team, so just wanted to let you know I appreciate all of your efforts on this great library 👍
The text was updated successfully, but these errors were encountered: