{"id":33058,"date":"2014-11-18T16:00:23","date_gmt":"2014-11-18T14:00:23","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=33058"},"modified":"2023-12-05T16:07:50","modified_gmt":"2023-12-05T14:07:50","slug":"java-reflection-tutorial-2","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2014\/11\/java-reflection-tutorial-2.html","title":{"rendered":"Java Reflection Tutorial &#8211; The ULTIMATE Guide (PDF Download)"},"content":{"rendered":"<p><strong>EDITORIAL NOTE<\/strong>: In this post, we feature a comprehensive Java Reflection Tutorial. Reflection is a feature in the Java programming language. It allows an executing Java program to examine or &#8220;introspect&#8221; upon itself, and manipulate internal properties of the program. For example, it&#8217;s possible for a Java class to obtain the names of all its members and display them.<\/p>\n<p>This tutorial is about reflection, the ability of a computer program to examine and modify the structure and behavior (specifically the values, meta-data, properties and functions) of the program at runtime.<\/p>\n<p>We are going to explain what reflection is in general and how can be used in Java. Real uses cases about different reflection uses are listed in the next chapters.<\/p>\n<p>Several code snippets will be shown; at the end of this article you can find a compressed file that contains all these examples (and some more).<\/p>\n<p>All code has been written using Eclipse Luna 4.4 and Java update 8.25, no third party libraries are needed.<\/p>\n<div class=\"toc\">\n<h3>Table Of Contents<\/h3>\n<dl>\n<dt><a href=\"#Reflection\">1. Reflection<\/a><\/dt>\n<dt><a href=\"#Introduction to reflection in Java\">2. Introduction to reflection in Java<\/a><\/dt>\n<dt><a href=\"#Use cases\">3. Use cases<\/a><\/dt>\n<dt><a href=\"#Reflection components and mechanisms\">4. Reflection components and mechanisms<\/a><\/dt>\n<dt><a href=\"#Classes\">5. Classes<\/a><\/dt>\n<dt><a href=\"#Interfaces\">6. Interfaces<\/a><\/dt>\n<dt><a href=\"#Enums\">7. Enums<\/a><\/dt>\n<dt><a href=\"#Primitive types\">8. Primitive types<\/a><\/dt>\n<dt><a href=\"#Fields\">9. Fields<\/a><\/dt>\n<dt><a href=\"#Methods\">10. Methods<\/a><\/dt>\n<dt><a href=\"#Constructors\">11. Constructors<\/a><\/dt>\n<dt><a href=\"#Getters and Setters\">12. Getters and Setters<\/a><\/dt>\n<dt><a href=\"#Static elements\">13. Static elements<\/a><\/dt>\n<dt><a href=\"#Arrays\">14. Arrays<\/a><\/dt>\n<dt><a href=\"#Collections\">15. Collections<\/a><\/dt>\n<dt><a href=\"#Annotations\">16. Annotations<\/a><\/dt>\n<dt><a href=\"#Generics\">17. Generics<\/a><\/dt>\n<dt><a href=\"#Class Loaders\">18. Class Loaders<\/a><\/dt>\n<dt><a href=\"#Dynamic Proxies\">19. Dynamic Proxies<\/a><\/dt>\n<dt><a href=\"#Java 8 Reflection features\">20. Java 8 Reflection features<\/a><\/dt>\n<dt><a href=\"#Summary\">21. Summary<\/a><\/dt>\n<dt><a href=\"#Download\">22. Download<\/a><\/dt>\n<dt><a href=\"#Resources\">23. Resources<\/a><\/dt>\n<\/dl>\n<\/div>\n<p>&nbsp;<\/p>\n<h2><a name=\"Reflection\"><\/a>1. Reflection<\/h2>\n<p>The concept of reflection in software means the ability to inspect, analyze and modify other code at runtime. For example imagine an application that takes as input some files containing source code (we do not care about what source code yet). The goal of this application is to count the number of methods that are contained in each passed class. This can be solved using reflection by analyzing the code and counting the elements which are actually methods, ignoring other kind of elements like attributes, interfaces, etc, and grouping them by classes.<\/p>\n<p>Purely speaking, this example is not really reflection, because the code does not have to be analyzed at runtime and the task can be done in any other stage, but it can be also done at runtime and then we would be actually talking about reflection.<\/p>\n<p>Another example would be an application that analyzes the content of given classes and executes the methods that contain a specific annotation with arguments provided in runtime: In the Java Junit framework we have for example the annotation <code>@Test<\/code>. This is actually what Junit does; and does it using reflection.<\/p>\n<h2><a name=\"Introduction to reflection in Java\"><\/a>2. Introduction to reflection in Java<\/h2>\n<p>In Java, it is possible to inspect fields, classes, methods, annotations, interfaces, etc. at runtime. You do not need to know how classes or methods are called, neither the parameters that are needed, all of that can be retrieved at runtime using reflection. It is also possible to instantiate new classes, to create new instances and to execute their methods, all of it using reflection.<\/p>\n<p>Reflection is present in Java since the beginning of the times via its reflection API. The class <a href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Class.html\"><code>Class<\/code><\/a> contains all the reflection related methods that can be applied to classes and objects like the ones that allow a programmer to retrieve the class name, to retrieve the public methods of a class, etc. Other important classes are <a href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/reflect\/Method.html\"><code>Method<\/code><\/a>, <a href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/reflect\/Field.html\"><code>Field<\/code><\/a> and <a href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/reflect\/Type.html\"><code>Type<\/code><\/a> containing specific reflection methods that we are going to see in this tutorial.<\/p>\n<p>Although reflection is very useful in many scenarios, it should not be used for everything. If some operation can be executed without using reflection, then we should not use it. Here are some reasons:<\/p>\n<ul>\n<li>The performance is affected by the use of reflection since all compilation optimizations cannot be applied: reflection is resolved at runtime and not at compile stages.<\/li>\n<li>Security vulnerabilities have to be taken into consideration since the use of reflection may not be possible when running in secure contexts like Applets.<\/li>\n<li>Another important disadvantage that is good to mention here is the maintenance of the code. If your code uses reflection heavily it is going to be more difficult to maintain. The classes and methods are not directly exposed in the code and may vary dynamically so it can get difficult to change the number of parameters that a method expects if the code that calls this method is invoked via reflection.<\/li>\n<li>Tools that automatically refactor or analyze the code may have trouble when a lot of reflection is present.<\/li>\n<\/ul>\n<h2><a name=\"Use cases\"><\/a>3. Use cases<\/h2>\n<p>Despite all the limitations, reflection is a very powerful tool in Java that can be taken into consideration in several scenarios.<\/p>\n<p>In general, reflection can be used to observe and modify the behavior of a program at runtime. Here is a list with the most common use cases:<\/p>\n<ul>\n<li>IDEs can heavily make use of reflection in order to provide solutions for auto completion features, dynamic typing, hierarchy structures, etc. For example, IDEs like Eclipse or PHP Storm provide a mechanism to retrieve dynamically the arguments expected for a given method or a list of public methods starting by &#8220;get&#8221; for a given instance. All these are done using reflection.<\/li>\n<li>Debuggers use reflection to inspect dynamically the code that is being executed.<\/li>\n<li>Test tools like Junit or Mockito use reflection in order to invoke desired methods containing specific syntax or to mock specific classes, interfaces and methods.<\/li>\n<li>Dependency injection frameworks use reflection to inject beans and properties at runtime and initialize all the context of an application.<\/li>\n<li>Code analysis tools like PMD or Findbugs use reflection in order to analyze the code against the list of code violations that are currently configured.<\/li>\n<li>External tools that make use of the code dynamically may use reflection as well.<\/li>\n<\/ul>\n<p>In this tutorial we are going to see several examples of use of reflection in Java. We will see how to get all methods for a given instance, without knowing what kind of class this instance is and we are going to invoke different methods depending on their syntax.<\/p>\n<p>We are not just going to show what other tutorials do, but we will go one step forward by indicating how to proceed when using reflection with generics, annotations, arrays, collections and other kind of objects. Finally we will explain the main new features coming out with Java 8 related to this topic.<\/p>\n<h2><a name=\"Reflection components and mechanisms\"><\/a>4. Reflection components and mechanisms<\/h2>\n<p>In order to start coding and using reflection in Java we first have to explain a couple of concepts that may be relevant.<\/p>\n<ul>\n<li><code>Interface<\/code> in Java is a contract with the applications that may use them. Interfaces contain a list of methods that are exposed and that have to be implemented by the subclasses implementing these interfaces. Interfaces cannot be instantiated. Since Java 8 they can contain default method implementations although this is not the common use.<\/li>\n<li><code>Class<\/code> is the implementation of a series of methods and the container of a series of properties. It can be instantiated.<\/li>\n<li><code>Object<\/code> is an instance of a given class.<\/li>\n<li><code>Method<\/code> is some code performing some actions. They have return types as outputs and input parameters.<\/li>\n<li><code>Field<\/code> is a property of a class.<\/li>\n<li><code>Enums<\/code> are elements containing a set of predefined constants.<\/li>\n<li><code>Private<\/code> element is an element that is only visible inside a class and cannot be accessed from outside. It can be a method, a field&#8230;<\/li>\n<li><code>Static<\/code> elements are elements that belong to the class and not to a specific instance. Static elements can be fields used across all instances of a given class, methods that can be invoked without need to instantiate the class, etc. This is very interesting while using reflection since it is different to invoke a static method than a non static one where you need an instance of a class to execute it.<\/li>\n<li><code>Annotation<\/code> is code Meta data informing about the code itself.<\/li>\n<li><code>Collection<\/code> is a group of elements, can be a List, a Map, a Queue, etc.<\/li>\n<li><code>Array<\/code> is an object containing a fixed number of values. Its length is fixed and is specified on creation.<\/li>\n<li><code>Dynamic proxy<\/code> is a class implementing a list of interfaces specified at runtime. They use the class <code>java.lang.reflect.Proxy<\/code>. We will see this more in detail in the next chapters.<\/li>\n<li><code>Class loader<\/code> is an object in charge of loading classes given the name of a class. In Java, every class provide methods to retrieve the class loader: <code>Class.getClassLoader()<\/code>.<\/li>\n<li><code>Generics <\/code>were introduced in java update 5. They offer compile time safety by indicating what type or sub types a collection is going to use. For example using generics you can prevent that an application using a list containing strings would try to add a Double to the list in compile time.<\/li>\n<\/ul>\n<p>The different nature of these components is important in order to use reflection within them. Is not the same to try to invoke a private method than a public one; it is different to get an annotation name or an interface one, etc. We will see examples for all of these in the next chapters.<\/p>\n<h2><a name=\"Classes\"><\/a>5. Classes<\/h2>\n<p>Everything in Java is about classes, reflection as well. Classes are the starting point when we talk about reflection. The class <code>java.lang.Class<\/code> contains several methods that allow programmers to retrieve information about classes and objects (and other elements) at runtime.<\/p>\n<p>In order to retrieve the class information from a single instance we can write (in this case, for the <code>String<\/code> class):<\/p>\n<pre class=\"brush:java\">\tClass&lt;? extends String&gt; stringGetClass = stringer.getClass();\n<\/pre>\n<p>Or directly from the class name without instantiation:<\/p>\n<pre class=\"brush:java\">\tClass&lt;String&gt; stringclass = String.class;\n<\/pre>\n<p>or using the <code>java.lang.Class.forName(String)<\/code> method:<\/p>\n<pre class=\"brush:java\">\tClass.forName( \"java.lang.String\" )\n<\/pre>\n<p>From a class object we can retrieve all kind of information like declared methods, constructors, visible fields, annotations, types&#8230;In this tutorial all these is explained in the following chapters.<\/p>\n<p>It is also possible to check properties for a given class like for example if a class is a primitive, or an instance:<\/p>\n<pre class=\"brush:java\">\tstringGetClass.isInstance( \"dani\" );\n\tstringGetClass.isPrimitive();\n<\/pre>\n<p>It is also possible to create new instances of a given class using the method <code>java.lang.Class.newInstance()<\/code> passing the right arguments:<\/p>\n<pre class=\"brush:java\">String newInstanceStringClass = stringclass.newInstance();\n\n        String otherInstance = (String)Class.forName( \"java.lang.String\" ).newInstance();\n<\/pre>\n<p>The <code>java.lang.Class.newInstance()<\/code> method can be used only when the class contains a public default constructor or a constructor without arguments, if this is not the case, this method cannot be used. In these cases where the <code>java.lang.Class.newInstance()<\/code> method cannot be used the solution is to retrieve a proper constructor at runtime and create an instance using this constructor with the arguments that it is expecting. We will see in the chapter related to constructors.<\/p>\n<h2><a name=\"Interfaces\"><\/a>6. Interfaces<\/h2>\n<p>Interfaces are elements that cannot be instantiated and that contain the exposed methods that should be implemented by their subclasses. Related to reflection there is nothing special regarding interfaces.<\/p>\n<p>Interfaces can be accessed like a class using their qualified name. All methods available for classes are available for interfaces as well. Here is an example of how to access interface class information at runtime:<\/p>\n<pre class=\"brush:java\">\/\/ can be accessed like a class\nSystem.out.println( \"interface name: \" + InterfaceExample.class.getName() );\n<\/pre>\n<p>Assuming that the <code>InterfaceExample<\/code> element is an interface.<\/p>\n<p>One obvious difference between classes and interfaces is that interfaces cannot be instantiated using reflection via the <code>newInstance()<\/code> method:<\/p>\n<pre class=\"brush:java\">\/\/ cannot be instantiated:  java.lang.InstantiationException\nInterfaceExample.class.newInstance();\n<\/pre>\n<p>The snippet above will throw an <code>InstantiationException<\/code> at runtime. At compile time no error appears.<\/p>\n<h2><a name=\"Enums\"><\/a>7. Enums<\/h2>\n<p>Enums are special java types that allow variables to be a set of constants. These constants are predefined in the enum declaration:<\/p>\n<pre class=\"brush:java\">enum ExampleEnum\n{\n\tONE, TWO, THREE, FOUR\n};\n<\/pre>\n<p>Java contains several enums specific methods:<\/p>\n<ul>\n<li><code>java.lang.Class.isEnum()<\/code>: Returns true if the element is of the type enum. False otherwise<\/li>\n<li><code>java.lang.Class.getEnumConstants()<\/code>: Gets all constants for the given element (which is an enum). In case the element is not an enum an exception is thrown.<\/li>\n<li><code>java.lang.reflect.Field.isEnumConstant()<\/code>: Returns true in case the field used is an enum constant. False otherwise. Only applicable to fields.<\/li>\n<\/ul>\n<p>We are going to see an example of how to use the main enum methods related to reflection. First of all we create an instance of the enum:<\/p>\n<pre class=\"brush:java\">ExampleEnum value = ExampleEnum.FOUR;\n<\/pre>\n<p>We can check if the element is an enum using the method <code>isEnum()<\/code>:<\/p>\n<pre class=\"brush:java\">System.out.println( \"isEnum \" + value.getClass().isEnum() );\n<\/pre>\n<p>In order to retrieve all the enum constants we can do something like the following using the method <code>getEnumConstants()<\/code>:<\/p>\n<pre class=\"brush:java\">ExampleEnum[] enumConstants = value.getClass().getEnumConstants();\nfor( ExampleEnum exampleEnum : enumConstants )\n{\n        System.out.println( \"enum constant \" + exampleEnum );\n}\n<\/pre>\n<p>Finally we can check how to use the field related method <code>isEnumConstants()<\/code>. First we retrieve all declared fields for the given class (we will see more in detail in the next chapters all methods related reflection utilities) and after that we can check if the field is an enum constant or not:<\/p>\n<pre class=\"brush:java\">Field[] flds = value.getClass().getDeclaredFields();\nfor( Field f : flds )\n{\n\t\/\/ check for each field if it is an enum constant or not\n\tSystem.out.println( f.getName() + \" \" + f.isEnumConstant() );\n}\n<\/pre>\n<p>The output of all these pieces of code will be something like the following:<\/p>\n<pre class=\"brush:bash\">isEnum true\nenum constant ONE\nenum constant TWO\nenum constant THREE\nenum constant FOUR\nONE true\nTWO true\nTHREE true\nFOUR true\nENUM$VALUES false\n<\/pre>\n<p>The string <code>ENUM$VALUES false<\/code> refers to the internal enum values field. For more information about enums and how to handle them, please visit <a href=\"http:\/\/docs.oracle.com\/javase\/tutorial\/java\/javaOO\/enum.html\">http:\/\/docs.oracle.com\/javase\/tutorial\/java\/javaOO\/enum.html<\/a>.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h2><a name=\"Primitive types\"><\/a>8. Primitive types<\/h2>\n<p>In Java, there are a couple of types that are handled differently because of its nature and behavior: when we are talking about reflection, primitive types like int, float, double, etc. can be accessed and used almost like any other classes.<br \/>\nHere are a couple of examples of how to use reflection when we are working with primitive types:<\/p>\n<p>\u2022 It is possible to retrieve a class object from a primitive type as for any other non primitive type:<\/p>\n<pre class=\"brush:java\">\tClass&lt;Integer&gt; intClass = int.class;\n<\/pre>\n<p>\u2022 But It is not possible to create new instances for primitive types using reflection:<\/p>\n<pre class=\"brush:java\">\tInteger intInstance = intClass.newInstance();\n<\/pre>\n<p>\u2022 It is possible to check if a given class belongs to a primitive type or not using the method <code>java.lang.Class.isPrimitive()<\/code>:<\/p>\n<pre class=\"brush:java\">        System.out.println( \"is primitive: \" + intClass.isPrimitive() );\n<\/pre>\n<p>In this case an exception of the type <code> java.lang.InstantiationException<\/code> is going to be thrown.<\/p>\n<h2><a name=\"Fields\"><\/a>9. Fields<\/h2>\n<p>Class fields can be handled in runtime using reflection. Classes offer several methods to access their fields at runtime. The most important ones are:<br \/>\n\u2022 <code>java.lang.Class.getDeclaredFields()<\/code>: It returns an array with all declared fields for the class. It returns all private fields as well.<br \/>\n\u2022 <code>java.lang.Class.getFields()<\/code>: It returns an array with all accessible fields for the class.<br \/>\n\u2022 <code>java.lang.Class.getField(String)<\/code>: It returns a field with the name passed as parameter. It throws an exception if the field does not exist or is not accessible.<br \/>\n\u2022 <code>java.lang.Class.getDeclaredFields()<\/code>: It returns a field with the given name, if the field does not exist it throws an exception.<br \/>\nThese methods return an array of elements (or a single one) of the type <code>java.lang.reflect.Field<\/code>. This class contains several interesting methods that can be used at runtime that allow a programmer to read the properties and the values of the specific field.<\/p>\n<p>Here is a class that uses this functionality:<\/p>\n<pre class=\"brush:java; wrap-lines:false\">String stringer = \"this is a String called stringer\";\n\nClass&lt;? extends String&gt; stringGetClass = stringer.getClass();\n\nClass&lt;String&gt; stringclass = String.class;\n\nField[] fields = stringclass.getDeclaredFields();\n\nfor( Field field : fields )\n{\nSystem.out.println( \"*************************\" );\nSystem.out.println( \"Name: \" + field.getName() );\nSystem.out.println( \"Type: \" + field.getType() );\n\n\/\/ values\nif( field.isAccessible() )\n{\n\tSystem.out.println( \"Get: \" + field.get( stringer ) );\n\t\/\/ depending on the type we can access the fields using these methods\n\t\/\/ System.out.println( \"Get boolean: \" + field.getBoolean( stringer ) );\n\t\/\/ System.out.println( \"Get short: \" + field.getShort( stringer ) );\n\t\/\/ ...\n}\nSystem.out.println( \"Modifiers:\" + field.getModifiers() );\nSystem.out.println( \"isAccesible: \" + field.isAccessible() );\n\n}\n\n\/\/ stringclass.getField( \"hashCode\" );\/\/exception\n\nField fieldHashCode = stringclass.getDeclaredField( \"hash\" );\/\/ all fields can be\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t \/\/ accessed this way\n\n\/\/ fieldHashCode.get( stringer ); \/\/ this produces an java.lang.IllegalAccessException\n\n\/\/ we change the visibility\nfieldHashCode.setAccessible( true );\n\n\/\/ and we can access it\nObject value = fieldHashCode.get( stringer );\nint valueInt = fieldHashCode.getInt( stringer );\nSystem.out.println( value );\n\nSystem.out.println( valueInt );\n<\/pre>\n<p>In the snippet shown above you can see that the <code>Field<\/code> contains several methods to get the values of a given field like <code>get()<\/code> or type specific ones like <code>getInt()<\/code>. We also can see in the pasted code how we can change the way the visibility of a given field by using the method <code>setAccessible()<\/code>.<\/p>\n<p>This is not always possible and under specific conditions and environments may be prevented. However this allows us to make a private field accessible and access its value and properties via reflection. This is very useful in testing frameworks like <code>Mockito<\/code> or <code>PowerMock<\/code>.<\/p>\n<p>The output or the program would be:<\/p>\n<pre class=\"brush:bash\">*************************\nName: value\nType: class [C\nModifiers:18\nisAccesible: false\n*************************\nName: hash\nType: int\nModifiers:2\nisAccesible: false\n*************************\nName: serialVersionUID\nType: long\nModifiers:26\nisAccesible: false\n*************************\nName: serialPersistentFields\nType: class [Ljava.io.ObjectStreamField;\nModifiers:26\nisAccesible: false\n*************************\nName: CASE_INSENSITIVE_ORDER\nType: interface java.util.Comparator\nModifiers:25\nisAccesible: false\n0\n0\n<\/pre>\n<h2><a name=\"Methods\"><\/a>10. Methods<\/h2>\n<p>In order to retrieve all visible methods for a given class we can do the following:<\/p>\n<pre class=\"brush:java\">Class&lt;String&gt; stringclass = String.class;\nMethod[] methods = stringclass.getMethods();\n<\/pre>\n<p>Using the method <code>java.lang.Class.getMethods()<\/code> all visible or accessible methods for a given class are retrieved.<br \/>\nWe can also retrieve an specific method using its name and the type of the arguments he is expecting to receive, as an example:<\/p>\n<pre class=\"brush:java\">\tMethod methodIndexOf = stringclass.getMethod( \"indexOf\", String.class );\n<\/pre>\n<p>For a given method (an instance of the type <code>java.lang.reflect.Method<\/code>), we can access all its properties. The following snippet shows a couple of them like name, default values, return type, modifiers, parameters, parameter types or the exceptions thrown, we can also check if a method is accessible or not:<\/p>\n<pre class=\"brush:java\">\/\/ All methods for the String class\nfor( Method method : methods )\n{\nSystem.out.println( \"****************************************************\" );\nSystem.out.println( \"name: \" + method.getName() );\nSystem.out.println( \"defaultValue: \" + method.getDefaultValue() );\n\nSystem.out.println( \"generic return type: \" + method.getGenericReturnType() );\nSystem.out.println( \"return type: \" + method.getReturnType() );\n\nSystem.out.println( \"modifiers: \" + method.getModifiers() );\n\n\/\/ Parameters\nParameter[] parameters = method.getParameters();\nSystem.out.println( parameters.length + \" parameters:\" );\n\/\/ also method.getParameterCount() is possible\nfor( Parameter parameter : parameters )\n{\n\tSystem.out.println( \"parameter name: \" + parameter.getName() );\n\tSystem.out.println( \"parameter type: \" + parameter.getType() );\n}\nClass&lt;?&gt;[] parameterTypes = method.getParameterTypes();\nSystem.out.println( parameterTypes.length + \" parameters:\" );\nfor( Class&lt;?&gt; parameterType : parameterTypes )\n{\n\tSystem.out.println( \"parameter type name: \" + parameterType.getName() );\n}\n\n\/\/ Exceptions\nClass&lt;?&gt;[] exceptionTypes = method.getExceptionTypes();\nSystem.out.println( exceptionTypes.length + \" exception types: \" );\nfor( Class&lt;?&gt; exceptionType : exceptionTypes )\n{\n\tSystem.out.println( \"exception name \" + exceptionType.getName() );\n}\n\nSystem.out.println( \"is accesible: \" + method.isAccessible() );\nSystem.out.println( \"is varArgs: \" + method.isVarArgs() );\n\n}\n<\/pre>\n<p>It is also possible to instantiate given methods for specific objects passing the arguments that we want, we should assure that the amount and type of the arguments is correct:<\/p>\n<pre class=\"brush:java\">\tObject indexOf = methodIndexOf.invoke( stringer, \"called\" );\n<\/pre>\n<p>This last feature is very interesting when we want to execute specific methods at runtime under special circumstances. Also in the creation of Invocation handlers for dynamic proxies is very useful, we will see this point at the end of the tutorial.<\/p>\n<h2><a name=\"Constructors\"><\/a>11. Constructors<\/h2>\n<p>Constructors can be used via reflection as well. Like other class methods they can be retrieved in runtime and several properties can be analyzed and checked like the accessibility, the number of parameters, their types, etc.<\/p>\n<p>In order to retrieve all visible constructors from a class, we can do something like:<\/p>\n<pre class=\"brush:java\">\/\/ get all visible constructors\nConstructor&lt;?&gt;[] constructors = stringGetClass.getConstructors();\n<\/pre>\n<p>In the snippet above we are retrieving all visible constructors. If we want to get all the constructors, including the private ones, we can do something like:<\/p>\n<pre class=\"brush:java\">\/\/all constructors\nConstructor&lt;?&gt;[] declaredConstructors = \tstringclass.getDeclaredConstructors();\n<\/pre>\n<p>General information about constructors such as parameters, types, names, visibility, annotations associated, etc. can be retrieved in the following way:<\/p>\n<pre class=\"brush:java\">for( Constructor&lt;?&gt; constructor : constructors )\n{\n\tint numberParams = constructor.getParameterCount() ;\n\tSystem.out.println( \"constructor \" + constructor.getName() );\n\tSystem.out.println( \"number of arguments \" + numberParams);\n\t\/\/ public, private, etc.\n\tint modifiersConstructor = constructor.getModifiers();\n\tSystem.out.println( \"modifiers \" + modifiersConstructor );\n\t\/\/ array of parameters, more info in the methods section\n\tParameter[] parameters = constructor.getParameters();\n\t\/\/ annotations array, more info in the annotations section\n\tAnnotation[] annotations = constructor.getAnnotations();\n\n}\n<\/pre>\n<p>Constructors can also be used to create new instances. This may be very useful in order to access private or not visible constructors. This should be done only under very special circumstances and depending on the system where the application is running may not work because of security reasons as explained at the beginning of this tutorial.<\/p>\n<p>In order to create a new instance of a class using a specific constructor we can do something like the following:<\/p>\n<pre class=\"brush:java\">\/\/ can be used to create new instances (no params in this case)\nString danibuizaString = (String)constructor.newInstance(  );\n<\/pre>\n<p>In has to be taken into consideration that the amount of parameters and their type should match the constructor instance ones. Also the accessibility of the constructor has to be set to true in order to invoke it (if it was not accesible). This can be done in the same way as we did for class methods and fields.<\/p>\n<h2><a name=\"Getters and Setters\"><\/a>12. Getters and Setters<\/h2>\n<p>Getters and setters are not different to any other class method inside a class. The main difference is that they are a standard way to access private fields.<\/p>\n<p>Here is a description of both:<\/p>\n<p>\u2022 Getters are used to retrieve the value of a private field inside a class. Its name starts with &#8220;get&#8221; and ends with the name of the property in camel case. They do not receive any parameter and their return type is the same than the property that they are returning. They are public.<br \/>\n\u2022 Setters are used to modify the value of a private field inside a class. Its name starts with &#8220;set&#8221; and ends with the name of the property in camel case. They receive one parameters of the same type than the property that they are modifying and they do not return any value (void). They are public.<br \/>\n[ulp id=&#8217;pPtNHTV8vXeWUAbg&#8217;]<br \/>\n&nbsp;<br \/>\nFor example, for the private property <code>private int count;<\/code> we can have the getter and setter methods:<\/p>\n<pre class=\"brush:java\">public int getCount(){\n\treturn this.count;\n}\n\npublic void setCount(int count){\n\tthis.count = count;\n}\n<\/pre>\n<p>Following these standards we can use reflection to access (read and modify) at runtime all the private properties of a class exposed via getters and setters. This mechanism is used by several known libraries like Spring Framework or Hibernate, where they expect classes to expose their properties using these kinds of methods.<\/p>\n<p>Here is an example of how to use getters and setters using reflection:<\/p>\n<pre class=\"brush:java\">Car car = new Car( \"vw touran\", \"2010\", \"12000\" );\n\nMethod[] methods = car.getClass().getDeclaredMethods();\n\n\/\/ all getters, original values\nfor( Method method : methods )\n{\n\tif( method.getName().startsWith( \"get\" ) )\n\t{\n\t\tSystem.out.println( method.invoke( car ) );\n\t}\n}\n\n\/\/ setting values\nfor( Method method : methods )\n{\n\n\tif( method.getName().startsWith( \"set\" ) )\n\t{\n\t\tmethod.invoke( car, \"destroyed\" );\n\t}\n}\n\n\/\/ get new values\nfor( Method method : methods )\n{\n\tif( method.getName().startsWith( \"get\" ) )\n\t{\n\t\tSystem.out.println( method.invoke( car ) );\n\t}\n}\n<\/pre>\n<p>Where the class <code>Car<\/code>. looks like the following:<\/p>\n<pre class=\"brush:java\">public class Car\n{\n\n\tprivate String name;\n\tprivate Object price;\n\tprivate Object year;\n\n\tpublic Car( String name, String year, String price )\n\t{\n\tthis.name = name;\n\tthis.price = price;\n\tthis.year = year;\n\t}\n\n\tpublic String getName()\n\t{\n\treturn name;\n\t}\n\n\tpublic void setName( String name )\n\t{\n\tthis.name = name;\n\t}\n\n\tpublic Object getPrice()\n\t{\n\treturn price;\n\t}\n\n\tpublic void setPrice( Object price )\n\t{\n\tthis.price = price;\n\t}\n\n\tpublic Object getYear()\n\t{\n\treturn year;\n\t}\n\n\tpublic void setYear( Object year )\n\t{\n\tthis.year = year;\n\t}\n\n}\n<\/pre>\n<p>The output will be something like:<\/p>\n<pre class=\"brush:bash\">\tvw touran\n\t2010\n\t12000\n\tdestroyed\n\tdestroyed\n\tdestroyed\n<\/pre>\n<h2><a name=\"Static elements\"><\/a>13. Static elements<\/h2>\n<p>Static classes, methods and fields behave completely different than instance ones. The main reason is that they do not need to be instantiated or created before they are invoked. They can be used without previous instantiation.<\/p>\n<p>This fact changes everything: static methods can be invoked without instantiating their container classes, static class fields are stateless (so thread safe), static elements are very useful in order to create singletons and factories&#8230; Summarizing, static elements are a very important mechanism in Java.<\/p>\n<p>In this chapter we are going to show the main differences between static and instance elements in relation to reflection: How to create static elements at runtime and how to invoke them.<\/p>\n<p>For example, for the next static inline class:<\/p>\n<pre class=\"brush:java\">\tpublic class StaticReflection\n\t{\n\n    \tstatic class StaticExample\n    \t{\n        int counter;\n    \t}\n\t...\n<\/pre>\n<p>In order to retrieve static inline classes we have the following options:<\/p>\n<pre class=\"brush:java\">\/\/ 1 access static class\nSystem.out.println( \"directly \" + StaticExample.class.getName() );\n<\/pre>\n<pre class=\"brush:java\">\/\/2 using for name directly throws an exception\nClass&lt;?&gt; forname = Class.forName(\"com.danibuiza.javacodegeeks.reflection.StaticReflection.StaticExample\" );\n<\/pre>\n<pre class=\"brush:java\">\/\/3 using $ would work but is not that nice    \nClass&lt;?&gt; forname = Class.forName(\"com.danibuiza.javacodegeeks.reflection.StaticReflection$StaticExample\" );\n<\/pre>\n<pre class=\"brush:java\">\/\/ 4 another way iterating through all classes declared inside this class\nClass&lt;?&gt;[] classes = StaticReflection.class.getDeclaredClasses();\nfor( Class&lt;?&gt; class1 : classes )\n{\n\tSystem.out.println( \"iterating through declared classes \" + class1.getName() );\n}\n<\/pre>\n<p>The main difference is that the class is contained inside another class; this has nothing to do with reflection but with the nature of inline classes.<\/p>\n<p>In order to get static methods from a class, there are no differences with the access to instance ones (this applies to fields as well):<\/p>\n<pre class=\"brush:java\">\/\/ access static methods in the same way as instance ones\nMethod mathMethod = Math.class.getDeclaredMethod( \"round\", double.class );\n<\/pre>\n<p>In order to invoke static methods or fields we do not need to create or specify an instance of the class, since the method (or the field) belongs to the class itself, not to a single instance:<\/p>\n<pre class=\"brush:java\">\/\/ methods: object instance passed can be null since method is static\nObject result = mathMethod.invoke( null, new Double( 12.4 ) );\n\n\/\/ static field access, instance can be null\nField counterField = Counter.class.getDeclaredField( \"counter\" );\nSystem.out.println( counterField.get( null ) );\n<\/pre>\n<h2><a name=\"Arrays\"><\/a>14. Arrays<\/h2>\n<p>The class <code>java.lang.reflect.Array<\/code> offers several functionalities for handling arrays; it includes various static reflective methods:<\/p>\n<p>\u2022 <code>java.lang.reflect.Array.newInstance(Class&lt;?&gt;, int)<\/code>: Creates a new instance of an array of the type passed as parameter with the length given in the second argument. Is similar to the method with the same name in the <code>java.lang.Class<\/code> class but this one contains parameters that allows the programmer to set the type of the array and its length.<br \/>\n\u2022 <code>java.lang.reflect.Array.set(Object, int, Object)<\/code>: Sets an element (passed index) of the given array with the object passed as argument.<br \/>\n\u2022 <code>java.lang.reflect.Array.getLength(Object)<\/code>: Returns the length of the array as int.<br \/>\n\u2022 <code>java.lang.reflect.Array.get(Object, int)<\/code>: Retrieves the element of the array positioned in the pased index. Returns an object.<br \/>\n\u2022 <code>java.lang.reflect.Array.getInt(Object, int)<\/code>: Similar method for the primitive type int. Returns an int. There are methods available for all primitive types.<\/p>\n<p>Here is an example of how we can use all these methods:<\/p>\n<pre class=\"brush:java\">\/\/ using the Array class it is possible to create new arrays passing the type and the length via reflection\nString[] strArrayOne = (String[])Array.newInstance( String.class, 10 );\n\n\/\/ it contains utility methods for setting values\nArray.set( strArrayOne, 0, \"member0\" );\nArray.set( strArrayOne, 1, \"member1\" );\nArray.set( strArrayOne, 9, \"member9\" );\n\n\/\/ and for getting values as well\nSystem.out.println( \"strArrayOne[0] : \" + Array.get( strArrayOne, 0 ) );\nSystem.out.println( \"strArrayOne[1] : \" + Array.get( strArrayOne, 1 ) );\nSystem.out.println( \"strArrayOne[3] (not initialized) : \" + Array.get( strArrayOne, 3 ) );\nSystem.out.println( \"strArrayOne[9] : \" + Array.get( strArrayOne, 9 ) );\n\n\/\/ also methods to retrieve the lenght of the array\nSystem.out.println( \"lenght strArrayOne: \" + Array.getLength( strArrayOne ) );\n\n\/\/ primitive types work as well\nint[] intArrayOne = (int[])Array.newInstance( int.class, 10 );\n\nArray.set( intArrayOne, 0, 1 );\nArray.set( intArrayOne, 1, 2 );\nArray.set( intArrayOne, 9, 10 );\n\n\/\/ and specific getters and setters for primitive types\nfor( int i = 0; i &lt; Array.getLength( intArrayOne ); ++i )\n{\n\tSystem.out.println( \"intArrayOne[\" + i + \"] : \" + Array.getInt( intArrayOne, i ) );\n}\n<\/pre>\n<p>The output of the program above would be:<\/p>\n<pre class=\"&quot;brush:bash\">strArrayOne[0] : member0\nstrArrayOne[1] : member1\nstrArrayOne[3] (not initialized) : null\nstrArrayOne[9] : member9\nlenght strArrayOne: 10\nintArrayOne[0] : 1\nintArrayOne[1] : 2\nintArrayOne[2] : 0\nintArrayOne[3] : 0\nintArrayOne[4] : 0\nintArrayOne[5] : 0\nintArrayOne[6] : 0\nintArrayOne[7] : 0\nintArrayOne[8] : 0\nintArrayOne[9] : 10\n<\/pre>\n<p>The class <code><\/code> contains a method that permits to check if an instance is an array or not, obviously, this method is available for all classes in Java. It is called <code>java.lang.Class.isArray()<\/code> and can be used in the following way:<\/p>\n<pre class=\"brush:java\">\/\/ retrieve the class from an instance\nClass&lt;String[]&gt; stringArrayClassUsingInstance = String[].class;\nSystem.out.println( \"stringArrayClassUsingInstance is array: \" + stringArrayClassUsingInstance.isArray() );\n\n\/\/ using class for name and passing [I\nClass&lt;?&gt; intArrayUsingClassForName = Class.forName( \"[I\" );\nSystem.out.println( \"intArrayUsingClassForName is array: \" + intArrayUsingClassForName.isArray() );\n\n\/\/ or [Ljava.lang.String\nClass&lt;?&gt; stringArrayClassUsingClassForName = Class.forName( \"[Ljava.lang.String;\" );\nSystem.out.println( \"stringArrayClassUsingClassForName is array: \"\n\t+ stringArrayClassUsingClassForName.isArray() );\n\n\/\/ this has no much sense in my opinion since we are creating an array at runtime and\n\/\/ getting the class to create a new one...\nClass&lt;? extends Object&gt; stringArrayClassUsingDoubleLoop = Array.newInstance( String.class, 0 ).getClass();\nSystem.out.println( \"stringArrayClassUsingClassForName is array: \" + stringArrayClassUsingDoubleLoop.isArray() );   \n<\/pre>\n<p>The output of the snippet above would be:<\/p>\n<pre class=\"brush:bash\">stringArrayClassUsingInstance is array: true\nintArrayUsingClassForName is array: true\nstringArrayClassUsingClassForName is array: true\nstringArrayClassUsingClassForName is array: true\n<\/pre>\n<h2><a name=\"Collections\"><\/a>15. Collections<\/h2>\n<p>Collections do not have many remarkable specific features related to reflection. Here is an example of how we can handle collection based elements. As already said, there are not many differences to any other Java type.<\/p>\n<p>The following method prints all the class names of all elements of a collection, it previously checks if the passed element is a collection instance or not:<\/p>\n<pre class=\"brush:java\">private static void reflectionCollections( Object ref )\n{\n\/\/check is collection\t\n\tif( ref instanceof Collection )\n\t{\n\t\tSystem.out.println( \"A collection:  \" + ref.getClass().getName() );\n\t\t@SuppressWarnings( \"rawtypes\" )\n\t\t\/\/ not nice\n\t\tIterator items = ( (Collection)ref ).iterator();\n\t\twhile( items != null &amp;&amp; items.hasNext() )\n\t\t{\n\t\t\tObject item = items.next();\n\t\t\tSystem.out.println( \"Element of the collection:  \" + item.getClass().getName() );\n\t\t}\n\t}\n\telse\n\t{\n\t\tSystem.out.println( \"Not a collection:  \" + ref.getClass().getName() );\n\t}\n}\n<\/pre>\n<p>In the code shown, reflection is just used to check the instance type passed as parameter and to retrieve the class names of the elements inside the collection. We can call this method in the following way using different elements (some are collection based, some not):<\/p>\n<pre class=\"brush:java\">Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();\nmap.put( \"1\", \"a\" );\nmap.put( \"2\", \"b\" );\nmap.put( \"3\", \"c\" );\nmap.put( \"4\", \"d\" );\n\nreflectionCollections( map );\nreflectionCollections( map.keySet() );\nreflectionCollections( map.values() );\n\nList&lt;String&gt; list = new ArrayList&lt;String&gt;();\nlist.add( \"10\" );\nlist.add( \"20\" );\nlist.add( \"30\" );\nlist.add( \"40\" );\n\nreflectionCollections( list );\nreflectionCollections( \"this is an string\" );\n<\/pre>\n<p>And we would get the following output:<\/p>\n<pre class=\"brush:bash\">Not a collection:  java.util.HashMap\nA collection:  java.util.HashMap$KeySet\nElement of the collection:  java.lang.String\nElement of the collection:  java.lang.String\nElement of the collection:  java.lang.String\nElement of the collection:  java.lang.String\nA collection:  java.util.HashMap$Values\nElement of the collection:  java.lang.String\nElement of the collection:  java.lang.String\nElement of the collection:  java.lang.String\nElement of the collection:  java.lang.String\nA collection:  java.util.ArrayList\nElement of the collection:  java.lang.String\nElement of the collection:  java.lang.String\nElement of the collection:  java.lang.String\nElement of the collection:  java.lang.String\nNot a collection:  java.lang.String\n<\/pre>\n<h2><a name=\"Annotations\"><\/a>16. Annotations<\/h2>\n<p>All annotations of a class, package, method, field, etc. can be retrieved using reflection. Annotations can be evaluated in runtime for each element and their values can be retrieved. The following snippet shows how to retrieve all annotations for a given class and how to print out their properties and values:<\/p>\n<pre class=\"brush:java\">Class&lt;ReflectableClass&gt; object = ReflectableClass.class;\n\/\/ Retrieve all annotations from the class\nAnnotation[] annotations = object.getAnnotations();\nfor( Annotation annotation : annotations )\n{\nSystem.out.println( annotation );\n}\n<\/pre>\n<p>The following example explains how to check if an element (field, method, class&#8230;) is marked with an specific annotation or not:<\/p>\n<pre class=\"brush:java\">\/\/ Checks if an annotation is present\nif( object.isAnnotationPresent( Reflectable.class ) )\n{\n\t\/\/ Gets the desired annotation\n\tAnnotation annotation = object.getAnnotation( Reflectable.class );\n\n\tSystem.out.println( annotation + \" present in class \" + \t\t\tobject.getClass() );\/\/ java.lang.class\n\tSystem.out.println( annotation + \" present in class \" + \tobject.getTypeName() );\/\/ \tcom.danibuiza.javacodegeeks.reflection.ReflectableClass\n\n}\n<\/pre>\n<p>These snippets may be applicable to methods, fields and all elements that can be annotated.<\/p>\n<p>You can find a very good article and extensive tutorial about Java annotations with several examples related to Java reflection in the following link: <a href=\"http:\/\/www.javacodegeeks.com\/2014\/11\/java-annotations-tutorial.html\">http:\/\/www.javacodegeeks.com\/2014\/11\/java-annotations-tutorial.html<\/a>.<\/p>\n<h2><a name=\"Generics\"><\/a>17. Generics<\/h2>\n<p>Generics were introduced in Java in the update 5 and since then are a very important feature that helps to maintain the code clean and more usable. Parameterized elements are not different than other elements in Java, so all the topics explained in this tutorial apply to these elements as well.<\/p>\n<p>Java contains specific reflection mechanisms to handle generics as well.<br \/>\n\u2022 It is possible to check at runtime if a specific element (class, method or field) is parameterized or not.<br \/>\n\u2022 It is also possible to retrieve the parameters type using reflection as well.<\/p>\n<p>Here is an example of how we can do this:<\/p>\n<pre class=\"brush:java\">Method getInternalListMethod = GenericsClass.class.getMethod( \"getInternalList\", null );\n\n\/\/ we get the return type\nType getInternalListMethodGenericReturnType = getInternalListMethod.getGenericReturnType();\n\n\/\/ we can check if the return type is parameterized (using ParameterizedType)\nif( getInternalListMethodGenericReturnType instanceof ParameterizedType )\n{\n\tParameterizedType parameterizedType = (ParameterizedType)getInternalListMethodGenericReturnType;\n\t\/\/ we get the type of the arguments for the parameterized type\n\tType[] typeArguments = parameterizedType.getActualTypeArguments();\n\tfor( Type typeArgument : typeArguments )\n\t{\n\t\t\/\/ warning not nice\n\t\t\/\/ we can work with that now\n\t\tClass typeClass = (Class)typeArgument;\n\t\tSystem.out.println( \"typeArgument = \" + typeArgument );\n\t\tSystem.out.println( \"typeClass = \" + typeClass );\n\t}\n}\n<\/pre>\n<p>In the code listed above we can see that the main class related to reflection and generics is <code>java.lang.reflect.ParameterizedType<\/code> and one of its most important methods <code>java.lang.reflect.ParameterizedType.getActualTypeArguments()<\/code>.<\/p>\n<h2><a name=\"Class Loaders\"><\/a>18. Class Loaders<\/h2>\n<p>Elements in Java are loaded using class loaders. Class loaders can be implemented by implementing the abstract class <code>https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/ClassLoader.html<\/code>. They provide functionalities to load classes using their names as parameters. A typical mechanism for loading classes is to find in the class path the file that belongs to the given name and open it converting it into a Java class. Every system (JVM) has a default one.<\/p>\n<p>In order to retrieve the systems default class loader we can do the following:<\/p>\n<pre class=\"brush:java\">ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();\n<\/pre>\n<p>A programmer can also get the specific class loader used for loading a given class or instance. If nothing else is specified or configured, the default one is used. For example:<\/p>\n<pre class=\"brush:java\">ClassLoader classClassLoader = ReflectableClass.class.getClassLoader();\n<\/pre>\n<p>Using a class loader we can load classes at runtime passing as parameter the qualified name of a class, this name can be generated dynamically:<\/p>\n<pre class=\"brush:java\">Class&lt;?&gt; reflectableClassInstanceLoaded = systemClassLoader\n                .loadClass( \"com.danibuiza.javacodegeeks.reflection.ReflectableClass\" );\n<\/pre>\n<p>Another possibility is to to this using <code>Class.forName()<\/code> method and specify the class loader that we want to use as parameter:<\/p>\n<pre class=\"brush:java\">Class&lt;?&gt; reflectableClassInstanceForName = Class\n                .forName( \"com.danibuiza.javacodegeeks.reflection.ReflectableClass\", true, systemClassLoader ); \n<\/pre>\n<p>For more information about class loaders in Java please visit the official API where you can find more information: <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lansystemClassLoaderg\/reflect\/InvocationHandler.html\">https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lansystemClassLoaderg\/reflect\/InvocationHandler.html<\/a><\/p>\n<h2><a name=\"Dynamic Proxies\"><\/a>19. Dynamic Proxies<\/h2>\n<p>Dynamic proxies are classes that implement a list of interfaces specified at runtime, that is, specified when the class is created. We may have proxy interfaces, classes and instances as for any other java element.<\/p>\n<p>Each instance of a proxy class contains an invocation handler object. An invocation handler object is an instance of a class that implements the interface <code>https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/reflect\/InvocationHandler.html<\/code>.<\/p>\n<p>When a proxy instance is used to invoke a given method, this one will be forwarded to the invoke method of the proxy instance invocation handler. The method (using reflection) and the expected arguments are passed to the invocation handler. The result of the original invocation is the returned result from the invocation handler. In order to explain that in an easier way we are going to show an example.<\/p>\n<p>For more information about java dynamic proxies please visit <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/reflect\/Proxy.html\">https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/lang\/reflect\/Proxy.html<\/a>.<br \/>\nIn this tutorial we are going to show how to create a simple proxy class and how to redirect methods through the proxy and execute the desired actions using reflection.<\/p>\n<p>The main example program would look like:<\/p>\n<pre class=\"brush:java\">\/\/ an invocation handler for our needs\nInvocationHandler myHandler = new HandlerImpl();\n\n\/\/ we can create dynamic proxy clases using the Proxy class\nInformationInterface proxy = InformationInterface)Proxy.newProxyInstance(InformationInterface.class.getClassLoader(),  new Class[] {InformationInterface.class },  myHandler);\n\n\/\/ all calls to the proxy will be passed to the handler -&gt; the handler \timplementation can be\n\/\/ decided on runtime as well\nSystem.out.println( proxy.getInfo() ); \n<\/pre>\n<p>We can see that we have 3 main topics to explain here. The first one is the invocation handler; we need to create an instance of an invocation handler and implement there the actions that we should take when methods of the proxy are called and forwarded to it.<br \/>\nA new class implementing <code>java.lang.reflect.InvocationHandler<\/code> is created and the method <code>com.danibuiza.javacodegeeks.reflection.HandlerImpl.invoke(Object, Method, Object[])<\/code> is overriden:<\/p>\n<pre class=\"brush:java\">public class HandlerImpl implements InvocationHandler\n{\n\n    @Override\n    public Object invoke( Object obj, Method method, Object[] arguments ) throws Throwable\n    {\n        System.out.println( \"using proxy \" + obj.getClass().getName() );\n        System.out.println( \"method \" + method.getName() + \" from interface \" + method.getDeclaringClass().getName() );\n\n        \/\/ we can check dynamically the interface and load the implementation that we want\n        if( method.getDeclaringClass().getName().equals( \"com.danibuiza.javacodegeeks.reflection.InformationInterface\" ) )\n        {\n            InformationClass informationImpl = InformationClass.class.newInstance();\n            return method.invoke( informationImpl, arguments );\n        }\n\n        return null;\n    }\n}\n<\/pre>\n<p>Using reflection we can take the actions that we want in the <code>invoke()<\/code> method.<\/p>\n<p>The next step is to create the proxy itself. We do this by using the invocation handler instance created before and the class (and its interface) that we want to proxy.<\/p>\n<pre class=\"brush:java\">InformationInterface proxy = (InformationInterface)Proxy.newProxyInstance( \nInformationInterface.class.getClassLoader(), new Class[] { \nInformationInterface.class }, myHandler );\n<\/pre>\n<p>Once the proxy and invocation handler are created, we can start to use them. All methods of the proxy are going to be forwarded now to the invocation handler and will be handled there.<\/p>\n<pre class=\"brush:java\">\/\/the getInfo() method will be forwarded via the invocation handler\nSystem.out.println( proxy.getInfo() );\n<\/pre>\n<p>This is a very simple example of dynamic proxies in Java but it explains how reflection can be used in this scenario.<\/p>\n<h2><a name=\"Java 8 Reflection features\"><\/a>20. Java 8 Reflection features<\/h2>\n<p>There are not many changes and enhancements in Java 8 related to reflection. The most important one is the possibility to retrieve the method parameters with their proper names. Until now it was only possible to get the parameters with the names &#8220;arg0&#8221;, &#8220;arg1&#8221;, etc. This was kind of confusing and not that clean to work with.<\/p>\n<p>Here is an example:<\/p>\n<pre class=\"brush:java\">Class&lt;String&gt; stringClass = String.class;\nfor( Method methodStringClass : stringClass.getDeclaredMethods() )\n{\n\tSystem.out.println( \"method \" + methodStringClass.getName() );\n\tfor( Parameter paramMethodStringClass : \tmethodStringClass.getParameters() )\n\t{\n\t\t\/\/ arg0, arg1, etc because the eclipse compiling tool (different \tthan javac) does\n\t\t\/\/ not support -parameters option yet\n\t\tSystem.out.println( \" parameter name \" + \tparamMethodStringClass.getName() );\n\t\tSystem.out.println( \" parameter type \" + \tparamMethodStringClass.getType() );\n\t}\n}\n<\/pre>\n<p>The mentioned method is <code>java.lang.reflect.Executable.getParameters()<\/code>.<\/p>\n<p>It has to be mentioned that in order to get this feature working you need to compile your application with the javac argument -parameters:<\/p>\n<pre class=\"brush:bash\">javac -parameters &lt;class&gt;\n<\/pre>\n<p>Or using maven you should pass this parameter in the pom.xml file:<\/p>\n<pre class=\"brush:xml\">&lt;plugin&gt;\n\t&lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;\n\t&lt;artifactId&gt;maven-compiler-plugin&lt;\/artifactId&gt;\n\t&lt;version&gt;3.1&lt;\/version&gt;\n &lt;configuration&gt;\n\t\t&lt;source&gt;1.8&lt;\/source&gt;\n\t\t&lt;target&gt;1.8&lt;\/target&gt;\n\t &lt;compilerArgument&gt;-parameters&lt;\/compilerArgument&gt;\n &lt;\/configuration&gt;\n&lt;\/plugin&gt;\n<\/pre>\n<p>For those using Eclipse, it has to be mentioned that Eclipse does not use javac as compiler. At the moment of writing this article, the Eclipse compiler did not support this feature, so you need to compile your sources yourself using maven, ant or any other tool.<\/p>\n<p>For the ones with big interest in Java 8 Lambdas here is a white paper with extensive information about the translation of java 8 Lambdas into &#8220;normal&#8221; java, reflection is involved: <a href=\" http:\/\/cr.openjdk.java.net\/~briangoetz\/lambda\/lambda-translation.html\"> http:\/\/cr.openjdk.java.net\/~briangoetz\/lambda\/lambda-translation.html<\/a>.<\/p>\n<p>Another interesting link about the use of the <code>getParameters()<\/code> in Java 8: <a href=\"https:\/\/docs.oracle.com\/javase\/tutorial\/reflect\/member\/methodparameterreflection.html\">https:\/\/docs.oracle.com\/javase\/tutorial\/reflect\/member\/methodparameterreflection.html<\/a>.<\/p>\n<h2><a name=\"Summary\"><\/a>21. Summary<\/h2>\n<p>So that&#8217;s it. In this tutorial we explained in detail what is software reflection, how it can be used in Java and under what circumstances should and should not be used.<\/p>\n<p>Reflection is a mechanism that allows programmers to analyze, modify and invoke code at runtime without previously knowing exactly what code they are executing.<\/p>\n<p>Different use cases and applications can fit for using reflection like auto completion features in IDEs, unit testing processors, dependency injection or code analysis tools. It can also be used in normal applications for specific tasks but it should be avoided if possible since the performance and the security of the code will be affected. The maintenance effort will be increased in case the source code contains a lot of reflection.<\/p>\n<p>Reflection in Java can be used in classes, methods, packages, fields, interfaces, annotations, etc. Using reflection it is possible to create new instance of any kind of object at runtime and to check its properties and values.<\/p>\n<p>Since Java 8 the method <a href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/reflect\/Executable.html#getParameters--\"><code>getParameters()<\/code><\/a> has been slightly modified and allows programmers to retrieve the actual names of method parameters, we explained this in the Java 8 related chapter.<\/p>\n<p>The intention of this tutorial is to give an introduction and a deep overview of all the possibilities that Java offers in terms of reflection. For more specific tasks that may not be covered here, please refer to the links and resources section.<\/p>\n<p>All examples shown in this tutorial can be downloaded in the download section.<\/p>\n<h2><a name=\"Download\"><\/a>22. Download<\/h2>\n<p>This was a tutorial on Java Reflection.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this tutorial here: <a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/11\/java_reflection.zip\"><strong>java_reflection<\/strong><\/a><\/div>\n<h2><a name=\"Resources\"><\/a>23. Resources<\/h2>\n<p>Apart from all links listed in the article and all the examples shown, if you would like to have more theoretical information about reflection in Java you can visit:<\/p>\n<p>\u2022 The reflection API. Oracle tutorial: <a href=\" http:\/\/docs.oracle.com\/javase\/tutorial\/reflect\/\"> http:\/\/docs.oracle.com\/javase\/tutorial\/reflect\/<\/a>.<br \/>\n\u2022 Wikipedia article about reflection: <a href=\" http:\/\/en.wikipedia.org\/wiki\/Reflection_%28computer_programming%29\"> http:\/\/en.wikipedia.org\/wiki\/Reflection_%28computer_programming%29<\/a><br \/>\n\u2022 The reflection API: <a href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/reflect\/package-summary.html\">http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/reflect\/package-summary.html<\/a><br \/>\n\u2022 Dynamic proxies: &#8220;<a href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/technotes\/guides\/reflection\/proxy.html\">http:\/\/docs.oracle.com\/javase\/8\/docs\/technotes\/guides\/reflection\/proxy.html<\/a><br \/>\n\u2022 Enhancements in Java 8: <a href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/technotes\/guides\/reflection\/enhancements.html#a8\">http:\/\/docs.oracle.com\/javase\/8\/docs\/technotes\/guides\/reflection\/enhancements.html#a8<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>EDITORIAL NOTE: In this post, we feature a comprehensive Java Reflection Tutorial. Reflection is a feature in the Java programming language. It allows an executing Java program to examine or &#8220;introspect&#8221; upon itself, and manipulate internal properties of the program. For example, it&#8217;s possible for a Java class to obtain the names of all its &hellip;<\/p>\n","protected":false},"author":601,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[557,1039],"class_list":["post-33058","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-reflection","tag-ultimate"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Reflection Tutorial - The ULTIMATE Guide (PDF Download)<\/title>\n<meta name=\"description\" content=\"Check out our Java Reflection Tutorial where we explained in detail what is software reflection! You can download our FREE Java Reflection Ultimate Guide!\" \/>\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\/2014\/11\/java-reflection-tutorial-2.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Reflection Tutorial - The ULTIMATE Guide (PDF Download)\" \/>\n<meta property=\"og:description\" content=\"Check out our Java Reflection Tutorial where we explained in detail what is software reflection! You can download our FREE Java Reflection Ultimate Guide!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2014\/11\/java-reflection-tutorial-2.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=\"2014-11-18T14:00:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-05T14:07:50+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=\"Dani Buiza\" \/>\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=\"Dani Buiza\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"22 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/11\\\/java-reflection-tutorial-2.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/11\\\/java-reflection-tutorial-2.html\"},\"author\":{\"name\":\"Dani Buiza\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/c1d18d0e5a1ad668bab93bb4124027e5\"},\"headline\":\"Java Reflection Tutorial &#8211; The ULTIMATE Guide (PDF Download)\",\"datePublished\":\"2014-11-18T14:00:23+00:00\",\"dateModified\":\"2023-12-05T14:07:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/11\\\/java-reflection-tutorial-2.html\"},\"wordCount\":4918,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/11\\\/java-reflection-tutorial-2.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"Reflection\",\"Ultimate\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/11\\\/java-reflection-tutorial-2.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/11\\\/java-reflection-tutorial-2.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/11\\\/java-reflection-tutorial-2.html\",\"name\":\"Java Reflection Tutorial - The ULTIMATE Guide (PDF Download)\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/11\\\/java-reflection-tutorial-2.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/11\\\/java-reflection-tutorial-2.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2014-11-18T14:00:23+00:00\",\"dateModified\":\"2023-12-05T14:07:50+00:00\",\"description\":\"Check out our Java Reflection Tutorial where we explained in detail what is software reflection! You can download our FREE Java Reflection Ultimate Guide!\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/11\\\/java-reflection-tutorial-2.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/11\\\/java-reflection-tutorial-2.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/11\\\/java-reflection-tutorial-2.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\\\/2014\\\/11\\\/java-reflection-tutorial-2.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\":\"Java Reflection Tutorial &#8211; The ULTIMATE Guide (PDF Download)\"}]},{\"@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\\\/c1d18d0e5a1ad668bab93bb4124027e5\",\"name\":\"Dani Buiza\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5ad57f031b639d68a6e473652c4907bb6ee1fd4b691e97dff4c14c6ea4d9188c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5ad57f031b639d68a6e473652c4907bb6ee1fd4b691e97dff4c14c6ea4d9188c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5ad57f031b639d68a6e473652c4907bb6ee1fd4b691e97dff4c14c6ea4d9188c?s=96&d=mm&r=g\",\"caption\":\"Dani Buiza\"},\"description\":\"Daniel Gutierrez Diez holds a Master in Computer Science Engineering from the University of Oviedo (Spain) and a Post Grade as Specialist in Foreign Trade from the UNED (Spain). Daniel has been working for different clients and companies in several Java projects as programmer, designer, trainer, consultant and technical lead.\",\"sameAs\":[\"http:\\\/\\\/danibuiza.github.io\\\/yo\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/dani-buiza\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Reflection Tutorial - The ULTIMATE Guide (PDF Download)","description":"Check out our Java Reflection Tutorial where we explained in detail what is software reflection! You can download our FREE Java Reflection Ultimate Guide!","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\/2014\/11\/java-reflection-tutorial-2.html","og_locale":"en_US","og_type":"article","og_title":"Java Reflection Tutorial - The ULTIMATE Guide (PDF Download)","og_description":"Check out our Java Reflection Tutorial where we explained in detail what is software reflection! You can download our FREE Java Reflection Ultimate Guide!","og_url":"https:\/\/www.javacodegeeks.com\/2014\/11\/java-reflection-tutorial-2.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2014-11-18T14:00:23+00:00","article_modified_time":"2023-12-05T14:07:50+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":"Dani Buiza","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Dani Buiza","Est. reading time":"22 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2014\/11\/java-reflection-tutorial-2.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/11\/java-reflection-tutorial-2.html"},"author":{"name":"Dani Buiza","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/c1d18d0e5a1ad668bab93bb4124027e5"},"headline":"Java Reflection Tutorial &#8211; The ULTIMATE Guide (PDF Download)","datePublished":"2014-11-18T14:00:23+00:00","dateModified":"2023-12-05T14:07:50+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/11\/java-reflection-tutorial-2.html"},"wordCount":4918,"commentCount":1,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/11\/java-reflection-tutorial-2.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["Reflection","Ultimate"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2014\/11\/java-reflection-tutorial-2.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2014\/11\/java-reflection-tutorial-2.html","url":"https:\/\/www.javacodegeeks.com\/2014\/11\/java-reflection-tutorial-2.html","name":"Java Reflection Tutorial - The ULTIMATE Guide (PDF Download)","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/11\/java-reflection-tutorial-2.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/11\/java-reflection-tutorial-2.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2014-11-18T14:00:23+00:00","dateModified":"2023-12-05T14:07:50+00:00","description":"Check out our Java Reflection Tutorial where we explained in detail what is software reflection! You can download our FREE Java Reflection Ultimate Guide!","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/11\/java-reflection-tutorial-2.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2014\/11\/java-reflection-tutorial-2.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2014\/11\/java-reflection-tutorial-2.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\/2014\/11\/java-reflection-tutorial-2.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":"Java Reflection Tutorial &#8211; The ULTIMATE Guide (PDF Download)"}]},{"@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\/c1d18d0e5a1ad668bab93bb4124027e5","name":"Dani Buiza","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5ad57f031b639d68a6e473652c4907bb6ee1fd4b691e97dff4c14c6ea4d9188c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5ad57f031b639d68a6e473652c4907bb6ee1fd4b691e97dff4c14c6ea4d9188c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5ad57f031b639d68a6e473652c4907bb6ee1fd4b691e97dff4c14c6ea4d9188c?s=96&d=mm&r=g","caption":"Dani Buiza"},"description":"Daniel Gutierrez Diez holds a Master in Computer Science Engineering from the University of Oviedo (Spain) and a Post Grade as Specialist in Foreign Trade from the UNED (Spain). Daniel has been working for different clients and companies in several Java projects as programmer, designer, trainer, consultant and technical lead.","sameAs":["http:\/\/danibuiza.github.io\/yo\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/dani-buiza"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/33058","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\/601"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=33058"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/33058\/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=33058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=33058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=33058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}