ClassBuilder.BodyBuilder
com.jsmacrosce.jsmacros.core.library.impl.classes.ClassBuilder.BodyBuilder
Builds a method or constructor body from ordered Java statements and calls back into the script. buildBody() and buildBody() return this builder; the body is written only when finish is called. Unlike guestBody, this permits Java statements around guest calls. A guest call is emitted as a statement, so use its token to place the result, such as in a local variable.
js
const bump = builder.addMethod(Reflection.getClass("void"), "bump");
bump.makePublic();
bump.buildBody()
.appendJavaCode("int before = this.total")
.appendGuestCode(JavaWrapper.methodToJava(function (self, args) {
return 1;
}), "", "Object step = ")
.appendJavaCode("this.total = before + ((java.lang.Integer) step).intValue()")
.finish();Nested type declared in ClassBuilder.
Methods
appendGuestCode
appendGuestCode(MethodWrapper<Object, Object, Object, ?> code, String argsAsObjects, String tokenBefore): ClassBuilder.BodyBuilder#Appends a call to a script handler. The handler receives the instance (or
null for a static method or class initializer) and an object array of arguments; box primitive arguments in argsAsObjects. The generated call is a statement, not an expression: use tokenBefore to place its result, or leave the result unused. Pass a handler wrapped with JavaWrapper.methodToJava. // a value put on a local, then used by the java around it
.appendGuestCode(JavaWrapper.methodToJava(function (self, args) {
return self.size();
}), "", "Object size = ")
.appendJavaCode("System.out.println(size)")
// a primitive argument boxed, and a value thrown away
.appendGuestCode(JavaWrapper.methodToJava(function (self, args) {
print(`a total of ${self.total}`);
}), "java.lang.Integer.valueOf($1)", "")Parameters:
code: the handler wrapped withJavaWrapper.methodToJavaargsAsObjects: comma-separated Java source for the handler arguments, without bracketstokenBefore: Java source to prepend to the handler call, ornullfor none
Returns: self for chaining
appendJavaCode
appendJavaCode(String code): ClassBuilder.BodyBuilder#Appends Java source to the body and adds a semicolon and newline. Supply the statement content without a trailing semicolon; any variables used by a later guest call must be declared here.
.appendJavaCode("int before = this.total")
.appendJavaCode("this.total = before + 1")
.appendJavaCode("if (this.total == 10) { this.total = 0; }")Parameters:
code: the inside of a java statement, without the semicolon
Returns: self for chaining
finish
finish(): ClassBuilder<T>#Writes the assembled body into its method or constructor and returns the class builder. Compilation failures are reported here as CannotCompileException; without this call, the method or constructor retains its existing empty body.
Returns: the class builder, so more members can be added