ProxyBuilder
com.jsmacrosce.jsmacros.core.library.impl.classes.ProxyBuilder
Builds subclass or interface proxies whose selected methods are handled by script callbacks.
Unmatched methods proceed to the superclass implementation. Obtain a builder with Reflection.createClassProxyBuilder, register handlers, and construct an instance.
Handlers receive a ProxyReference and boxed method arguments; primitive numeric returns are converted to the declared type. Use JavaWrapper.methodToJava for script callbacks.
For void methods, declared exceptions and runtime exceptions are rethrown; other handler exceptions are suppressed after being logged to the script context, or printed to standard error when no context exists. Exceptions from non-void handlers propagate through the call. The examples demonstrate interface and superclass proxies, overload fallback, and calling the overridden implementation.
// an interface is proxied too, with Object underneath it
const runnable = Reflection.getClass("java.lang.Runnable");
const builder = Reflection.createClassProxyBuilder(runnable);
// a bare name covers every overload of that name. a full signature covers
// just the one, and is what wins when both would match
builder.addMethod("run", JavaWrapper.methodToJava(function (ref, args) {
print("the proxy ran");
}));
const task = builder.buildInstance([]);
task.run();
// extending a concrete class is the same builder. `self` is the instance
// the method was called on and `parent` reaches the real one, so the parts
// that were not named here still behave normally
const listBuilder = Reflection.createClassProxyBuilder(
Reflection.getClass("java.util.ArrayList"));
listBuilder.addMethod("toString", JavaWrapper.methodToJava(function (ref, args) {
return `a proxy over a list of ${ref.self.size()}`;
}));
const list = listBuilder.buildInstance([]);
list.add("diamond");
print(list.toString());Type Parameters
T: the type of the class being extended, which is what the built instances come back as
Nested types
Constructors
new ProxyBuilder
clazz is the superclass; for an interface, it is added to interfaces and Object is used as the superclass. Register method handlers and construct instances afterward. Usually obtained through Reflection.createClassProxyBuilder.Parameters:
clazz: class to extend or interface to add to the proxyinterfaces: additional interfaces implemented by the proxy
Fields
factory
proxiedMethodDefaults
proxiedMethodDefaults: Map<String, MethodWrapper<ProxyBuilder.ProxyReference<T>, Object[], ?, ?>>read-only#proxiedMethods
proxiedMethods: Map<ProxyBuilder.MethodSigParts, MethodWrapper<ProxyBuilder.ProxyReference<T>, Object[], ?, ?>>read-only#Methods
addMethod
addMethod(String methodNameOrSig, MethodWrapper<ProxyBuilder.ProxyReference<T>, Object[], ?, ?> proxyMethod): ProxyBuilder<T>Since1.6.0#size()I or indexOf(Ljava/lang/Object;)I. Handlers receive a ProxyReference and boxed arguments; numeric results are converted to primitive return types. Use JavaWrapper.methodToJava for script callbacks. const builder = Reflection.createClassProxyBuilder(
Reflection.getClass("java.util.ArrayList"));
// every `add`, whatever it is declared as
builder.addMethod("add", JavaWrapper.methodToJava(function (ref, args) {
print(`adding ${args.length} argument(s)`);
return ref.parent.apply(args as any);
}));
// the same method given both ways. a signature is looked for first, so
// `size()I` is the handler that runs, and the bare name is only a
// fallback for any other `size` the class turns out to have
builder.addMethod("size", JavaWrapper.methodToJava(function (ref, args) {
return 0;
}));
builder.addMethod("size()I", JavaWrapper.methodToJava(function (ref, args) {
print(`the real size, which is ${ref.parent.apply([] as any)}`);
return ref.parent.apply([] as any);
}));Parameters:
methodNameOrSig: bare method name or JVM method signatureproxyMethod: handler for matching method calls
buildInstance
null: inspecting their runtime types throws NullPointerException. For an interface proxy, the superclass is Object and its no-argument constructor is used. If no compatible constructor is found, this throws NoSuchMethodException. // the constructor is worked out from what is passed, so this is
// java.util.ArrayList's no argument one
const list = builder.buildInstance([]);
list.add("diamond");
// and this is the one taking a single element
const started = builder.buildInstance(["diamond"]);
print(`${started.size()} from the start`);Parameters:
constructorArgs: arguments passed to the selected superclass constructor
V; the <init> name may be omitted. Unlike buildInstance(java.lang.Object[]), this selects the constructor by declared parameter types, which can disambiguate arguments such as null. A referenced class that cannot be resolved causes ClassNotFoundException. // a constructor taking a String and an int, named rather than inferred
const started = builder.buildInstance("(Ljava/lang/String;I)V", ["diamond", 3]);Parameters:
constructorSig: JVM constructor signatureconstructorArgs: arguments passed to the selected superclass constructor
// the types and the values, side by side. this is the way to reach a
// constructor whose parameters the arguments alone would not pick out
const started = builder.buildInstance(
[Reflection.getClass("java.lang.String"), Reflection.getClass("int")],
["diamond", 3]);Parameters:
constructorSig: parameter types of the superclass constructorconstructorArgs: arguments passed to that constructor