Class Method.Builder

java.lang.Object
be.imgn.gaia.processing.source.Method.Builder
Enclosing class:
Method

public static final class Method.Builder extends Object
A builder of Methods.
  • Method Details

    • name

      public Method.Builder name(String newName)
      Renames the method, such as a copy made with Method.toBuilder().
      Parameters:
      newName - the new name
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the name is not a valid identifier
      IllegalStateException - if this is a constructor
    • addModifiers

      public Method.Builder addModifiers(Iterable<Modifier> more)
      Adds modifiers.
      Parameters:
      more - the modifiers
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if a modifier is not allowed on a method, or on a constructor if this is one; is repeated; or conflicts with another one, such as public with private or abstract with final
      IllegalStateException - if a modifier is abstract or native and the method has code, or it is static and the method has a receiver parameter
    • addModifiers

      public Method.Builder addModifiers(Modifier... more)
      Adds modifiers.
      Parameters:
      more - the modifiers
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if a modifier is not allowed on a method, or on a constructor if this is one; is repeated; or conflicts with another one, such as public with private or abstract with final
      IllegalStateException - if a modifier is abstract or native and the method has code, or it is static and the method has a receiver parameter
    • addAnnotation

      public Method.Builder addAnnotation(Annotation annotation)
      Adds an annotation.
      Parameters:
      annotation - the annotation
      Returns:
      this builder
    • addAnnotation

      public Method.Builder addAnnotation(Class<? extends Annotation> annotation)
      Adds an annotation without members.
      Parameters:
      annotation - the annotation type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the class is not an annotation type
    • addAnnotation

      public Method.Builder addAnnotation(ClassName annotation)
      Adds an annotation without members.
      Parameters:
      annotation - the annotation type
      Returns:
      this builder
    • addAnnotations

      public Method.Builder addAnnotations(Iterable<Annotation> more)
      Adds annotations.
      Parameters:
      more - the annotations
      Returns:
      this builder
    • addJavadoc

      public Method.Builder addJavadoc(String format, @Nullable Object... args)
      Adds Javadoc text. End each line with \n.
      Parameters:
      format - the text, with placeholders
      args - the arguments of the placeholders
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the format and the arguments do not match
    • addJavadoc

      public Method.Builder addJavadoc(CodeBlock text)
      Adds Javadoc text. End each line with \n.
      Parameters:
      text - the text
      Returns:
      this builder
    • addTypeVariable

      public Method.Builder addTypeVariable(TypeVariableName typeVariable)
      Declares a type variable, such as T in <T> T first(List<T> list).
      Parameters:
      typeVariable - the type variable, with its bounds
      Returns:
      this builder
      Throws:
      IllegalStateException - if this is a compact constructor
    • addTypeVariables

      public Method.Builder addTypeVariables(Iterable<TypeVariableName> more)
      Declares type variables.
      Parameters:
      more - the type variables, with their bounds
      Returns:
      this builder
      Throws:
      IllegalStateException - if this is a compact constructor and there are type variables
    • returns

      public Method.Builder returns(TypeName type)
      Sets the return type.
      Parameters:
      type - the return type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the type is a wildcard
      IllegalStateException - if this is a constructor
    • returns

      public Method.Builder returns(Type type)
      Sets the return type.
      Parameters:
      type - the return type: a class, or a generic type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the type names a local or anonymous class, as TypeName.of(java.lang.reflect.Type) names it
      IllegalStateException - if this is a constructor
    • addParameter

      public Method.Builder addParameter(Parameter parameter)
      Adds a parameter.
      Parameters:
      parameter - the parameter
      Returns:
      this builder
      Throws:
      IllegalStateException - if this is a compact constructor, or the parameter is a receiver parameter but is not first or the method is static
    • addParameter

      public Method.Builder addParameter(TypeName type, String name, Modifier... modifiers)
      Adds a parameter.
      Parameters:
      type - the type
      name - the name
      modifiers - the modifiers: none, or final
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the name is not a valid identifier, the type is void or a wildcard, or a modifier is not final, is repeated, or is given to a receiver parameter
      IllegalStateException - if this is a compact constructor, or the parameter is a receiver parameter but is not first or the method is static
    • addParameter

      public Method.Builder addParameter(Type type, String name, Modifier... modifiers)
      Adds a parameter.
      Parameters:
      type - the type: a class, or a generic type
      name - the name
      modifiers - the modifiers: none, or final
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the type is void, or names a local or anonymous class, as TypeName.of(java.lang.reflect.Type) names it; the name is not a valid identifier; or a modifier is not final, is repeated, or is given to a receiver parameter
      IllegalStateException - if this is a compact constructor, or the parameter is a receiver parameter but is not first or the method is static
    • addParameters

      public Method.Builder addParameters(Iterable<Parameter> more)
      Adds parameters.
      Parameters:
      more - the parameters
      Returns:
      this builder
      Throws:
      IllegalStateException - if this is a compact constructor and there are parameters, or a receiver parameter is not first or the method is static
    • varargs

      public Method.Builder varargs(boolean isVarargs)
      Makes the last parameter variable arity, or not. Its type must be an array.
      Parameters:
      isVarargs - whether the last parameter is variable arity
      Returns:
      this builder
    • varargs

      public Method.Builder varargs()
      Makes the last parameter variable arity. Its type must be an array.
      Returns:
      this builder
    • addException

      public Method.Builder addException(TypeName exception)
      Adds an exception to the throws clause, unless it is there already.
      Parameters:
      exception - the exception type: a class or a type variable
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the exception type is not a class or a type variable
      IllegalStateException - if this is a compact constructor
    • addException

      public Method.Builder addException(Class<? extends Throwable> exception)
      Adds an exception to the throws clause.
      Parameters:
      exception - the exception type
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the class is local or anonymous
      IllegalStateException - if this is a compact constructor
    • addException

      public Method.Builder addException(Type exception)
      Adds an exception to the throws clause, such as a generic exception type of a method.
      Parameters:
      exception - the exception type: a class or a type variable
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the type is a class that is not a Throwable, or is neither a class nor a type variable, or names a local or anonymous class
      IllegalStateException - if this is a compact constructor
    • addExceptions

      public Method.Builder addExceptions(Iterable<? extends TypeName> more)
      Adds exceptions to the throws clause, except those there already.
      Parameters:
      more - the exception types: classes or type variables
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if an exception type is not a class or a type variable
      IllegalStateException - if this is a compact constructor and there are exceptions
    • addCode

      public Method.Builder addCode(String format, @Nullable Object... args)
      Adds code to the body.
      Parameters:
      format - the code, with placeholders
      args - the arguments of the placeholders
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the format and the arguments do not match
      IllegalStateException - if the method is abstract or native
    • addCode

      public Method.Builder addCode(CodeBlock block)
      Adds code to the body.
      Parameters:
      block - the code
      Returns:
      this builder
      Throws:
      IllegalStateException - if the method is abstract or native
    • addNamedCode

      public Method.Builder addNamedCode(String format, Map<String,?> arguments)
      Adds code whose placeholders name their arguments; see CodeBlock.Builder.addNamed(String, Map).
      Parameters:
      format - the code, with named placeholders
      arguments - the arguments, by name
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if a name is not valid, or a placeholder has no argument
      IllegalStateException - if the method is abstract or native
    • addStatement

      public Method.Builder addStatement(String format, @Nullable Object... args)
      Adds a statement to the body: the code, a semicolon and a new line.
      Parameters:
      format - the statement, without its semicolon, with placeholders
      args - the arguments of the placeholders
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the format and the arguments do not match
      IllegalStateException - if the method is abstract or native
    • addStatement

      public Method.Builder addStatement(CodeBlock statement)
      Adds a statement to the body: the code, a semicolon and a new line.
      Parameters:
      statement - the statement, without its semicolon
      Returns:
      this builder
      Throws:
      IllegalStateException - if the method is abstract or native
    • addComment

      public Method.Builder addComment(String format, @Nullable Object... args)
      Adds a line comment to the body; see CodeBlock.Builder.addComment(String, Object...).
      Parameters:
      format - the comment, without //, with placeholders
      args - the arguments of the placeholders
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the format and the arguments do not match, or the comment has a line break
      IllegalStateException - if the method is abstract or native
    • beginControlFlow

      public Method.Builder beginControlFlow(String controlFlow, @Nullable Object... args)
      Parameters:
      controlFlow - the code before the opening brace, with placeholders
      args - the arguments of the placeholders
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the format and the arguments do not match
      IllegalStateException - if the method is abstract or native
    • beginControlFlow

      public Method.Builder beginControlFlow(CodeBlock controlFlow)
      Opens a block in the body; see CodeBlock.Builder.beginControlFlow(CodeBlock).
      Parameters:
      controlFlow - the code before the opening brace
      Returns:
      this builder
      Throws:
      IllegalStateException - if the method is abstract or native
    • nextControlFlow

      public Method.Builder nextControlFlow(String controlFlow, @Nullable Object... args)
      Closes a block in the body and opens the next one; see CodeBlock.Builder.nextControlFlow(String, Object...).
      Parameters:
      controlFlow - the code between the braces, with placeholders
      args - the arguments of the placeholders
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the format and the arguments do not match
      IllegalStateException - if the method is abstract or native
    • nextControlFlow

      public Method.Builder nextControlFlow(CodeBlock controlFlow)
      Closes a block in the body and opens the next one; see CodeBlock.Builder.nextControlFlow(CodeBlock).
      Parameters:
      controlFlow - the code between the braces
      Returns:
      this builder
      Throws:
      IllegalStateException - if the method is abstract or native
    • endControlFlow

      public Method.Builder endControlFlow()
      Closes a block in the body.
      Returns:
      this builder
      Throws:
      IllegalStateException - if the method is abstract or native
    • endControlFlow

      public Method.Builder endControlFlow(String controlFlow, @Nullable Object... args)
      Closes a block in the body with code after its closing brace; see CodeBlock.Builder.endControlFlow(String, Object...).
      Parameters:
      controlFlow - the code after the closing brace, without its semicolon, with placeholders
      args - the arguments of the placeholders
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the format and the arguments do not match
      IllegalStateException - if the method is abstract or native
    • endControlFlow

      public Method.Builder endControlFlow(CodeBlock controlFlow)
      Closes a block in the body with code after its closing brace; see CodeBlock.Builder.endControlFlow(CodeBlock).
      Parameters:
      controlFlow - the code after the closing brace, without its semicolon
      Returns:
      this builder
      Throws:
      IllegalStateException - if the method is abstract or native
    • defaultValue

      public Method.Builder defaultValue(String format, @Nullable Object... args)
      Sets the default value of an annotation type member.
      Parameters:
      format - the value after default, with placeholders
      args - the arguments of the placeholders
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the format and the arguments do not match, or the value is not balanced
      IllegalStateException - if this is a constructor
    • defaultValue

      public Method.Builder defaultValue(CodeBlock value)
      Sets the default value of an annotation type member.
      Parameters:
      value - the value after default
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the value is not balanced
      IllegalStateException - if this is a constructor
    • build

      public Method build()
      Builds the method.
      Returns:
      the method
      Throws:
      IllegalStateException - if it is varargs but its last parameter is not an array, or its Javadoc or code is not balanced