| Start |
|---|
| Introduction Basics The Reflection API allows one to check the class structure of the code at runtime and invoke code dynamically. This is very powerful, but it is also dangerous since the compiler is not able to statically determine whether dynamic invocations are valid. A simple example would be to get the public constructors and methods of a given class: With this information it is possible to instance the object and call different methods dynamically. Reflection and Generic Types Generic type information is available for:
The following example shows how to extract the generic type information in all three cases: This results in the following output: |
| Dynamic Proxies Dynamic Proxies do not really have much to do with Reflection but they are part of the API. It's basically a way to create a dynamic implementation of an interface. This could be helpful when creating mockup services. The result of this code is this: |
| Getting and Setting fields Using the Reflection API, it is possible to change or get the value of a field at runtime. For example, you could use it in an API to retrieve different fields based on a factor, like the OS. You can also remove modifiers like To do so, you will need to use the method Class#getField() (opens new window) in a way such as the one shown below: Getting fields is much easier. We can use Field#get() (opens new window) and its variants to get its value: Do note this: When using Class#getDeclaredField (opens new window), use it to get a field in the class itself: Here, |
| Misuse of Reflection API to change private and final variables Reflection is useful when it is properly used for right purpose. By using reflection, you can access private variables and re-initialize final variables. Below is the code snippet, which is not recommended. Output: Explanation: In normal scenario,
|
| Evil Java hacks with Reflection The Reflection API could be used to change values of private and final fields even in the JDK default library. This could be used to manipulate the behaviour of some well known classes as we will see. What is not possible Lets start first with the only limitation means the only field we can't change with Reflection. That is the Java But it won't be listed in the System class if we run this code Thats because of the Crazy Strings Each Java String is represented by the JVM as an instance of the Strings are supposed to be immutable, but it is possible to use "evil" reflection to change them. The example below show how we can change the characters in a String by replacing its So this code will print "you stink!" 1 = 42 The same idea could be used with the Integer Class Everything is true And according to this stackoverflow post (opens new window) we can use reflection to do something really evil. Note that what we are doing here is going to cause the JVM to behave in inexplicable ways. This is very dangerous. |
Call constructor Getting the Constructor ObjectYou can obtain Where the If you know the precise parameter types of the constructor you want to access, you can filter the specific constructor. The next example returns the public constructor of the given class which takes a If no constructor matches the given constructor arguments a New Instance using Constructor Object |
| Invoking a method Using reflection, a method of an object can be invoked during runtime. The example shows how to invoke the methods of a |
| Getting the Constants of an Enumeration Giving this enumeration as Example: In Java an enum class is like any other class but has some definied constants for the enum values. Additionally it has a field that is an array that holds all the values and two static methods with name the output will be: NORTH So we could examine enum classes with Reflection like any other class. But the Reflection API offers three enum-specific methods. enum check Returns true for classes that represents an enum type. retrieving values Returns an array of all enum values like Compass.values() but without the need of an instance. enum constant check Lists all the class fields that are enum values. |
| Get Class given its (fully qualified) name Given a It can be specified, if the class should be initialized (second parameter of |
| Call overloaded constructors using reflection Example: Invoke different constructors by passing relevant parameters output: Explanation:
|
| Call constructor of nested class If you want to create an instance of an inner nested class you need to provide a class object of the enclosing class as an extra parameter with Class#getDeclaredConstructor (opens new window). If the nested class is static you will not need this enclosing instance. RemarksPerformanceKeep in mind that reflection might decrease performance, only use it when your task cannot be completed without reflection. From the Java tutorial The Reflection API (opens new window) : Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications. |
Read Article
https://develop.horje.com/learn/1434/reference