Class ReflectiveNamespace
ReflectionParser),
other than that most of the work is done behind the scenes, and it's not needed to use it directly.
You can initiate this class using XReflection.namespaced(). Like other reflection classes, this should
be used as a temporary class and you should cache the results of ReflectiveHandle classes.
This class is used just like XReflection except that it
allows enhanced performance and security checks. Performance because a single reflection lookup
object is used for all reflections that use this namespace, security because of the same lookup
object, the MethodHandles.lookup() which is caller sensitive.
This class also provides an import statement feature. For example look at the following code:
XReflection.of(Test.class).method("public List<String> getNames();").unreflect();
Assuming that the class and the method exist, this works, it knows about the "List" type from List
because it's predefined and hardcoded. But if we look at another code:
XReflection.of(Test.class).method("public MyCustomClass getCustomData();").unreflect();
This will fail, because it doesn't know where MyCustomClass is, you could give it the fully qualified name:
XReflection.of(Test.class).method("public my.package.MyCustomClass getCustomData();").unreflect();
But what if you want to keep using this type a lot? It makes the code look very ugly. That is what this class is for:
ReflectiveNamespace ns = XReflection.namespaced().imports(MyCustomClass.class);
ns.of(Test.class).method("public MyCustomClass getCustomData();").unreflect();
Also, all the types that are passed to or parsed from this namespace (e.g. from
of(Class), ofMinecraft(String), classHandle(String))
are imported automatically. Making this a powerful mini-IDE!
Note that sometimes you need to import remapped classes manually if you're going to be using of(Class)
since these class names are going to be remapped at runtime, and if you use the obfuscated names in
string-based API signatures, it'll fail:
ReflectiveNamespace ns = XReflection.namespaced();
// If you don't do the following, then this whole thing won't work.
// The reason why adding this manual import works is because "MinecraftKey" class
// will be remapped to "ResourceLocation" at runtime, so the following code will be
// translated to ns.imports("MinecraftKey", ResourceLocation.class);
ns.imports("MinecraftKey", MinecraftKey.class);
// Alternatively you could just use "ResourceLocation" instead of "MinecraftKey" here in the string.
ns.of(MinecraftKey.class).method("public static MinecraftKey fromNamespaceAndPath(String namespace, String path);").unreflect();
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionSimilar toXReflection.classHandle()classHandle(String declaration) Similar toXReflection.classHandle()Imports the specified classes into this namespace so the names can be used directly.Imports a class with a custom name.voidlink(ClassHandle handle) Same asXReflection.of(Class)Similar toXReflection.ofMinecraft()ofMinecraft(String declaration) Similar toXReflection.ofMinecraft()
-
Constructor Details
-
ReflectiveNamespace
protected ReflectiveNamespace()
-
-
Method Details
-
imports
Imports the specified classes into this namespace so the names can be used directly. (For more info readReflectiveNamespace).This can also override predefined Java standard types if the names are the same.
-
imports
Imports a class with a custom name.- Parameters:
name- the custom name of the class.clazz- the actual definition of the class.- See Also:
-
getImports
-
link
-
getLookup
-
of
Same asXReflection.of(Class)- Since:
- 11.0.0
- See Also:
-
classHandle
Similar toXReflection.classHandle()- Since:
- 11.0.0
- See Also:
-
classHandle
Similar toXReflection.classHandle()- Since:
- 12.0.0
- See Also:
-
ofMinecraft
Similar toXReflection.ofMinecraft()- Since:
- 11.0.0
- See Also:
-
ofMinecraft
Similar toXReflection.ofMinecraft()- Since:
- 12.0.0
- See Also:
-