Class ReflectiveNamespace

java.lang.Object
com.cryptomorin.xseries.reflection.ReflectiveNamespace

public class ReflectiveNamespace extends Object
This class is mostly only useful if you're planning to use string-based API (see 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();