Interface MethodMaker
-
Method Summary
Modifier and TypeMethodDescriptionSwitch this method to be abstract.Access aVarHandle
via a pseudo field.addInnerClass
(String className) Add an inner class to this method.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.static MethodMaker
begin
(MethodHandles.Lookup lookup, String name, MethodType type) Begin defining a standalone method, defined in the same nest as the lookup class.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.void
Convenience method which defines an exception handler here.Define an exception handler here, which catches exceptions between the given labels.Define an exception handler here, which catches exceptions between the given labels.class_()
Returns a variable of typeClass
which represents the enclosing class of this method.Concatenate variables and constants together into a newString
in the same matter as the Java concatenation operator.Access a static or instance field in the enclosing object of this method.final_()
Switch this method to be final.void
Define a finally handler which is generated for every possible exit path between the start label and here.finish()
Finishes the definition of a standalone method.Type[]
Returns the parameter types of this method.Returns the return type of this method.void
Generates an unconditional goto statement to the given label, which doesn't need to be positioned yet.invoke
(MethodHandle handle, Object... values) Invoke a method via aMethodHandle
.Invoke a static or instance method on the enclosing object of this method.void
invokeSuperConstructor
(Object... values) Invoke asuper
constructor method on the enclosing object of this method, from within a constructor.void
invokeThisConstructor
(Object... values) Invoke athis
constructor method on the enclosing object of this method, from within a constructor.label()
Returns a new label, initially unpositioned.void
lineNum
(int num) Define a line number to represent the location of the next code instruction.name()
Returns the name of this method.native_()
Switch this method to be native.Allocate a new object.void
nop()
Append an instruction which does nothing, which can be useful for debugging.override()
Verifies that this method overrides an inherited virtual method.param
(int index) Returns a variable which accesses a parameter of the method being built.int
Returns the number of parameters which can be accessed by theparam
method.private_()
Switch this method to be private.Switch this method to be protected.public_()
Switch this method to be public.receiverType
(Type type) Define a receiver type for the sole purpose of annotating the implicitthis
parameter.void
return_()
Generates a return void statement.void
Generates a statement which returns a variable or a constant.Define a signature for this member, which is a string for supporting generic types.static_()
Switch this method to be static.super_()
Returns a variable which is used for invoking superclass methods.Switch this method to be synchronized.Indicate that this method is synthetic.this_()
Returns a variable which accesses the enclosing object of this method.Declare that this method throws the given exception type.Returns a new uninitialized variable with the given type.varargs()
Indicate that this method supports a variable number of arguments.Methods inherited from interface org.cojen.maker.Maker
addAnnotation, addAttribute, classMaker
-
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 objectretType
- a class or name; can be null if the method returns voidname
- method name; use null or "_" if unnamedparamTypes
- classes or names; can be null if the method accepts no parameters- Throws:
IllegalArgumentException
- if a type is unsupported- See Also:
-
begin
Begin defining a standalone method, defined in the same nest as the lookup class.- Parameters:
lookup
- define the method using this lookup objectname
- method name; use null or "_" if unnamedtype
- defines the return type and parameter types- See Also:
-
name
-
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. -
private_
MethodMaker private_()Switch this method to be private. Methods are package-private by default. -
protected_
MethodMaker protected_()Switch this method to be protected. Methods are package-private by default.- Specified by:
protected_
in interfaceMaker
- Returns:
- this
-
static_
MethodMaker static_()Switch this method to be static. Methods are non-static by default.- Specified by:
static_
in interfaceMaker
- Returns:
- this
- Throws:
IllegalStateException
- ifthis
or any parameters have been accessed
-
final_
MethodMaker final_()Switch this method to be final. Methods are non-final by default. -
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. -
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
Define a receiver type for the sole purpose of annotating the implicitthis
parameter.- Parameters:
type
- must be the same asthis
type- Returns:
- this MethodMaker
- Throws:
IllegalStateException
- if not an instance method, or ifthis
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_
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
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. -
class_
-
this_
Variable this_()Returns a variable which accesses the enclosing object of this method.- Throws:
IllegalStateException
- if making a static method
-
super_
-
param
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 theparam
method. -
var
Returns a new uninitialized variable with the given type. Callset
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
-
goto_
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_
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 constantIllegalStateException
- if this method must return void
-
field
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
Invoke a static or instance method on the enclosing object of this method.- Parameters:
name
- the method name enclosing classvalues
-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 constantIllegalStateException
- if the method isn't found- See Also:
-
invokeSuperConstructor
Invoke asuper
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 constantIllegalStateException
- if not defining a constructor, or if a matching constructor isn't found
-
invokeThisConstructor
Invoke athis
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 constantIllegalStateException
- if not defining a constructor, or if a matching constructor isn't found
-
invoke
Invoke a method via aMethodHandle
. If making a class to be loadedexternally
, the handle must be trulyConstable
.- Parameters:
handle
- runtime method handlevalues
-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 constantIllegalStateException
- if defining an external class and the handle isn't trulyConstable
-
new_
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 orClass
instancevalues
-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 constantIllegalStateException
- if the constructor isn't found
-
catch_
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 unsupportedIllegalStateException
- if the start label is unpositioned
-
catch_
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 givenIllegalStateException
- if the start label is unpositioned
-
catch_
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 anythinghandler
- receives a variable which references the exception instance- Throws:
IllegalArgumentException
- if the type is unsupportedIllegalStateException
- if the start label is unpositioned
-
finally_
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
Concatenate variables and constants together into a newString
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
Access aVarHandle
via a pseudo field. If making a class to be loadedexternally
, the handle must be trulyConstable
.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 callingtoMethodHandle
, which is then passed to theinvoke
method.- Parameters:
handle
- runtime variable handlevalues
-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 coordinatesIllegalStateException
- if defining an external class and the handle isn't trulyConstable
-
nop
void nop()Append an instruction which does nothing, which can be useful for debugging. -
addInnerClass
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 nameIllegalStateException
- 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:
-