High performance Java reflection
Go to file
2022-11-10 14:40:46 -04:00
.settings Skip tests that aren't supported in Java 17+. Make HasPublicConstructor constructor public. Rename/format/clean up. Eclipse encoding. 2022-11-10 14:40:46 -04:00
build Revert "Repack ASM classes in esotericsoftware namespace." 2017-07-17 00:50:24 +02:00
lib Revert "Repack ASM classes in esotericsoftware namespace." 2017-07-17 00:50:24 +02:00
src/com/esotericsoftware/reflectasm Make generated FieldAccess class synthetic 2022-01-31 16:39:17 +01:00
test/com/esotericsoftware/reflectasm Skip tests that aren't supported in Java 17+. Make HasPublicConstructor constructor public. Rename/format/clean up. Eclipse encoding. 2022-11-10 14:40:46 -04:00
.classpath Use Java 7 JDK. 2018-08-24 17:49:28 +02:00
.gitignore Added gitignore 2013-11-11 10:38:11 +01:00
.project 2009-10-25 09:45:37 +00:00
.travis.yml Remove oraclejdk7 from travis build since it's no longer supported 2018-06-12 08:01:23 +02:00
CONTRIBUTING.md Prefer "3-Clause BSD License" over "New BSD License" 2017-02-28 13:15:36 +01:00
license.txt Added BSD license text. 2010-03-29 08:09:40 +00:00
pom.xml [maven-release-plugin] prepare for next development iteration 2019-03-18 22:13:09 +01:00
project.yaml Revert "Repack ASM classes in esotericsoftware namespace." 2017-07-17 00:50:24 +02:00
README.md Update README for release 1.11.9 2019-03-18 22:15:48 +01:00

Build Status Maven Central

Please use the ReflectASM discussion group for support.

Overview

ReflectASM is a very small Java library that provides high performance reflection by using code generation. An access class is generated to set/get fields, call methods, or create a new instance. The access class uses bytecode rather than Java's reflection, so it is much faster. It can also access primitive fields via bytecode to avoid boxing.

Performance

The source code for these benchmarks is included in the project. The above charts were generated on Oracle's Java 7u3, server VM.

Installation

To use reflectasm with maven, please use the following snippet in your pom.xml

    <dependency>
        <groupId>com.esotericsoftware</groupId>
        <artifactId>reflectasm</artifactId>
        <version>1.11.9</version>
    </dependency>

Usage

Method reflection with ReflectASM:

SomeClass someObject = ...
MethodAccess access = MethodAccess.get(SomeClass.class);
access.invoke(someObject, "setName", "Awesome McLovin");
String name = (String)access.invoke(someObject, "getName");

Field reflection with ReflectASM:

SomeClass someObject = ...
FieldAccess access = FieldAccess.get(SomeClass.class);
access.set(someObject, "name", "Awesome McLovin");
String name = (String)access.get(someObject, "name");

Constructor reflection with ReflectASM:

ConstructorAccess<SomeClass> access = ConstructorAccess.get(SomeClass.class);
SomeClass someObject = access.newInstance();

Avoiding Name Lookup

For maximum performance when methods or fields are accessed repeatedly, the method or field index should be used instead of the name:

SomeClass someObject = ...
MethodAccess access = MethodAccess.get(SomeClass.class);
int addNameIndex = access.getIndex("addName");
for (String name : names)
    access.invoke(someObject, addNameIndex, "Awesome McLovin");

Iterate all fields:

FieldAccess access = FieldAccess.get(SomeClass.class);
for(int i = 0, n = access.getFieldCount(); i < n; i++) {
    access.set(instanceObject, i, valueToPut);              
}

Visibility

ReflectASM can always access public members. An attempt is made to define access classes in the same classloader (using setAccessible) and package as the accessed class. If the security manager allows setAccessible to succeed, then protected and default access (package private) members can be accessed. If setAccessible fails, no exception is thrown, but only public members can be accessed. Private members can never be accessed.

Exceptions

Stack traces when using ReflectASM are a bit cleaner. Here is Java's reflection calling a method that throws a RuntimeException:

Exception in thread "main" java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.example.SomeCallingCode.doit(SomeCallingCode.java:22)
Caused by: java.lang.RuntimeException
	at com.example.SomeClass.someMethod(SomeClass.java:48)
	... 5 more

Here is the same but when ReflectASM is used:

Exception in thread "main" java.lang.RuntimeException
	at com.example.SomeClass.someMethod(SomeClass.java:48)
	at com.example.SomeClassMethodAccess.invoke(Unknown Source)
	at com.example.SomeCallingCode.doit(SomeCallingCode.java:22)

If ReflectASM is used to invoke code that throws a checked exception, the checked exception is thrown. Because it is a compilation error to use try/catch with a checked exception around code that doesn't declare that exception as being thrown, you must catch Exception if you care about catching a checked exception in code you invoke with ReflectASM.