HubSpot/reflectasm

Name: reflectasm

Owner: HubSpot

Description: High performance Java reflection

Created: 2015-02-18 21:08:23.0

Updated: 2015-02-18 21:56:58.0

Pushed: 2017-09-19 19:29:57.0

Homepage: null

Size: 419

Language: Java

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

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.10.0</version>
</dependency>

If you already have asm in a different version (than the one pulled in by reflectasm - see pom.xml) in your classpath, you should use the shaded reflectasm (contains relocated asm classes):

endency>
roupId>com.esotericsoftware</groupId>
rtifactId>reflectasm</artifactId>
ersion>1.10.0</version>
lassifier>shaded</classifier>
xclusions>
<exclusion>
  <groupId>org.ow2.asm</groupId>
  <artifactId>asm</artifactId>
</exclusion>
exclusions>
pendency>
Usage

Method reflection with ReflectASM:

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

Field reflection with ReflectASM:

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

Constructor reflection with ReflectASM:

tructorAccess<SomeClass> access = ConstructorAccess.get(SomeClass.class);
Class 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:

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

Iterate all fields:

dAccess access = FieldAccess.get(SomeClass.class);
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:

ption 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)
ed by: java.lang.RuntimeException
at com.example.SomeClass.someMethod(SomeClass.java:48)
... 5 more

Here is the same but when ReflectASM is used:

ption 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.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.