Class Method

java.lang.Object
be.imgn.gaia.processing.source.Method

public final class Method extends Object
A method, a constructor, or the compact constructor of a record.
Method.newBuilder("max")
        .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
        .returns(int.class)
        .addParameter(int.class, "a")
        .addParameter(int.class, "b")
        .addStatement("return a > b ? a : b")
        .build();

Methods are values: two methods written the same are equal.

  • Method Details

    • newBuilder

      public static Method.Builder newBuilder(String name)
      Returns a builder of methods, which return void unless told otherwise.
      Parameters:
      name - the name of the method
      Returns:
      a new builder
      Throws:
      IllegalArgumentException - if the name is not a valid identifier
    • newConstructorBuilder

      public static Method.Builder newConstructorBuilder()
      Returns a builder of constructors.
      Returns:
      a new builder
    • newCompactConstructorBuilder

      public static Method.Builder newCompactConstructorBuilder()
      Returns a builder of compact constructors, for records: a constructor without parameter list, whose code runs before the components are assigned.
      Returns:
      a new builder
    • overriding

      public static Method.Builder overriding(ExecutableElement method)
      Returns a builder of a method that overrides another one: the same name, type variables, return type, parameters and exceptions, annotated with @Override, and without abstract, default, native, synchronized or strictfp.
      Parameters:
      method - the method to override
      Returns:
      a new builder, to add the code to
      Throws:
      IllegalArgumentException - if the method is private, static or final, a member of a final type, or not a method; or a type in its signature has no name, as TypeName.of(TypeMirror) names it
    • overriding

      public static Method.Builder overriding(ExecutableElement method, DeclaredType enclosing, Types types)
      Returns a builder of a method that overrides another one, as a member of a type: type variables of the type are replaced by its type arguments. For example, overriding Comparable.compareTo(T) in Comparable<String> gives compareTo(String).
      Parameters:
      method - the method to override
      enclosing - the type the method is a member of, such as Comparable<String>
      types - the type utilities of the processing environment
      Returns:
      a new builder, to add the code to
      Throws:
      IllegalArgumentException - if the method is private, static or final, a member of a final type, or not a method; it is not a member of the type; or a type in its signature has no name, as TypeName.of(TypeMirror) names it
    • name

      public String name()
      Returns the name.
      Returns:
      the name of the method; <init> for a constructor, <compact> for a compact constructor
    • isConstructor

      public boolean isConstructor()
      Returns whether this is a constructor, compact or not.
      Returns:
      true for a constructor
    • isCompactConstructor

      public boolean isCompactConstructor()
      Returns whether this is the compact constructor of a record.
      Returns:
      true for a compact constructor
    • modifiers

      public Set<Modifier> modifiers()
      Returns the modifiers.
      Returns:
      the modifiers
    • annotations

      public List<Annotation> annotations()
      Returns the annotations.
      Returns:
      the annotations, in order
    • javadoc

      public CodeBlock javadoc()
      Returns the Javadoc.
      Returns:
      the Javadoc text, empty if there is none
    • typeVariables

      public List<TypeVariableName> typeVariables()
      Returns the declared type variables.
      Returns:
      the type variables, in order
    • returnType

      public @Nullable TypeName returnType()
      Returns the return type.
      Returns:
      the return type, or null for a constructor
    • parameters

      public List<Parameter> parameters()
      Returns the parameters.
      Returns:
      the parameters, in order
    • isVarargs

      public boolean isVarargs()
      Returns whether the last parameter is variable arity, written String... names.
      Returns:
      true for a varargs method
    • exceptions

      public List<TypeName> exceptions()
      Returns the exceptions in the throws clause.
      Returns:
      the exceptions, in order
    • code

      public CodeBlock code()
      Returns the code of the body.
      Returns:
      the code, empty for no body or an empty one
    • defaultValue

      public CodeBlock defaultValue()
      Returns the default value of an annotation type member.
      Returns:
      the value after default, empty if there is none
    • toBuilder

      public Method.Builder toBuilder()
      Returns a builder that starts with this method.
      Returns:
      a new builder
    • equals

      public boolean equals(@Nullable Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object