BetterReflection is a Java library that caches reflection to achieve lower access time.
Note: BetterReflection is essentially caching reflections. There might be better alternatives for what you are trying to achieve.
Every update has been packed into a Jar and released on GitHub. This library is also being hosted on Maven Central, starting with version 1.0
<dependency>
<groupId>top.wavelength</groupId>
<artifactId>Java-BetterReflection</artifactId>
<version>LATEST_VERSION</version>
</dependency>
Note: the package used to be me.wavelength.betterreflection up until version 1.0, where it's been renamed to top.wavelength.betterreflection
There are no dependencies needed to use this library. Once you've downloaded it and set it up in your IDE you can start using it.
You can temporarily cache a class by simply creating a new instance of BetterReflectionClass
BetterReflectionClass<ClassName> className = new BetterReflectionClass<?>(ClassName.class); // You can also provide a string, containing the package and the name in this format: "com.example.Class".
Temporarily caching a class, though, might be worse than using normal reflections, because the BetterReflection class caches everything as soon as instantiated. To achieve actually better results than normal reflections you must actually permanently cache said classes.
The BetterReflection class is a class that keeps in an arraylist the cached classes
private BetterReflection betterReflection;
public ExampleClass() {
betterReflection = new BetterReflection();
}
public BetterReflection getBetterReflection() {
return betterReflection;
}
public class MyClass {
...
BetterReflectionClass<ClassNeeded> classNeeded = exampleClass.getBetterReflection().getBetterReflectionClass(ClassNeeded.class);
...
}
Note: There are some cons to using this method. RAM usage could be somewhat high and once there are many classes looping through a list containing all of them might get expensive
public class ListOfMyClasses {
...
public static final BetterReflectionClass ClassNeeded = new BetterReflectionClass(ClassNeeded.class);
...
}
import static ListOfMyClasses.*;
public class MyClass {
...
ClassNeeded.getField(""); // <-- This is directly referencing the field "ClassNeeded" from the class "ListOfMyClasses".
...
}
There are several ways you can contribute.
There are never enough tests! You can create a new test anytime you want to, but your test must meet specific requirements.
- Your test must extend the Test class
- Your test must be added to the correct array inside of the Tester class
- Your test must use the Timer class to return the time passed
- Your test must tackle somewhat real scenarios
- Your test must have a good English description
If you have some spare time and are generous enough, feel free to implement some proper benchmark and create a pull request. OpenJDK JHM is suggested, but if you're into some other benchmarking libraries feel free to use them instead
If you spot issues in the code, think there are better ways to achieve something or think that there are methods that should be added, feel free to add them.
Of course, contributing is not limited to code, you can also open issues to report bugs, performance issues or request features.