Skip to content
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

HiddenField checkstyle error when generating builder methods #1190

Closed
samkeshmiri opened this issue Jan 8, 2021 · 1 comment
Closed

HiddenField checkstyle error when generating builder methods #1190

samkeshmiri opened this issue Jan 8, 2021 · 1 comment

Comments

@samkeshmiri
Copy link

Hi, first time writing an issue, so bear with me as I try to explain it...

Let's start with the error:

[ERROR] ../generated/Item.java:119:55: 'name' hides a field. [HiddenField]

When I tried to build my project using maven, the eclipse checkstyle plugin flagged every builder method with a HiddenField error. I understand this error is cause by 'variable shadowing'. It doesn't cause any compilation issues. It is also possible to add a rule in the checkstyle for setters to ignore parameter names that are identical to instance variables, so long as the method name begins with 'set'. For builder methods however, it is not possible as they begin with 'with':

private String name;

public Item withName(final String name) { this.name = name; return this; }

The checkstyle docs say it is possible to add a rule to ignore the formatting of parameters but not method names. Which I tried to use initially but of course that entails adding every parameter as a rule.

I was able to make my project build by removing every HiddenField property in the checkstyle.xml file.
Alternatively, I added SuppressWarnings filter to the checkstyle.xml and added the annotation to each of the classes that had HiddenField errors like so:

@SuppressWarnings({"HiddenField"})

I think it would be ideal if the builder method parameters were prefixed with 'p' or 'new' so that checkstyle files don't have to be tampered with and it's possible to stick with the default coding conventions.

e.g.:
public Item withName(final String newName) { this.name = newName; return this; }

@joelittlejohn
Copy link
Owner

joelittlejohn commented Jan 11, 2021

We won't update the code we're generating based on rules like this I'm afraid. I don't think the alternative is easier to read (it's best if those withXXX methods are similar to over accessors) but also there are other checkstyle warnings that we will no doubt have.

The solution to this is to reinstate the @Generated annotation (#835), because this indicates to static analysis libraries to ignore these classes when examining the code. for now I suggest you generate these source files to a src folder, or package, you can tell checkstyle to ignore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants