Skip to content

ClassBuilder.MethodBuilder ​

com.jsmacrosce.jsmacros.core.library.impl.classes.ClassBuilder.MethodBuilder

Builds a method of the generated class. A script obtains one from addMethod(Class, String, Class...) or addConstructor(Class...). These methods configure its signature, modifiers, exceptions, annotations, and how it is added. Use body(String) for Java body source,
guestBody for a script handler,
buildBody() to combine Java statements and handler calls,
body(MethodWrapper) to customize the Javassist representations,
compile(String) to add a complete Java method declaration, or endAbstract() to add an abstract method.

js
// a plain method, with a body written in java
const describe = builder.addMethod(Reflection.getClass("java.lang.String"), "describe",  
 Reflection.getClass("java.lang.Object"));  
describe.makePublic();  
describe.body("return \"an object: \" + $1;");  

// an abstract one, for a class that leaves it to a subclass
const reset = builder.addMethod(Reflection.getClass("void"), "reset");  
reset.makePublic();  
reset.endAbstract();

Nested type declared in ClassBuilder.

Constructors ​

new ClassBuilder.MethodBuilder ​

new ClassBuilder.MethodBuilder(ClassBuilder<T> classBuilder, CtClass methodReturnType, String methodName, CtClass... params)#
Creates the internal builder initialized with a method return type, name, and parameter types. The class builder uses it for methods and constructors.

Parameters:

  • methodReturnType: the type the method returns
  • methodName: the name of the method
  • params: the parameter types, in order

Methods ​

addAnnotation ​

Starts an annotation for this method. The returned builder commits it when finished; annotations with runtime retention are stored separately from other retention types.

Parameters:

  • type: the annotation type to put on the method
Returns: a builder for the annotation, which is put on the method when it is finished

body ​

body(String code_src): ClassBuilder<T>#
Adds the configured method with a body written as Java source and returns the class builder. The argument is the method body without braces; its signature and modifiers come from this builder. This form does not provide a callback into the building script; use guestBody or buildBody() for script callbacks. Generic methods and varargs are not supported in the body source.

Parameters:

  • code_src: the source of the inside of the method, without the braces
Returns: the class builder, so more members can be added
body(MethodWrapper<CtClass, CtBehavior, Object, ?> buildBody): ClassBuilder<T>#
Lets a script handler edit the class and method representations before the method is added, then returns the class builder. The handler runs synchronously on the calling thread.

Parameters:

  • buildBody: the handler, given the class and the method to finish, made with JavaWrapper.methodToJava
Returns: the class builder, so more members can be added

buildBody ​

Adds the configured method with an initially empty body and returns a builder for composing Java statements and script-handler calls. Handler calls are statements; use the token arguments to appendGuestCode to place their result in Java source. Unlike guestBody, this form does not automatically convert a handler result to the method's return type.
Returns: a builder for the body, whose finish call hands back the class builder

compile ​

compile(String code): ClassBuilder<T>#
Adds a complete method declaration written in Java source and returns the class builder. The declaration supplies the method signature and modifiers; this builder's configured signature and modifiers are not used.
builder.addMethod(Reflection.getClass("boolean"), "isEmpty")
  .compile("public boolean isEmpty() { return this.size() == 0; }");

Parameters:

  • code: the source of the whole method, not just its body
Returns: the class builder, so more members can be added

endAbstract ​

endAbstract(): ClassBuilder<T>#
Adds an abstract method with the configured parameter types, declared exceptions, and method annotations. For a class, ensure the class is abstract; the method visibility must also be configured as needed.
// what an interface method looks like
const required = builder.addMethod(Reflection.getClass("void"), "reset");
required.makePublic();
required.endAbstract();
Returns: the class builder, so more members can be added

exceptions ​

exceptions(Class<?>... exceptions): ClassBuilder.MethodBuilder#
Sets the method's declared exception types. This records the declaration; it does not throw an exception or change what the method body can throw at runtime.

Parameters:

  • exceptions: the exception types to declare
Returns: self for chaining

guestBody ​

guestBody(MethodWrapper<Object, Object, Object, ?> methodBody): ClassBuilder<T>#
Adds the configured method with a body that calls the supplied script handler. The handler receives the instance followed by an array of method arguments; primitive arguments are boxed, and a primitive return value is unboxed. Supply a JavaWrapper.methodToJava wrapper. Use buildBody() to mix Java statements with handler calls.

Parameters:

  • methodBody: the handler, made with JavaWrapper.methodToJava
Returns: the class builder, so more members can be added

makePackagePrivate ​

makePackagePrivate(): ClassBuilder.MethodBuilder#
Makes the method package-private, preserving its static flag and clearing any previous visibility.
Returns: self for chaining

makePrivate ​

Makes the method private, preserving its static flag. Visibility calls replace any previously selected visibility.
Returns: self for chaining

makeProtected ​

Makes the method protected, preserving its static flag and replacing any previous visibility.
Returns: self for chaining

makePublic ​

Makes the method public, preserving its static flag and replacing any previous visibility.
Returns: self for chaining

rename ​

Sets the name used when this method is added. If called more than once, the last name is used.

Parameters:

  • newName: the name to add the method under
Returns: self for chaining

toggleStatic ​

Toggles the static flag. A static method has no instance; its Java body cannot use this, and a guestBody handler receives no instance reference.
Returns: self for chaining