61

Suppose I'm trying to write a function to return an instance of the current type. Is there a way to make T refer to the exact subtype (so T should refer to B in class B)?

class A {
    <T extends A> foo();
}

class B extends A {
    @Override
    T foo();
}
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
jz87
  • 9,199
  • 10
  • 37
  • 42
  • 2
    I think the answer by @PaulBellora is excellent. To summarize it: there is a solution but it's fragile; it's better to use this solution when you write and control the whole tree of classes/subclasses. A careless programmer that extends the abstract class could produce a subclass that doesn't make sense or is outright wrong (see **EvilLeafClass** below). – The Impaler Apr 16 '18 at 15:36
  • Better to altogether avoid the confusion caused by recursive generic types and use [the Self type](http://manifold.systems/docs.html#the-self-type) via **Manifold**. – Scott Mar 08 '19 at 23:04

6 Answers6

84

To build on StriplingWarrior's answer, I think the following pattern would be necessary (this is a recipe for a hierarchical fluent builder API).

SOLUTION

First, a base abstract class (or interface) that lays out the contract for returning the runtime type of an instance extending the class:

/**
 * @param <SELF> The runtime type of the implementor.
 */
abstract class SelfTyped<SELF extends SelfTyped<SELF>> {

   /**
    * @return This instance.
    */
   abstract SELF self();
}

All intermediate extending classes must be abstract and maintain the recursive type parameter SELF:

public abstract class MyBaseClass<SELF extends MyBaseClass<SELF>>
extends SelfTyped<SELF> {

    MyBaseClass() { }

    public SELF baseMethod() {

        //logic

        return self();
    }
}

Further derived classes can follow in the same manner. But, none of these classes can be used directly as types of variables without resorting to rawtypes or wildcards (which defeats the purpose of the pattern). For example (if MyClass wasn't abstract):

//wrong: raw type warning
MyBaseClass mbc = new MyBaseClass().baseMethod();

//wrong: type argument is not within the bounds of SELF
MyBaseClass<MyBaseClass> mbc2 = new MyBaseClass<MyBaseClass>().baseMethod();

//wrong: no way to correctly declare the type, as its parameter is recursive!
MyBaseClass<MyBaseClass<MyBaseClass>> mbc3 =
        new MyBaseClass<MyBaseClass<MyBaseClass>>().baseMethod();

This is the reason I refer to these classes as "intermediate", and it's why they should all be marked abstract. In order to close the loop and make use of the pattern, "leaf" classes are necessary, which resolve the inherited type parameter SELF with its own type and implement self(). They should also be marked final to avoid breaking the contract:

public final class MyLeafClass extends MyBaseClass<MyLeafClass> {

    @Override
    MyLeafClass self() {
        return this;
    }

    public MyLeafClass leafMethod() {

        //logic

        return self(); //could also just return this
    }
}

Such classes make the pattern usable:

MyLeafClass mlc = new MyLeafClass().baseMethod().leafMethod();
AnotherLeafClass alc = new AnotherLeafClass().baseMethod().anotherLeafMethod();

The value here being that method calls can be chained up and down the class hierarchy while keeping the same specific return type.


DISCLAIMER

The above is an implementation of the curiously recurring template pattern in Java. This pattern is not inherently safe and should be reserved for the inner workings of one's internal API only. The reason is that there is no guarantee the type parameter SELF in the above examples will actually be resolved to the correct runtime type. For example:

public final class EvilLeafClass extends MyBaseClass<AnotherLeafClass> {

    @Override
    AnotherLeafClass self() {
        return getSomeOtherInstanceFromWhoKnowsWhere();
    }
}

This example exposes two holes in the pattern:

  1. EvilLeafClass can "lie" and substitute any other type extending MyBaseClass for SELF.
  2. Independent of that, there's no guarantee self() will actually return this, which may or may not be an issue, depending on the use of state in the base logic.

For these reasons, this pattern has great potential to be misused or abused. To prevent that, allow none of the classes involved to be publicly extended - notice my use of the package-private constructor in MyBaseClass, which replaces the implicit public constructor:

MyBaseClass() { }

If possible, keep self() package-private too, so it doesn't add noise and confusion to the public API. Unfortunately this is only possible if SelfTyped is an abstract class, since interface methods are implicitly public.

As zhong.j.yu points out in the comments, the bound on SELF might simply be removed, since it ultimately fails to ensure the "self type":

abstract class SelfTyped<SELF> {

   abstract SELF self();
}

Yu advises to rely only on the contract, and avoid any confusion or false sense of security that comes from the unintuitive recursive bound. Personally, I prefer to leave the bound since SELF extends SelfTyped<SELF> represents the closest possible expression of the self type in Java. But Yu's opinion definitely lines up with the precedent set by Comparable.


CONCLUSION

This is a worthy pattern that allows for fluent and expressive calls to your builder API. I've used it a handful of times in serious work, most notably to write a custom query builder framework, which allowed call sites like this:

List<Foo> foos = QueryBuilder.make(context, Foo.class)
    .where()
        .equals(DBPaths.from_Foo().to_FooParent().endAt_FooParentId(), parentId)
        .or()
            .lessThanOrEqual(DBPaths.from_Foo().endAt_StartDate(), now)
            .isNull(DBPaths.from_Foo().endAt_PublishedDate())
            .or()
                .greaterThan(DBPaths.from_Foo().endAt_EndDate(), now)
            .endOr()
            .or()
                .isNull(DBPaths.from_Foo().endAt_EndDate())
            .endOr()
        .endOr()
        .or()
            .lessThanOrEqual(DBPaths.from_Foo().endAt_EndDate(), now)
            .isNull(DBPaths.from_Foo().endAt_ExpiredDate())
        .endOr()
    .endWhere()
    .havingEvery()
        .equals(DBPaths.from_Foo().to_FooChild().endAt_FooChildId(), childId)
    .endHaving()
    .orderBy(DBPaths.from_Foo().endAt_ExpiredDate(), true)
    .limit(50)
    .offset(5)
    .getResults();

The key point being that QueryBuilder wasn't just a flat implementation, but the "leaf" extending from a complex hierarchy of builder classes. The same pattern was used for the helpers like Where, Having, Or, etc. all of which needed to share significant code.

However, you shouldn't lose sight of the fact that all this only amounts to syntactic sugar in the end. Some experienced programmers take a hard stance against the CRT pattern, or at least are skeptical of the its benefits weighed against the added complexity. Their concerns are legitimate.

Bottom-line, take a hard look at whether it's really necessary before implementing it - and if you do, don't make it publicly extendable.

Community
  • 1
  • 1
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
  • +1. This can be a good solution if you know that people extending MyClass will always follow the rules you've established. There is no way to enforce this using compilation rules, though. – StriplingWarrior Sep 08 '11 at 22:51
  • 3
    @StriplingWarrior Agreed - it's fit more for the internal workings of one's API, not to be publicly extendable. I actually used this construct recently for a fluent builder pattern. For example one could call `new MyFinalClass().foo().bar()` where `foo()` is declared in a superclass and `bar()` is defined in `MyFinalClass`. Only the `final` concrete classes are exposed outside the package so my teammembers can't mess with `self()` etc. I hadn't realized until now that intermediate classes maintaining the type parameter couldn't be used directly though. – Paul Bellora Sep 08 '11 at 23:24
  • That's a comprehensive answer. Now if we just remove the bound of SELF, it looks simpler and won't confuse new guys. The bound does not seem to be very useful anyway. – ZhongYu Apr 15 '13 at 23:06
  • @zhong.j.yu The bound is necessary for a hierarchical fluent builder pattern, since builder methods (e.g. `baseMethod` in my walkthrough or `lessThanOrEqual` in my example) must return `SELF`. – Paul Bellora Apr 15 '13 at 23:55
  • 1
    how do you mean? without the bound for SELF, everything still works fine. – ZhongYu Apr 16 '13 at 00:05
  • @zhong.j.yu You're right actually - thanks for pointing that out. Now that you helped jog my memory, the bound only became necessary for my use of *nested* builders. In my bottom snippet this would be for example handing off to an `Or` object with `or()` and then returning to the outer builder with `endOr()`. In a nutshell, the bound was needed so that the different builders could talk to each other. But that's an extended aspect that I didn't explore in the answer. Outside of that I think whether to have the bound comes down preference. I'll update the answer to that affect when I get time. – Paul Bellora Apr 16 '13 at 00:32
  • This works, with caveats, including not having concrete intermediate classes, requiring generics, preventing the top level class from extending an existing (concrete) class, exposure to the EvilLeafClass possibility, etc. I actually like my solution better, though it requires a call before each method invocation. – ggb667 Nov 07 '13 at 15:33
  • @GGB667 Yep, there are many trade-offs for the fluent prettiness, as I hope the answer articulates - it's a tough sell. – Paul Bellora Nov 07 '13 at 16:55
  • `EvilLeafClass can substitute any type for SELF`: Sounds like you're talking about a security risk but in practice I think this pattern just opens you up to non-malicious programming errors. From a security perspective, an attacker can execute malicious code before returning the wrong type so the ability to do the latter doesn't seem to matter much. Am I right? – Gili Jul 27 '16 at 18:16
  • @Gili Sorry for the late reply. I didn't mean unsafe in the sense of a security risk, just programming errors. However, bugs caused by heap pollution can be hard to diagnose, so type unsafe code should still be taken seriously. I list some examples at the bottom of [this answer](http://stackoverflow.com/a/14469627/697449). – Paul Bellora Dec 29 '16 at 18:22
  • 2
    By already providing a (final) implementation for `self()` in the `SelfTyped` class there is no need to have its trivial implementation in each sub class. `@SuppressWarnings("unchecked") protected final SELF self() { return (SELF) this; }` This way `EvilLeafClass` cannot return arbitrary objects. Instead, if it doesn't follow the contract, a `ClassCastException` will be thrown. – neXus Mar 12 '19 at 17:50
  • 1
    @PaulBellora One thing I've just noticed is that `MyBaseClass> mbc2 = new MyBaseClass<>().baseMethod();` does work (compiles and runs successfully) without requiring MyBaseClass to be abstract. At least in my case, this is sufficient since the only advantage of the type is to be able to chain all the available methods properly. – Octavian Theodor May 06 '19 at 11:54
10

You should be able to do this using the recursive generic definition style that Java uses for enums:

class A<T extends A<T>> {
    T foo();
}
class B extends A<B> {
    @Override
    B foo();
}
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
4

I may not fully understood the question, but isn't it enough to just do this (notice casting to T):

   private static class BodyBuilder<T extends BodyBuilder> {

        private final int height;
        private final String skinColor;
        //default fields
        private float bodyFat = 15;
        private int weight = 60;

        public BodyBuilder(int height, String color) {
            this.height = height;
            this.skinColor = color;
        }

        public T setBodyFat(float bodyFat) {
            this.bodyFat = bodyFat;
            return (T) this;
        }

        public T setWeight(int weight) {
            this.weight = weight;
            return (T) this;
        }

        public Body build() {
            Body body = new Body();
            body.height = height;
            body.skinColor = skinColor;
            body.bodyFat = bodyFat;
            body.weight = weight;
            return body;
        }
    }

then subclasses won't have to use overriding or covariance of types to make mother class methods return reference to them...

    public class PersonBodyBuilder extends BodyBuilder<PersonBodyBuilder> {

        public PersonBodyBuilder(int height, String color) {
            super(height, color);
        }

    }
Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166
3

Just write:

class A {
    A foo() { ... }
}

class B extends A {
    @Override
    B foo() { ... }
}

assuming you're using Java 1.5+ (covariant return types).

dm3
  • 2,038
  • 2
  • 17
  • 20
  • I'm not sure that OP wants A's `foo` to return type `A`...just something that extends it. It looks like only in the specific case of `B` does he want `foo` to return a `B`. – Michael McGowan Sep 08 '11 at 21:35
  • 1
    @Michael: This may well suit the OP's needs. If he calls `new A().foo();`, he should have a strongly-typed `A`. If he calls `new B().foo();`, he should have a strongly-typed `B`. The only thing that's lacking is that the definition of `A` does not enforce that any `B` extending it *must* return a `B` from `foo`. – StriplingWarrior Sep 08 '11 at 21:43
  • Well, in case OP wants to _return an instance of the current type_ - that's what he should be doing. Otherwise he should use the recursive generic type definition (must.. forget.. the pain...). – dm3 Sep 08 '11 at 21:44
  • but this requires overriding methods in subclass to change return type. – Tomasz Mularczyk Jul 03 '16 at 12:07
3

If you want something akin to Scala's

trait T {
  def foo() : this.type
}

then no, this is not possible in Java. You should also note that there is not much you can return from a similarly typed function in Scala, apart from this.

jpalecek
  • 47,058
  • 7
  • 102
  • 144
1

I found a way do this, it's sort of silly but it works:

In the top level class (A):

protected final <T> T a(T type) {
        return type
}

Assuming C extends B and B extends A.

Invoking:

C c = new C();
//Any order is fine and you have compile time safety and IDE assistance.
c.setA("a").a(c).setB("b").a(c).setC("c");
Community
  • 1
  • 1
ggb667
  • 1,881
  • 2
  • 20
  • 44