[LIB] ReflectionHelper 1.0.1 - Cohesive Reflection

Discussion in 'Resources' started by Icyene, Nov 23, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Icyene

    Reflection has always been a key aspect of many extensive plugins. It is simple to use, can end up shrinking code, and gives you a general feeling of happy fuzziness once you make some sort of hack with it.

    That said, it is undeniable that reflection is, to put it mildly, ugly. What I aim to provide with ReflectionHelper is a fluent, cohesive, one-liner way to do reflection. Practically every aspect of native reflection is bundled in 181 lines, and if you find something that isn't, I provided a getRaw() method which returns the native reflection object. And of course, I added a lot of bells and whistles (outlined later), the most sonorous being generic types. Now you don't need to cast your get()s and invoke()s!

    Alright, that's my nice little intro. Now, in the example below, you can see the difference ReflectionHelper makes.

    Without ReflectionHelper
    Code:Java
    1.  
    2. try {
    3. Field f = aClass.getDeclaredField("test");
    4. f.setAccessible(true);
    5. String returned = (String)f.get(null);
    6. } catch (Exception e) {
    7. //Here you have code to handle any exceptions which never occur in well crafted invocations
    8. }
    9.  


    With ReflectionHelper
    Code:Java
    1.  
    2. String returned = ReflectionHelper.field("test").in(aClass.class).ofType(String.class).get();
    3.  


    Is that not so much more readable? ReflectionHelper also takes care of access privileges. It will automatically execute setAccessible(true), and will even allow you to modify final fields (a hack which is described in the additional information section at the bottom of this page). I do not advocate changing final fields; it can affect JVM stability, but if you really have to, just know you can.

    Performance decrease is negligible; the only bottleneck comes from calling the methods and a 1-2 field settings inside the methods.

    Below is what general reflection looks like:

    Code:Java
    1.  
    2. Set test = ReflectionHelper.method("test").in(instance).withReturnType(Set.class).invoke();
    3.  
    4. Set test2 = ReflectionHelper.field("test2").in(instance).ofType(Set.class).get();
    5.  
    6. MyObject ob = ReflectionHelper.constructor("test4").in(YourClass.class).newInstance(someArgs);
    7.  
    8. java.lang.Method m = ReflectionHelper.method("test5").in(instance).getRaw();
    9.  


    The Bells and Whistles
    Additional Information for the Hacky Mind
    tl;dr
    ReflectionHelper is a simple reflection API which makes reflection more readable. Now go read the above text to see how to use it >:-D


    I'm sold. Where do I get it?

    Before asking a question for support, read the above wall of text, and if your answer isn't there, ask. If you want a feature added, ask.


    ReflectionHelper is licensed under the Lesser General Public License (LGPL).​
     
  2. Offline

    Lolmewn

    Oh, nice! :D
     
  3. Offline

    Icyene

    ReflectionHelper 1.0.1 released

    ReflectionHelper 1.0.1 is now finished, containing come minor speed improvements and cleaning up a crash with constructors.
     
Thread Status:
Not open for further replies.

Share This Page