Class MethodDecl

  • All Implemented Interfaces:
    ASTNode

    public final class MethodDecl
    extends Object
    implements ASTNode
    AST node for a method declaration - modifiers, signature and optional body.
    • Constructor Detail

      • MethodDecl

        public MethodDecl​(String name,
                          SourceType returnType,
                          SourceLocation location)
        Creates a declaration with no modifiers, parameters or body.
        Parameters:
        name - the method name
        returnType - the declared return type
        location - the source location, null for unknown
      • MethodDecl

        public MethodDecl​(String name,
                          SourceType returnType)
        Creates a declaration with an unknown source location.
        Parameters:
        name - the method name
        returnType - the declared return type
    • Method Detail

      • getName

        public String getName()
        Returns:
        the name
      • setName

        public void setName​(String name)
        Sets the method name.
        Parameters:
        name - the new name
      • getModifiers

        public Set<Modifier> getModifiers()
        Returns:
        the modifiers
      • getReturnType

        public SourceType getReturnType()
        Returns:
        the return type
      • setReturnType

        public void setReturnType​(SourceType returnType)
        Sets the declared return type.
        Parameters:
        returnType - the new return type
      • getTypeParameters

        public NodeList<SourceType> getTypeParameters()
        Returns:
        the type parameters
      • getBody

        public BlockStmt getBody()
        Returns:
        the body
      • setBody

        public void setBody​(BlockStmt body)
        Sets the body, reparenting the new block and releasing the old one.
        Parameters:
        body - the new body, or null to make the method bodyless
      • getLocation

        public SourceLocation getLocation()
        Description copied from interface: ASTNode
        Gets the source location of this node.
        Specified by:
        getLocation in interface ASTNode
        Returns:
        the source location, or null if unknown
      • getParent

        public ASTNode getParent()
        Description copied from interface: ASTNode
        Gets the parent node in the AST tree.
        Specified by:
        getParent in interface ASTNode
        Returns:
        the parent
      • setParent

        public void setParent​(ASTNode parent)
        Sets the enclosing AST node.
        Specified by:
        setParent in interface ASTNode
        Parameters:
        parent - the new parent
      • withName

        public MethodDecl withName​(String name)
        Sets the method name.
        Parameters:
        name - the new name
        Returns:
        this declaration
      • withReturnType

        public MethodDecl withReturnType​(SourceType returnType)
        Sets the declared return type.
        Parameters:
        returnType - the new return type
        Returns:
        this declaration
      • withModifiers

        public MethodDecl withModifiers​(Set<Modifier> modifiers)
        Replaces the whole modifier set.
        Parameters:
        modifiers - the modifiers to hold
        Returns:
        this declaration
      • addModifier

        public MethodDecl addModifier​(Modifier modifier)
        Adds one modifier, keeping the existing ones.
        Parameters:
        modifier - the modifier to add
        Returns:
        this declaration
      • addAnnotation

        public MethodDecl addAnnotation​(AnnotationExpr annotation)
        Appends an annotation.
        Parameters:
        annotation - the annotation to add
        Returns:
        this declaration
      • addParameter

        public MethodDecl addParameter​(ParameterDecl parameter)
        Appends a parameter at the end of the parameter list.
        Parameters:
        parameter - the parameter to add
        Returns:
        this declaration
      • addTypeParameter

        public MethodDecl addTypeParameter​(SourceType typeParam)
        Appends a generic type parameter.
        Parameters:
        typeParam - the type parameter to add
        Returns:
        this declaration
      • addThrowsType

        public MethodDecl addThrowsType​(SourceType throwsType)
        Appends a declared thrown type.
        Parameters:
        throwsType - the thrown type to add
        Returns:
        this declaration
      • withBody

        public MethodDecl withBody​(BlockStmt body)
        Sets the body, reparenting the new block and releasing the old one.
        Parameters:
        body - the new body, or null to make the method bodyless
        Returns:
        this declaration
      • hasBody

        public boolean hasBody()
        Returns:
        true if the method has a body rather than being abstract or native
      • isPublic

        public boolean isPublic()
        Returns:
        true if the public modifier is set
      • isProtected

        public boolean isProtected()
        Returns:
        true if the protected modifier is set
      • isPrivate

        public boolean isPrivate()
        Returns:
        true if the private modifier is set
      • isStatic

        public boolean isStatic()
        Returns:
        true if the static modifier is set
      • isFinal

        public boolean isFinal()
        Returns:
        true if the final modifier is set
      • isAbstract

        public boolean isAbstract()
        Returns:
        true if the abstract modifier is set
      • isSynchronized

        public boolean isSynchronized()
        Returns:
        true if the synchronized modifier is set
      • isNative

        public boolean isNative()
        Returns:
        true if the native modifier is set
      • isDefault

        public boolean isDefault()
        Returns:
        true if the default modifier is set
      • getSignature

        public String getSignature()
        Renders the name and parameter types as name(type,type), ignoring the return type.
        Returns:
        the signature string
      • getChildren

        public List<ASTNode> getChildren()
        Description copied from interface: ASTNode
        Lists the direct children a node holds, in source order.
        Specified by:
        getChildren in interface ASTNode
        Returns:
        the direct children, empty for a leaf
      • accept

        public <T> T accept​(SourceVisitor<T> visitor)
        Description copied from interface: ASTNode
        Accepts a visitor for this node.
        Specified by:
        accept in interface ASTNode
        Type Parameters:
        T - the return type of the visitor
        Parameters:
        visitor - the visitor to accept
        Returns:
        the result from the visitor