![]() So inside the getCachedFunction method, as you can see above, there’s a fast path leveraging the ClassValue for caching and there’s the slow createAndCacheFunction path executed only if nothing has been cached so far. It takes the JavaBean object and a single fieldA or even nested field separated by periods, for example, įor optimal performance, I’m caching the dynamically created function that is the actual way of reading the content of a given fieldName. The following method is the utility used to read a value from a JavaBean field. #JAVA REFLECTION NESTED FIELD CODE#Also, I should say if you are quite familiar with the nice things that came up within Java 8, you will find the below code snippets fairly easy to understand. The target method in question here is the actual getter method that has direct access to the field we want to read. Its single method delegates a call to the actual target method with a code defined inside of lambda body. In a nutshell, the technique I’m about to better describe below leverages LambdaMetafactory and MethodHandle in order to dynamically create an implementation of Function. It also later allowed lambda expression and method reference in Java 8 as well as string concatenation in Java 9 to benefit from it. Briefly, invokedynamic (or “indy”) was the greatest thing introduced in Java 7 in order to pave the way for implementing dynamic languages on top of the JVM through dynamic method invocation. The enabler of the technique used to avoid the very slow reflection is the invokedynamic bytecode instruction. But I was interested in getting my own hands dirty with something different that I knew would be way faster than any library built on top of the widely known Java Reflection. You can easily argue I could’ve basically used Apache Commons BeanUtils or one of its alternatives to achieve the same result. Now, I’ll elaborate a little bit more around the JavaBeanUtil class, that I put in place to read the value for a given fieldName from a particular javaBeanObject, which in that occasion turned out to be FxTransaction. In the article Specification Pattern, for the sake of sanity, I didn’t mention about an underlying component to nicely make that thing happen. By Carlos Raphael A faster alternative to Java Reflection ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |