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

Collection fields are unnecessarily initialized to "null" #1346

Closed
saberya opened this issue Dec 2, 2021 · 7 comments
Closed

Collection fields are unnecessarily initialized to "null" #1346

saberya opened this issue Dec 2, 2021 · 7 comments
Milestone

Comments

@saberya
Copy link

saberya commented Dec 2, 2021

GenerationConfig has a option of "isInitializeCollections",
if it is true,
the result is

private List<Foo> fooList = new ArrayList<>();

if it is false
the result is

private List<Foo> fooList = null;

How can i get the result

private List<Foo> fooList;

@unkish
Copy link
Collaborator

unkish commented Dec 2, 2021

Hi

private List<Foo> fooList; is equally same as private List<Foo> fooList = null;

Please see JLS 4.12.5. Initial Values of Variables

For all reference types (§4.3), the default value is null.

@saberya
Copy link
Author

saberya commented Dec 2, 2021

@unkish
but

private List<Foo> fooList = null;

is not what i expect.
Beacuse the java Class with this kind of expression is not a good habit to definition a field.

@unkish
Copy link
Collaborator

unkish commented Dec 2, 2021

I'm sorry but I don't understand why is that a problem or what kind of problem are you trying to solve

@saberya
Copy link
Author

saberya commented Dec 2, 2021

@unkish
I use this tool to generate original pojo in a project before developing,

private List<Foo> fooList = null;

this kind of code is not friendly

@unkish
Copy link
Collaborator

unkish commented Dec 3, 2021

At present it's not possible to get the described result without either:

  • making changes to generator
  • overriding DefaultRule with custom one

@joelittlejohn
Copy link
Owner

Not sure when this began happening, but I agree it would be nice to remove it. It's annoying noise.

Feel free to submit a PR for this.

@joelittlejohn joelittlejohn changed the title How can i generate collection fileld without initialized value and "null" Collection fields are unnecessarily initialized to "null" Dec 3, 2021
@joelittlejohn joelittlejohn modified the milestones: 1.1.3, 1.1.2 Dec 6, 2021
@joelittlejohn
Copy link
Owner

Closed by #1347

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

3 participants