-
-
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] @EnumNameConstants #2731
Comments
You can use @FieldNameConstants
public enum Permissions {
@FieldNameConstants.Include READ_USERS,
@FieldNameConstants.Include WRITE_USERS,
;
} I don't know if this is intended, I was acutally supprised that it seems to be okay to add annotations to enum values. In general I think that it would be way better if you could define your own annotation that accepts an enum attribute ( |
@Rawi01 I just tried that with the latest Lombok version ( |
I don't think that javac allows |
This is one of the important feature for our projects for maintainability. As for all enums we do create which is requested by requestor. I would request lombok team to build this feature. |
@daniel-shuy I just tried it again using 1.18.20 and it works in Eclipse and in |
Interesting, looks like its an issue with the
Sure, whatever works. Maybe
I don't know about @ravidesai47's use case, but I can add one more if it might help increase the priority of this request 😝 @JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
@JsonSubTypes( {
@JsonSubTypes.Type(value = Automobile.class),
@JsonSubTypes.Type(value = Plane.class),
@JsonSubTypes.Type(value = Ship.class),
})
@Data
abstract class Vehicle {
private Double latitude;
private Double longitude;
}
enum VehicleType {
AUTOMOBILE,
PLANE,
SHIP,
;
}
@JsonTypeName(VehicleType.AUTOMOBILE) // does not compile!
class Automobile extends Vehicle {
private Integer wheelCount;
} This will cause the Why is there a need for an enum? There is currently no way to get all subclasses of |
My use-case is related to jackson json polymorphism. We have lot of json polymorphed objects which has a polymorphism based on Enum which requires this feature to generated auto-generated name fields. We create enum name text fields in all such enums to use them in @JsonSubTypes annotation. It becomes maintenance headache as each time new enum is added, removed or modified we need to change the respective TEXT field. Enum value has a significance in object hence we can't replace name based strategy to any other strategy. If client is written in Angular, react it might not understand java types either. Example: @JsonTypeInfo(use = Id.NAME, include = As.EXISTING_PROPERTY, property = "vehicleType", visible = true)
@JsonSubTypes( {
@JsonSubTypes.Type(value = Automobile.class, name = AUTOMOBILE_TEXT),
@JsonSubTypes.Type(value = Plane.class, name = PLANE_TEXT),
@JsonSubTypes.Type(value = Ship.class, name = SHIP_TEXT),
})
@Data
abstract class Vehicle {
private VehicleType vehicleType;
private Double latitude;
private Double longitude;
}
enum VehicleType {
AUTOMOBILE,
PLANE,
SHIP,
;
public static final String AUTOMOBILE_TEXT = "AUTOMOBILE";
public static final String PLANE_TEXT = "PLANE";
public static final String SHIP_TEXT = "SHIP";
} Hope this helps in increasing priority of this feature. I am sure many more developers and organizations must be interested in this feature as it adds a lot of value to Json Polymorphed code for sure. I am not sure about other use-cases. As I have came across the mentioned use-case only till now. This use-case is valid across every organization I have worked for. |
@ravidesai47 oh wow, I'm surprised we have the same use case 😄 |
@daniel-shuy I think this is very common use-case in all complex systems. |
@daniel-shuy I think it needs to be discussed in https://groups.google.com/g/project-lombok forum. Someone from team project lombok would be able to help there increasing priority of this feature. |
+1 I have exactly the same use case with jackson json polymorphism (and some on others annotations). |
+1 This feature is needed to reference particular enum value in OpenApi descriptions as they're also annotations-based Strings with same limitations @Schema(title = "Your enum property",
description = """
Yada-yada. \s
The property appeared on Jan 1, 2023. \s
For the legacy entities, special value \s
""" + YourEnumType.LEGACY.name() + " is returned",
required = true,
example = YourEnumType.USEFUL_EXAMPLE_VALUE.name()) // NB! also useful!
@NotNull
YourEnumType yourEnumType; |
+1 we have a very similar use case. Does this feature have a shot? |
+1 |
1 similar comment
+1 |
The solution, @Rawi01 suggested can be used in IntelliJ if you set @FieldNameConstants(onlyExplicitlyIncluded = true)
public enum Role{
@FieldNameConstants.Include ADMIN,
@FieldNameConstants.Include USER
}
private String foo(){
return Role.Fields.USER;
} |
It works! 🤯 |
Describe the feature
Currently, there are 2 main ways to define String constants in Java:
Enums can be enumerated (using
Enum#values()
,EnumSet
, etc), and provides better typesafety (enum method parameters, exhaustive switch case with Java 13 Enhanced Switch), but the main issue is that it cannot be used as a String constant in annotation parameters (becauseEnum#name()
is not a compile time constant).WIth static final constants, for Android, some of the typesafety can be supplemented with Android's TypeDef annotations (eg. @StringDef), but for non-Android environments, there is no alternative. Manually definiting a Collection to enumerate static final constants is also error prone. A
HashSet
/HashMap
is also slower than anEnumSet
/EnumMap
.I suggest to create an annotation called
@EnumNameConstants
(inspired by @FieldNameConstants), that when annotated on an enum, will generate an inner type with string constants of the name of each enum value.eg.
should generate
This will provide all the advantages of using enums, while removing its greatest limitation.
Describe the target audience
Example use case:
Additional context
The text was updated successfully, but these errors were encountered: