3

I want to have an abstract Java class like this:

abstract class AbstractFoo<F extends AbstractFoo<F, L>, L extends FooListener<F, L>> {
  private final Class<L> listenerClass;
  protected AbstractFoo(Class<L> listenerClass) {
    this.listenerClass = listenerClass;
  }

  interface FooListener<F extends AbstractFoo<F, L>, L extends FooListener<F, L>> {
    void callback(F foo);
  }

  // Bar might implement FooListener, but I don't control it,
  // so I have no guarantee
  public void externalMethod(Bar bar) {
    if (listenerClass.isInstance(bar)) {
      L listener = listenerClass.cast(bar);

      listener.callback(this); // does not compile
    }
  }
}

listener.callback(this); doesn't compile because there's no guarantee that this is the same type as F. Is it possible to somehow guarantee that F is a supertype of this?

Heath Borders
  • 30,998
  • 16
  • 147
  • 256
  • There is no guarantee your instance of `AbstractFoo` is an instance of `Foo`. We also don't know how `Bar` is defined. You might be able to do something if `AbstractFoo<...> implements Foo` so the type system knows that `this` can satisfy the requirement that the first argument is of type `F`. – Erwin Bolwidt May 13 '19 at 04:07
  • Sorry, I fixed my generics declaration. Also, I don't have guarantees about how `Bar` is defined either, so I can't make any assumptions that way. – Heath Borders May 13 '19 at 13:17
  • You can try `> void callback(T foo);` – Maurice Perry May 13 '19 at 13:28

1 Answers1

1

What you are trying to do is emulate the SELF type in Java using generics. See this link or a different site for some means to do so. However, there is no way to enforce that F (or the SELF type) is actually the same type, for example (see type parameter of ConcreteFoo2):

static class Bar implements FooListener<ConcreteFoo, Bar> {

    @Override
    public void callback(final ConcreteFoo foo) {
        // TODO Auto-generated method stub

    }

}

static class ConcreteFoo2 extends AbstractFoo<ConcreteFoo, Bar> {

    protected ConcreteFoo2(final Class<Bar> listenerClass) {
        super(listenerClass);
    }

}

static class ConcreteFoo extends AbstractFoo<ConcreteFoo, Bar> {

    protected ConcreteFoo(final Class<Bar> listenerClass) {
        super(listenerClass);
    }

}

Instead of further going this way, I would first think about the design choices made that led you here:

  • Do the listeners really need to know the concrete class?

  • Does the AbstractFoo really need to know the concrete implementation of the listener class?

Maybe fewer type parameters are actually the solution, relying on the interfaces alone.


EDIT: One possible solution, if you don't want to cast (F) this would be to provide an abstract method protected abstract F getSelf(); that the concrete implementations implement by returning this.

See this simplified code for example:

static final class Bar implements FooListener<ConcreteFoo> {

    @Override
    public void callback(final ConcreteFoo foo) {
        // TODO Auto-generated method stub

    }

}

static final class ConcreteFoo extends AbstractFoo<ConcreteFoo> {

    protected ConcreteFoo(final Class<? extends FooListener<ConcreteFoo>> listenerClass) {
        super(listenerClass);
    }

    @Override
    protected ConcreteFoo getSelf() {
        return this;
    }

}

static abstract interface FooListener<FOO extends AbstractFoo<FOO>> {

    void callback(FOO abstractFoo);
}

static abstract class AbstractFoo<SELF extends AbstractFoo<SELF>> {

    private final Class<? extends FooListener<SELF>> listenerClass;

    protected AbstractFoo(final Class<? extends FooListener<SELF>> listenerClass) {
        this.listenerClass = listenerClass;
    }

    protected abstract SELF getSelf();

    // Bar might implement FooListener, but I don't control it,
    // so I have no guarantee
    public void externalMethod(final Bar bar) {
        if (listenerClass.isInstance(bar)) {
            final FooListener<SELF> listener = listenerClass.cast(bar);

            listener.callback(getSelf()); // compiles
        }
    }
}
sfiss
  • 2,119
  • 13
  • 19