Improving field get and set performance with ASM
Posted
by ng
on Stack Overflow
See other posts from Stack Overflow
or by ng
Published on 2010-06-11T12:37:03Z
Indexed on
2010/06/11
13:32 UTC
Read the original article
Hit count: 292
I would like to avoid reflection in an open source project I am developing. Here I have classes like the following.
public class PurchaseOrder {
@Property
private Customer customer;
@Property
private String name;
}
I scan for the @Property annotation to determine what I can set and get from the PurchaseOrder reflectively. There are many such classes all using java.lang.reflect.Field.get() and java.lang.reflect.Field.set().
Ideally I would like to generate for each property an invoker like the following.
public interface PropertyAccessor<S, V> {
public void set(S source, V value);
public V get(S source);
}
Now when I scan the class I can create a static inner class of PurchaseOrder like so.
static class customer_Field implements PropertyAccessor<PurchaseOrder, Customer> {
public void set(PurchaseOrder order, Customer customer) {
order.customer = customer;
}
public Customer get(PurchaseOrder order) {
return order.customer;
}
}
With these I totally avoid the cost of reflection. I can now set and get from my instances with native performance. Can anyone tell me how I would do this. A code example would be great. I have searched the net for a good example but can find nothing like this. The ASM examples are pretty poor also.
The key here is that I have an interface that I can pass around. So I can have various implementations, perhaps one with Java Reflection as a default, one with ASM, and maybe one with Javassist?
Any help would be greatly appreciated.
© Stack Overflow or respective owner