Lombok: Generate fieldnames as constants

Created on 6 Jul 2016  路  7Comments  路  Source: projectlombok/lombok

Hi! Thank you for this great framework!

I have a question if I can hook into the generation process in order to generate a constant for every field name.

What I would like to do is the following:

@Getter
@Setter
@Constants
public class StuffDTO implements Serializable {
    private String name;
}

generates

public class StuffDTO {

    public static final String PROPERTY_NAME = "name";

    private String name;

    @SuppressWarnings("all")
    @javax.annotation.Generated("lombok")
    public String getName() {
        return this.name;
    }

    @SuppressWarnings("all")
    @javax.annotation.Generated("lombok")
    public void setName(final String name) {
        this.name = name;
    }

}

Kind regards

All 7 comments

+1
It seems like here https://github.com/rzwitserloot/lombok/pull/46 we even have a pull request for this feature.

I think that this feature is very important.
Few popular use cases would be:
1) You have localization messages and want to bind field names to localization keys
2) You have bindings DTO<->GUI form and want to bind fields to IDs in form

In this cases I always end up with something like this:

public class LoginForm {
  public static final String FIELD_EMAIL = "email";
  public static final String FIELD_PASSWORD = "password";

  private String email;
  private String password;
}

Having this feature would be a relief...

yes this feature is good to have a generic code with no regression on field renaming

Due to PR #46, something very similar to this (@FieldNameConstants) was already merged. So, I think that this should be closed.

@victorwss is there a plan to publish lombok 2.0.0 to maven/jcenter? I'm not seeing it in any repo.

@liemle3893 The last published version is 1.16.20 from January. However, lombok evolved significantly since then, so I'm hoping that @rspilker and @rzwitserloot publishes a new version soon.

In the mean time, get the lombok edge to enjoy the new features and bugfixes, or even build it yourself from the master branch.

To use the edge version (or a version built by yourself from the source files) with maven or gradle, you may need to juggle with some settings to use a locally installed JAR instead of something got from a repository.

Lombok 1.16.22 introduced @FieldNameConstants, so this issue can be closed.
However I suggest minor improvement: https://github.com/rzwitserloot/lombok/issues/1706

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lombokissues picture lombokissues  路  3Comments

x9nico picture x9nico  路  3Comments

rn4n picture rn4n  路  3Comments

Bryksin picture Bryksin  路  3Comments

rspilker picture rspilker  路  3Comments