Spring Tool Suite
  1. Spring Tool Suite
  2. STS-1529

Provide quickfix to quickly create @Autowired constructor for class with final fields

    Details

      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.

        Activity

        Hide
        Kaitlin Sherwood (c) added a comment -

        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;
          }
        }
        
        Show
        Kaitlin Sherwood (c) added a comment - 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; } }
        Hide
        Oliver Gierke added a comment -

        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.

        Show
        Oliver Gierke added a comment - 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.
        Hide
        Kaitlin Sherwood (c) added a comment -

        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.

        Show
        Kaitlin Sherwood (c) added a comment - 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.
        Hide
        Oliver Gierke added a comment -

        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.

        Show
        Oliver Gierke added a comment - 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.
        Hide
        Kaitlin Sherwood (c) added a comment -

        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?

        Show
        Kaitlin Sherwood (c) added a comment - 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?
        Hide
        Kaitlin Sherwood (c) added a comment -

        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.

        Show
        Kaitlin Sherwood (c) added a comment - 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.
        Hide
        Oliver Gierke added a comment -

        Cool thanks. Assert is not that crucial.

        Show
        Oliver Gierke added a comment - Cool thanks. Assert is not that crucial.
        Hide
        Kaitlin Sherwood (c) added a comment -

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

        Show
        Kaitlin Sherwood (c) added a comment - Opened a bug STS-2616 to not put the @Autowired if it is a non-Spring project.

          People

          • Assignee:
            Kaitlin Sherwood (c)
            Reporter:
            Oliver Gierke
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              First Response Date: