Annotation Type This


  • @Retention(RUNTIME)
    @Target({TYPE_USE,TYPE_PARAMETER})
    @PolymorphicQualifier
    @TargetLocations({RECEIVER,RETURN})
    public @interface This
    Write @This on the return type of a method that always returns its receiver ( this). For example:
    
     class MyBuilder {
       @This MyBuilder setName(String name) {
         this.name = name;
         return this;
       }
     }
     

    Strictly speaking, this is a polymorphic annotation, but when you write it on a return type, the Returns Receiver Checker automatically adds it to the receiver, so the above method is equivalent to:

    
     @This MyBuilder setName(@This MyBuilder this, String name) {
       this.name = name;
       return this;
     }
     

    While it would be natural to make @This the default annotation for receivers, it leads to false positives warnings due to https://github.com/typetools/checker-framework/issues/2931, so this defaulting is currently elided.