3

I develop Java programs in Emacs occasionally, and adding a dependency to a dependency-injected class is a common action.

For example, imagine adding the dependency Qux to the following class:

// import statements here

public class Foo {

  private final Bar bar;
  private final Baz baz;

  public Foo(
      Bar bar,
      Baz baz) {
    this.bar = bar;
    this.baz = baz;
  }

  // methods here
}

The following need to happen in order to inject Qux.

  1. Qux must be imported.
  2. The field private final Qux qux must be added to the class.
  3. The parameter Qux qux must be added to the constructor.
  4. The assignment this.qux = qux must be added to the constructor.

After injecting Qux, the class should look like this:

// import statements here

public class Foo {

  private final Bar bar;
  private final Baz baz;
  private final Qux qux;

  public Foo(
      Bar bar,
      Baz baz,
      Qux qux) {
    this.bar = bar;
    this.baz = baz;
    this.qux = qux;
  }

  // methods here
}

I use emacs-eclim, so the first step is already handled via automatic imports. The remainder, however, I have to do by hand, since I do not see any function in eclim that will automatically inject the dependency for me.

How can I use Emacs to quicken this task? The injection is fully defined by the type Qux; everything else is boilerplate, making it a juicy target for automation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.