Reflection utils

A utility library to handle Java reflection with ease

License

License

GroupId

GroupId

com.github.ekryd.reflection-utils
ArtifactId

ArtifactId

reflection-utils
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Reflection utils
A utility library to handle Java reflection with ease
Project URL

Project URL

https://github.com/Ekryd/reflection-utils
Source Code Management

Source Code Management

https://github.com/Ekryd/reflection-utils

Download reflection-utils

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.ekryd.reflection-utils/reflection-utils/ -->
<dependency>
    <groupId>com.github.ekryd.reflection-utils</groupId>
    <artifactId>reflection-utils</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.ekryd.reflection-utils/reflection-utils/
implementation 'com.github.ekryd.reflection-utils:reflection-utils:1.1.0'
// https://jarcasting.com/artifacts/com.github.ekryd.reflection-utils/reflection-utils/
implementation ("com.github.ekryd.reflection-utils:reflection-utils:1.1.0")
'com.github.ekryd.reflection-utils:reflection-utils:jar:1.1.0'
<dependency org="com.github.ekryd.reflection-utils" name="reflection-utils" rev="1.1.0">
  <artifact name="reflection-utils" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.ekryd.reflection-utils', module='reflection-utils', version='1.1.0')
)
libraryDependencies += "com.github.ekryd.reflection-utils" % "reflection-utils" % "1.1.0"
[com.github.ekryd.reflection-utils/reflection-utils "1.1.0"]

Dependencies

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
org.hamcrest : hamcrest-all jar 1.3

Project Modules

There are no modules declared in this project.

Reflection-utils Icon

Build Status Coverage Status Maven Central Coverity

This library will make it easier to get and set internal fields in classes using java reflection. The library is optimized for convenience and not for speed, so it works best with unit tests.

Mocking is great! Sometimes the easiest way to insert mocks are by Java reflection. Especially if you cannot use constructor arguments or setter methods.

Example of how to use the ReflectionHelper in a test

    @Test
    public void storeMethodShouldSaveEntity() throws Exception {
        // Setup entities
        EntityHandler handler = new EntityHandlerImpl();        
        StoreDao storeDao = mock(StoreDao.class);
        
        // *** This is the ReflectionHelper! You don't need to know the name of field nor the type, just set the instance ***
        new ReflectionHelper(handler).setField(storeDao);
        
        // Perform method
        handler.store();

        // Verify save method
        verify(storeDao).save();
    }

See more

Versions

Version
1.1.0
1.0.2
1.0.0
0.0.5
0.0.4