Class JavaCodeGenerator
java.lang.Object
io.jans.service.custom.script.jit.JavaCodeGenerator
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassHelper class to generate classesclassHelper class to build methodsstatic classSimple scope for generation of shadowed local variables -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new JavaCodeGenerator that writes to the specified writer -
Method Summary
Modifier and TypeMethodDescriptionBegins a new blockboundedLoop(String indexVar, String lowerBound, String upperBound, Consumer<JavaCodeGenerator> body) Simple bounded loop over an int index variablevoidEmmit a single-line comment.Declares an uninitialized local variableDeclares an initialized local variabledeclareExisting(String name) Declare a pre-existing variable in the current scope.static StringdeclareOnScope(JavaCodeGenerator.Scope scope, String type, String name, String initialValue) Declares an initialized local variable at the beginning of the current scope.declareOnScope(String type, String name, String initialValue) Declares an initialized local variable at the beginning of the current scope.endBlock()Ends the current blockFore-each loop over an iterablevoidClose all open scopes and generate the codevoidgenerateImport(Class<?> aClass) Generates an importgetScope()ifElse(String condition, Consumer<JavaCodeGenerator> accept, Consumer<JavaCodeGenerator> reject) Generate an if-else statementifElse(Consumer<JavaCodeGenerator> condition, Consumer<JavaCodeGenerator> accept, Consumer<JavaCodeGenerator> reject) Generate an if-else statementifThen(String condition, Consumer<JavaCodeGenerator> accept) Generates a simple if-then statementifThen(Consumer<JavaCodeGenerator> condition, Consumer<JavaCodeGenerator> accept) Generates a simple if-then statementloop(Consumer<JavaCodeGenerator> body) Unbounded looppackageMethod(String returnType, String name) Create a private methodparent()Prints an object.Prints a formatted string using the specified format string and arguments.printfOnScope(String format, Object... args) Prints the specified code at the beginning of the current scope.println()Print a new linePrints an Object and then terminates the line.Print the specified string so it is a valid java string literal.privateMethod(String returnType, String name) Create a private methodpublicClass(String name) Build a new public classpublicMethod(String returnType, String name) Create a public method
-
Constructor Details
-
JavaCodeGenerator
public JavaCodeGenerator()Creates a new JavaCodeGenerator that writes to the specified writer
-
-
Method Details
-
unique
- Returns:
- the name with a unique integer appended. The number is unique only for the current scope.
-
parent
- Returns:
- this code generator's parent or null if top-level
-
generateImport
Generates an import -
declare
Declares an uninitialized local variable -
declareExisting
Declare a pre-existing variable in the current scope. No code is emitted. -
declare
Declares an initialized local variable -
declareOnScope
Declares an initialized local variable at the beginning of the current scope. Useful to declare temporary variables on complex expressions. -
declareOnScope
public static String declareOnScope(JavaCodeGenerator.Scope scope, String type, String name, String initialValue) Declares an initialized local variable at the beginning of the current scope. Useful to declare temporary variables on complex expressions.- Returns:
- the resolved name of the variable
-
printfOnScope
Prints the specified code at the beginning of the current scope. Useful to initialize temporary variables on complex expressions. -
boundedLoop
public JavaCodeGenerator boundedLoop(String indexVar, String lowerBound, String upperBound, Consumer<JavaCodeGenerator> body) Simple bounded loop over an int index variable -
loop
Unbounded loop -
forEach
public JavaCodeGenerator forEach(String type, String elementVar, String collection, Consumer<JavaCodeGenerator> body) Fore-each loop over an iterable -
comment
Emmit a single-line comment. Supports new lines. -
variable
- Returns:
- the mangled name of the variable if it's shadowing another on an outer scope
-
printQuoted
Print the specified string so it is a valid java string literal. -
ifThen
Generates a simple if-then statement -
ifThen
public JavaCodeGenerator ifThen(Consumer<JavaCodeGenerator> condition, Consumer<JavaCodeGenerator> accept) Generates a simple if-then statement -
ifElse
public JavaCodeGenerator ifElse(String condition, Consumer<JavaCodeGenerator> accept, Consumer<JavaCodeGenerator> reject) Generate an if-else statement -
getScope
- Returns:
- the current scope
-
ifElse
public JavaCodeGenerator ifElse(Consumer<JavaCodeGenerator> condition, Consumer<JavaCodeGenerator> accept, Consumer<JavaCodeGenerator> reject) Generate an if-else statement -
generate
Close all open scopes and generate the code -
beginBlock
Begins a new block -
endBlock
Ends the current block -
println
Print a new line -
println
Prints an Object and then terminates the line. -
print
Prints an object. -
printf
Prints a formatted string using the specified format string and arguments. -
publicClass
Build a new public class -
packageMethod
Create a private method -
privateMethod
Create a private method -
publicMethod
Create a public method
-