INTRODUCTION
According to Wikipedia; In computer science, reflection is the ability of a computer program to examine, introspect and modify its own structure and behavior at runtime.
Thus, the Java Reflection API provides the ability to examine or modify the runtime behavior of applications running on the Java virtual machine.
Using reflection in Java, we can inspect a class and obtain information about fields, methods, constructors, implemented interfaces, superclasses at runtime. We can invoke a method at runtime. Using reflection, we can also dynamically create and access Java arrays, examine enumerations. Using reflection, we can even access a private field and invoke a private method of another class!
Where is the Reflection API used?
Although it is not necessary to use the reflection API in your application, but you may be seeing its use in many tools or applications that you are using.
1. The IDE, like Eclipse, provides the list of methods in a class, automatically filling in the field or the name of the method.
2. Its persistence structure that corresponds to the fields in its objects with the fields in the database table at run time.
3. Junit obtaining information about the methods to be invoked.
4. Spring framework obtaining the information of the class using the definition of the bean and also obtaining the setters and getters or constructor of the class. Then you can say that the injection of dependence in the spring depends heavily on reflection.
Disadvantages of reflection in Java
Reflection provides a lot of power, so it should not be used interchangeably. If it is possible to perform an operation without using reflection, it is preferable to avoid using it. Some of the disadvantages of reflection are -
1. Performance overload - As the reflection resolves types dynamically, certain optimizations of the JVM cannot be executed. Thus, the reflected operations have a slower performance than their non-reflected counterparts. This means that the use of reflection in sections of code that is frequently called in performance sensitive applications should be avoided.
2. Security Restrictions - Reflection requires a runtime permission that can not be present during execution under a security manager. Therefore, the code that must be executed in a restricted security context must avoid reflection.
3. Against object-oriented principles - Because reflection allows code to execute operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected collateral effects. Reflective code interrupts abstractions and, therefore, can change behavior with platform updates.
Java reflection API
Some of the main classes and interfaces that you will use while using reflection in Java are the following:
• Class - For each type of object, the JVM instantiates an immutable instance of java.lang.Class (Note that the class itself is not part of the Reflection API), which provides methods to examine the object's runtime properties, its members and type information. The class also provides the ability to create new classes and objects. More importantly, it is the entry point for all Reflection APIs.
• Member - Reflection defines a java.lang.reflect.Member interface that is implemented by java.lang.reflect.Field, java.lang.reflect.Method and java.lang.reflect.Constructor that can be used to obtain information about the members of the class.
• Field - The field class provides methods for accessing type information and establishing and obtaining values for a field in a given object.
• Method - The Method class provides methods to obtain the type information for the parameters and the return value. It can also be used to invoke methods on a particular object.
• Constructor - The Constructor class provides methods to obtain information about the constructors of the class. If you reflexively call a constructor, it will create a new instance of an object for a given class.
• Array - The Array class provides static methods for dynamically creating and accessing Java arrays. Reflection provides methods to access matrix types and matrix component types, create new matrices, and retrieve and define values of array components.
To getting expert-level training for Java Training in your location – java training in chennai | java training in bangalore | java training in pune | java training in chennai | java training in bangalore | java training in tambaram | java training in omr | java training in velachery | java training in annanagar | java training in chennai | java interview questions and answers | core java interview questions and answers | java training in marathahalli | java training in btm layout | java training in jayanagar | java training in chennai | java training in usa | For getting java online training | java online training
No comments:
Post a Comment