Class ClassDecl

  • All Implemented Interfaces:
    ASTNode, TypeDecl

    public final class ClassDecl
    extends Object
    implements TypeDecl
    A Java class declaration: name, modifiers, supertypes, and its member fields, methods, constructors, initializers, and inner types.
    • Constructor Detail

      • ClassDecl

        public ClassDecl​(String name,
                         SourceLocation location)
        Creates an empty class declaration.
        Parameters:
        name - the simple class name
        location - the source location, or null for unknown
      • ClassDecl

        public ClassDecl​(String name)
        Creates an empty class declaration at an unknown location.
        Parameters:
        name - the simple class name
    • Method Detail

      • setName

        public void setName​(String name)
        Sets the class name.
        Parameters:
        name - the simple class name
      • getSuperclass

        public SourceType getSuperclass()
        Returns:
        the superclass
      • setSuperclass

        public void setSuperclass​(SourceType superclass)
        Sets the superclass type.
        Parameters:
        superclass - the extended type
      • getTypeParameters

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

        public List<BlockStmt> getStaticInitializers()
        Returns:
        the static initializers
      • getInstanceInitializers

        public List<BlockStmt> getInstanceInitializers()
        Returns:
        the instance initializers
      • getLocation

        public SourceLocation getLocation()
        Description copied from interface: ASTNode
        Gets the source location of this node.
        Specified by:
        getLocation in interface ASTNode
        Returns:
        the location
      • 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 parent node.
        Specified by:
        setParent in interface ASTNode
        Parameters:
        parent - the new parent
      • withName

        public ClassDecl withName​(String name)
        Sets the class name.
        Parameters:
        name - the simple class name
        Returns:
        this declaration
      • withSuperclass

        public ClassDecl withSuperclass​(SourceType superclass)
        Sets the superclass type.
        Parameters:
        superclass - the extended type
        Returns:
        this declaration
      • withModifiers

        public ClassDecl withModifiers​(Set<Modifier> modifiers)
        Replaces all modifiers with the given set.
        Parameters:
        modifiers - the modifiers to apply
        Returns:
        this declaration
      • addModifier

        public ClassDecl addModifier​(Modifier modifier)
        Adds a modifier.
        Parameters:
        modifier - the modifier to add
        Returns:
        this declaration
      • addAnnotation

        public ClassDecl addAnnotation​(AnnotationExpr annotation)
        Adds an annotation.
        Parameters:
        annotation - the annotation to add
        Returns:
        this declaration
      • addInterface

        public ClassDecl addInterface​(SourceType iface)
        Adds an implemented interface.
        Parameters:
        iface - the interface type
        Returns:
        this declaration
      • addTypeParameter

        public ClassDecl addTypeParameter​(SourceType typeParam)
        Adds a type parameter.
        Parameters:
        typeParam - the type parameter
        Returns:
        this declaration
      • addField

        public ClassDecl addField​(FieldDecl field)
        Adds a field member.
        Parameters:
        field - the field declaration
        Returns:
        this declaration
      • addMethod

        public ClassDecl addMethod​(MethodDecl method)
        Adds a method member.
        Parameters:
        method - the method declaration
        Returns:
        this declaration
      • addConstructor

        public ClassDecl addConstructor​(ConstructorDecl constructor)
        Adds a constructor.
        Parameters:
        constructor - the constructor declaration
        Returns:
        this declaration
      • addInnerType

        public ClassDecl addInnerType​(TypeDecl innerType)
        Adds a nested type.
        Parameters:
        innerType - the nested type declaration
        Returns:
        this declaration
      • addStaticInitializer

        public ClassDecl addStaticInitializer​(BlockStmt block)
        Adds a static initializer block.
        Parameters:
        block - the initializer body
        Returns:
        this declaration
      • addInstanceInitializer

        public ClassDecl addInstanceInitializer​(BlockStmt block)
        Adds an instance initializer block.
        Parameters:
        block - the initializer body
        Returns:
        this declaration
      • getConstructor

        public ConstructorDecl getConstructor​(SourceType... paramTypes)
        Finds a constructor by exact parameter types.
        Parameters:
        paramTypes - the parameter types to match
        Returns:
        the matching constructor, or null if none
      • getMethod

        public MethodDecl getMethod​(String name,
                                    SourceType... paramTypes)
        Finds a method by name and exact parameter types.
        Parameters:
        name - the method name
        paramTypes - the parameter types to match
        Returns:
        the matching method, or null if none
      • 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