Reflection
com.jsmacrosce.jsmacros.core.library.impl.FReflection
Loads classes and uses Java reflection through the script global Reflection.
Minecraft class and member names depend on the loader, runtime mappings, and Minecraft target;
they are not universally intermediary names. Primary class lookup uses this library's loader,
including jars added with loadJarFile(String). The two-name getClass(String, String) overload uses the default loader for its fallback. Legacy xyz.wagyourtail. names are rewritten before lookup.
Returned classes, methods and fields are ordinary Java reflection objects. Names absent from the shipped TypeScript package model are typed as unknown; raw Minecraft members are not modeled by those typings. Resolve methods separately from invoking them, for example with invokeMethod(Method, Object, Object...).
// Resolve and construct a JDK class, then look up and invoke a method separately.
const ArrayList = Reflection.getClass("java.util.ArrayList");
const list = Reflection.newInstance(ArrayList);
list.add("diamond");
const size = Reflection.getDeclaredMethod(ArrayList, "size");
print(`the list holds ${Reflection.invokeMethod(size, list)}`);A declared-field lookup can find non-public fields, but reading one requires access and may be denied by Java access restrictions.
Accessible in scripts via the global Reflection variable.
Nested types
Fields
classLoader
Methods
compileJavaClass
// Recompiling the same name creates a separate class.
const first = Reflection.compileJavaClass("com.example.Counter",
"package com.example; public class Counter { public int n = 0; }");
const second = Reflection.compileJavaClass("com.example.Counter",
"package com.example; public class Counter { public int n = 100; }");
const versions = Reflection.getAllCompiledJavaClassVersions("com.example.Counter");
print(`that name now has ${versions.size()} compiled versions`);
// Keep the class reference to choose which version to instantiate.
const instance = Reflection.newInstance(first);
GlobalVars.putObject("counter", instance);
// getCompiledJavaClass gives the most recent compilation of that name
print(`and the newest is the second one: ${Reflection.getCompiledJavaClass("com.example.Counter") === second}`);Parameters:
className: the fully qualified name of the class, including the packagecode: the java code to compile
createClassBuilder
createClassBuilder(String cName, Class<T> clazz, Class<?>... interfaces): ClassBuilder<T>Since1.6.5#Parameters:
cName: generated class nameclazz: superclassinterfaces: interfaces implemented by the generated class
Type Parameters:
T: superclass type
createClassProxyBuilder
Parameters:
clazz: superclass to proxyinterfaces: interfaces implemented by the proxy
Type Parameters:
T: superclass type
createLibrary
Parameters:
className: the fully qualified name of the class, including the packagejavaCode: the source code of the library
createLibraryBuilder
createLibraryBuilder(String name, boolean perExec, String... acceptedLangs): LibraryBuilder#Parameters:
name: name of the script globalperExec: whether to create a library per script run rather than per profileacceptedLangs: guest languages allowed to use the library; empty means all
getAllCompiledJavaClassVersions
Parameters:
className: the fully qualified class name
getClass
void, resolve directly to their primitive classes. The legacy xyz.wagyourtail. package prefix is rewritten before loading. Returns an ordinary Class; use getClass(String, String) for an alternative name when the first is absent. Minecraft class names depend on the runtime mappings.Parameters:
name: name of class likepath.to.class
Type Parameters:
T: type represented by the resolved class
// Put the runtime's expected name first; the alternative is tried only if it is absent.
const List = Reflection.getClass("example.Missing", "java.util.ArrayList");
const list = Reflection.newInstance(List);
list.add("item");Parameters:
name: first tryname2: second try
Type Parameters:
T: type represented by the resolved class
getClassFromClassBuilderResult
getClassName
null for local and anonymous classes. Arrays and primitive classes use their canonical names. const ArrayList = Reflection.getClass("java.util.ArrayList");
// a Class names itself, whatever the instance it came from was
print(`the class is ${Reflection.getClassName(ArrayList)}`);
// an instance names its runtime type, which is the subclass
print(`the instance is ${Reflection.getClassName(Reflection.newInstance(ArrayList))}`);
// a primitive class names the primitive
print(`and a primitive is ${Reflection.getClassName(Reflection.getClass("int"))}`);Parameters:
o: a Class or an object whose runtime class should be named
null if it has nonegetCompiledJavaClass
getDeclaredField
Parameters:
c: class to searchname: field name
See:
name, then name2 if the first lookup throws NoSuchFieldException or SecurityException.Parameters:
c: class declaring the fieldname: primary field namename2: fallback field name
getDeclaredMethod
setAccessible(true), subject to Java access restrictions. const StringClass = Reflection.getClass("java.lang.String");
const startsWith = Reflection.getDeclaredMethod(
StringClass, "startsWith", StringClass);
print(Reflection.invokeMethod(startsWith, "hello", "he"));Parameters:
c: the class to look the method up onname: the method's nameparameterTypes: the exact classes of the method's parameters, none for no parameters
getDeclaredMethod(Class<?> c, String name, String name2, Class<?>... parameterTypes): MethodSince1.2.3#name, then name2 if lookup by the first name throws NoSuchMethodException or SecurityException. Both names use the same exact parameter types.Parameters:
c: class declaring the methodname: primary method namename2: fallback method nameparameterTypes: exact parameter types
getField
Parameters:
c: class to searchname: field name
name2 if lookup by name throws NoSuchFieldException or SecurityException.Parameters:
c: class to searchname: primary field namename2: fallback field name
getMethod
name, then name2 if the first lookup throws NoSuchMethodException or SecurityException.Parameters:
c: class to searchname: primary method namename2: fallback method nameparameterTypes: exact parameter types
getReflect
const stack = Client.getRegistryManager().getItemStack("minecraft:diamond");
const reflected = Reflection.getReflect(stack.getRaw());
print(`calling through it: ${reflected.call("getCount")}`);
print(`the private count field says ${reflected.field("count").get()}`);Parameters:
obj: the object to wrap
invokeMethod
null as the receiver for a static method. The underlying reflective invocation enforces access and wraps target exceptions in InvocationTargetException. // a number out of a script is a double, and the coercion makes it fit an int
// parameter anyway. a null receiver means a static method
const Integer = Reflection.getClass("java.lang.Integer");
const bitCount = Reflection.getDeclaredMethod(Integer, "bitCount",
Reflection.getClass("int"));
print(`the bit count of 3 is ${Reflection.invokeMethod(bitCount, null, 3)}`);
// anything that is not a number is passed through untouched
const ArrayList = Reflection.getClass("java.util.ArrayList");
const list = Reflection.newInstance(ArrayList);
const add = Reflection.getDeclaredMethod(ArrayList, "add",
Reflection.getClass("java.lang.Object"));
Reflection.invokeMethod(add, list, "diamond");
print(`the list holds ${list.size()}`);Parameters:
m: method to invokec: receiver, ornullfor a static methodobjects: invocation arguments
null for a void methodloadJarFile
true; a missing path throws FileNotFoundException. const first = Reflection.loadJarFile("libs/mylib.jar");
const second = Reflection.loadJarFile("libs/mylib.jar");
print(`loaded ${first}, and again ${second}`);
const MyThing = Reflection.getClass("com.example.MyThing");
print(`that is ${Reflection.getClassName(MyThing)}`);Parameters:
file: path to the jar, resolved relative to the script's folder
true if the jar was added or was already loadednewInstance
null argument causes a NullPointerException before constructor lookup.Parameters:
c: class to instantiateobjects: constructor arguments
Type Parameters:
T: type of instance to construct
newInstance0
Parameters:
c: the class to constructobjects: constructor arguments
Type Parameters:
T: type of instance to construct