Skip to content

ProxyBuilder ​

com.jsmacrosce.jsmacros.core.library.impl.classes.ProxyBuilder

Since1.6.0

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.

js
// 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 ​

new ProxyBuilder(Class<T> clazz, Class<?>[] interfaces)#
Configures proxy generation for a class or interface without constructing a proxy instance. For a class, 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 proxy
  • interfaces: additional interfaces implemented by the proxy

Fields ​

factory ​

factory: ProxyFactoryread-only#
Javassist factory configured with this builder's superclass, interfaces, and method filter.

proxiedMethodDefaults ​

proxiedMethodDefaults: Map<String, MethodWrapper<ProxyBuilder.ProxyReference<T>, Object[], ?, ?>>read-only#
Fallback handlers keyed by bare method name; used for matching methods without an exact signature handler.

proxiedMethods ​

proxiedMethods: Map<ProxyBuilder.MethodSigParts, MethodWrapper<ProxyBuilder.ProxyReference<T>, Object[], ?, ?>>read-only#
Handlers keyed by method name, parameter types, and return type.

Methods ​

addMethod ​

addMethod(String methodNameOrSig, MethodWrapper<ProxyBuilder.ProxyReference<T>, Object[], ?, ?> proxyMethod): ProxyBuilder<T>Since1.6.0#
Registers a handler for a method name or JVM method signature. A bare name is a fallback for all overloads; an exact signature takes precedence. Signatures use JVM descriptors, such as 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 signature
  • proxyMethod: handler for matching method calls
Returns: this builder

buildInstance ​

buildInstance(Object[] constructorArgs): TSince1.6.0#
Builds an instance by selecting a superclass constructor from the arguments' runtime types. It first tries an exact type match, then accepts assignable supertypes or other numeric types; numeric arguments are converted to the selected parameter types. Arguments cannot be 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
Returns: the constructed proxy instance
buildInstance(String constructorSig, Object[] constructorArgs): TSince1.6.0#
Builds an instance using parameter types parsed from a JVM constructor signature. Include the return descriptor, normally 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 signature
  • constructorArgs: arguments passed to the selected superclass constructor
Returns: the constructed proxy instance
buildInstance(Class<?>[] constructorSig, Object[] constructorArgs): TSince1.6.0#
Builds an instance using the supplied parameter types and corresponding arguments. Numeric arguments are converted when Util.tryAutoCastNumber(Class, Object) supports the declared parameter type. The parameter types must describe a superclass constructor; its return type is not used.
// 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 constructor
  • constructorArgs: arguments passed to that constructor
Returns: the constructed proxy instance