Class Wrapper
- java.lang.Object
-
- org.glassfish.pfl.dynamic.codegen.spi.Wrapper
-
public final class Wrapper extends java.lang.Object
Main API for runtime code generation. This API generates bytecode dynamically at runtime, allowing direct construction of Class instances from the generated bytecodes. Such classes can then be instantiated and used in a running program. This can be used for a variety of advanced applications aimed at extending the Java language or providing significant performance enhancements.The supported language is best thought of as a subset of Java 1.0.2. The following features from Java 5 and earlier are currently NOT supported:
- Generics.
- Annotations (this may be added at some point).
- For loops (of any sort, but this may be added through extension at some point).
- Enums.
- Autoboxing.
- Varargs methods.
- Nested classes.
- Compiler generated default constructors.
- Compiler generated calls to super() in a constructor.
- Anonymous classes.
- do loops.
- Static fields in interfaces.
- Labels, or labelled break or continue.
As much as possible, this class uses standard Java keywords prefixed by "_" as method names. This helps to remember what the method name is for creating an if statement (_if of course). All public methods are static, and using static imports for this class is advisable: simple use
import static org.glassfish.dynamic.codegen.spi.Wrapper.*
to get easy access to all of the _xxx methods.A typical use of this class looks something like:
_clear() ; // clean up any state from previous uses // optional: _setClassLoader(cl) ; // If you need a specific ClassLoader, set it early // so that your generated code can be checked against // those classes that it references. _package( "YYY" ) ; // set the package to use for the generated code _import( "AAA.BBB" ) ; // repeat as needed. As in Java, this makes "BBB" // a synonym for "AAA.BBB". Collisions are not // permitted. _class( ... ) ; // _interface is used for interfaces. _data( ... ) ; // define the class data members (repeat as needed) _initializer() ; // define a static initializer _method( ... ) ; // define a method (use _constructor for a constructor) _arg( ... ) ; // repeat as needed for the method arguments. _body() ; // begin the method body definition. // define the contents of the body using any of the available // statements. _end() ; // of method _end() ; // of class _end() ; // of package Class<?> newClass = _generate( ... ) ;
Alternatively, the last line could be
which makes it easy to create an instance of the generated class by calling:GenericClass<T> gc = _generate( T.class, props ) ;
T instance = gc.create( <constructor args> ) ;
Currently only one class can be defined at a time. There is a grammar defined below that gives more detail. One small note: it is necessary to call _expr( expr ) in order to convert expr into a statement that is incorporated into a body. If this call is omitted, the generated code will NOT contain expr, leading to a difficult to find bug. I will probably fix this eventually, and treat all dangling expressions as errors.
This class provides many short utility methods that operate against a current context that is maintained in a ThreadLocal. This removes the bookkeeping that would otherwise be needed to keep track of various internal factories needed for generating statements and expressions. Note that this class is designed to be statically imported.
The point of view of the API of this class is that calls to create expressions are nested, while calls to create statements are not. That is, constructing an expression will often lead to nested method calls, but constructing a statement is done with a sequence of method calls. Note that everything could be done as a single nested method call per class, but this is unnatural and hard to debug in Java (Java is not Lisp, but you could make it look sort of like Lisp if you tried hard enough).
All of the expression methods can be called any place where an expression is valid, which basically means anywhere a statement can be defined. The other methods can only be called if the sequence of method calls is contained in the language defined by the following grammar:
START : _package IMPORTS CLASS _end ; IMPORTS : _import IMPORTS | empty ; CLASS : _class CDECLS _end ; CDECLS : CDECL CDECLS | empty ; CDECL : _data | _initializer STMTS _end | METHOD | CONSTRUCTOR ; METHOD : _method ARGS _body STMTS _end ; CONSTRUCTOR : _constructor ARGS _body STMTS _end ; ARGS : _arg ARGS | empty ; STMTS : STMT STMTS | empty ; STMT : IF | TRY | SWITCH | WHILE | SIMPLE ; IF : _if STMTS _end | _if STMTS _else STMTS _end ; TRY : _try STMTS CATCHES _end | _try STMTS _finally STMTS _end | _try STMTS CATCHES _finally STMTS _end ; CATCHES : _catch STMTS CATCHES | empty ; SWITCH : _switch CASES | _switch CASES _default STMTS _end ; CASES : _case STMTS CASES | empty ; XXX modify this to support multiple case labels per statement list WHILE : _while STMTS _end ; SIMPLE : _assign | _define | _return | _throw | _expr ;
Any method that requires an expression can use any expression so long as all variables references in the expression are still in scope. This means that:
- Variables representing data members can be referenced anywhere between the _class _body and _end.
- Variables representing method parameters can be referenced anywhere between the _method _body and _end.
- Variables representing _define statements can be referenced with in the scope of their block (that is, until the corresponding "}" in java. Here we don't use "{}" to delimit scope in the generated code).
- Variables representing _catch statements can be referenced until the next corresponding _catch, _finally, or _end. Any attempt to use an Expression that references a Variable that is out of scope will result in an IllegalStateException.
The _classGenerator() method is used here to get the ClassGeneratorImpl, which can then be used in the CodeGenerator API to create source code or byte code directly. Also note that Util.display can be used to dump the contents of the ClassGeneratorImpl for debugging purposes.
For convenience, methods are provided to display the AST (@see _displayAST), generate source code (@see _sourceCode), and generate byteCode (@see _byteCode).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Wrapper.BodyContext
private static class
Wrapper.ClassContext
private static class
Wrapper.Context
private static class
Wrapper.Environment
private static class
Wrapper.IfStatementContext
private static class
Wrapper.MethodContext
private static class
Wrapper.Operation
private static class
Wrapper.PackageContext
private static class
Wrapper.SwitchStatementContext
private static class
Wrapper.TryStatementContext
private static class
Wrapper.WhileStatementContext
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
CLASS_GENERATION_DIRECTORY
Set this to enable dumping the generated byte codes to a class file in the given directory.private static java.lang.String
CODEGEN_PREFIX
private static java.lang.String
DEBUG_PREFIX
static java.lang.String
DUMP_AFTER_SETUP_VISITOR
Debugging option used to dump the contents of the AST after the setup visitor runs.static java.lang.String
DUMP_CONSTANT_POOL
Causes contents of constant pool to be dumped.private static StateEngine
engine
private static State
S_BODY
private static State
S_CLASS
private static State
S_DEFAULT
private static State
S_DONE
private static State
S_ELSE
private static State
S_FINAL
private static State
S_IF
private static State
S_INIT
private static State
S_METHOD
private static State
S_PACKAGE
private static State
S_SWITCH
private static State
S_TRY
static java.lang.String
SOURCE_GENERATION_DIRECTORY
Option used to enable generation of source files while generating bytecode.private static java.lang.ThreadLocal<Wrapper.Environment>
tl
static java.lang.String
TRACE_BYTE_CODE_GENERATION
Debugging option used to trace the byte code generation.static java.lang.String
USE_ASM_VERIFIER
Debugging option used to enable the ASM verifier, which can be helpful for debugging errors in the code generation.
-
Constructor Summary
Constructors Modifier Constructor Description private
Wrapper()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static Expression
_arg(Type type, java.lang.String name)
Add an argument to the current method.static void
_assign(Expression var, Expression expr)
Indicates an assignment statement of the form var = expr.private static Expression
_binary(Expression left, ExpressionFactory.BinaryOperator op, Expression right)
static void
_body()
Indicates the start of the definition of the body of a method.static Type
_boolean()
Return a representation of the boolean type.(package private) static Type
_byte()
Return a representation of the byte type.static byte[]
_byteCode(java.lang.ClassLoader cl, java.util.Properties options)
Generate byte codes for the current ClassGenerator.static byte[]
_byteCode(ClassGenerator cgen, java.lang.ClassLoader cl, java.util.Properties options)
Generate byte codes for the ClassGenerator.static Expression
_call(Expression target, java.lang.String ident, java.util.List<Expression> args)
Generate a call to an instance method, using the Java method overload resolution algorithm to determine the signature.static Expression
_call(Expression target, java.lang.String ident, Expression... args)
Generate a call to an instance method, using the Java method overload resolution algorithm to determine the signature.static Expression
_call(Expression target, java.lang.String ident, Signature signature, java.util.List<Expression> args)
Generate a call to an instance method.static Expression
_call(Expression target, java.lang.String ident, Signature signature, Expression... args)
Generate a call to an instance method.static Expression
_call(Type target, java.lang.String ident, java.util.List<Expression> args)
Generate a call to a static method, using the Java method overload resolution algorithm to determine the signature.static Expression
_call(Type target, java.lang.String ident, Expression... args)
Generate a call to a static method, using the Java method overload resolution algorithm to determine the signature.static Expression
_call(Type target, java.lang.String ident, Signature signature, java.util.List<Expression> args)
Generate a call to a static method.static Expression
_call(Type target, java.lang.String ident, Signature signature, Expression... args)
Generate a call to a static method.static Expression
_cast(Type type, Expression expr)
Create an expression representing the type cast of expr to type.static Expression
_catch(Type type, java.lang.String name)
Indicate the start of a catch clause in a try statement.(package private) static Type
_char()
Return a representation of the char type.static void
_class(int modifiers, java.lang.String name, Type superClass, java.util.List<Type> impls)
Define a class.static void
_class(int modifiers, java.lang.String name, Type superClass, Type... impls)
Define a class.static Type
_Class()
Return a representation of the java.lang.Class type.static ClassGenerator
_classGenerator()
Obtain the ClassGeneratorImpl that is constructed by the Wrapper methods.static void
_clear()
Discard the current Class generated by Wrapper calls, so that another Class may be generated.static Expression
_const(boolean c)
Return a constant expression representing the value c.static Expression
_const(byte c)
Return a constant expression representing the value c.static Expression
_const(char c)
Return a constant expression representing the value c.static Expression
_const(double c)
Return a constant expression representing the value c.static Expression
_const(float c)
Return a constant expression representing the value c.static Expression
_const(int c)
Return a constant expression representing the value c.static Expression
_const(long c)
Return a constant expression representing the value c.static Expression
_const(short c)
Return a constant expression representing the value c.static Expression
_const(java.lang.String c)
Return a constant expression representing the value c.static Expression
_const(Type c)
Return a constant expression representing the value c.static void
_constructor(int modifiers, java.util.List<Type> exceptions)
Begin defining a constructor in the current class.static void
_constructor(int modifiers, Type... exceptions)
Begin defining a constructor in the current class.static Expression
_data(int modifiers, Type type, java.lang.String name)
Define a data member in a class.static Expression
_define(Type type, java.lang.String name, Expression expr)
Indicates the introduction of a new local variable initialized to the given expression.static void
_displayAST(ClassGenerator cg, java.io.PrintStream ps)
Dump the contents of the AST for the current Class defined by Wrapper calls.(package private) static Type
_double()
Return a representation of the double type.static void
_else()
Indicate the start of the false branch of an if statement.static void
_end()
Terminates the definition of the current statement, method, constructor, initializer, class, or package.static void
_expr(Expression expr)
Indicate that expr should be executed as a statement for its side effects.static Expression
_field(Expression expr, java.lang.String fieldName)
Return an expression used to access a field in an object given by expr.static Expression
_field(Type type, java.lang.String fieldName)
Return an expression used to access a static data member in a class given by the type.static void
_finally()
Indicate the start of a finally clause in a try statement.(package private) static Type
_float()
Return a representation of the float type.static java.lang.Class<?>
_generate(java.lang.Class<?> anchorClass, java.util.Properties props)
Generate a class for the current ClassGenerator, in the same classloader and package as a specified "anchor" class to which the caller has access.static java.lang.Class<?>
_generate(java.lang.Class<?> anchorClass, java.util.Properties props, java.io.PrintStream ps)
Generate a class for the current ClassGenerator, in the same classloader and package as a specified "anchor" class to which the caller has access.static java.lang.Class<?>
_generate(java.lang.ClassLoader cl, java.security.ProtectionDomain pd, java.util.Properties props)
Deprecated.as of Java 11, use_generate(Class, Properties)
static java.lang.Class<?>
_generate(java.lang.ClassLoader cl, java.security.ProtectionDomain pd, java.util.Properties props, java.io.PrintStream ps)
Deprecated.as of Java 11, use_generate(Class, Properties, PrintStream)
private static java.lang.Class<?>
_generate(ClassGenerator cg, java.lang.Class<?> anchorClass, java.util.Properties props, java.io.PrintStream ps)
Generate a class for the current ClassGenerator.private static java.lang.Class<?>
_generate(ClassGenerator cg, java.lang.ClassLoader cl, java.security.ProtectionDomain pd, java.util.Properties props, java.io.PrintStream ps)
Generate a class for the current ClassGenerator.static void
_if(Expression expr)
Indicate the start of an if statement with the given expression as the condition.static ImportList
_import()
Return an ImportList that can be shared across multiple class generations.static Type
_import(java.lang.String name)
Used to create short names for types.static void
_import(ImportList ilist)
Set the ImportList for the current class generation.static Expression
_index(Expression expr, Expression index)
Return an expression used to access an element in an array given by expr.static Type
_int()
Return a representation of the int type.static void
_interface(int modifiers, java.lang.String name, java.util.List<Type> impls)
Define an interface.static void
_interface(int modifiers, java.lang.String name, Type... impls)
Define an interface.(package private) static Type
_long()
Return a representation of the long type.static void
_method(int modifiers, Type type, java.lang.String name, java.util.List<Type> exceptions)
Begin defining a method in the current class.static void
_method(int modifiers, Type type, java.lang.String name, Type... exceptions)
Begin defining a method in the current class.static Expression
_ne(Expression left, Expression right)
Create an expression representing the application of the != operator to the left and right expressions in the form (left op right).static Expression
_new(Type type, java.util.List<Expression> args)
Create an expression representing the construction of a new instance of the given type using the constructor with the signature determined by the Java method overload resolution algorithm and the list of expressions as arguments.static Expression
_new(Type type, Expression... args)
Create an expression representing the construction of a new instance of the given type using the constructor with the signature determined by the Java method overload resolution algorithm and the list of expressions as arguments.static Expression
_new(Type type, Signature signature, java.util.List<Expression> args)
Create an expression representing the construction of a new instance of the given type using the constructor with the given signature and the list of expressions as arguments.static Expression
_new(Type type, Signature signature, Expression... args)
Create an expression representing the construction of a new instance of the given type using the constructor with the given signature and the list of expressions as arguments.static Expression
_new_array_init(Type type, java.util.List<Expression> args)
Create an expression representing the construction of a new array with the given component type using the given expressions to initialize the array.static Expression
_new_array_init(Type type, Expression... args)
Create an expression representing the construction of a new array with the given component type using the given expressions to initialize the array.static Expression
_null()
Return the null expression.static Type
_Object()
Return a representation of the java.lang.Object type.static void
_package()
Same as _package( "" ).static void
_package(java.lang.String name)
_package must be called first to set the package name for this class.static void
_return()
Indicates the end of execution in a method.static void
_return(Expression expr)
Indicates the end of execution in a method with a return of the value of the expression.static Signature
_s(Type rtype, java.util.List<Type> types)
Create a signature that may be used for calling a method or constructor.static Signature
_s(Type rtype, Type... types)
Create a signature that may be used for calling a method or constructor.static void
_setClassLoader(java.lang.ClassLoader cl)
Set the ClassLoader for this thread that will be used for validating references to pre-existing classes from generated code.(package private) static Type
_short()
Return a representation of the short type.static void
_sourceCode(java.io.PrintStream ps, java.util.Properties options)
Generate the Java source code for the current Class defined by Wrapper calls.static void
_sourceCode(java.util.Properties options)
Generate source code into a specified output directory.static void
_sourceCode(ClassGenerator cg, java.io.PrintStream ps, java.util.Properties options)
Generate the Java source code for the ClassGenerator.static void
_sourceCode(ClassGenerator cg, java.util.Properties options)
Generate source code into a specified output directory.static Type
_String()
Return a representation of the java.lang.String type.static Expression
_super(java.lang.String ident, java.util.List<Expression> exprs)
Generate a call to an instance method in the current super class using the Java method overload resolution algorithm to determine the signature.static Expression
_super(java.lang.String ident, Expression... exprs)
Generate a call to an instance method in the current super class using the Java method overload resolution algorithm to determine the signature.static Expression
_super(java.lang.String ident, Signature signature, java.util.List<Expression> exprs)
Generate a call to an instance method in the current super class.static Expression
_super(java.lang.String ident, Signature signature, Expression... exprs)
Generate a call to an instance method in the current super class.static Expression
_super(java.util.List<Expression> exprs)
Invoke a superclass constructor as the first statement in a constructor for a class using the Java method overload resolution algorithm to determine the signature.static Expression
_super(Expression... exprs)
Invoke a superclass constructor as the first statement in a constructor for a class using the Java method overload resolution algorithm to determine the signature.static Expression
_super(Signature signature, java.util.List<Expression> exprs)
Invoke a superclass constructor as the first statement in a constructor for a class.static Expression
_super(Signature signature, Expression... exprs)
Invoke a superclass constructor as the first statement in a constructor for a class.static Type
_t(java.lang.String name)
Return the reference type for the given class name.static Expression
_this()
Return an expression representing "this".static Expression
_this(java.util.List<Expression> exprs)
Invoke another constructor as the first statement in a constructor for a class using the Java method overload resolution algorithm to determine the signature.static Expression
_this(Expression... exprs)
Invoke another constructor as the first statement in a constructor for a class using the Java method overload resolution algorithm to determine the signature.static Expression
_this(Signature signature, java.util.List<Expression> exprs)
Invoke another constructor as the first statement in a constructor for a class.static Expression
_this(Signature signature, Expression... exprs)
Invoke another constructor as the first statement in a constructor for a class.static Type
_thisClass()
Return the type of the current class.static void
_throw(Expression expr)
Indicates a throw statement that throws the given expression.static void
_try()
Indicate the start of a try statement.static Expression
_v(java.lang.String name)
Construct the expression that refers to the variable named name.static Type
_void()
Return a representation of the void type.private static void
addCommonTransitions(State state)
private static Wrapper.Environment
env()
static Pair<java.lang.String,java.lang.String>
splitClassName(java.lang.String name)
Split the class name into a pair of the package name and the unqualified class name.
-
-
-
Field Detail
-
S_INIT
private static final State S_INIT
-
S_DONE
private static final State S_DONE
-
S_PACKAGE
private static final State S_PACKAGE
-
S_CLASS
private static final State S_CLASS
-
S_METHOD
private static final State S_METHOD
-
S_BODY
private static final State S_BODY
-
S_IF
private static final State S_IF
-
S_ELSE
private static final State S_ELSE
-
S_TRY
private static final State S_TRY
-
S_FINAL
private static final State S_FINAL
-
S_SWITCH
private static final State S_SWITCH
-
S_DEFAULT
private static final State S_DEFAULT
-
engine
private static final StateEngine engine
-
tl
private static java.lang.ThreadLocal<Wrapper.Environment> tl
-
CODEGEN_PREFIX
private static final java.lang.String CODEGEN_PREFIX
- See Also:
- Constant Field Values
-
DEBUG_PREFIX
private static final java.lang.String DEBUG_PREFIX
- See Also:
- Constant Field Values
-
CLASS_GENERATION_DIRECTORY
public static final java.lang.String CLASS_GENERATION_DIRECTORY
Set this to enable dumping the generated byte codes to a class file in the given directory.- See Also:
- Constant Field Values
-
SOURCE_GENERATION_DIRECTORY
public static final java.lang.String SOURCE_GENERATION_DIRECTORY
Option used to enable generation of source files while generating bytecode. Set to name of directory that should contain the generated source file.- See Also:
- Constant Field Values
-
DUMP_AFTER_SETUP_VISITOR
public static final java.lang.String DUMP_AFTER_SETUP_VISITOR
Debugging option used to dump the contents of the AST after the setup visitor runs.- See Also:
- Constant Field Values
-
TRACE_BYTE_CODE_GENERATION
public static final java.lang.String TRACE_BYTE_CODE_GENERATION
Debugging option used to trace the byte code generation.- See Also:
- Constant Field Values
-
DUMP_CONSTANT_POOL
public static final java.lang.String DUMP_CONSTANT_POOL
Causes contents of constant pool to be dumped.- See Also:
- Constant Field Values
-
USE_ASM_VERIFIER
public static final java.lang.String USE_ASM_VERIFIER
Debugging option used to enable the ASM verifier, which can be helpful for debugging errors in the code generation.- See Also:
- Constant Field Values
-
-
Method Detail
-
addCommonTransitions
private static void addCommonTransitions(State state)
-
env
private static Wrapper.Environment env()
-
_classGenerator
public static ClassGenerator _classGenerator()
Obtain the ClassGeneratorImpl that is constructed by the Wrapper methods. This is only needed for using Wrapper with custom vistors.
-
_setClassLoader
public static void _setClassLoader(java.lang.ClassLoader cl)
Set the ClassLoader for this thread that will be used for validating references to pre-existing classes from generated code. Applications that use special ClassLoaders (such as the app server) should call this method before starting to generate a new Class. A good place to do this is just before the _package call.
-
_byteCode
public static byte[] _byteCode(java.lang.ClassLoader cl, java.util.Properties options)
Generate byte codes for the current ClassGenerator. cl is used to resolve any references to other classes. options may be used to control some aspects of the code generation, such as debugging options. Supported options include DUMP_AFTER_SETUP_VISITOR, TRACE_BYTE_CODE_GENERATION, USE_ASM_VERIFIER.
-
_byteCode
public static byte[] _byteCode(ClassGenerator cgen, java.lang.ClassLoader cl, java.util.Properties options)
Generate byte codes for the ClassGenerator. cl is used to resolve any references to other classes. options may be used to control some aspects of the code generation, such as debugging options. Supported options include DUMP_AFTER_SETUP_VISITOR, TRACE_BYTE_CODE_GENERATION, USE_ASM_VERIFIER.
-
_generate
public static java.lang.Class<?> _generate(java.lang.ClassLoader cl, java.security.ProtectionDomain pd, java.util.Properties props, java.io.PrintStream ps)
Deprecated.as of Java 11, use_generate(Class, Properties, PrintStream)
Generate a class for the current ClassGenerator. Basically equivalent to _byteCode followed by _makeClass. cl is used to resolve any references to other classes and to load the class given by the current ClassGenerator. options may be used to control some aspects of the code generation, such as debugging options.
-
_generate
public static java.lang.Class<?> _generate(java.lang.ClassLoader cl, java.security.ProtectionDomain pd, java.util.Properties props)
Deprecated.as of Java 11, use_generate(Class, Properties)
Generate a class for the current ClassGenerator. Basically equivalent to _byteCode followed by _makeClass. cl is used to resolve any references to other classes and to load the class given by the current ClassGenerator. options may be used to control some aspects of the code generation, such as debugging options.
-
_generate
private static java.lang.Class<?> _generate(ClassGenerator cg, java.lang.ClassLoader cl, java.security.ProtectionDomain pd, java.util.Properties props, java.io.PrintStream ps)
Generate a class for the current ClassGenerator. Basically equivalent to _byteCode followed by _makeClass. cl is used to resolve any references to other classes and to load the class given by the current ClassGenerator. options may be used to control some aspects of the code generation, such as debugging options.
-
_generate
public static java.lang.Class<?> _generate(java.lang.Class<?> anchorClass, java.util.Properties props, java.io.PrintStream ps)
Generate a class for the current ClassGenerator, in the same classloader and package as a specified "anchor" class to which the caller has access.- Parameters:
anchorClass
- an existing class used as a reference for the new one.props
- options to control some aspects of the code generation, such as debugging.ps
- a stream to which debug messages should be written, if any- Since:
- since 4.1.0
-
_generate
public static java.lang.Class<?> _generate(java.lang.Class<?> anchorClass, java.util.Properties props)
Generate a class for the current ClassGenerator, in the same classloader and package as a specified "anchor" class to which the caller has access.- Parameters:
anchorClass
- an existing class used as a reference for the new one.props
- options to control some aspects of the code generation, such as debugging.- Since:
- since 4.1.0
-
_generate
private static java.lang.Class<?> _generate(ClassGenerator cg, java.lang.Class<?> anchorClass, java.util.Properties props, java.io.PrintStream ps)
Generate a class for the current ClassGenerator. Basically equivalent to _byteCode followed by _makeClass. cl is used to resolve any references to other classes and to load the class given by the current ClassGenerator. options may be used to control some aspects of the code generation, such as debugging options.
-
_sourceCode
public static void _sourceCode(java.io.PrintStream ps, java.util.Properties options) throws java.io.IOException
Generate the Java source code for the current Class defined by Wrapper calls. options may be used to control some aspects of the formatting of the source code, but no options are currently defined. The generate source code is written to the PrintStream.- Throws:
java.io.IOException
-
_sourceCode
public static void _sourceCode(ClassGenerator cg, java.io.PrintStream ps, java.util.Properties options) throws java.io.IOException
Generate the Java source code for the ClassGenerator. options may be used to control some aspects of the formatting of the source code, but no options are currently defined. The generate source code is written to the PrintStream.- Throws:
java.io.IOException
-
_sourceCode
public static void _sourceCode(java.util.Properties options) throws java.io.IOException
Generate source code into a specified output directory. options must set SOURCE_GENERATION_DIRECTORY to the name of the output directory.- Throws:
java.io.IOException
-
_sourceCode
public static void _sourceCode(ClassGenerator cg, java.util.Properties options) throws java.io.IOException
Generate source code into a specified output directory. options must set SOURCE_GENERATION_DIRECTORY to the name of the output directory.- Throws:
java.io.IOException
-
_displayAST
public static void _displayAST(ClassGenerator cg, java.io.PrintStream ps)
Dump the contents of the AST for the current Class defined by Wrapper calls. The AST is dumped to the PrintStream.
-
_clear
public static void _clear()
Discard the current Class generated by Wrapper calls, so that another Class may be generated.
-
_s
public static Signature _s(Type rtype, Type... types)
Create a signature that may be used for calling a method or constructor. rtype is the return type, and types gives all of the argument types. types is empty if there are no arguments.
-
_s
public static Signature _s(Type rtype, java.util.List<Type> types)
Create a signature that may be used for calling a method or constructor. rtype is the return type, and types gives all of the argument types. types is empty if there are no arguments.
-
_t
public static Type _t(java.lang.String name)
Return the reference type for the given class name. name may be either the Class name, if the full name has been imported using an _import statement, or a fully qualified Class name.
-
_void
public static Type _void()
Return a representation of the void type. This is used whenever a method has a void return type.
-
_boolean
public static Type _boolean()
Return a representation of the boolean type.
-
_byte
static Type _byte()
Return a representation of the byte type.
-
_short
static Type _short()
Return a representation of the short type.
-
_char
static Type _char()
Return a representation of the char type.
-
_int
public static Type _int()
Return a representation of the int type.
-
_long
static Type _long()
Return a representation of the long type.
-
_float
static Type _float()
Return a representation of the float type.
-
_double
static Type _double()
Return a representation of the double type.
-
_Object
public static Type _Object()
Return a representation of the java.lang.Object type.
-
_String
public static Type _String()
Return a representation of the java.lang.String type.
-
_Class
public static Type _Class()
Return a representation of the java.lang.Class type.
-
splitClassName
public static Pair<java.lang.String,java.lang.String> splitClassName(java.lang.String name)
Split the class name into a pair of the package name and the unqualified class name. If name is the name of a class in the anonymous global package, the package name is "".
-
_package
public static void _package(java.lang.String name)
_package must be called first to set the package name for this class. After _package, all required _import calls must be made, followed by one _class call. name may be "", in which case the generated class is in the default package.
-
_package
public static void _package()
Same as _package( "" ). Note that this method MUST be called before _class or _interface. This is a bit surprising, since Java allows omission of the package statement, but codegen requires an explicit package call.
-
_import
public static Type _import(java.lang.String name)
Used to create short names for types. Name must be a fully qualified class name. The last name becomes the short name used in _t to look up the full type. The result Type can also be used directly.
-
_import
public static ImportList _import()
Return an ImportList that can be shared across multiple class generations.
-
_import
public static void _import(ImportList ilist)
Set the ImportList for the current class generation. Mainly useful for generating source code.
-
_class
public static void _class(int modifiers, java.lang.String name, Type superClass, Type... impls)
Define a class. This must be followed by calls to _data, _initializer, or _method in any order.
-
_class
public static void _class(int modifiers, java.lang.String name, Type superClass, java.util.List<Type> impls)
Define a class. This must be followed by calls to _data, _initializer, or _method in any order.
-
_interface
public static void _interface(int modifiers, java.lang.String name, Type... impls)
Define an interface. This must be followed by calls to _method only. All _method calls must define abstract methods. Note that static data in interfaces is not currently supported.
-
_interface
public static void _interface(int modifiers, java.lang.String name, java.util.List<Type> impls)
Define an interface. This must be followed by calls to _method only. All _method calls must define abstract methods. Note that static data in interfaces is not currently supported.
-
_data
public static Expression _data(int modifiers, Type type, java.lang.String name)
Define a data member in a class. If static, it must be initialized in the class initializer, otherwise it must be initialized in a constructor.
-
_method
public static void _method(int modifiers, Type type, java.lang.String name, Type... exceptions)
Begin defining a method in the current class. Must be followed first be all _arg calls for the method, then by _body(), then by any statements, and finally by _end(). _body() may be followed immediately by _end(), in which case the method has an empty body. Abstract methods must have an empty body.
-
_method
public static void _method(int modifiers, Type type, java.lang.String name, java.util.List<Type> exceptions)
Begin defining a method in the current class. Must be followed first by all _arg calls for the method, then by _body(), then by any statements, and finally by _end(). _body() may be followed immediately by _end(), in which case the method has an empty body. Abstract methods must have an empty body.
-
_constructor
public static void _constructor(int modifiers, Type... exceptions)
Begin defining a constructor in the current class. Must be followed first by all _arg calls for the constructor, then by _body(), then by optional statements, and finally by _end(). Note that the first statement in any constructor must be a this() or super() call. Constructors may not have empty bodies.
-
_constructor
public static void _constructor(int modifiers, java.util.List<Type> exceptions)
Begin defining a constructor in the current class. Must be followed first by all _arg calls for the constructor, then by _body(), then by any statements, and finally by _end(). Note that the first statement in any constructor must be a _this() or _super() call. Constructors may not have empty bodies. Note that no default constructor is automatically generated using this API.
-
_arg
public static Expression _arg(Type type, java.lang.String name)
Add an argument to the current method.
-
_body
public static void _body()
Indicates the start of the definition of the body of a method. Must be followed by 0 or more statements, and be terminated by _end().
-
_end
public static void _end()
Terminates the definition of the current statement, method, constructor, initializer, class, or package. Restores the context for the previous definition.
-
_expr
public static void _expr(Expression expr)
Indicate that expr should be executed as a statement for its side effects.
-
_assign
public static void _assign(Expression var, Expression expr)
Indicates an assignment statement of the form var = expr. Var must be an assignable expression, such as a local variable, a non-final field reference, ior an array index expression.
-
_define
public static Expression _define(Type type, java.lang.String name, Expression expr)
Indicates the introduction of a new local variable initialized to the given expression.
-
_return
public static void _return()
Indicates the end of execution in a method. Can only occur inside a definition of a method with a void return type.
-
_return
public static void _return(Expression expr)
Indicates the end of execution in a method with a return of the value of the expression. The type of the expression must be assignment compatible with the return type of the enclosing method.
-
_throw
public static void _throw(Expression expr)
Indicates a throw statement that throws the given expression. The type of expr must support Throwable.
-
_if
public static void _if(Expression expr)
Indicate the start of an if statement with the given expression as the condition. All subsequent statements until the next matching _else are part of the true branch of this if statement.
-
_else
public static void _else()
Indicate the start of the false branch of an if statement. All subsequent statements until the next matching _end are part of the false branch of the corresponding if statement.
-
_try
public static void _try()
Indicate the start of a try statement. All subsequent statements until the next matching _catch or _finally are part of this try statement.
-
_catch
public static Expression _catch(Type type, java.lang.String name)
Indicate the start of a catch clause in a try statement. All subsequent statements until the next matching _catch or _finally are part of this catch clause.
-
_finally
public static void _finally()
Indicate the start of a finally clause in a try statement. All subsequent statements until the next matching _end are part of this finally clause.
-
_v
public static Expression _v(java.lang.String name)
Construct the expression that refers to the variable named name. This is looked up following the Java name scope rules.
-
_null
public static Expression _null()
Return the null expression.
-
_const
public static Expression _const(boolean c)
Return a constant expression representing the value c.
-
_const
public static Expression _const(char c)
Return a constant expression representing the value c.
-
_const
public static Expression _const(byte c)
Return a constant expression representing the value c.
-
_const
public static Expression _const(short c)
Return a constant expression representing the value c.
-
_const
public static Expression _const(int c)
Return a constant expression representing the value c.
-
_const
public static Expression _const(long c)
Return a constant expression representing the value c.
-
_const
public static Expression _const(float c)
Return a constant expression representing the value c.
-
_const
public static Expression _const(double c)
Return a constant expression representing the value c.
-
_const
public static Expression _const(java.lang.String c)
Return a constant expression representing the value c.
-
_const
public static Expression _const(Type c)
Return a constant expression representing the value c. Here c is a type, and this corresponds to the Java ".class" reference.
-
_call
public static Expression _call(Expression target, java.lang.String ident, Signature signature, Expression... args)
Generate a call to an instance method. The full signature must be specified.
-
_call
public static Expression _call(Expression target, java.lang.String ident, Signature signature, java.util.List<Expression> args)
Generate a call to an instance method. The full signature must be specified.
-
_call
public static Expression _call(Expression target, java.lang.String ident, Expression... args)
Generate a call to an instance method, using the Java method overload resolution algorithm to determine the signature.
-
_call
public static Expression _call(Expression target, java.lang.String ident, java.util.List<Expression> args)
Generate a call to an instance method, using the Java method overload resolution algorithm to determine the signature.
-
_call
public static Expression _call(Type target, java.lang.String ident, Signature signature, Expression... args)
Generate a call to a static method. The full signature must be specified.
-
_call
public static Expression _call(Type target, java.lang.String ident, Signature signature, java.util.List<Expression> args)
Generate a call to a static method. The full signature must be specified.
-
_call
public static Expression _call(Type target, java.lang.String ident, Expression... args)
Generate a call to a static method, using the Java method overload resolution algorithm to determine the signature.
-
_call
public static Expression _call(Type target, java.lang.String ident, java.util.List<Expression> args)
Generate a call to a static method, using the Java method overload resolution algorithm to determine the signature.
-
_super
public static Expression _super(java.lang.String ident, Signature signature, Expression... exprs)
Generate a call to an instance method in the current super class. The full signature must be specified.
-
_super
public static Expression _super(java.lang.String ident, Signature signature, java.util.List<Expression> exprs)
Generate a call to an instance method in the current super class. The full signature must be specified.
-
_super
public static Expression _super(java.lang.String ident, Expression... exprs)
Generate a call to an instance method in the current super class using the Java method overload resolution algorithm to determine the signature.
-
_super
public static Expression _super(java.lang.String ident, java.util.List<Expression> exprs)
Generate a call to an instance method in the current super class using the Java method overload resolution algorithm to determine the signature.
-
_super
public static Expression _super(Signature signature, Expression... exprs)
Invoke a superclass constructor as the first statement in a constructor for a class. The full signature must be specified. This may only be used as the first expression in a constructor. Every constructor must begin with either a super(...) call or a this(...) call.
-
_super
public static Expression _super(Signature signature, java.util.List<Expression> exprs)
Invoke a superclass constructor as the first statement in a constructor for a class. The full signature must be specified. This may only be used as the first expression in a constructor. Every constructor must begin with either a super(...) call or a this(...) call.
-
_super
public static Expression _super(java.util.List<Expression> exprs)
Invoke a superclass constructor as the first statement in a constructor for a class using the Java method overload resolution algorithm to determine the signature. This may only be used as the first expression in a constructor. Every constructor must begin with either a super(...) call or a this(...) call.
-
_super
public static Expression _super(Expression... exprs)
Invoke a superclass constructor as the first statement in a constructor for a class using the Java method overload resolution algorithm to determine the signature. This may only be used as the first expression in a constructor. Every constructor must begin with either a super(...) call or a this(...) call.
-
_this
public static Expression _this(Signature signature, Expression... exprs)
Invoke another constructor as the first statement in a constructor for a class. The full signature must be specified. This may only be used as the first expression in a constructor. Every constructor must begin with either a super(...) call or a this(...) call.
-
_this
public static Expression _this(Signature signature, java.util.List<Expression> exprs)
Invoke another constructor as the first statement in a constructor for a class. The full signature must be specified. This may only be used as the first expression in a constructor. Every constructor must begin with either a super(...) call or a this(...) call.
-
_this
public static Expression _this(Expression... exprs)
Invoke another constructor as the first statement in a constructor for a class using the Java method overload resolution algorithm to determine the signature. This may only be used as the first expression in a constructor. Every constructor must begin with either a super(...) call or a this(...) call.
-
_this
public static Expression _this(java.util.List<Expression> exprs)
Invoke another constructor as the first statement in a constructor for a class using the Java method overload resolution algorithm to determine the signature. This may only be used as the first expression in a constructor. Every constructor must begin with either a super(...) call or a this(...) call.
-
_binary
private static Expression _binary(Expression left, ExpressionFactory.BinaryOperator op, Expression right)
-
_ne
public static Expression _ne(Expression left, Expression right)
Create an expression representing the application of the != operator to the left and right expressions in the form (left op right).
-
_cast
public static Expression _cast(Type type, Expression expr)
Create an expression representing the type cast of expr to type.
-
_new
public static Expression _new(Type type, Signature signature, Expression... args)
Create an expression representing the construction of a new instance of the given type using the constructor with the given signature and the list of expressions as arguments.
-
_new
public static Expression _new(Type type, Signature signature, java.util.List<Expression> args)
Create an expression representing the construction of a new instance of the given type using the constructor with the given signature and the list of expressions as arguments.
-
_new
public static Expression _new(Type type, Expression... args)
Create an expression representing the construction of a new instance of the given type using the constructor with the signature determined by the Java method overload resolution algorithm and the list of expressions as arguments.
-
_new
public static Expression _new(Type type, java.util.List<Expression> args)
Create an expression representing the construction of a new instance of the given type using the constructor with the signature determined by the Java method overload resolution algorithm and the list of expressions as arguments.
-
_new_array_init
public static Expression _new_array_init(Type type, Expression... args)
Create an expression representing the construction of a new array with the given component type using the given expressions to initialize the array. We really only support single dimensional arrays here. The size of the resulting array is the number of expressions given here.
-
_new_array_init
public static Expression _new_array_init(Type type, java.util.List<Expression> args)
Create an expression representing the construction of a new array with the given component type using the given expressions to initialize the array. We really only support single dimensional arrays here. The size of the resulting array is the number of expressions given here. Equivalent to new A[] = { exprList }.
-
_thisClass
public static Type _thisClass()
Return the type of the current class.
-
_this
public static Expression _this()
Return an expression representing "this".
-
_field
public static Expression _field(Expression expr, java.lang.String fieldName)
Return an expression used to access a field in an object given by expr. This expression may appear on the left side of an assignment statement.
-
_field
public static Expression _field(Type type, java.lang.String fieldName)
Return an expression used to access a static data member in a class given by the type. This expression may appear on the left side of an assignment statement (but probably shouldn't).
-
_index
public static Expression _index(Expression expr, Expression index)
Return an expression used to access an element in an array given by expr. This expression may appear on the left side of an assignment statement.
-
-