[STS1529] PROVIDE QUICKFIX TO QUICKLY CREATE AUTOWIRED CONSTRUCTOR FOR

[STS1529] PROVIDE QUICKFIX TO QUICKLY CREATE AUTOWIRED CONSTRUCTOR FOR






[#STS-1529] Provide quickfix to quickly create @Autowired constructor for class with final fields

[STS-1529] Provide quickfix to quickly create @Autowired constructor for class with final fields Created: 30/Jan/11  Updated: 03/May/12  Resolved: 25/Apr/12

Status:

Resolved

Project:

Spring Tool Suite

Component/s:

SUPPORT

Affects Version/s:

2.6.0.M1

Fix Version/s:

3.0.0.M1


Type:

Improvement

Priority:

Minor

Reporter:

Oliver Gierke

Assignee:

Kaitlin Duck Sherwood

Resolution:

Complete

Votes:

0

Labels:

Y5M8, autowired, quickfix

Remaining Estimate:

Not Specified

Time Spent:

Not Specified

Original Estimate:

Not Specified



 Description 

 

The latest M1 release for STS 2.6.0 contains a new feature of adding @Autowired to a constructor using the quick fix feature. Would be cool, if you could take that even one step further by offering an option to generate a constructor for all final fields of a class and annotate with @Autowired in one go:

public class MyService {

  private final MyFooRepo fooRepo;
  private final MyBarRepo barRepo;

}

Currently this requires to let Eclipse generate the constructor for the fields and triggering the quick fix newly introduced.



 Comments 

 

Comment by Kaitlin Duck Sherwood [ 19/Apr/12 ]

To be clear, am I correct that you are wanting a quick fix to transform the above into the below?

public class MyService {

  private final MyFooRepo fooRepo;
  private final MyBarRepo barRepo;

  @Autowired
  MyService(MyFooRepo autogenFooRepo, MyBarRepo autogenBarRepo) {
    this.fooRepo = autogenFooRepo;
    this.barRepo = autogenBarRepo;
  }
}

Comment by Oliver Gierke [ 19/Apr/12 ]

Exactly, (with the minor change that the constructor shall be public) is there a chance we can have the generated code inside a template? I usually add an Assert.notNull(parameter); for each of the parameters but fear that might be a bit too much for people wanting to avoid the dependency to Spring API.

Comment by Kaitlin Duck Sherwood [ 19/Apr/12 ]

Hmmm. I don't see how you could profitably use templates: I don't see any way to expand a list in a template, which is what you would need to do in order to do an assignment and an Assert.notNull(parameter) for each of the parameters.

Comment by Oliver Gierke [ 19/Apr/12 ]

That's what I was thinking as well just thinking I mentioned the idea as you're probably much deeper into the internals. Anyway, just go ahead with this one (including the public constructor), I am perfectly fine with this solution.

Comment by Kaitlin Duck Sherwood [ 20/Apr/12 ]

Are you aware that there is an existing way to generate constructors from fields in Eclipse?
Right-click on a Type > Source > Generate Constructor using Fields...
It takes a few more keystrokes if you want to exclude non-final fields, and doesn't put the @Autowired in. Is that significantly different from what you want?

When you were mentioning the Spring API dependency, were you concerned about the Assert?

Comment by Kaitlin Duck Sherwood [ 25/Apr/12 ]

Okay, I have checked in. The quickfix generates the @Autowired constructor with the final fields but does not do the null check. If you really want the null check, please open another bug for that.

Comment by Oliver Gierke [ 26/Apr/12 ]

Cool thanks. Assert is not that crucial.

Comment by Kaitlin Duck Sherwood [ 03/May/12 ]

Opened a bug STS-2616 to not put the @Autowired if it is a non-Spring project.

Generated at Wed Oct 06 06:48:55 UTC 2021 using Jira 8.5.11#805011-sha1:102a195bf8687d9dd541cc7f5503132d0561ea57.





Tags: constructor, autowired, create, [sts1529], provide, quickfix, quickly