Java Reflection Utility Classes
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
gradle/wrapper
src
.gitignore
.travis.yml
LICENSE
README.md
build.gradle
gradlew
gradlew.bat
release.sh
settings.gradle

README.md

Build Status Maven Central Apache 2.0 Coverage Status

cronn reflection-util

Utility classes that simplify common use cases of Java Reflection.

We ship two utility classes PropertyUtils and ClassUtils that are described in the following sections.

PropertyUtils

Replacement for org.apache.commons.beanutils.PropertyUtils with deterministic behaviour and support to retrieve PropertyDescriptors via Java 8 method references.

Example:

class MyPojo {

    private Long number;

    public Long getNumber() {
        return number;
    }

    public void setNumber(Long number) {
        this.number = number;
    }

}
PropertyDescriptor numberProperty = PropertyUtils.getPropertyDescriptor(MyPojo.class, MyPojo::getNumber);
MyPojo pojo = new MyPojo();
PropertyUtils.write(pojo, numberProperty, 12345L);
Long number = PropertyUtils.read(pojo, numberProperty);
assertEquals(12345L, number);

ClassUtils

interface MyInterface {

    void doSomething();

}
String methodName = ClassUtils.getVoidMethodName(MyInterface.class, MyInterface::doSomething);
assertEquals("doSomething", methodName);
Class<?>[] interfaces = { MyInterface.class };
Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), interfaces, (p, method, args) -> null);
assertEquals(MyInterface.class, ClassUtils.getRealClass(proxy));

Usage

Add the following Maven dependency to your project:

<dependency>
    <groupId>de.cronn</groupId>
    <artifactId>reflection-util</artifactId>
    <version>2.1.0</version>
</dependency>

Dependencies

  • Java 8+

Related Work