net.sourceforge.pmd.lang.java.ast.ASTReceiverParameter Maven / Gradle / Ivy
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.ast;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
* Receiver parameter. A receiver parameter is syntactically part of a
* {@linkplain ASTFormalParameters formal parameter list}, though it does
* not declare a variable or affect the arity of the method in any way.
* Its only purpose is to annotate the type of the object on which the
* method call is issued. It was introduced with Java 8.
*
* For example:
*
* class Foo {
* abstract void foo(@Bar Foo this);
* }
*
*
* Receiver parameters are only allowed on two types of declarations:
*
* - Instance method declarations of a class or interface (not annotation) type
*
- Constructor declaration of a non-static inner class. It then has
* the type of the enclosing instance.
*
* In both cases it must be the first parameter of the formal parameter
* list, and is entirely optional.
*
*
*
* ReceiverParameter ::= {@link ASTClassOrInterfaceType ClassOrInterfaceType} (<IDENTIFIER> ".")? "this"
*
*
*/
public final class ASTReceiverParameter extends AbstractJavaNode {
ASTReceiverParameter(int id) {
super(id);
}
@Override
protected R acceptVisitor(JavaVisitor super P, ? extends R> visitor, P data) {
return visitor.visit(this, data);
}
/**
* Returns the type of the receiver parameter (eg {@code Foo} in {@code Foo this}.
* In an instance method, that type must be the class or interface in which the method
* is declared, and the name of the receiver parameter must be {@code this}.
*
*
In an inner class's constructor, the type of the receiver parameter
* must be the class or interface which is the immediately enclosing type
* declaration of the inner class, call it C, and the name of the parameter
* must be {@code Identifier.this} where {@code Identifier} is the simple name of C.
*/
@NonNull
public ASTClassOrInterfaceType getReceiverType() {
return (ASTClassOrInterfaceType) getChild(0);
}
}