{"id":43720,"date":"2015-09-18T21:51:58","date_gmt":"2015-09-18T18:51:58","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=43720"},"modified":"2023-12-06T14:22:05","modified_gmt":"2023-12-06T12:22:05","slug":"how-to-use-reflection-effectively","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-use-reflection-effectively.html","title":{"rendered":"How to use Reflection effectively"},"content":{"rendered":"<p><em>This article is part of our Academy Course titled <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/advanced-java.html\">Advanced Java<\/a>.<\/p>\n<p>This course is designed to help you make the most effective use of Java. It discusses advanced topics, including object creation, concurrency, serialization, reflection and many more. It will guide you through your journey to Java mastery! Check it out <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/advanced-java.html\">here<\/a>!<\/em><\/p>\n<div class=\"toc\">\n<h4>Table Of Contents<\/h4>\n<dl>\n<dt><a href=\"#introduction\">1. Introduction<\/a><\/dt>\n<dt><a href=\"#api\">2. Reflection API<\/a><\/dt>\n<dt><a href=\"#generic\">3. Accessing generic type parameters<\/a><\/dt>\n<dt><a href=\"#visibility\">4. Reflection API and visibility <\/a><\/dt>\n<dt><a href=\"#pitfalls\">5. Reflection API pitfalls<\/a><\/dt>\n<dt><a href=\"#handles\">6. Method Handles<\/a><\/dt>\n<dt><a href=\"#argnames\">7. Method Argument Names<\/a><\/dt>\n<dt><a href=\"#next\">8. What\u2019s next<\/a><\/dt>\n<dt><a href=\"#download\">9. Download the Source Code<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"introduction\"><\/a>1. Introduction<\/h2>\n<p>In this part of the tutorial we are going to briefly look through a very interesting subject called <strong>reflection<\/strong>. <strong>Reflection<\/strong> is the ability of the program to examine or introspect itself at runtime. <strong>Reflection<\/strong> is an extremely useful and powerful feature which significantly expands the capabilities of the program to perform its own inspections, modifications or transformations during its execution, without a single line of code change. Not every programming language implementation has this feature supported, but luckily Java has adopted it since its beginning.<\/p>\n<h2><a name=\"api\"><\/a>2. Reflection API<\/h2>\n<p>The <strong>Reflection API<\/strong>, which is part of the Java standard library, provides a way to explore intrinsic class details at runtime, dynamically create new class instances (without the explicit usage of the <code>new<\/code> operator), dynamically invoke methods, introspect annotations (annotations have been covered in <strong>part 5<\/strong> of the tutorial, <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/how-and-when-to-use-enums-and-annotations\/\"><strong>How and when to use Enums and Annotations<\/strong><\/a>), and much, much more. It gives the Java developers a freedom to write the code which could adapt, verify, execute and even modify itself while it is being running.<\/p>\n<p>The <strong>Reflection API<\/strong> is designed in quite intuitive way and is hosted under the <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/reflect\/package-summary.html\"><strong><code>java.lang.reflect<\/code><\/strong><\/a> package. Its structure follows closely the Java language concepts and has all the elements to represent classes (including generic versions), methods, fields (members), constructors, interfaces, parameters and annotations. The entry point for the <strong>Reflection API<\/strong> is the respective instance of the <strong><code>Class&lt; ? &gt;<\/code><\/strong> class. For example, the simplest way to list all public methods of the class String is by using the <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/Class.html#getMethods%28%29\"><code>getMethods()<\/code><\/a> method call:<\/p>\n<pre class=\"brush:java\">final Method[] methods = String.class.getMethods();\nfor( final Method method: methods ) {\n    System.out.println( method.getName() );\n}\n<\/pre>\n<p>Following the same principle, we can list all public fields of the class String is by using the <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/Class.html#getFields%28%29\"><code>getFields()<\/code><\/a> method call, for example:<\/p>\n<pre class=\"brush:java\">final Field[] fields = String.class.getFields();\nfor( final Field field: fields ) {\n    System.out.println( field.getName() );\n}\n<\/pre>\n<p>Continuing the experimentation with the String class using reflection, let us try to create a new instance and call the <code>length()<\/code> method on it, all that by using the <strong>reflection API<\/strong> only.<\/p>\n<pre class=\"brush:java\">final Constructor&lt; String &gt; constructor = String.class.getConstructor( String.class );\nfinal String str = constructor.newInstance( \"sample string\" );\nfinal Method method = String.class.getMethod( \"length\" );\nfinal int length = ( int )method.invoke( str );\n\/\/ The length of the string is 13 characters\n<\/pre>\n<p>Probably the most demanded use cases for <strong>reflection<\/strong> revolve around annotation processing. Annotations by themselves (excluding the ones from Java standard library) do not have any effect on the code. However, Java applications can use <strong>reflection<\/strong> to inspect the annotations present on the different Java elements of their interest at runtime and apply some logic depending on annotation and its attributes. For example, let us take a look on the way to introspect if the specific annotation is present on a class definition:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:java\">@Retention( RetentionPolicy.RUNTIME )\n@Target( ElementType.TYPE )\npublic @interface ExampleAnnotation {\n    \/\/ Some attributes here\n}\n\n@ExampleAnnotation\npublic class ExampleClass {\n    \/\/ Some getter and setters here\n}\n<\/pre>\n<p>Using the <strong>reflection API<\/strong>, it could be easily done using the <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/Class.html#getAnnotation%28java.lang.Class%29\"><strong><code>getAnnotation()<\/code><\/strong><\/a> method call. The returned non-null value indicates that annotation is present, for example:<\/p>\n<pre class=\"brush:java\">final ExampleAnnotation annotation =\n    ExampleClass.class.getAnnotation( ExampleAnnotation.class );\n\nif( annotation != null ) {\n    \/\/ Some implementation here\n}\n<\/pre>\n<p>Most of the Java APIs nowadays are including annotations to facilitate their usage and integration for developers. The recent versions of the very popular Java specifications like <strong>Java API for RESTful Web Services<\/strong> (<a href=\"https:\/\/jcp.org\/en\/jsr\/detail?id=339\" target=\"_blank\" rel=\"noopener\">https:\/\/jcp.org\/en\/jsr\/detail?id=339<\/a>), <strong>Bean Validation<\/strong> (<a href=\"https:\/\/jcp.org\/en\/jsr\/detail?id=349\" target=\"_blank\" rel=\"noopener\">https:\/\/jcp.org\/en\/jsr\/detail?id=349<\/a>), <strong>Java Temporary Caching API<\/strong> (<a href=\"https:\/\/jcp.org\/en\/jsr\/detail?id=107\" target=\"_blank\" rel=\"noopener\">https:\/\/jcp.org\/en\/jsr\/detail?id=107<\/a>), <strong>Java Message Service<\/strong> (<a href=\"https:\/\/jcp.org\/en\/jsr\/detail?id=343\" target=\"_blank\" rel=\"noopener\">https:\/\/jcp.org\/en\/jsr\/detail?id=343<\/a>), <strong>Java Persistence<\/strong> (<a href=\"https:\/\/jcp.org\/en\/jsr\/detail?id=338\" target=\"_blank\" rel=\"noopener\">https:\/\/jcp.org\/en\/jsr\/detail?id=338<\/a>) and many, many more are built on top of annotations and their implementations usually heavily use the <strong>Reflection API<\/strong> to gather metadata about the application being run.<\/p>\n<h2><a name=\"generic\"><\/a>3. Accessing generic type parameters<\/h2>\n<p>Since the introduction of generics (generics are covered in <strong>part 4<\/strong> of the tutorial, <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/how-and-when-to-use-generics\/\"><strong>How and When To Use Generics<\/strong><\/a>), the <strong>Reflection API<\/strong> has been extended to support the introspection of generic types. The use case which often pops up in many different applications is to figure out the type of the generic parameters that particular class, method or other element has been declared with. Let us take a look on the example class declaration:<\/p>\n<pre class=\"brush:java\">public class ParameterizedTypeExample {\n    private List&lt; String &gt; strings;\n\n    public List&lt; String &gt; getStrings() {\n        return strings;\n    }\n}\n<\/pre>\n<p>Now, while inspecting the class using <strong>Reflection<\/strong> it would be very handy to know that the <code>strings<\/code> property is declared as generic type <code>List<\/code> with <code>String<\/code> type parameter. The code snippet below illustrates how it could be done:<\/p>\n<pre class=\"brush:java\">final Type type = ParameterizedTypeExample.class\n    .getDeclaredField( \"strings\" ).getGenericType();\n\nif( type instanceof ParameterizedType ) {\n    final ParameterizedType parameterizedType = ( ParameterizedType )type;\n    for( final Type typeArgument: parameterizedType.getActualTypeArguments() ) {\n        System.out.println( typeArgument );\n    }\n}\n<\/pre>\n<p>The following generic type parameter will be printed on the console:<\/p>\n<pre class=\"brush:bash\">\nclass java.lang.String\n<\/pre>\n<h2><a name=\"visibility\"><\/a>4. Reflection API and visibility<\/h2>\n<p>In the <strong>part 1<\/strong> of the tutorial, <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/how-to-create-and-destroy-objects\/\"><strong>How to create and destroy objects<\/strong><\/a>, we have met first time the accessibility an visibility rules which are supported by the Java language. It may come up as a surprise, but the <strong>reflection API<\/strong> is able to modify, in a certain way, the visibility rules on a given class member.<\/p>\n<p>Let us take a look on the following example of the class with a single private field name. The getter to this filed is provided, but the setter is not, and this is by intention.<\/p>\n<pre class=\"brush:java\">public static class PrivateFields {\n    private String name;\n\n    public String getName() {\n        return name;\n    }\n}\n<\/pre>\n<p>Obviously, it is apparent for any Java developer that the <code>name<\/code> field cannot be set using Java language syntax constructs as the class does not provide the way to do that. <strong>Reflection API<\/strong> on the rescue, let see how it could be done by changing field\u2019s visibility and accessibility scope.<\/p>\n<pre class=\"brush:java\">final PrivateFields instance = new PrivateFields();\nfinal Field field = PrivateFields.class.getDeclaredField( \"name\" );\nfield.setAccessible( true );\nfield.set( instance, \"sample name\" );\nSystem.out.println( instance.getName() );\n<\/pre>\n<p>The following output will be printed on the console:<\/p>\n<pre class=\"brush:bash\">\nsample name\n<\/pre>\n<p>Please notice that without the <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/reflect\/AccessibleObject.html#setAccessible%28boolean%29\"><code>field.setAccessible( true )<\/code><\/a> call, the exception will be raised at runtime saying that the member of class with modifiers <code>private<\/code> cannot be accessed.<\/p>\n<p>This feature of the <strong>reflection API<\/strong> is often used by test scaffolding or dependency injection frameworks in order to get access to the intrinsic (or not exposable) implementation details. Please, try to avoid using it in your applications unless you really have no other choice.<br \/>\n[ulp id=&#8217;w6F4W4SAMiyTapBF&#8217;]<br \/>\n&nbsp;<\/p>\n<h2><a name=\"pitfalls\"><\/a>5. Reflection API pitfalls<\/h2>\n<p>Also, be aware that even though the <strong>reflection API<\/strong> is very powerful, it has a few pitfalls. First of all, it is a subject of security permissions and may not be available in all environments your code is running on. Secondly, it could have a performance impact on your applications. From execution prospective, the calls to <strong>reflection API<\/strong> are quite expensive.<\/p>\n<p>Lastly, <strong>reflection API<\/strong> does not provide enough type safety guarantees, forcing developers to use Object instances in most of the places, and is quite limited in transforming constructor \/ method arguments or method return values.<\/p>\n<p>Since the Java 7 release, there are a couple of useful features which could provide much faster, alternative way to access some functionality used to be available only through <strong>reflection<\/strong> calls. The next section will introduce you to them.<\/p>\n<h2><a name=\"handles\"><\/a>6. Method Handles<\/h2>\n<p>The Java 7 release introduced a new very important feature into the JVM and Java standard library &#8211; method handles. Method handle is a typed, directly executable reference to an underlying method, constructor or field (or similar low-level operation) with optional transformations of arguments or return values. They are in many respects better alternative to method invocations performed using the <strong>Reflection API<\/strong>. Let us take a look on a code snippet which uses method handles to dynamically invoke the method <code>length()<\/code> on the <code>String<\/code> class.<\/p>\n<pre class=\"brush:java\">final MethodHandles.Lookup lookup = MethodHandles.lookup();\nfinal MethodType methodType = MethodType.methodType( int.class );\nfinal MethodHandle methodHandle =\n    lookup.findVirtual( String.class, \"length\", methodType );\nfinal int length = ( int )methodHandle.invokeExact( \"sample string\" );\n\/\/ The length of the string is 13 characters\n<\/pre>\n<p>The example above is not very complicated and just outlines the basic idea of what method handles are capable of. Please compare it with the same implementation which uses the <strong><a href=\"#api\">Reflection API<\/a><\/strong> from Reflection API section. However it does look a bit more verbose but from the performance and type safety prospective method handles are better alternative.<\/p>\n<p>Method handles are very powerful tool and they build a necessary foundation for effective implementation of dynamic (and scripting) languages on the JVM platform. In the <strong>part 12<\/strong> of the tutorial, <strong>Dynamic languages support<\/strong>, we are going to look on couple of those languages.<\/p>\n<h2><a name=\"argnames\"><\/a>7. Method Argument Names<\/h2>\n<p>The well-known issue which Java developers have been facing for years is the fact that method argument names are not preserved at runtime and were wiped out completely. Several community projects, like for example <strong>Paranamer<\/strong> (<a href=\"https:\/\/github.com\/paul-hammant\/paranamer\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/paul-hammant\/paranamer<\/a>), tried to solve this issue by injecting some additional metadata into the generated byte code. Luckily, Java 8 changed that by introducing new compiler argument <code>\u2013parameters<\/code> which injects the exact method argument names into the byte code. Let us take a look on the following method:<\/p>\n<pre class=\"brush:java\">public static void performAction( final String action, final Runnable callback ) {\n\t\/\/ Some implementation here\n}\n<\/pre>\n<p>In the next step, let us use the <strong>Reflection API<\/strong> to inspect method argument names for this method and make sure they are preserved:<\/p>\n<pre class=\"brush:java\">final Method method = MethodParameterNamesExample.class\n    .getDeclaredMethod( \"performAction\", String.class, Runnable.class );\nArrays.stream( method.getParameters() )\n    .forEach( p -&gt; System.out.println( p.getName() ) );\n<\/pre>\n<p>With the <code>-parameters<\/code> compiler option specified, the following argument names will be printed on the console:<\/p>\n<pre class=\"brush:bash\">\naction\ncallback\n<\/pre>\n<p>This very long-awaited feature is really a great relief for developers of many Java libraries and frameworks. From now on, much more pieces of useful metadata could be extracted using just pure Java <strong>Reflection API<\/strong> without a need to introduce any additional workarounds (or hacks).<\/p>\n<h2><a name=\"next\"><\/a>8. What\u2019s next<\/h2>\n<p>In this part of the tutorial we have covered <strong>reflection API<\/strong>, which is the way to inspect your code, extract useful metadata out of it or even modify it. Despite all its drawbacks, <strong>reflection API<\/strong> is very widely used in most (if not all) Java applications these days. In next part of the tutorial we are going to talk about scripting and dynamic languages support in Java.<\/p>\n<h2><a name=\"download\"><\/a>9. Download the Source Code<\/h2>\n<p>This was a lesson of <strong>Reflection<\/strong>, part 11 of <strong>Advanced Java course<\/strong>. You may download the source code here: <strong><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/advanced-java-part-11.zip\">advanced-java-part-11<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses advanced topics, including object creation, concurrency, serialization, reflection and many more. It will guide you through your journey to Java mastery! Check it out here! Table Of Contents &hellip;<\/p>\n","protected":false},"author":141,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[343,88,580,557,65],"class_list":["post-43720","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-annotations","tag-concurrency","tag-generics","tag-reflection","tag-serialization"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to use Reflection effectively - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses\" \/>\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\/2015\/09\/how-to-use-reflection-effectively.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use Reflection effectively - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-use-reflection-effectively.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=\"2015-09-18T18:51:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-06T12:22:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/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 Redko\" \/>\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 Redko\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-use-reflection-effectively.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-use-reflection-effectively.html\"},\"author\":{\"name\":\"Andrey Redko\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/771a6504862edc45322776832cbce413\"},\"headline\":\"How to use Reflection effectively\",\"datePublished\":\"2015-09-18T18:51:58+00:00\",\"dateModified\":\"2023-12-06T12:22:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-use-reflection-effectively.html\"},\"wordCount\":1538,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-use-reflection-effectively.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"Annotations\",\"Concurrency\",\"Generics\",\"Reflection\",\"Serialization\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-use-reflection-effectively.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-use-reflection-effectively.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-use-reflection-effectively.html\",\"name\":\"How to use Reflection effectively - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-use-reflection-effectively.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-use-reflection-effectively.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2015-09-18T18:51:58+00:00\",\"dateModified\":\"2023-12-06T12:22:05+00:00\",\"description\":\"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-use-reflection-effectively.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-use-reflection-effectively.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-use-reflection-effectively.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/how-to-use-reflection-effectively.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\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"How to use Reflection effectively\"}]},{\"@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\\\/771a6504862edc45322776832cbce413\",\"name\":\"Andrey Redko\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g\",\"caption\":\"Andrey Redko\"},\"description\":\"Andriy is a well-grounded software developer with more then 12 years of practical experience using Java\\\/EE, C#\\\/.NET, C++, Groovy, Ruby, functional programming (Scala), databases (MySQL, PostgreSQL, Oracle) and NoSQL solutions (MongoDB, Redis).\",\"sameAs\":[\"http:\\\/\\\/aredko.blogspot.com\\\/\",\"http:\\\/\\\/ca.linkedin.com\\\/in\\\/aredko\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/andrey-redko\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to use Reflection effectively - Java Code Geeks","description":"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses","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\/2015\/09\/how-to-use-reflection-effectively.html","og_locale":"en_US","og_type":"article","og_title":"How to use Reflection effectively - Java Code Geeks","og_description":"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses","og_url":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-use-reflection-effectively.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2015-09-18T18:51:58+00:00","article_modified_time":"2023-12-06T12:22:05+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Andrey Redko","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Andrey Redko","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-use-reflection-effectively.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-use-reflection-effectively.html"},"author":{"name":"Andrey Redko","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/771a6504862edc45322776832cbce413"},"headline":"How to use Reflection effectively","datePublished":"2015-09-18T18:51:58+00:00","dateModified":"2023-12-06T12:22:05+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-use-reflection-effectively.html"},"wordCount":1538,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-use-reflection-effectively.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["Annotations","Concurrency","Generics","Reflection","Serialization"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-use-reflection-effectively.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-use-reflection-effectively.html","url":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-use-reflection-effectively.html","name":"How to use Reflection effectively - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-use-reflection-effectively.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-use-reflection-effectively.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2015-09-18T18:51:58+00:00","dateModified":"2023-12-06T12:22:05+00:00","description":"This article is part of our Academy Course titled Advanced Java. This course is designed to help you make the most effective use of Java. It discusses","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-use-reflection-effectively.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-use-reflection-effectively.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-use-reflection-effectively.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/how-to-use-reflection-effectively.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":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"How to use Reflection effectively"}]},{"@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\/771a6504862edc45322776832cbce413","name":"Andrey Redko","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g","caption":"Andrey Redko"},"description":"Andriy is a well-grounded software developer with more then 12 years of practical experience using Java\/EE, C#\/.NET, C++, Groovy, Ruby, functional programming (Scala), databases (MySQL, PostgreSQL, Oracle) and NoSQL solutions (MongoDB, Redis).","sameAs":["http:\/\/aredko.blogspot.com\/","http:\/\/ca.linkedin.com\/in\/aredko"],"url":"https:\/\/www.javacodegeeks.com\/author\/andrey-redko"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/43720","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\/141"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=43720"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/43720\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=43720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=43720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=43720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}