Class MethodRefExpr

  • All Implemented Interfaces:
    ASTNode, Expression

    public final class MethodRefExpr
    extends Object
    implements Expression
    A method reference expression - Type::method, expr::method or Type::new.
    • Constructor Detail

      • MethodRefExpr

        public MethodRefExpr​(Expression receiver,
                             String methodName,
                             String ownerClass,
                             MethodRefKind kind,
                             SourceType type,
                             SourceLocation location)
        Creates a method reference and adopts the receiver as a child.
        Parameters:
        receiver - the bound receiver, or null for an unbound reference
        methodName - the referenced method's name, or "new" for a constructor
        ownerClass - internal name of the declaring class
        kind - which form of method reference this is
        type - the functional interface type the reference targets
        location - source position, or null for an unknown one
        Throws:
        NullPointerException - if methodName, ownerClass, kind or type is null
      • MethodRefExpr

        public MethodRefExpr​(Expression receiver,
                             String methodName,
                             String ownerClass,
                             MethodRefKind kind,
                             SourceType type)
        Creates a method reference with an unknown source position.
        Parameters:
        receiver - the bound receiver, or null for an unbound reference
        methodName - the referenced method's name, or "new" for a constructor
        ownerClass - internal name of the declaring class
        kind - which form of method reference this is
        type - the functional interface type the reference targets
        Throws:
        NullPointerException - if methodName, ownerClass, kind or type is null
    • Method Detail

      • getReceiver

        public Expression getReceiver()
        Returns:
        the receiver
      • setReceiver

        public void setReceiver​(Expression receiver)
        Replaces the receiver, reparenting it and releasing the old one.
        Parameters:
        receiver - the new receiver, or null to unbind
      • getMethodName

        public String getMethodName()
        Returns:
        the method name
      • setMethodName

        public void setMethodName​(String methodName)
        Replaces the referenced method's name.
        Parameters:
        methodName - the new name
      • getOwnerClass

        public String getOwnerClass()
        Returns:
        the owner class
      • getDescriptor

        public String getDescriptor()
        Returns:
        the descriptor
      • withDescriptor

        public MethodRefExpr withDescriptor​(String descriptor)
        Attaches the JVM descriptor recovered for the referenced method.
        Parameters:
        descriptor - the method descriptor
        Returns:
        this expression
      • getType

        public SourceType getType()
        Description copied from interface: Expression
        Gets the inferred type of this expression.
        Specified by:
        getType in interface Expression
        Returns:
        the type
      • 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 node this expression hangs under.
        Specified by:
        setParent in interface ASTNode
        Parameters:
        parent - the owning node
      • staticRef

        public static MethodRefExpr staticRef​(String ownerClass,
                                              String methodName,
                                              SourceType type)
        Creates a static method reference, ClassName::staticMethod.
        Parameters:
        ownerClass - internal name of the declaring class
        methodName - the static method's name
        type - the functional interface type the reference targets
        Returns:
        the method reference
      • instanceRef

        public static MethodRefExpr instanceRef​(String ownerClass,
                                                String methodName,
                                                SourceType type)
        Creates an unbound instance method reference, ClassName::instanceMethod.
        Parameters:
        ownerClass - internal name of the declaring class
        methodName - the instance method's name
        type - the functional interface type the reference targets
        Returns:
        the method reference
      • boundRef

        public static MethodRefExpr boundRef​(Expression receiver,
                                             String methodName,
                                             String ownerClass,
                                             SourceType type)
        Creates a bound method reference, expr::method.
        Parameters:
        receiver - the expression the reference is bound to
        methodName - the instance method's name
        ownerClass - internal name of the declaring class
        type - the functional interface type the reference targets
        Returns:
        the method reference
      • constructorRef

        public static MethodRefExpr constructorRef​(String ownerClass,
                                                   SourceType type)
        Creates a constructor reference, ClassName::new.
        Parameters:
        ownerClass - internal name of the constructed class
        type - the functional interface type the reference targets
        Returns:
        the method reference
      • arrayConstructorRef

        public static MethodRefExpr arrayConstructorRef​(SourceType arrayType)
        Creates an array constructor reference, int[]::new, taking the owner from the array type's source form.
        Parameters:
        arrayType - the array type being allocated
        Returns:
        the method reference
      • withReceiver

        public MethodRefExpr withReceiver​(Expression receiver)
        Replaces the receiver, reparenting it and releasing the old one.
        Parameters:
        receiver - the new receiver, or null to unbind
        Returns:
        this expression
      • withMethodName

        public MethodRefExpr withMethodName​(String methodName)
        Replaces the referenced method's name.
        Parameters:
        methodName - the new name
        Returns:
        this expression
      • getOwnerSimpleName

        public String getOwnerSimpleName()
        Returns:
        the owner class name without its package, inner class parts kept
      • isConstructorRef

        public boolean isConstructorRef()
        Returns:
        true for an object or array constructor reference
      • 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