Java reflection utilities
This library contains utility classes related to reflection.
I found myself re-implementing these type of methods over and over again whenever I needed reflection in some project. As there seldom is enough time to do this properly and take things like caching into consideration, the solutions were always sub-optimal. That is why this library was created.
Contributing
Have you found a bug? Did you create new featue?
Please see the contributing page for more information.
Utility-class Classes
For getting or finding classes and interacting with them.
The difference between getting and finding is that a get
operation throws exceptions if nothing is found, and find
returns null
.
Utility-class Methods
For getting or finding methods and invoking them.
Utility-class Constructors
For getting or finding constructors and invoking them.
Package 'beans'
Containing the BeanReflection
class, this package provides access to Java's built-in Introspector.getBeanInfo
but falls back to public field access when available.
Furthermore, the properties can be 'chained' safely concatenating them with dots inbetween ('.'
). Array indices are also supported by square brackets ('['
and ']'
).
Package 'dto'
The AbstractDto
superclass can be extended when your class is merely a Data Transfer Object. Such DTO's are all about simply representing a datastructure and normally don't need any methods. Subclasses merely need to provide public fields containing their datastructure and will be automatically provided with equals
, hashCode
, toString
and clone
implementations including all accessible fields from the subclass.
Package 'strings'
The ToStringBuilder
is a convenient builder for toString()
representations containing named field appenders. It is part of the reflection library because it can easily be instantiated for any bean by the static reflect
method.
Package 'errorhandling'
Contains the exception types that can be thrown by this library, all subclasses of java.lang.RuntimeException
.
Java 9 module concerns
If you're using the Java 9 module system (e.g. project jigsaw), you can still use this reflection library. Actually the library itself is neatly published as a java 9 module called nl.talsmasoftware.reflection
even though the classes are still java 5 binary compatible.
However, out of the box, our module cannot reflect your classes if you haven't opened them up. So be sure to either declare your module as an open
module or explicitly open up a package for reflection by this module.
Prepend your module with the open
keyword to open it up for reflection like this:
open module com.example.myapp {
requires nl.talsmasoftware.reflection;
}
Or you can define a specific package for reflection:
module com.example.myapp {
opens com.example.myapp.dto to nl.talsmasoftware.reflection;
requires nl.talsmasoftware.reflection;
}