Skip to content

Commit e4a82b5

Browse files
committed
2 parents 0138ec7 + cb27e75 commit e4a82b5

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
![](https://raw.github.com/wiki/EsotericSoftware/reflectasm/images/logo.png)
2+
3+
Please use the [ReflectASM discussion group](http://groups.google.com/group/reflectasm-users) for support.
4+
5+
## Overview
6+
7+
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.
8+
9+
## Performance
10+
11+
![](http://chart.apis.google.com/chart?chma=100&chtt=Field%20Set/Get&chs=700x62&chd=t:1402081,11339107&chds=0,11339107&chxl=0:|Java%20Reflection|FieldAccess&cht=bhg&chbh=10&chxt=y&chco=6600FF)
12+
13+
![](http://chart.apis.google.com/chart?chma=100&chtt=Method%20Call&chs=700x62&chd=t:97390,208750&chds=0,208750&chxl=0:|Java%20Reflection|MethodAccess&cht=bhg&chbh=10&chxt=y&chco=6600AA)
14+
15+
![](http://chart.apis.google.com/chart?chma=100&chtt=Constructor&chs=700x62&chd=t:2853063,5828993&chds=0,5828993&chxl=0:|Java%20Reflection|ConstructorAccess&cht=bhg&chbh=10&chxt=y&chco=660066)
16+
17+
The source code for these benchmarks is included in the project. The above charts were generated on Oracle's Java 7u3, server VM.
18+
19+
## Usage
20+
21+
Method reflection with ReflectASM:
22+
23+
```java
24+
SomeClass someObject = ...
25+
MethodAccess access = MethodAccess.get(SomeClass.class);
26+
access.invoke(someObject, "setName", "Awesome McLovin");
27+
String name = (String)access.invoke(someObject, "getName");
28+
```
29+
30+
Field reflection with ReflectASM:
31+
32+
```java
33+
SomeClass someObject = ...
34+
FieldAccess access = FieldAccess.get(SomeClass.class);
35+
access.set(someObject, "name", "Awesome McLovin");
36+
String name = (String)access.get(someObject, "name");
37+
```
38+
39+
Constructor reflection with ReflectASM:
40+
41+
```java
42+
ConstructorAccess<SomeClass> access = ConstructorAccess.get(SomeClass.class);
43+
SomeClass someObject = access.newInstance();
44+
```
45+
46+
## Avoiding Name Lookup
47+
48+
For maximum performance when methods or fields are accessed repeatedly, the method or field index should be used instead of the name:
49+
50+
```java
51+
SomeClass someObject = ...
52+
MethodAccess access = MethodAccess.get(SomeClass.class);
53+
int addNameIndex = access.getIndex("addName");
54+
for (String name : names)
55+
access.invoke(someObject, addNameIndex, "Awesome McLovin");
56+
```
57+
58+
## Visibility
59+
60+
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.
61+
62+
## Exceptions
63+
64+
Stack traces when using ReflectASM are a bit cleaner. Here is Java's reflection calling a method that throws a RuntimeException:
65+
66+
Exception in thread "main" java.lang.reflect.InvocationTargetException
67+
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
68+
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
69+
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
70+
at java.lang.reflect.Method.invoke(Method.java:597)
71+
at com.example.SomeCallingCode.doit(SomeCallingCode.java:22)
72+
Caused by: java.lang.RuntimeException
73+
at com.example.SomeClass.someMethod(SomeClass.java:48)
74+
... 5 more
75+
76+
Here is the same but when ReflectASM is used:
77+
78+
Exception in thread "main" java.lang.RuntimeException
79+
at com.example.SomeClass.someMethod(SomeClass.java:48)
80+
at com.example.SomeClassMethodAccess.invoke(Unknown Source)
81+
at com.example.SomeCallingCode.doit(SomeCallingCode.java:22)
82+
83+
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.

0 commit comments

Comments
 (0)