CSC/ECE 517 Fall 2011/ch4 4f rs: Difference between revisions
Line 28: | Line 28: | ||
Common languages that exhibit reflection to varying degrees are Ruby, Java, Smalltalk, C# and more. | Common languages that exhibit reflection to varying degrees are Ruby, Java, Smalltalk, C# and more. | ||
== Types of reflection == | == Types of reflection == | ||
There are two basic types of reflection. They are: | |||
* Behavioral Reflection: This type of reflection changes the behaviour of the execution of the program based observation and modification. | |||
* Structural Reflection: This type of reflection deals with changing the very structure of the program such as the data structures and flow of control in the program. | |||
==Implementation== | ==Implementation== |
Revision as of 16:07, 20 October 2011
Introduction
Reflection is a relatively common computer programming concept where in the program has the inherant ability to examine itself at runtime. Based on its observations the program can modify it's behaviour for the given situation.The modifications can be any aspect of the programming language .i.e syntax, semantics, or implementation.
Reflection as a concept was first introduced in the doctoral dissertation of Dr. Brian Cantwell Smith in 1992.
Reflection
Reflection is the ability of a program or a computation to examine and modify itself at run time even though it does not have enough information at compile time. For example in a high level object oriented language the ability to inspect its class , interfaces and methods at run time without knowing their names at compile time. It provides the ability to modify , instantiate and access to methods such as getter's and setter's of a class. Since the program need not have all the information at compile time it makes the program more dynamic.
In the simple illustration provided below in java we can see how the name of a class can be obtained from the class.
import java.lang.reflect.*; public class Fclass{ public static void main(String[] args){ Class cls = java.lang.Integer.class; String info; info = cls.getName(); // It will show java.lang.Integer System.out.println(info); } }
Some of the common information that can be obtained from reflection are :
- Object type
- Super classes of any class
- Methods
- class fields
- Interfaces
Common languages that exhibit reflection to varying degrees are Ruby, Java, Smalltalk, C# and more.
Types of reflection
There are two basic types of reflection. They are:
- Behavioral Reflection: This type of reflection changes the behaviour of the execution of the program based observation and modification.
- Structural Reflection: This type of reflection deals with changing the very structure of the program such as the data structures and flow of control in the program.
Implementation
There are many challenges faced by designers of reflective languages. They must provide as much leeway as possible for late binging and at the same time they must optimize the compilation of the static components so that they are ready for run-time. Reflection is in essense a connection between the object-level (program) and meta-level (the processor). The ability of the object level to be able to observe and modify the meta-level information is the consequence of this connection. These actions can be termed as introspection and intercession. These actions can implemented with the implementation of the following components:
- Ability to create first class objects ie reification.
- Ability to convert a string that denotes a class or a method into its corresponding reference or invocation.
- Ability to use and create symbolic links to methods and classes.
Reflection by example
Applications of Reflection
- Reflection has become invaluable to programmers who need to connect code with data. For example in a GUI environment a button might need to invoke different methods in different classes. Reflection can be used here to call the method on any given class.
- Programmers who deal with a multitude of classes at the same time can use reflection to create a “serializer” that for a given class uses reflection to go through all the instance variables and processes them accordingly.
- Reflection is used in large test frameworks where reflection helps in identifying the test methods for different scenarios.
- Reflection provides the ability to morph the code based on dynamic situations. This provides a sense of artificial intelligence to the program as whole.
- Reflection can be used to debug and verify code as it provides access to the insides of a program.
- Reflection is very useful when the software is upgraded regularly. It provides an easy method to check for available methods and classes. This prevents the errors caused by the absense of methods and classes when they are deprecated.
Advantages and disadvantages of refletion
Advantages
- Extensibility: Reflection provides the capability of using external and user defined classes by instantiation of extensibility objects using their fully qualified names.
- Class browsers in IDEs: The ability to examine the members of classes makes implementation of visual aids , auto-completion and documentation easy in development tools for programmers.
- Debugging: Reflection allows the user to observe the private members in classes. This capability can be used to debug classes and their interactions.
- Test Harness : Reflection can be used to call a set of testing APIs defined on a class for maximum coverage of testing.
Disadvantages
- Reflection introduces lot of performance overhead when compared to non-reflective code. Hence it should be used judiciously.
- Since it provides access to the internals of a class or an encapsulated object security becomes a major issue.
- Since it is run-time binding we lose the security of compile time checks and verification.