• Stars
    star
    1,525
  • Rank 30,714 (Top 0.7 %)
  • Language
    Java
  • License
    BSD 3-Clause "New...
  • Created almost 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

High performance Java reflection

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.

More Repositories

1

kryo

Java binary serialization and cloning: fast, efficient, automatic
HTML
6,187
star
2

spine-runtimes

2D skeletal animation runtimes for Spine.
C++
4,375
star
3

kryonet

TCP/UDP client/server library for Java, based on Kryo
Java
1,818
star
4

yamlbeans

Java object graphs, to and from YAML automatically
Java
561
star
5

spine-scripts

Scripts to export from PhotoShop and other tools to Spine's JSON data format.
JavaScript
363
star
6

tablelayout

Table-based layout for Java UI toolkits: libgdx, Swing, Android, TWL
Java
281
star
7

spine-superspineboy

Example platformer game for spine-libgdx.
Java
224
star
8

minlog

Minimal overhead Java logging
Java
172
star
9

clippy

Multifunctional Windows productivity tool for programmers and other power users
C
127
star
10

wildcard

Efficient file system pattern matching in Java
Java
99
star
11

jsonbeans

Java object graphs, to and from JSON automatically
Java
91
star
12

kryonetty

Client/server library for Java, based on Netty and Kryo
Java
68
star
13

scar

Java-based utilities for building and packaging Java applications
Java
43
star
14

spine-workshop

Files from a very, very old Spine coding workshop.
Java
30
star
15

powermate

Control Windows volume using a Griffin PowerMate
Java
30
star
16

spine-editor

Issue tracking for the Spine editor.
29
star
17

tcpserver

Simple Java-based TCP server and client for passing strings
Java
21
star
18

dnsmadeeasy

Dynamic DNS updater for DnsMadeEasy
Java
18
star
19

oauth

Making OAuth in Java dead simple
Java
16
star
20

deconz

Java API for using DeCONZ to control Phillips Hue and other ZigBee devices
Java
9
star
21

hsl

Java library for HSLuv colors, interpolation, and conversion to and from RGB.
Java
9
star
22

dark-souls-saver

Quick save and backups for your Dark Souls game save file
Java
6
star
23

usbuirt

Java API to control a USB-UIRT device on Windows
Java
1
star
24

arcane

Nate's MTG Deck Builder and Rules Viewer
Java
1
star