{"id":98601,"date":"2019-09-24T13:00:03","date_gmt":"2019-09-24T10:00:03","guid":{"rendered":"https:\/\/www.javacodegeeks.com\/?p=98601"},"modified":"2019-09-24T13:49:19","modified_gmt":"2019-09-24T10:49:19","slug":"think-twice-before-using-reflection","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html","title":{"rendered":"Think Twice Before Using Reflection"},"content":{"rendered":"<h3 class=\"wp-block-heading\">Introduction<\/h3>\n<p>Sometimes, as a developer, you may bump into a situation when it\u2019s not possible to instantiate an object using the <code>new<\/code> operator because its class name is stored somewhere in configuration XML or you need to invoke a method which name is specified as an annotation property. In such cases, you always have an answer: \u201cUse reflection!\u201d.<\/p>\n<p>In the new version of <a href=\"http:\/\/cuba-platform.com\" target=\"_blank\" rel=\"noopener noreferrer\">CUBA framework<\/a>, we decided to improve many aspects of the architecture and one of the most significant change was deprecating \u201cclassic\u201c event listeners in the controllers UI. In the previous version of the framework, a lot of boilerplate code registering listeners in screen&#8217;s <code>init()<\/code> method made your code almost unreadable, so the new concept should have cleaned this up.<\/p>\n<p>You can always implement method listener by storing <code>java.lang.reflect.Method<\/code> instances for annotated methods and invoke them like it is implemented in many frameworks, but we decided to have a look at other options. Reflection calls have their cost and if you develop a production-class framework, even tiny improvement may pay back in a short time.<\/p>\n<p>In this article, we\u2019ll look at reflection API, pros and cons for its usage and review other options to replace reflection API calls &#8211; AOT and code generation and LambdaMetafactory.<\/p>\n<h3 class=\"wp-block-heading\">Reflection &#8211; good old reliable API<\/h3>\n<p>&#8220;Reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at runtime&#8221; according to Wikipedia.<\/p>\n<p>For most Java developers reflection is not a new thing and it is used in many cases. I\u2019d dare to say that Java won\u2019t become what it is now without reflection. Just think about annotation processing, data serialization, method binding via annotations or configuration files\u2026 For the most popular IoC frameworks reflection API is a cornerstone because of extensive usage of class proxying, method reference usage, etc. Also, you can add aspect-oriented programming to this list &#8211; some AOP frameworks rely on reflection for method execution interception.<\/p>\n<p>Are there any problems with reflection? We can think about three of them:<\/p>\n<p><em>Speed<\/em> &#8211; reflection calls are slower than direct calls. We can see a great improvement in reflection API performance with every JVM release, JIT compiler\u2019s optimization algorithms are getting better, but reflective method invocations are still about three times slower than direct ones.<\/p>\n<p><em>Type safety<\/em> &#8211; if you use method reference in your code, it is just a method reference. If you write a code that invokes a method via its reference and passes wrong parameters, the invocation will fail at runtime, not at compile time or load-time.<\/p>\n<p><em>Traceability<\/em> &#8211; if a reflective method call fails, it might be tricky to find a line of code that caused this, because stack trace is usually huge. You need to dig really deep into all these <code>invoke()<\/code> and <code>proxy()<\/code> calls.<\/p>\n<p>But if you look into event listener implementations in Spring or JPA callbacks in Hibernate &#8211; you will see familiar <code>java.lang.reflect.Method<\/code> references inside. And I doubt that it will be changed in the nearest future &#8211; mature frameworks are big and complex, used in many mission-critical systems, so developers should introduce big changes carefully.<\/p>\n<p>Let\u2019s have a look at other options.<\/p>\n<h3 class=\"wp-block-heading\">AOT compilation and code generation &#8211; make applications fast again<\/h3>\n<p>The first candidate for reflection replacement &#8211; code generation. Nowadays we can see a rise of new frameworks like <a href=\"https:\/\/micronaut.io\/\" target=\"_blank\" rel=\"noopener noreferrer\">Micronaut<\/a> and <a href=\"https:\/\/quarkus.io\/\" target=\"_blank\" rel=\"noopener noreferrer\">Quarkus<\/a> that are targeted to two aims: fast start time and low memory footprint. Those two metrics are vital in the age of microservices and serverless applications. And recent frameworks are trying to get rid of reflection completely by using ahead-of-time compilation and code generation. By using annotation processing, type visitors and other techniques they add direct method calls, object instantiations, etc. into your code, therefore making applications faster. Those do not create and inject beans during startup using <code>Class.newInstance()<\/code>, do not use reflective method calls in listeners, etc. Looks very promising, but are there any trade-offs here? And the answer is &#8211; yes.<\/p>\n<p>The first one &#8211; you run the code that is not yours exactly. Code generation changes your original code, therefore if something goes wrong you cannot tell whether it is your mistake or it is a glitch in the code processing algorithms. And don\u2019t forget that now you should debug generated code, but not your code.<\/p>\n<p>The second trade-off &#8211; you must use a separate tool\/plugin provided by the vendor to use the framework. You cannot \u201cjust\u201d run the code, you should pre-process it in a special way. And if you use the framework in production, you should apply the vendor\u2019s bugfixes to both framework codebase and code processing tool.<\/p>\n<p>Code generation has been known for a long time, it hasn\u2019t appeared with <a href=\"https:\/\/micronaut.io\/\" target=\"_blank\" rel=\"noopener noreferrer\">Micronaut<\/a> or <a href=\"https:\/\/quarkus.io\/\" target=\"_blank\" rel=\"noopener noreferrer\">Quarkus<\/a>. For example, in <a href=\"https:\/\/cuba-platform.com\" target=\"_blank\" rel=\"noopener noreferrer\">CUBA<\/a> we use class enhancement during compile-time using custom Grails plugin and <a href=\"http:\/\/www.javassist.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Javassist<\/a> library. We add extra code to generate entity update events and include bean validation messages to the class code as String fields for the nice UI representation.<\/p>\n<p>But implementing code generation for event listeners looked a bit extreme because it would require a complete change of the internal architecture. Is there such a thing as reflection, but faster?<\/p>\n<h3 class=\"wp-block-heading\">LambdaMetafactory &#8211; faster method invocation<\/h3>\n<p>In Java 7, there was introduced a new JVM instruction &#8211; <code>invokedynamic<\/code>. Initially targeted at dynamic languages implementations based on JVM, it has become a good replacement for API calls. This API may give us a performance improvement over traditional reflection. And there are special classes to construct invokedynamic calls in your Java code:<\/p>\n<ul class=\"wp-block-list\">\n<li><code>MethodHandle<\/code> &#8211; this class was introduced in Java 7, but it is still not well-known.<\/li>\n<li><code>LambdaMetafactory<\/code> &#8211; was introduced in Java 8. It is further development of dynamic invocation idea. This API is based on MethodHandle.<\/li>\n<\/ul>\n<p>Method handles API is a good replacement for standard reflection because JVM will perform all pre-invocation checks only once &#8211; during <code>MethodHandle<\/code> creation. Long story short &#8211; a method handle is a typed, directly executable reference to an underlying method, constructor, field, or similar low-level operation, with optional transformations of arguments or return values.<\/p>\n<p>Surprisingly, pure MethodHandle reference invocation does not provide better performance comparing to reflection API unless you make MethodHandle references static as discussed in this <a href=\"http:\/\/mail.openjdk.java.net\/pipermail\/mlvm-dev\/2018-February\/006806.html\" target=\"_blank\" rel=\"noopener noreferrer\">email list<\/a>.<\/p>\n<p>But <code>LambdaMetafactory<\/code> is another story &#8211; it allows us to generate an instance of a functional interface in the runtime that contains a reference to a method resolved by <code>MethodHandle<\/code>. Using this lambda object, we can invoke the referenced method directly. Here is an example:<\/p>\n<div>\n<div id=\"highlighter_199980\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">01<\/div>\n<div class=\"line number2 index1 alt1\">02<\/div>\n<div class=\"line number3 index2 alt2\">03<\/div>\n<div class=\"line number4 index3 alt1\">04<\/div>\n<div class=\"line number5 index4 alt2\">05<\/div>\n<div class=\"line number6 index5 alt1\">06<\/div>\n<div class=\"line number7 index6 alt2\">07<\/div>\n<div class=\"line number8 index7 alt1\">08<\/div>\n<div class=\"line number9 index8 alt2\">09<\/div>\n<div class=\"line number10 index9 alt1\">10<\/div>\n<div class=\"line number11 index10 alt2\">11<\/div>\n<div class=\"line number12 index11 alt1\">12<\/div>\n<div class=\"line number13 index12 alt2\">13<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java keyword\">private<\/code> <code class=\"java plain\">BiConsumer createVoidHandlerLambda(Object bean, Method method) <\/code><code class=\"java keyword\">throws<\/code> <code class=\"java plain\">Throwable {<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">MethodHandles.Lookup caller = MethodHandles.lookup();<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">CallSite site = LambdaMetafactory.metafactory(caller,<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java string\">\"accept\"<\/code><code class=\"java plain\">,<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">MethodType.methodType(BiConsumer.<\/code><code class=\"java keyword\">class<\/code><code class=\"java plain\">),<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">MethodType.methodType(<\/code><code class=\"java keyword\">void<\/code><code class=\"java plain\">.<\/code><code class=\"java keyword\">class<\/code><code class=\"java plain\">, Object.<\/code><code class=\"java keyword\">class<\/code><code class=\"java plain\">, Object.<\/code><code class=\"java keyword\">class<\/code><code class=\"java plain\">),<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">caller.findVirtual(bean.getClass(), method.getName(),<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">MethodType.methodType(<\/code><code class=\"java keyword\">void<\/code><code class=\"java plain\">.<\/code><code class=\"java keyword\">class<\/code><code class=\"java plain\">, method.getParameterTypes()[<\/code><code class=\"java value\">0<\/code><code class=\"java plain\">])),<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">MethodType.methodType(<\/code><code class=\"java keyword\">void<\/code><code class=\"java plain\">.<\/code><code class=\"java keyword\">class<\/code><code class=\"java plain\">, bean.getClass(), method.getParameterTypes()[<\/code><code class=\"java value\">0<\/code><code class=\"java plain\">]));<\/code><\/div>\n<div class=\"line number10 index9 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">MethodHandle factory = site.getTarget();<\/code><\/div>\n<div class=\"line number11 index10 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">BiConsumer listenerMethod = (BiConsumer) factory.invoke();<\/code><\/div>\n<div class=\"line number12 index11 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java keyword\">return<\/code> <code class=\"java plain\">listenerMethod;<\/code><\/div>\n<div class=\"line number13 index12 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Please note that with this approach we can just use <code>java.util.function.BiConsumer<\/code> instead of <code>java.lang.reflect.Method<\/code>, therefore it won\u2019t require too much refactoring. Let&#8217;s consider event listener handler code &#8211; it is a simplified adaptation from Spring Framework:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<div>\n<div id=\"highlighter_62183\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<div class=\"line number7 index6 alt2\">7<\/div>\n<div class=\"line number8 index7 alt1\">8<\/div>\n<div class=\"line number9 index8 alt2\">9<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java keyword\">public<\/code> <code class=\"java keyword\">class<\/code> <code class=\"java plain\">ApplicationListenerMethodAdapter<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java keyword\">implements<\/code> <code class=\"java plain\">GenericApplicationListener {<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java keyword\">private<\/code> <code class=\"java keyword\">final<\/code> <code class=\"java plain\">Method method;<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java keyword\">public<\/code> <code class=\"java keyword\">void<\/code> <code class=\"java plain\">onApplicationEvent(ApplicationEvent event) {<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">Object bean = getTargetBean();<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">Object result = <\/code><code class=\"java keyword\">this<\/code><code class=\"java plain\">.method.invoke(bean, event);<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">handleResult(result);<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">}<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"java plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>And that is how it can be changed with Lambda-based method reference:<\/p>\n<div>\n<div id=\"highlighter_968417\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<div class=\"line number7 index6 alt2\">7<\/div>\n<div class=\"line number8 index7 alt1\">8<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java keyword\">public<\/code> <code class=\"java keyword\">class<\/code> <code class=\"java plain\">ApplicationListenerLambdaAdapter <\/code><code class=\"java keyword\">extends<\/code> <code class=\"java plain\">ApplicationListenerMethodAdapter {<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java keyword\">private<\/code> <code class=\"java keyword\">final<\/code> <code class=\"java plain\">BiFunction funHandler;<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java keyword\">public<\/code> <code class=\"java keyword\">void<\/code> <code class=\"java plain\">onApplicationEvent(ApplicationEvent event) {<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">Object bean = getTargetBean();<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">Object result = handler.apply(bean, event);<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">handleResult(result);<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">}<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"java plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>The code has subtle changes and functionality is the same. But it has some advantages over traditional reflection:<\/p>\n<p><em>Type safety<\/em> &#8211; you specify method signature in <code>LambdaMetafactory.metafactory<\/code> call, therefore you won\u2019t be able to bind \u201cjust\u201d methods as event listeners.<\/p>\n<p><em>Traceability<\/em> &#8211; lambda wrapper adds only one extra call to method invocation stack trace. It makes debugging much easier.<\/p>\n<p><em>Speed<\/em> &#8211; this is a thing that should be measured.<\/p>\n<h2 class=\"wp-block-heading\">Benchmarking<\/h2>\n<p>For the new version of CUBA framework, we created a JMH-based microbenchmark to compare execution time and throughput for \u201ctraditional\u201d reflection method call, lambda-based one and we added direct method calls just for comparison. Both method references and lambdas were created and cached before test execution.<\/p>\n<p>We used the following benchmark testing parameters:<\/p>\n<div>\n<div id=\"highlighter_286812\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java color1\">@BenchmarkMode<\/code><code class=\"java plain\">({Mode.Throughput, Mode.AverageTime})<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java color1\">@Warmup<\/code><code class=\"java plain\">(iterations = <\/code><code class=\"java value\">5<\/code><code class=\"java plain\">, time = <\/code><code class=\"java value\">1000<\/code><code class=\"java plain\">, timeUnit = TimeUnit.MILLISECONDS)<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java color1\">@Measurement<\/code><code class=\"java plain\">(iterations = <\/code><code class=\"java value\">10<\/code><code class=\"java plain\">, time = <\/code><code class=\"java value\">1000<\/code><code class=\"java plain\">, timeUnit = TimeUnit.MILLISECONDS)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>You can download the benchmark from <a href=\"https:\/\/github.com\/cuba-rnd\/entity-lambda-accessors-benchmark\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub<\/a> and run the test by yourself.<\/p>\n<p>For JVM 11.0.2 and JMH 1.21 we got the following results (numbers may slightly vary from run to run):<\/p>\n<table class=\"wp-block-table\">\n<thead>\n<tr>\n<th>Test &#8211; Get Value<\/th>\n<th>Throughput (ops\/us)<\/th>\n<th>Execution Time (us\/op)<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>LambdaGetTest<\/td>\n<td>72<\/td>\n<td>0.0118<\/td>\n<\/tr>\n<tr>\n<td>ReflectionGetTest<\/td>\n<td>65<\/td>\n<td>0.0177<\/td>\n<\/tr>\n<tr>\n<td>DirectMethodGetTest<\/td>\n<td>260<\/td>\n<td>0.0048<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"wp-block-table\">\n<thead>\n<tr>\n<th>Test &#8211; Set Value<\/th>\n<th>Throughput (ops\/us)<\/th>\n<th>Execution Time (us\/op)<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>LambdaSetTest<\/td>\n<td>96<\/td>\n<td>0.0092<\/td>\n<\/tr>\n<tr>\n<td>ReflectionSetTest<\/td>\n<td>58<\/td>\n<td>0.0173<\/td>\n<\/tr>\n<tr>\n<td>DirectMethodSetTest<\/td>\n<td>415<\/td>\n<td>0.0031<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As you can see, the lambda-based method handlers are about 30% faster on the average. There is a good discussion <a href=\"https:\/\/www.mail-archive.com\/mlvm-dev@openjdk.java.net\/msg06747.html\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a> regarding lambda-based method invocation performance. The outcome &#8211; classes generated by LambdaMetafactory can be inlined, gaining some performance improvement. And it is faster than reflection because reflective calls had to pass security checks on every invocation.<\/p>\n<p>This benchmark is pretty anemic and does not take into account class hierarchy, final methods, etc., it measures \u201cjust\u201d method calls, but it was sufficient for our purpose.<\/p>\n<h3 class=\"wp-block-heading\">Implementation<\/h3>\n<p>In CUBA you can use <code>@Subscribe<\/code> annotation to make a method \u201clisten\u201d to various CUBA-specific application events. Internally we use this new MethodHandles\/LambdaMetafactory based API for faster listener invocations. All the method handles are cached after the first invocation.<\/p>\n<p>The new architecture has made the code cleaner and more manageable, especially in case of complex UI with a lot of event handlers. Just have a look at the simple example. Assume that you need to recalculate order amount based on products added to this order. You have a method <code>calculateAmount()<\/code> and you need to invoke it as soon as a collection of products in the order has changed. Here is the old version of the UI controller:<\/p>\n<div>\n<div id=\"highlighter_378953\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">01<\/div>\n<div class=\"line number2 index1 alt1\">02<\/div>\n<div class=\"line number3 index2 alt2\">03<\/div>\n<div class=\"line number4 index3 alt1\">04<\/div>\n<div class=\"line number5 index4 alt2\">05<\/div>\n<div class=\"line number6 index5 alt1\">06<\/div>\n<div class=\"line number7 index6 alt2\">07<\/div>\n<div class=\"line number8 index7 alt1\">08<\/div>\n<div class=\"line number9 index8 alt2\">09<\/div>\n<div class=\"line number10 index9 alt1\">10<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java keyword\">public<\/code> <code class=\"java keyword\">class<\/code> <code class=\"java plain\">OrderEdit <\/code><code class=\"java keyword\">extends<\/code> <code class=\"java plain\">AbstractEditor&lt;Order&gt; {<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java color1\">@Inject<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java keyword\">private<\/code> <code class=\"java plain\">CollectionDatasource&lt;OrderLine, UUID&gt; linesDs;<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java color1\">@Override<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java keyword\">public<\/code> <code class=\"java keyword\">void<\/code> <code class=\"java plain\">init(<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">Map&lt;String, Object&gt; params) {<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">linesDs.addCollectionChangeListener(e -&gt; calculateAmount());<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">}<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"java plain\">...<\/code><\/div>\n<div class=\"line number10 index9 alt1\"><code class=\"java plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>And here how it looks in the new version:<\/p>\n<div>\n<div id=\"highlighter_737199\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<div class=\"line number7 index6 alt2\">7<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java keyword\">public<\/code> <code class=\"java keyword\">class<\/code> <code class=\"java plain\">OrderEdit <\/code><code class=\"java keyword\">extends<\/code> <code class=\"java plain\">StandardEditor&lt;Order&gt; {<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java color1\">@Subscribe<\/code><code class=\"java plain\">(id = <\/code><code class=\"java string\">\"linesDc\"<\/code><code class=\"java plain\">, target = Target.DATA_CONTAINER)<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java keyword\">protected<\/code> <code class=\"java keyword\">void<\/code> <code class=\"java plain\">onOrderLinesDcCollectionChange (CollectionChangeEvent&lt;OrderLine&gt; event) {<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">calculateAmount();<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">}<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java plain\">...<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>The code is cleaner and we were able to get rid of the \u201cmagic\u201d <code>init()<\/code> method that is usually stuffed with event handler creation statements. And we don\u2019t even need to inject data component into the controller &#8211; the framework will find it by the component ID.<\/p>\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n<p>Despite the recent introduction of the new generation of the frameworks (<a href=\"https:\/\/micronaut.io\" target=\"_blank\" rel=\"noopener noreferrer\">Micronaut<\/a>, <a href=\"https:\/\/quarkus.io\" target=\"_blank\" rel=\"noopener noreferrer\">Quarkus<\/a>) that have some advantages over \u201ctraditional\u201d frameworks, there is a huge amount of reflection-based code, thanks to <a href=\"https:\/\/spring.io\" target=\"_blank\" rel=\"noopener noreferrer\">Spring<\/a>. We\u2019ll see how the market will change in the nearest future, but nowadays Spring is the obvious leader among Java application frameworks, therefore we\u2019ll be dealing with the reflection API for quite a long time.<\/p>\n<p>And if you think about using reflection API in your code, whether you\u2019re implementing your own framework or just an application, consider two other options &#8211; code generation and, especially, LambdaMetafactory. The latter will increase code execution speed, whilst development won\u2019t take more time compared to \u201ctraditional\u201d reflection API usage.<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td>\n<p>Published on Java Code Geeks with permission by Andrey Belyaev, partner at our <a href=\"\/\/www.javacodegeeks.com\/join-us\/jcg\/\" target=\"_blank\" rel=\"noopener noreferrer\">JCG program<\/a>. See the original article here: <a href=\"https:\/\/www.cuba-platform.com\/blog\/think-twice-before-using-reflection\/\" target=\"_blank\" rel=\"noopener noreferrer\">Think Twice Before Using Reflection<\/a><\/p>\n<p>Opinions expressed by Java Code Geeks contributors are their own.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Sometimes, as a developer, you may bump into a situation when it\u2019s not possible to instantiate an object using the new operator because its class name is stored somewhere in configuration XML or you need to invoke a method which name is specified as an annotation property. In such cases, you always have an &hellip;<\/p>\n","protected":false},"author":42197,"featured_media":112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-98601","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Think Twice Before Using Reflection - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Interested to learn about Reflection? Check our article explaining how to use reflection in order to instantiate an object using the new operator\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Think Twice Before Using Reflection - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Interested to learn about Reflection? Check our article explaining how to use reflection in order to instantiate an object using the new operator\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-24T10:00:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-09-24T10:49:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Andrey Belyaev\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andrey Belyaev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2019\\\/09\\\/think-twice-before-using-reflection.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2019\\\/09\\\/think-twice-before-using-reflection.html\"},\"author\":{\"name\":\"Andrey Belyaev\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/a0a815f052d837744e017db13fae49d0\"},\"headline\":\"Think Twice Before Using Reflection\",\"datePublished\":\"2019-09-24T10:00:03+00:00\",\"dateModified\":\"2019-09-24T10:49:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2019\\\/09\\\/think-twice-before-using-reflection.html\"},\"wordCount\":1722,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2019\\\/09\\\/think-twice-before-using-reflection.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2019\\\/09\\\/think-twice-before-using-reflection.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2019\\\/09\\\/think-twice-before-using-reflection.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2019\\\/09\\\/think-twice-before-using-reflection.html\",\"name\":\"Think Twice Before Using Reflection - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2019\\\/09\\\/think-twice-before-using-reflection.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2019\\\/09\\\/think-twice-before-using-reflection.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2019-09-24T10:00:03+00:00\",\"dateModified\":\"2019-09-24T10:49:19+00:00\",\"description\":\"Interested to learn about Reflection? Check our article explaining how to use reflection in order to instantiate an object using the new operator\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2019\\\/09\\\/think-twice-before-using-reflection.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2019\\\/09\\\/think-twice-before-using-reflection.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2019\\\/09\\\/think-twice-before-using-reflection.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"java-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2019\\\/09\\\/think-twice-before-using-reflection.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Think Twice Before Using Reflection\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/a0a815f052d837744e017db13fae49d0\",\"name\":\"Andrey Belyaev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/465ce96a8e6894ad8d47e2cd2df326f69572e84c6623e41926f871a0097ef108?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/465ce96a8e6894ad8d47e2cd2df326f69572e84c6623e41926f871a0097ef108?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/465ce96a8e6894ad8d47e2cd2df326f69572e84c6623e41926f871a0097ef108?s=96&d=mm&r=g\",\"caption\":\"Andrey Belyaev\"},\"sameAs\":[\"https:\\\/\\\/www.cuba-platform.com\\\/blog\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/andrey-belyaev\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Think Twice Before Using Reflection - Java Code Geeks","description":"Interested to learn about Reflection? Check our article explaining how to use reflection in order to instantiate an object using the new operator","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html","og_locale":"en_US","og_type":"article","og_title":"Think Twice Before Using Reflection - Java Code Geeks","og_description":"Interested to learn about Reflection? Check our article explaining how to use reflection in order to instantiate an object using the new operator","og_url":"https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2019-09-24T10:00:03+00:00","article_modified_time":"2019-09-24T10:49:19+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","type":"image\/jpeg"}],"author":"Andrey Belyaev","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Andrey Belyaev","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html"},"author":{"name":"Andrey Belyaev","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/a0a815f052d837744e017db13fae49d0"},"headline":"Think Twice Before Using Reflection","datePublished":"2019-09-24T10:00:03+00:00","dateModified":"2019-09-24T10:49:19+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html"},"wordCount":1722,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html","url":"https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html","name":"Think Twice Before Using Reflection - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2019-09-24T10:00:03+00:00","dateModified":"2019-09-24T10:49:19+00:00","description":"Interested to learn about Reflection? Check our article explaining how to use reflection in order to instantiate an object using the new operator","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","width":150,"height":150,"caption":"java-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2019\/09\/think-twice-before-using-reflection.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Think Twice Before Using Reflection"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/a0a815f052d837744e017db13fae49d0","name":"Andrey Belyaev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/465ce96a8e6894ad8d47e2cd2df326f69572e84c6623e41926f871a0097ef108?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/465ce96a8e6894ad8d47e2cd2df326f69572e84c6623e41926f871a0097ef108?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/465ce96a8e6894ad8d47e2cd2df326f69572e84c6623e41926f871a0097ef108?s=96&d=mm&r=g","caption":"Andrey Belyaev"},"sameAs":["https:\/\/www.cuba-platform.com\/blog\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/andrey-belyaev"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/98601","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/42197"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=98601"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/98601\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/112"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=98601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=98601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=98601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}