Interface MethodMaker

All Superinterfaces:
Maker

public interface MethodMaker extends Maker
Allows new methods to be defined within a class.
See Also:
  • Method Details

    • begin

      static MethodMaker begin(MethodHandles.Lookup lookup, Object retType, String name, Object... paramTypes)
      Begin defining a standalone method, defined in the same nest as the lookup class.
      Parameters:
      lookup - define the method using this lookup object
      retType - a class or name; can be null if the method returns void
      name - method name; use null or "_" if unnamed
      paramTypes - classes or names; can be null if the method accepts no parameters
      Throws:
      IllegalArgumentException - if a type is unsupported
      See Also:
    • begin

      static MethodMaker begin(MethodHandles.Lookup lookup, String name, MethodType type)
      Begin defining a standalone method, defined in the same nest as the lookup class.
      Parameters:
      lookup - define the method using this lookup object
      name - method name; use null or "_" if unnamed
      type - defines the return type and parameter types
      See Also:
    • name

      String name()
      Returns the name of this method.
      Specified by:
      name in interface Maker
    • getReturnType

      Type getReturnType()
      Returns the return type of this method.
    • getParamTypes

      Type[] getParamTypes()
      Returns the parameter types of this method.
    • public_

      MethodMaker public_()
      Switch this method to be public. Methods are package-private by default.
      Specified by:
      public_ in interface Maker
      Returns:
      this
    • private_

      MethodMaker private_()
      Switch this method to be private. Methods are package-private by default.
      Specified by:
      private_ in interface Maker
      Returns:
      this
    • protected_

      MethodMaker protected_()
      Switch this method to be protected. Methods are package-private by default.
      Specified by:
      protected_ in interface Maker
      Returns:
      this
    • static_

      MethodMaker static_()
      Switch this method to be static. Methods are non-static by default.
      Specified by:
      static_ in interface Maker
      Returns:
      this
      Throws:
      IllegalStateException - if this or any parameters have been accessed
    • final_

      MethodMaker final_()
      Switch this method to be final. Methods are non-final by default.
      Specified by:
      final_ in interface Maker
      Returns:
      this
    • synchronized_

      MethodMaker synchronized_()
      Switch this method to be synchronized. Methods are non-synchronized by default.
      Returns:
      this
    • abstract_

      MethodMaker abstract_()
      Switch this method to be abstract. Methods are non-abstract by default.
      Returns:
      this
    • native_

      MethodMaker native_()
      Switch this method to be native. Methods are non-native by default.
      Returns:
      this
    • synthetic

      MethodMaker synthetic()
      Indicate that this method is synthetic. Methods are non-synthetic by default.
      Specified by:
      synthetic in interface Maker
      Returns:
      this
    • bridge

      MethodMaker bridge()
      Indicate that this method is a bridge, which implements an inherited method exactly, but it delegates to another method which returns a more specialized return type.
      Returns:
      this
    • receiverType

      MethodMaker receiverType(Type type)
      Define a receiver type for the sole purpose of annotating the implicit this parameter.
      Parameters:
      type - must be the same as this type
      Returns:
      this MethodMaker
      Throws:
      IllegalStateException - if not an instance method, or if this or any parameters have been accessed
    • varargs

      MethodMaker varargs()
      Indicate that this method supports a variable number of arguments.
      Returns:
      this
      Throws:
      IllegalStateException - if last parameter type isn't an array
    • throws_

      MethodMaker throws_(Object type)
      Declare that this method throws the given exception type.
      Parameters:
      type - a class or class name
      Returns:
      this
      Throws:
      IllegalArgumentException - if the type is unsupported
    • override

      MethodMaker override()
      Verifies that this method overrides an inherited virtual method.
      Returns:
      this
      Throws:
      IllegalStateException - if not overriding an inherited virtual method, or if the inherited method is final
    • signature

      MethodMaker signature(Object... components)
      Define a signature for this member, which is a string for supporting generic types. The components can be strings or types (class, ClassMaker, etc.), which are concatenated into a single string. Consult the JVM specification for the signature syntax.
      Specified by:
      signature in interface Maker
      Returns:
      this
    • class_

      Variable class_()
      Returns a variable of type Class which represents the enclosing class of this method.
      See Also:
    • this_

      Variable this_()
      Returns a variable which accesses the enclosing object of this method.
      Throws:
      IllegalStateException - if making a static method
    • super_

      Variable super_()
      Returns a variable which is used for invoking superclass methods. The type of the variable is the superclass, and when applicable, the instance is this_.
    • param

      Variable param(int index)
      Returns a variable which accesses a parameter of the method being built.
      Parameters:
      index - zero based index
      Throws:
      IndexOutOfBoundsException - if index is out of bounds
    • paramCount

      int paramCount()
      Returns the number of parameters which can be accessed by the param method.
    • var

      Variable var(Object type)
      Returns a new uninitialized variable with the given type. Call set to initialize it immediately.
      Parameters:
      type - a class, class name, or a variable
      Throws:
      IllegalArgumentException - if the type is unsupported
    • lineNum

      void lineNum(int num)
      Define a line number to represent the location of the next code instruction.
    • label

      Label label()
      Returns a new label, initially unpositioned. Call here to position it immediately.
    • goto_

      void goto_(Label label)
      Generates an unconditional goto statement to the given label, which doesn't need to be positioned yet.
      See Also:
    • return_

      void return_()
      Generates a return void statement.
      Throws:
      IllegalStateException - if this method cannot return void
    • return_

      void return_(Object value)
      Generates a statement which returns a variable or a constant.
      Parameters:
      value - Variable or constant
      Throws:
      IllegalArgumentException - if the value isn't a variable or a supported constant
      IllegalStateException - if this method must return void
    • field

      Field field(String name)
      Access a static or instance field in the enclosing object of this method.
      Parameters:
      name - field name
      Throws:
      IllegalStateException - if the field isn't found
    • invoke

      Variable invoke(String name, Object... values)
      Invoke a static or instance method on the enclosing object of this method.
      Parameters:
      name - the method name enclosing class
      values - Variables or constants
      Returns:
      the result of the method, which is null if void
      Throws:
      IllegalArgumentException - if a value isn't a variable or a supported constant
      IllegalStateException - if the method isn't found
      See Also:
    • invokeSuperConstructor

      void invokeSuperConstructor(Object... values)
      Invoke a super constructor method on the enclosing object of this method, from within a constructor.
      Parameters:
      values - Variables or constants
      Throws:
      IllegalArgumentException - if a value isn't a variable or a supported constant
      IllegalStateException - if not defining a constructor, or if a matching constructor isn't found
    • invokeThisConstructor

      void invokeThisConstructor(Object... values)
      Invoke a this constructor method on the enclosing object of this method, from within a constructor.
      Parameters:
      values - Variables or constants
      Throws:
      IllegalArgumentException - if a value isn't a variable or a supported constant
      IllegalStateException - if not defining a constructor, or if a matching constructor isn't found
    • invoke

      Variable invoke(MethodHandle handle, Object... values)
      Invoke a method via a MethodHandle. If making a class to be loaded externally, the handle must be truly Constable.
      Parameters:
      handle - runtime method handle
      values - Variables or constants
      Returns:
      the result of the method, which is null if void
      Throws:
      IllegalArgumentException - if a value isn't a variable or a supported constant
      IllegalStateException - if defining an external class and the handle isn't truly Constable
    • new_

      Variable new_(Object type, Object... values)
      Allocate a new object. If the type is an ordinary object, a matching constructor is invoked. If the type is an array, then no constructor is invoked, and the given values represent array dimension sizes.
      Parameters:
      type - class name or Class instance
      values - Variables or constants
      Returns:
      the new object
      Throws:
      IllegalArgumentException - if the type is unsupported, or if a value isn't a variable or a supported constant
      IllegalStateException - if the constructor isn't found
    • catch_

      Variable catch_(Label tryStart, Label tryEnd, Object type)
      Define an exception handler here, which catches exceptions between the given labels. Any code prior to the handler must not flow into it directly.
      Parameters:
      type - exception type to catch; pass null to catch anything
      Returns:
      a variable which references the exception instance
      Throws:
      IllegalArgumentException - if the type is unsupported
      IllegalStateException - if the start label is unpositioned
    • catch_

      Variable catch_(Label tryStart, Label tryEnd, Object... types)
      Define an exception handler here, which catches exceptions between the given labels. Any code prior to the handler must not flow into it directly. This variant supports matching on multiple exception types, and the effective exception type is the nearest common superclass.
      Parameters:
      types - exception types to catch; pass null to catch anything
      Returns:
      a variable which references the exception instance
      Throws:
      IllegalArgumentException - if a type is unsupported, or if no types are given
      IllegalStateException - if the start label is unpositioned
    • catch_

      void catch_(Label tryStart, Object type, Consumer<Variable> handler)
      Convenience method which defines an exception handler here. Code prior to the handler flows around it.
      Parameters:
      type - exception type to catch; pass null to catch anything
      handler - receives a variable which references the exception instance
      Throws:
      IllegalArgumentException - if the type is unsupported
      IllegalStateException - if the start label is unpositioned
    • finally_

      void finally_(Label tryStart, Runnable handler)
      Define a finally handler which is generated for every possible exit path between the start label and here.
      Parameters:
      handler - called for each exit path to generate handler code
      Throws:
      IllegalStateException - if the start label is unpositioned
    • concat

      Variable concat(Object... values)
      Concatenate variables and constants together into a new String in the same matter as the Java concatenation operator. If no values are given, the returned variable will refer to the empty string.
      Parameters:
      values - Variables or constants
      Returns:
      the result in a new String variable
      Throws:
      IllegalArgumentException - if a value isn't a variable or a supported constant
    • access

      Field access(VarHandle handle, Object... values)
      Access a VarHandle via a pseudo field. If making a class to be loaded externally, the handle must be truly Constable.

      All of the coordinate values must be provided up front, which are then used each time the VarHandle is accessed. Variable coordinates are read each time the access field is used — they aren't fixed to the initial value. In addition, the array of coordinate values isn't cloned, permitting changes without needing to obtain a new access field.

      A VarHandle can also be accessed by calling toMethodHandle, which is then passed to the invoke method.

      Parameters:
      handle - runtime variable handle
      values - Variables or constants for each coordinate
      Returns:
      a pseudo field which accesses the variable
      Throws:
      IllegalArgumentException - if a value isn't a variable or a supported constant, or if the number of values doesn't match the number of coordinates
      IllegalStateException - if defining an external class and the handle isn't truly Constable
    • nop

      void nop()
      Append an instruction which does nothing, which can be useful for debugging.
    • addInnerClass

      ClassMaker addInnerClass(String className)
      Add an inner class to this method. The actual class name will have a suitable suffix applied to ensure uniqueness. The inner class doesn't have access to the local variables of the enclosing method, and so they must be passed along explicitly.

      The returned ClassMaker instance isn't attached to this maker, and so it can be acted upon by a different thread.

      Parameters:
      className - simple class name; pass null to use default
      Throws:
      IllegalArgumentException - if not given a simple class name
      IllegalStateException - if the enclosing class or method is finished
    • finish

      MethodHandle finish()
      Finishes the definition of a standalone method.
      Throws:
      IllegalStateException - if already finished or if not a standalone method
      See Also: