Search code examples
javagwtdependency-injectionguicegwt-gin

GIN injection of field in abstract class


I'm working in some GWT application in which I have a hierarchy where I have an abstract presenter with some common functionality of derived classes. Something like:

public abstract class MyAbstractPresenter<T extends MyAbstractPresenter.CustomDisplay> extends Presenter<T>
{
public interface CustomDisplay extends View
{
  //some methods
}

//I want to inject this element
@Inject
private CustomObject myObj;

public MyAbstractPresenter(T display)
{
   super(display);
}
}

All the subclasses get injected properly. However, I want to be able to inject that particular field without having it to add it in the constructor of the subclasses. I tried to do field injection as you see , but it doesn't work as it is the subclasses the one that get injected.

Is there a proper way to achieve this Injection without letting the subclasses know about the existence of the field?


Solution

  • Apparently, as for the moment, there is no support for this type of behavior in GIN. A workaround would be to inject the required field in the concrete classes constructors even when they don't need it. Something like:

     public abstract class MyAbstractPresenter<T extends MyAbstractPresenter.CustomDisplay> extends Presenter<T>
     {
       public interface CustomDisplay extends View
       {
         //some methods
       }
    
       //I wanted to inject this element
       private final CustomObject myObj;
    
       public MyAbstractPresenter(T display, CustomObject obj)
       {
          super(display);
          myObj = obj;
       }
    }
    

    Then in any class that extends this abstract implementation, I would have to pass it on construction.

    public abstract class MyConcretePresenter extends MyAbstractPresenter<MyConcretePresenter.CustomDisplay>
    {
      public interface CustomDisplay extends MyAbstractPresenter.CustomDisplay
      {
         //some methods
      }
    
     @Inject  //it would get injected here instead.
     public MyConcretePresenter(CustomDisplay display, CustomObject obj)
     {
         super(display, obj);
     }
    }