CSC/ECE 517 Fall 2010/ch3 1c AE: Difference between revisions
Line 1: | Line 1: | ||
== Reflective Language Features vs. Reflective Packages == | == Reflective Language Features vs. Reflective Packages == | ||
=== Definition === | === Definition === | ||
Reflection is the integral quality of a program to dynamically manipulate its own state. With reflection, the program can infer and modify itself by representing its own state(implementation, structure and all aspects) that is self-accessible. Its domain is itself | |||
Programming languages that possess this quality are said to be relective. | |||
Reflection is a valuable language feature to facilitate metaprogramming. Reflection is defined as the ability of a programming language to be its own meta-language. Thus, reflection is writing programs that manipulate other programs or themselves. [5] | |||
e.g. In Java, reflection enables to discover information about the loaded classes: | |||
Fields, | |||
Methods and constructors | |||
Generics information | |||
Metadata annotations | |||
It also enables to use these metaobjects to their instances in run time environment. E.g. Method.invoke(Object o, Object… args) With the Java reflection API, you can interrogate an object to get all sorts of information about its class. | |||
Consider the following simple example: | |||
public class HelloWorld { | |||
public void printName() {� System.out.println(this.getClass().getName());� }� }� | |||
The line | |||
(new HelloWorld()).printName(); | |||
sends the string HelloWorld to standard out. Now let x be an instance of HelloWorld or one of its subclasses. The line | |||
x.printName(); | |||
sends the string naming the class to standard out. | |||
The printName method examines the object for its class (this.getClass()). In doing so, the decision of what to print is made by delegating to the object's class. The method acts on this decision by printing the returned name. Without being overridden, the printName method behaves differently for each subclass than it does for HelloWorld. The printName method is flexible; it adapts to the class that inherits it, causing the change in behavior. | |||
=== Pros and cons of reflection === | === Pros and cons of reflection === | ||
=== Reflective mechanisms === | === Reflective mechanisms === |
Revision as of 00:20, 5 October 2010
Reflective Language Features vs. Reflective Packages
Definition
Reflection is the integral quality of a program to dynamically manipulate its own state. With reflection, the program can infer and modify itself by representing its own state(implementation, structure and all aspects) that is self-accessible. Its domain is itself
Programming languages that possess this quality are said to be relective.
Reflection is a valuable language feature to facilitate metaprogramming. Reflection is defined as the ability of a programming language to be its own meta-language. Thus, reflection is writing programs that manipulate other programs or themselves. [5] e.g. In Java, reflection enables to discover information about the loaded classes: Fields, Methods and constructors Generics information Metadata annotations
It also enables to use these metaobjects to their instances in run time environment. E.g. Method.invoke(Object o, Object… args) With the Java reflection API, you can interrogate an object to get all sorts of information about its class. Consider the following simple example: public class HelloWorld {
public void printName() {� System.out.println(this.getClass().getName());� }� }�
The line (new HelloWorld()).printName(); sends the string HelloWorld to standard out. Now let x be an instance of HelloWorld or one of its subclasses. The line x.printName(); sends the string naming the class to standard out. The printName method examines the object for its class (this.getClass()). In doing so, the decision of what to print is made by delegating to the object's class. The method acts on this decision by printing the returned name. Without being overridden, the printName method behaves differently for each subclass than it does for HelloWorld. The printName method is flexible; it adapts to the class that inherits it, causing the change in behavior.
Pros and cons of reflection
Reflective mechanisms
Types of reflection like introspection, intercession, structural & behavioral -- Reflection at Different Stages of the Program Lifecycle
Reflective languages
1. Languages like LISP, CLOS, etc 2. OO-languages (java ‘s reflex package, C#’s, C++’s, etc)