Interface MethodReferenceExpression

All Superinterfaces:
ExpressionDef

public sealed interface MethodReferenceExpression extends ExpressionDef
A method reference used as an expression implementing a functional interface, e.g. MyClass::staticMethod.

It implements the same functional interface a ExpressionDef.Lambda does, but instead of a generated body it points directly at an existing method. Each form is its own type: StaticMethodReferenceExpression, InstanceMethodReferenceExpression and ConstructorMethodReferenceExpression.

Only a reference that needs no further input is an expression - an instance method becomes one once bound to a receiver.

Create one from the LambdaDef of the interface being implemented, or with ClassTypeDef.staticMethodReference(ClassTypeDef, MethodDef), ClassTypeDef.constructorReference(ClassTypeDef, MethodDef) and ClassTypeDef.methodReference(ExpressionDef, MethodDef).

Since:
2.2
Author:
Denis Stepanov
  • Method Details

    • of

      static MethodReferenceExpression of(ClassTypeDef functionalInterface, Method method)
      Implement the interface with a reference to a static method, e.g. MyClass::staticMethod.
      Parameters:
      functionalInterface - The interface the reference implements
      method - The referenced static method
      Returns:
      the method reference expression
      Since:
      2.2
    • of

      static MethodReferenceExpression of(ClassTypeDef functionalInterface, Constructor<?> constructor)
      Implement the interface with a reference to a constructor, e.g. MyClass::new.
      Parameters:
      functionalInterface - The interface the reference implements
      constructor - The referenced constructor
      Returns:
      the method reference expression
      Since:
      2.2
    • type

      ClassTypeDef type()
      Returns the functional interface being implemented.
      Specified by:
      type in interface ExpressionDef
      Returns:
      The type of the functional interface, e.g. Function<String, String>
    • isStatic

      default boolean isStatic()
      Whether the referenced method is called statically.

      The implementations are not visible outside the model, so the forms are told apart with this, isConstructor() and instance() rather than by type.

      Returns:
      True if this is a reference to a static method
    • isConstructor

      default boolean isConstructor()
      Whether the referenced method is a constructor.
      Returns:
      True if this is a reference to a constructor
    • instance

      default @Nullable ExpressionDef instance()
      The receiver a reference bound to an instance captures.
      Returns:
      The receiver, or null when the reference is not bound to one
    • target

      MethodDef target()
      Returns the method the interface declares, erased as the interface declares it.
      Returns:
      The method declared by the interface, e.g. Object apply(Object) for a Function
    • instantiated

      MethodDef instantiated()
      Returns the interface method with the type variables of the interface resolved.
      Returns:
      The interface method with its type variables resolved, e.g. String apply(String)
    • owner

      ClassTypeDef owner()
      Returns the type the referenced method is declared on.
      Returns:
      The type declaring the referenced method
    • method

      MethodDef method()
      Returns the method this reference points at.
      Returns:
      The referenced method
    • invoke

      default ExpressionDef.InvokeInstanceMethod invoke(List<? extends ExpressionDef> expressions)
      Invoke the method reference.
      Parameters:
      expressions - The expressions
      Returns:
      The method invocation
    • invoke

      default ExpressionDef.InvokeInstanceMethod invoke(ExpressionDef... expressions)
      Invoke the method reference.
      Parameters:
      expressions - The expressions
      Returns:
      The method invocation
    • nestedExpressionsStream

      default Stream<? extends ExpressionDef> nestedExpressionsStream()
      Description copied from interface: ExpressionDef
      Stream of nested expressions included in this expression.
      Specified by:
      nestedExpressionsStream in interface ExpressionDef
      Returns:
      The expressions