LibraryBuilder
com.jsmacrosce.jsmacros.core.library.impl.classes.LibraryBuilder
Builds a library class and registers it for use as a script global. The superclass and constructor parameters depend on whether the library is per execution and whether languages were specified. finishBuildAndFreeze() performs the registration; see addConstructor() for its generated constructor contract.
js
const builder = Reflection.createLibraryBuilder("Greetings", true);
builder.addConstructor().body("super($1);");
builder.finishBuildAndFreeze();Constructors
new LibraryBuilder
Configures a generated library class, selecting its superclass and recording its Library alias and resolved language types. Registration happens in finishBuildAndFreeze(), not during construction. A requested extension that is missing or is not a LanguageExtension causes IllegalArgumentException. The resolved languages are handed to the library registry rather than written into the Library annotation. A generated class is defined by the core's class loader, and a class reference in an annotation is resolved against the loader that defined the annotated class, so a reference to a language that lives in an extension's loader could not be read back.
Parameters:
runner: core providing the extensions used for language resolutionname: generated class name and script-global library aliasperExec: whether the library is per execution rather than sharedallowedLangs: language extension names to allow; an empty array imposes no explicit language restriction
Methods
addConstructor
addConstructor(): ClassBuilder.ConstructorBuilder#Adds the library constructor with the parameters the library registry constructs it with: the run context for a per-execution library and the core for a shared one, followed by the language class when languages were specified. The constructor is public. Its body must invoke the matching superclass constructor; when omitted, finishBuildAndFreeze() supplies a body that forwards all parameters.
Returns: a builder for the constructor, which is public and has a body still to be given
addConstructor(Class<?>... params): ClassBuilder.ConstructorBuilder#Starts a constructor builder; the constructor is added when one of its finishing methods is called. A constructor body must call the superclass itself when needed; superclass arguments are not forwarded automatically. Use addClinit() for a static class initializer, which runs when the JVM initializes the class.
const init = builder.addConstructor(Reflection.getClass("java.lang.String"));
init.makePublic();
init.body('super($1); this.label = $1;');Parameters:
params: the parameter types, in order
Returns: a builder for the constructor, which is added to the class when it is finished
Adds a constructor from its complete Java source declaration. Generic types, varargs, and annotations are not supported. Types in
java.lang may be unqualified, as in Java. public MyClass() { }
public MyClass(String text) { System.out.println(text); }
protected MyClass(String text, int number) { super(text, number, ""); }
public MyClass(String text, int number, String other) {
this(text, number);
this.other = other;
}Parameters:
code: the code for the constructor
Returns: self for chaining.
finishBuildAndFreeze
finishBuildAndFreeze(): Class<? extends BaseLibrary>#Builds and freezes the class, generating a constructor that forwards its parameters if none was added, then registers the class with the library registry.
Returns: the generated library class, which is also now registered