<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Sarao</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Sarao"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Sarao"/>
	<updated>2026-05-17T20:56:43Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=54320</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=54320"/>
		<updated>2011-10-30T01:13:22Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Listing all the methods unique to an object */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4f_sl#Introduction Reflection]. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby&amp;lt;ref&amp;gt;The Ruby cookbook by O'reilly publishers&amp;lt;/ref&amp;gt;. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
==Finding an Object’s class or super class==&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Listing an object’s methods==&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
==Listing all the methods unique to an object==&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Comments for the program:&lt;br /&gt;
The above program is used to list all the methods unique to the object. Here it finds out all the methods and subtracts all the Super class methods.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Getting a reference to a method==&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fixing bugs in someone else’s code==&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Responding to calls to undefined methods==&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''[http://www.ruby-doc.org/core-1.9.2/NoMethodError.html NoMethodError]'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the [http://en.wikipedia.org/wiki/Java_Virtual_Machine Java virtual machine]. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set [http://en.wikipedia.org/wiki/Application_programming_interface APIs] defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in Java==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in C++==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with [http://gcc.gnu.org/ GCC] (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Reflection drawbacks in general=&lt;br /&gt;
&lt;br /&gt;
In general, there are several drawbacks of using reflection in object-oriented programming languages: &lt;br /&gt;
&lt;br /&gt;
*Lose all the compile-check features, consequently lose in performance.&lt;br /&gt;
&lt;br /&gt;
*There are also some security restrictions to call reflection methods. &lt;br /&gt;
&lt;br /&gt;
*Lots of boilerplate codes, comes at a speed cost.&lt;br /&gt;
&lt;br /&gt;
=Summary of the Article=&lt;br /&gt;
&lt;br /&gt;
In this wiki page, we describe the reflection feature for object-oriented languages. We focus on introducing the reflection in Ruby, Java and C++ languages. Reflection provides objects that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can then invoke the type's methods or access its fields and properties. There are also some drawbacks for using reflection we metioned above. In all, reflection is a very useful tool for OOP languages.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=External links=&lt;br /&gt;
*[http://www.cs.indiana.edu/~jsobel/rop.html An Introduction to Reflection-Oriented Programming]&lt;br /&gt;
*[http://www.laputan.org/#Reflection Brian Foote's pages on Reflection in Smalltalk]&lt;br /&gt;
*[http://java.sun.com/docs/books/tutorial/reflect/index.html Java Reflection Tutorial] from Sun Microsystems&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection for computer programming]&lt;br /&gt;
&lt;br /&gt;
=Further reading=&lt;br /&gt;
* Ira R. Forman and Nate Forman, ''Java Reflection in Action'' (2005), ISBN 1-932394-18-4&lt;br /&gt;
* Ira R. Forman and Scott Danforth, ''Putting Metaclasses to Work'' (1999), ISBN  0-201-43305-2&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=54319</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=54319"/>
		<updated>2011-10-30T01:08:33Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Responding to calls to undefined methods */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4f_sl#Introduction Reflection]. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby&amp;lt;ref&amp;gt;The Ruby cookbook by O'reilly publishers&amp;lt;/ref&amp;gt;. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
==Finding an Object’s class or super class==&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Listing an object’s methods==&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
==Listing all the methods unique to an object==&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Getting a reference to a method==&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fixing bugs in someone else’s code==&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Responding to calls to undefined methods==&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''[http://www.ruby-doc.org/core-1.9.2/NoMethodError.html NoMethodError]'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the [http://en.wikipedia.org/wiki/Java_Virtual_Machine Java virtual machine]. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set [http://en.wikipedia.org/wiki/Application_programming_interface APIs] defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in Java==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in C++==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with [http://gcc.gnu.org/ GCC] (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Reflection drawbacks in general=&lt;br /&gt;
&lt;br /&gt;
In general, there are several drawbacks of using reflection in object-oriented programming languages: &lt;br /&gt;
&lt;br /&gt;
*Lose all the compile-check features, consequently lose in performance.&lt;br /&gt;
&lt;br /&gt;
*There are also some security restrictions to call reflection methods. &lt;br /&gt;
&lt;br /&gt;
*Lots of boilerplate codes, comes at a speed cost.&lt;br /&gt;
&lt;br /&gt;
=Summary of the Article=&lt;br /&gt;
&lt;br /&gt;
In this wiki page, we describe the reflection feature for object-oriented languages. We focus on introducing the reflection in Ruby, Java and C++ languages. Reflection provides objects that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can then invoke the type's methods or access its fields and properties. There are also some drawbacks for using reflection we metioned above. In all, reflection is a very useful tool for OOP languages.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=External links=&lt;br /&gt;
*[http://www.cs.indiana.edu/~jsobel/rop.html An Introduction to Reflection-Oriented Programming]&lt;br /&gt;
*[http://www.laputan.org/#Reflection Brian Foote's pages on Reflection in Smalltalk]&lt;br /&gt;
*[http://java.sun.com/docs/books/tutorial/reflect/index.html Java Reflection Tutorial] from Sun Microsystems&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection for computer programming]&lt;br /&gt;
&lt;br /&gt;
=Further reading=&lt;br /&gt;
* Ira R. Forman and Nate Forman, ''Java Reflection in Action'' (2005), ISBN 1-932394-18-4&lt;br /&gt;
* Ira R. Forman and Scott Danforth, ''Putting Metaclasses to Work'' (1999), ISBN  0-201-43305-2&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=54318</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=54318"/>
		<updated>2011-10-30T01:07:27Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch4_4f_sl#Introduction Reflection]. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby&amp;lt;ref&amp;gt;The Ruby cookbook by O'reilly publishers&amp;lt;/ref&amp;gt;. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
==Finding an Object’s class or super class==&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Listing an object’s methods==&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
==Listing all the methods unique to an object==&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Getting a reference to a method==&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fixing bugs in someone else’s code==&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Responding to calls to undefined methods==&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the [http://en.wikipedia.org/wiki/Java_Virtual_Machine Java virtual machine]. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set [http://en.wikipedia.org/wiki/Application_programming_interface APIs] defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in Java==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in C++==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with [http://gcc.gnu.org/ GCC] (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Reflection drawbacks in general=&lt;br /&gt;
&lt;br /&gt;
In general, there are several drawbacks of using reflection in object-oriented programming languages: &lt;br /&gt;
&lt;br /&gt;
*Lose all the compile-check features, consequently lose in performance.&lt;br /&gt;
&lt;br /&gt;
*There are also some security restrictions to call reflection methods. &lt;br /&gt;
&lt;br /&gt;
*Lots of boilerplate codes, comes at a speed cost.&lt;br /&gt;
&lt;br /&gt;
=Summary of the Article=&lt;br /&gt;
&lt;br /&gt;
In this wiki page, we describe the reflection feature for object-oriented languages. We focus on introducing the reflection in Ruby, Java and C++ languages. Reflection provides objects that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can then invoke the type's methods or access its fields and properties. There are also some drawbacks for using reflection we metioned above. In all, reflection is a very useful tool for OOP languages.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=External links=&lt;br /&gt;
*[http://www.cs.indiana.edu/~jsobel/rop.html An Introduction to Reflection-Oriented Programming]&lt;br /&gt;
*[http://www.laputan.org/#Reflection Brian Foote's pages on Reflection in Smalltalk]&lt;br /&gt;
*[http://java.sun.com/docs/books/tutorial/reflect/index.html Java Reflection Tutorial] from Sun Microsystems&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection for computer programming]&lt;br /&gt;
&lt;br /&gt;
=Further reading=&lt;br /&gt;
* Ira R. Forman and Nate Forman, ''Java Reflection in Action'' (2005), ISBN 1-932394-18-4&lt;br /&gt;
* Ira R. Forman and Scott Danforth, ''Putting Metaclasses to Work'' (1999), ISBN  0-201-43305-2&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=54317</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=54317"/>
		<updated>2011-10-30T01:07:00Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature [[reflection]]. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and [http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby&amp;lt;ref&amp;gt;The Ruby cookbook by O'reilly publishers&amp;lt;/ref&amp;gt;. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
==Finding an Object’s class or super class==&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Listing an object’s methods==&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
==Listing all the methods unique to an object==&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Getting a reference to a method==&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fixing bugs in someone else’s code==&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Responding to calls to undefined methods==&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the [http://en.wikipedia.org/wiki/Java_Virtual_Machine Java virtual machine]. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set [http://en.wikipedia.org/wiki/Application_programming_interface APIs] defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in Java==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in C++==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with [http://gcc.gnu.org/ GCC] (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Reflection drawbacks in general=&lt;br /&gt;
&lt;br /&gt;
In general, there are several drawbacks of using reflection in object-oriented programming languages: &lt;br /&gt;
&lt;br /&gt;
*Lose all the compile-check features, consequently lose in performance.&lt;br /&gt;
&lt;br /&gt;
*There are also some security restrictions to call reflection methods. &lt;br /&gt;
&lt;br /&gt;
*Lots of boilerplate codes, comes at a speed cost.&lt;br /&gt;
&lt;br /&gt;
=Summary of the Article=&lt;br /&gt;
&lt;br /&gt;
In this wiki page, we describe the reflection feature for object-oriented languages. We focus on introducing the reflection in Ruby, Java and C++ languages. Reflection provides objects that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can then invoke the type's methods or access its fields and properties. There are also some drawbacks for using reflection we metioned above. In all, reflection is a very useful tool for OOP languages.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=External links=&lt;br /&gt;
*[http://www.cs.indiana.edu/~jsobel/rop.html An Introduction to Reflection-Oriented Programming]&lt;br /&gt;
*[http://www.laputan.org/#Reflection Brian Foote's pages on Reflection in Smalltalk]&lt;br /&gt;
*[http://java.sun.com/docs/books/tutorial/reflect/index.html Java Reflection Tutorial] from Sun Microsystems&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection for computer programming]&lt;br /&gt;
&lt;br /&gt;
=Further reading=&lt;br /&gt;
* Ira R. Forman and Nate Forman, ''Java Reflection in Action'' (2005), ISBN 1-932394-18-4&lt;br /&gt;
* Ira R. Forman and Scott Danforth, ''Putting Metaclasses to Work'' (1999), ISBN  0-201-43305-2&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=54316</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=54316"/>
		<updated>2011-10-30T01:04:04Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature [[reflection]]. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and [[Metaprogramming]] is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby&amp;lt;ref&amp;gt;The Ruby cookbook by O'reilly publishers&amp;lt;/ref&amp;gt;. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
==Finding an Object’s class or super class==&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Listing an object’s methods==&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
==Listing all the methods unique to an object==&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Getting a reference to a method==&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fixing bugs in someone else’s code==&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Responding to calls to undefined methods==&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the [http://en.wikipedia.org/wiki/Java_Virtual_Machine Java virtual machine]. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set [http://en.wikipedia.org/wiki/Application_programming_interface APIs] defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in Java==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in C++==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with [http://gcc.gnu.org/ GCC] (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Reflection drawbacks in general=&lt;br /&gt;
&lt;br /&gt;
In general, there are several drawbacks of using reflection in object-oriented programming languages: &lt;br /&gt;
&lt;br /&gt;
*Lose all the compile-check features, consequently lose in performance.&lt;br /&gt;
&lt;br /&gt;
*There are also some security restrictions to call reflection methods. &lt;br /&gt;
&lt;br /&gt;
*Lots of boilerplate codes, comes at a speed cost.&lt;br /&gt;
&lt;br /&gt;
=Summary of the Article=&lt;br /&gt;
&lt;br /&gt;
In this wiki page, we describe the reflection feature for object-oriented languages. We focus on introducing the reflection in Ruby, Java and C++ languages. Reflection provides objects that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can then invoke the type's methods or access its fields and properties. There are also some drawbacks for using reflection we metioned above. In all, reflection is a very useful tool for OOP languages.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=External links=&lt;br /&gt;
*[http://www.cs.indiana.edu/~jsobel/rop.html An Introduction to Reflection-Oriented Programming]&lt;br /&gt;
*[http://www.laputan.org/#Reflection Brian Foote's pages on Reflection in Smalltalk]&lt;br /&gt;
*[http://java.sun.com/docs/books/tutorial/reflect/index.html Java Reflection Tutorial] from Sun Microsystems&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection for computer programming]&lt;br /&gt;
&lt;br /&gt;
=Further reading=&lt;br /&gt;
* Ira R. Forman and Nate Forman, ''Java Reflection in Action'' (2005), ISBN 1-932394-18-4&lt;br /&gt;
* Ira R. Forman and Scott Danforth, ''Putting Metaclasses to Work'' (1999), ISBN  0-201-43305-2&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=54315</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=54315"/>
		<updated>2011-10-30T01:02:23Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature [[reflection]]. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and [[Meta-programming]] is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby&amp;lt;ref&amp;gt;The Ruby cookbook by O'reilly publishers&amp;lt;/ref&amp;gt;. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
==Finding an Object’s class or super class==&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Listing an object’s methods==&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
==Listing all the methods unique to an object==&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Getting a reference to a method==&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fixing bugs in someone else’s code==&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Responding to calls to undefined methods==&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the [http://en.wikipedia.org/wiki/Java_Virtual_Machine Java virtual machine]. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set [http://en.wikipedia.org/wiki/Application_programming_interface APIs] defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in Java==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in C++==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with [http://gcc.gnu.org/ GCC] (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Reflection drawbacks in general=&lt;br /&gt;
&lt;br /&gt;
In general, there are several drawbacks of using reflection in object-oriented programming languages: &lt;br /&gt;
&lt;br /&gt;
*Lose all the compile-check features, consequently lose in performance.&lt;br /&gt;
&lt;br /&gt;
*There are also some security restrictions to call reflection methods. &lt;br /&gt;
&lt;br /&gt;
*Lots of boilerplate codes, comes at a speed cost.&lt;br /&gt;
&lt;br /&gt;
=Summary of the Article=&lt;br /&gt;
&lt;br /&gt;
In this wiki page, we describe the reflection feature for object-oriented languages. We focus on introducing the reflection in Ruby, Java and C++ languages. Reflection provides objects that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can then invoke the type's methods or access its fields and properties. There are also some drawbacks for using reflection we metioned above. In all, reflection is a very useful tool for OOP languages.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=External links=&lt;br /&gt;
*[http://www.cs.indiana.edu/~jsobel/rop.html An Introduction to Reflection-Oriented Programming]&lt;br /&gt;
*[http://www.laputan.org/#Reflection Brian Foote's pages on Reflection in Smalltalk]&lt;br /&gt;
*[http://java.sun.com/docs/books/tutorial/reflect/index.html Java Reflection Tutorial] from Sun Microsystems&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection for computer programming]&lt;br /&gt;
&lt;br /&gt;
=Further reading=&lt;br /&gt;
* Ira R. Forman and Nate Forman, ''Java Reflection in Action'' (2005), ISBN 1-932394-18-4&lt;br /&gt;
* Ira R. Forman and Scott Danforth, ''Putting Metaclasses to Work'' (1999), ISBN  0-201-43305-2&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=54314</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=54314"/>
		<updated>2011-10-30T01:01:31Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of [[dynamic languages]] such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature reflection. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and Meta-programming is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby&amp;lt;ref&amp;gt;The Ruby cookbook by O'reilly publishers&amp;lt;/ref&amp;gt;. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
==Finding an Object’s class or super class==&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Listing an object’s methods==&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
==Listing all the methods unique to an object==&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Getting a reference to a method==&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fixing bugs in someone else’s code==&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Responding to calls to undefined methods==&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the [http://en.wikipedia.org/wiki/Java_Virtual_Machine Java virtual machine]. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set [http://en.wikipedia.org/wiki/Application_programming_interface APIs] defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in Java==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in C++==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with [http://gcc.gnu.org/ GCC] (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Reflection drawbacks in general=&lt;br /&gt;
&lt;br /&gt;
In general, there are several drawbacks of using reflection in object-oriented programming languages: &lt;br /&gt;
&lt;br /&gt;
*Lose all the compile-check features, consequently lose in performance.&lt;br /&gt;
&lt;br /&gt;
*There are also some security restrictions to call reflection methods. &lt;br /&gt;
&lt;br /&gt;
*Lots of boilerplate codes, comes at a speed cost.&lt;br /&gt;
&lt;br /&gt;
=Summary of the Article=&lt;br /&gt;
&lt;br /&gt;
In this wiki page, we describe the reflection feature for object-oriented languages. We focus on introducing the reflection in Ruby, Java and C++ languages. Reflection provides objects that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can then invoke the type's methods or access its fields and properties. There are also some drawbacks for using reflection we metioned above. In all, reflection is a very useful tool for OOP languages.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=External links=&lt;br /&gt;
*[http://www.cs.indiana.edu/~jsobel/rop.html An Introduction to Reflection-Oriented Programming]&lt;br /&gt;
*[http://www.laputan.org/#Reflection Brian Foote's pages on Reflection in Smalltalk]&lt;br /&gt;
*[http://java.sun.com/docs/books/tutorial/reflect/index.html Java Reflection Tutorial] from Sun Microsystems&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection for computer programming]&lt;br /&gt;
&lt;br /&gt;
=Further reading=&lt;br /&gt;
* Ira R. Forman and Nate Forman, ''Java Reflection in Action'' (2005), ISBN 1-932394-18-4&lt;br /&gt;
* Ira R. Forman and Scott Danforth, ''Putting Metaclasses to Work'' (1999), ISBN  0-201-43305-2&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=53354</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=53354"/>
		<updated>2011-10-20T23:20:19Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature reflection. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and Meta-programming is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby&amp;lt;ref&amp;gt;The Ruby cookbook by O'reilly publishers&amp;lt;/ref&amp;gt;. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
==Finding an Object’s class or super class==&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Listing an object’s methods==&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
==Listing all the methods unique to an object==&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Getting a reference to a method==&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fixing bugs in someone else’s code==&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Responding to calls to undefined methods==&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in Java==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in C++==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Summary of the Article=&lt;br /&gt;
&lt;br /&gt;
In this wiki page, we describe the reflection feature for object-oriented languages. We focus on introducing the reflection in Ruby, Java and C++ languages. Reflection can be used for observing and/or modifying program execution at runtime. It allows inspection of classes, interfaces, fields and methods at runtime without knowing the names of the interfaces, fields, methods at compile time. It also allows instantiation of new objects and invocation of methods.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=External links=&lt;br /&gt;
*[http://www.cs.indiana.edu/~jsobel/rop.html An Introduction to Reflection-Oriented Programming]&lt;br /&gt;
*[http://www.laputan.org/#Reflection Brian Foote's pages on Reflection in Smalltalk]&lt;br /&gt;
*[http://java.sun.com/docs/books/tutorial/reflect/index.html Java Reflection Tutorial] from Sun Microsystems&lt;br /&gt;
&lt;br /&gt;
=Further reading=&lt;br /&gt;
* Ira R. Forman and Nate Forman, ''Java Reflection in Action'' (2005), ISBN 1-932394-18-4&lt;br /&gt;
* Ira R. Forman and Scott Danforth, ''Putting Metaclasses to Work'' (1999), ISBN  0-201-43305-2&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=53351</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=53351"/>
		<updated>2011-10-20T23:19:44Z</updated>

		<summary type="html">&lt;p&gt;Sarao: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature reflection. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and Meta-programming is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby&amp;lt;ref&amp;gt;The Ruby cookbook by O'reilly publishers&amp;lt;/ref&amp;gt;. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
==Finding an Object’s class or super class==&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Listing an object’s methods==&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
==Listing all the methods unique to an object==&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Getting a reference to a method==&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fixing bugs in someone else’s code==&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Responding to calls to undefined methods==&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in Java==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in C++==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
In this wiki page, we describe the reflection feature for object-oriented languages. We focus on introducing the reflection in Ruby, Java and C++ languages. Reflection can be used for observing and/or modifying program execution at runtime. It allows inspection of classes, interfaces, fields and methods at runtime without knowing the names of the interfaces, fields, methods at compile time. It also allows instantiation of new objects and invocation of methods.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=External links=&lt;br /&gt;
*[http://www.cs.indiana.edu/~jsobel/rop.html An Introduction to Reflection-Oriented Programming]&lt;br /&gt;
*[http://www.laputan.org/#Reflection Brian Foote's pages on Reflection in Smalltalk]&lt;br /&gt;
*[http://java.sun.com/docs/books/tutorial/reflect/index.html Java Reflection Tutorial] from Sun Microsystems&lt;br /&gt;
&lt;br /&gt;
=Further reading=&lt;br /&gt;
* Ira R. Forman and Nate Forman, ''Java Reflection in Action'' (2005), ISBN 1-932394-18-4&lt;br /&gt;
* Ira R. Forman and Scott Danforth, ''Putting Metaclasses to Work'' (1999), ISBN  0-201-43305-2&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=53350</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=53350"/>
		<updated>2011-10-20T23:18:39Z</updated>

		<summary type="html">&lt;p&gt;Sarao: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature reflection. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and Meta-programming is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby&amp;lt;ref&amp;gt;The Ruby cookbook by O'reilly publishers&amp;lt;/ref&amp;gt;. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
==Finding an Object’s class or super class==&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Listing an object’s methods==&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
==Listing all the methods unique to an object==&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Getting a reference to a method==&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Fixing bugs in someone else’s code==&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Responding to calls to undefined methods==&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in Java==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks in C++==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
In this wiki page, we describe the reflection feature for object-oriented languages. We focus on introducing the reflection in Ruby, Java and C++ languages. Reflection can be used for observing and/or modifying program execution at runtime. It allows inspection of classes, interfaces, fields and methods at runtime without knowing the names of the interfaces, fields, methods at compile time. It also allows instantiation of new objects and invocation of methods.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=External links=&lt;br /&gt;
*[http://www.cs.indiana.edu/~jsobel/rop.html An Introduction to Reflection-Oriented Programming]&lt;br /&gt;
*[http://www.laputan.org/#Reflection Brian Foote's pages on Reflection in Smalltalk]&lt;br /&gt;
*[http://java.sun.com/docs/books/tutorial/reflect/index.html Java Reflection Tutorial] from Sun Microsystems&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=53008</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=53008"/>
		<updated>2011-10-20T13:07:00Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature reflection. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and Meta-programming is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby&amp;lt;ref&amp;gt;The Ruby cookbook by O'reilly publishers&amp;lt;/ref&amp;gt;. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
==Finding an Object’s class or super class==&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Listing an object’s methods:==&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Listing all the methods unique to an object:==&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Getting a reference to a method:==&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Fixing bugs in someone else’s code:==&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Responding to calls to undefined methods:==&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
===General Drawbacks:===&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
==='''Limitations of using Reflection either explicitly described by programmer or extracted from debugging info:'''===&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=53006</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=53006"/>
		<updated>2011-10-20T13:03:31Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature reflection. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and Meta-programming is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
==Finding an Object’s class or super class==&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Listing an object’s methods:==&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Listing all the methods unique to an object:==&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Getting a reference to a method:==&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Fixing bugs in someone else’s code:==&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Responding to calls to undefined methods:==&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
===General Drawbacks:===&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
==='''Limitations of using Reflection either explicitly described by programmer or extracted from debugging info:'''===&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=53003</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=53003"/>
		<updated>2011-10-20T13:01:42Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature reflection. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and Meta-programming is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
=='''Finding an Object’s class or super class'''==&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Listing an object’s methods:'''==&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Listing all the methods unique to an object:'''==&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=='''Getting a reference to a method:'''==&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''Fixing bugs in someone else’s code:'''==&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=='''Responding to calls to undefined methods:'''==&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
===General Drawbacks:===&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
==='''Limitations of using Reflection either explicitly described by programmer or extracted from debugging info:'''===&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52723</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52723"/>
		<updated>2011-10-19T20:33:02Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Limitations of using Reflection either explicitly described by programmer or extracted from debugging info: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature reflection. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and Meta-programming is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2.Listing an object’s methods:'''&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''3.Listing all the methods unique to an object:'''&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4.Getting a reference to a method:'''&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5.Fixing bugs in someone else’s code:'''&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''6.Responding to calls to undefined methods:'''&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
===General Drawbacks:===&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
==='''Limitations of using Reflection either explicitly described by programmer or extracted from debugging info:'''===&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52722</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52722"/>
		<updated>2011-10-19T20:32:47Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* General Drawbacks: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature reflection. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and Meta-programming is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2.Listing an object’s methods:'''&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''3.Listing all the methods unique to an object:'''&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4.Getting a reference to a method:'''&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5.Fixing bugs in someone else’s code:'''&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''6.Responding to calls to undefined methods:'''&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
===General Drawbacks:===&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=='''Limitations of using Reflection either explicitly described by programmer or extracted from debugging info:'''==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52721</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52721"/>
		<updated>2011-10-19T20:32:34Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* General Drawbacks: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature reflection. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and Meta-programming is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2.Listing an object’s methods:'''&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''3.Listing all the methods unique to an object:'''&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4.Getting a reference to a method:'''&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5.Fixing bugs in someone else’s code:'''&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''6.Responding to calls to undefined methods:'''&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
=General Drawbacks:=&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=='''Limitations of using Reflection either explicitly described by programmer or extracted from debugging info:'''==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52720</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52720"/>
		<updated>2011-10-19T20:32:12Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Drawbacks of Reflection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature reflection. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and Meta-programming is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2.Listing an object’s methods:'''&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''3.Listing all the methods unique to an object:'''&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4.Getting a reference to a method:'''&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5.Fixing bugs in someone else’s code:'''&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''6.Responding to calls to undefined methods:'''&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
==General Drawbacks:==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=='''Limitations of using Reflection either explicitly described by programmer or extracted from debugging info:'''==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52719</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52719"/>
		<updated>2011-10-19T20:31:19Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Drawbacks of Reflection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature reflection. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and Meta-programming is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2.Listing an object’s methods:'''&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''3.Listing all the methods unique to an object:'''&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4.Getting a reference to a method:'''&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5.Fixing bugs in someone else’s code:'''&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''6.Responding to calls to undefined methods:'''&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
'''Limitations of using Reflection either explicitly described by programmer or extracted from debugging info:'''&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations to both of the techniques'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52718</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52718"/>
		<updated>2011-10-19T20:28:33Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Drawbacks of Reflection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature reflection. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and Meta-programming is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2.Listing an object’s methods:'''&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''3.Listing all the methods unique to an object:'''&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4.Getting a reference to a method:'''&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5.Fixing bugs in someone else’s code:'''&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''6.Responding to calls to undefined methods:'''&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
Because reflection involves types that are dynamically resolved, certain 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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitations'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52716</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52716"/>
		<updated>2011-10-19T20:23:14Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to examine aspects of the program from within the program itself. Java calls this feature reflection. This feature is supported by many languages. In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the use of reflection and Meta-programming is to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
For instance, sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created. One way to accomplish this is to write conditional loops and create the object. But if there are too many classes then this would become messy. This the point where reflection comes to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror. We use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
Commonly, Reflection helps us get to know about:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2.Listing an object’s methods:'''&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''3.Listing all the methods unique to an object:'''&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4.Getting a reference to a method:'''&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5.Fixing bugs in someone else’s code:'''&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''6.Responding to calls to undefined methods:'''&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52597</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52597"/>
		<updated>2011-10-19T05:20:57Z</updated>

		<summary type="html">&lt;p&gt;Sarao: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
'''Reflection in terms of Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2.Listing an object’s methods:'''&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''3.Listing all the methods unique to an object:'''&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4.Getting a reference to a method:'''&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5.Fixing bugs in someone else’s code:'''&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''6.Responding to calls to undefined methods:'''&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52594</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52594"/>
		<updated>2011-10-19T05:19:43Z</updated>

		<summary type="html">&lt;p&gt;Sarao: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
'''Reflection in terms of Ruby:'''&lt;br /&gt;
&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2.Listing an object’s methods:'''&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''3.Listing all the methods unique to an object:'''&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4.Getting a reference to a method:'''&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5.Fixing bugs in someone else’s code:'''&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''6.Responding to calls to undefined methods:'''&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52592</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52592"/>
		<updated>2011-10-19T05:18:13Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Definition of Reflection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2.Listing an object’s methods:'''&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''3.Listing all the methods unique to an object:'''&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4.Getting a reference to a method:'''&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5.Fixing bugs in someone else’s code:'''&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''6.Responding to calls to undefined methods:'''&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52591</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52591"/>
		<updated>2011-10-19T05:17:41Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2.Listing an object’s methods:'''&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''3.Listing all the methods unique to an object:'''&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4.Getting a reference to a method:'''&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''5.Fixing bugs in someone else’s code:'''&lt;br /&gt;
&lt;br /&gt;
This is used when you know which method has a bug and you know how to solve the issue, but you don’t have access to that method or don’t want to change the source file.&lt;br /&gt;
&lt;br /&gt;
In this case, we can extend the class from within our program and overwrite the buggy method with our own implementation.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
Find the buggy multiplier method :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 def double_your_pleasure(pleasure)&lt;br /&gt;
  return pleasure * 3 # FIXME: Actually triples your pleasure.&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m = Multiplier.new&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can reopen the class, alias the buggy implementation to another name and redefine it again.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Multiplier&lt;br /&gt;
 alias :double_your_pleasure_BUGGY :double_your_pleasure&lt;br /&gt;
  def double_your_pleasure(pleasure)&lt;br /&gt;
   return pleasure * 2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
m.double_your_pleasure(6) # =&amp;gt; 12&lt;br /&gt;
m.double_your_pleasure_BUGGY(6) # =&amp;gt; 18&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''6.Responding to calls to undefined methods:'''&lt;br /&gt;
&lt;br /&gt;
Rather than Ruby raising a ''NoMethodError'' when someone calls an undefined method, we want to intercept the method call and do something else with that. &lt;br /&gt;
We can do this by defining a ''method_missing'' method for our class. Whenever someone calls a function which is not defined, then method_missing is called.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class MyClass&lt;br /&gt;
  def defined_method&lt;br /&gt;
   'This method is defined.'&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def method_missing(m, *args)&lt;br /&gt;
   &amp;quot;Sorry, I don't know about any #{m} method.&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
o = MyClass.new&lt;br /&gt;
o.defined_method # =&amp;gt; &amp;quot;This method is defined.&amp;quot;&lt;br /&gt;
o.undefined_method&lt;br /&gt;
# =&amp;gt; &amp;quot;Sorry, I don't know about any undefined_method method.&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Most of the times, ''method_missing'' is implemented to delegate the implementation of methods to another class. For example, if a class implements ''method_missing'' method to catch all such not defined errors and then uses send to delegate it to another object.&lt;br /&gt;
&lt;br /&gt;
These are some of the most important usages of Reflection in Ruby.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52590</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52590"/>
		<updated>2011-10-19T05:13:11Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2.Listing an object’s methods:'''&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''3.Listing all the methods unique to an object:'''&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4.Getting a reference to a method:'''&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A method object can also be stored in a variable and passed onto other methods as arguments. This is useful to implement callbacks  and listeners. A method can also be used as a block.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
s = &amp;quot;sample string&amp;quot;&lt;br /&gt;
replacements = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;i&amp;quot;, &amp;quot;tring&amp;quot; =&amp;gt; &amp;quot;ubstitution&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
replacements.collect(&amp;amp;s.method(:gsub))&lt;br /&gt;
# =&amp;gt; [&amp;quot;simple string&amp;quot;, &amp;quot;sample substitution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52589</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52589"/>
		<updated>2011-10-19T05:12:18Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2.Listing an object’s methods:'''&lt;br /&gt;
&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''3.Listing all the methods unique to an object:'''&lt;br /&gt;
&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''4.Getting a reference to a method:'''&lt;br /&gt;
&lt;br /&gt;
The ''Object#methods'' returns an array of strings, each containing one of the methods supported by that particular object. We can pass any of those object’s ''method'' method and get a method object corresponding to that ''method'' of that object.&lt;br /&gt;
&lt;br /&gt;
Invoke the method's ''Method#call'' method, and it's just like calling the object's method directly.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
1.succ # =&amp;gt; 2&lt;br /&gt;
1.method(:succ).call # =&amp;gt; 2&lt;br /&gt;
&lt;br /&gt;
[1,2,3].method(:each).call { |x| puts x }&lt;br /&gt;
# 1&lt;br /&gt;
# 2&lt;br /&gt;
# 3&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52588</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52588"/>
		<updated>2011-10-19T05:10:09Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2.Listing an object’s methods:'''&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''3.Listing all the methods unique to an object:'''&lt;br /&gt;
When we check for all the methods available for a class, there are multiple methods which are defined in the object’s super class and mixed-in modules. But, if we need only the methods defined by that object.&lt;br /&gt;
&lt;br /&gt;
Then it can be done by subtracting the instance methods defined by the object’s super class leaving behind only the instance methods defined by the direct class.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Object&lt;br /&gt;
 def&lt;br /&gt;
  my_methods_direct&lt;br /&gt;
  my_super = self.class.superclass&lt;br /&gt;
  return my_super ? methods - my_super.instance_methods : methods&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52587</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52587"/>
		<updated>2011-10-19T05:07:19Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2.Listing an object’s methods:'''&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can also list all the public, private and protected methods of a particular object.  &lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.private_instance_methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;Array&amp;quot;, &amp;quot;Float&amp;quot;, &amp;quot;Integer&amp;quot;, &amp;quot;String&amp;quot;, &amp;quot;'&amp;quot;, &amp;quot;abort&amp;quot;, &amp;quot;at_exit&amp;quot;,&lt;br /&gt;
# &amp;quot;autoload&amp;quot;,&amp;quot;autoload?&amp;quot;, &amp;quot;binding&amp;quot;, &amp;quot;block_given?&amp;quot;, &amp;quot;callcc&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There is another powerful usage where the user can check if a particular method is supported by the object. This can be done using the ''Object.respond_to?''&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52586</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52586"/>
		<updated>2011-10-19T05:06:12Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	        # =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	        # =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''2.Listing an object’s methods:'''&lt;br /&gt;
If there is an unfamiliar object and the user wants to know the supported methods for that object, then he can use the methods call.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods&lt;br /&gt;
# =&amp;gt; [&amp;quot;name&amp;quot;, &amp;quot;private_class_method&amp;quot;, &amp;quot;object_id&amp;quot;, &amp;quot;new&amp;quot;,&lt;br /&gt;
# &amp;quot;singleton_methods&amp;quot;, &amp;quot;method_defined?&amp;quot;, &amp;quot;equal?&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output can also be sorted using the sort method.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Object.methods.sort&lt;br /&gt;
# =&amp;gt; [&amp;quot;&amp;lt;&amp;quot;, &amp;quot;&amp;lt;=&amp;quot;, &amp;quot;&amp;lt;=&amp;gt;&amp;quot;, &amp;quot;==&amp;quot;, &amp;quot;===&amp;quot;, &amp;quot;=~&amp;quot;, &amp;quot;&amp;gt;&amp;quot;, &amp;quot;&amp;gt;=&amp;quot;,&lt;br /&gt;
# &amp;quot;__id__&amp;quot;, &amp;quot;__send__&amp;quot;, &amp;quot;allocate&amp;quot;, &amp;quot;ancestors&amp;quot;, … ]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52585</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52585"/>
		<updated>2011-10-19T05:04:21Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	# =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	# =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52584</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52584"/>
		<updated>2011-10-19T05:03:52Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
String.superclass 	# =&amp;gt; Object&lt;br /&gt;
String.ancestors 		# =&amp;gt; [String, Enumerable, Comparable, Object, Kernel]&lt;br /&gt;
Array.ancestors 		# =&amp;gt; [Array, Enumerable, Object, Kernel]&lt;br /&gt;
MyArray.ancestors 	# =&amp;gt; [MyArray, Array, Enumerable, Object, Kernel]&lt;br /&gt;
Object.ancestors 		# =&amp;gt; [Object, Kernel]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52583</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52583"/>
		<updated>2011-10-19T05:03:10Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52582</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52582"/>
		<updated>2011-10-19T05:02:51Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class'''&lt;br /&gt;
  &lt;br /&gt;
 Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52581</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52581"/>
		<updated>2011-10-19T05:02:34Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class:'''&lt;br /&gt;
  &lt;br /&gt;
 Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   &lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52580</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52580"/>
		<updated>2011-10-19T05:02:12Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
'''1.Finding an Object’s class or super class:'''&lt;br /&gt;
  &lt;br /&gt;
 Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
   Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby doesn’t support multiple inheritance as the language allows mixin Modules that simulate it. The Modules included by a given Class (or another Module) are accessible from the ''Module#ancestors'' method.&lt;br /&gt;
&lt;br /&gt;
A class can have only one superclass , but it may have any number of ancestors.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52579</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52579"/>
		<updated>2011-10-19T05:01:06Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
   1.Finding an Object’s class or super class:&lt;br /&gt;
   Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
&lt;br /&gt;
   Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52578</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52578"/>
		<updated>2011-10-19T05:00:17Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
   1.Finding an Object’s class or super class:&lt;br /&gt;
   Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
&lt;br /&gt;
   Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52577</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52577"/>
		<updated>2011-10-19T04:59:25Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Reflection in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
   1.Finding an Object’s class or super class:&lt;br /&gt;
   Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
&lt;br /&gt;
   Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52576</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52576"/>
		<updated>2011-10-19T04:58:57Z</updated>

		<summary type="html">&lt;p&gt;Sarao: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
There are multiple usages of Reflection in Ruby. Some important instances of usage of Reflection in Ruby are shown below:&lt;br /&gt;
&lt;br /&gt;
   1.Finding an Object’s class or super class:&lt;br /&gt;
   Use the ''Object#class'' method to get the class of an object as a Class object. Use ''Class#superclass'' to get the parent Class of a Class object.&lt;br /&gt;
&lt;br /&gt;
   Example:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
'a string'.class 					# =&amp;gt; String&lt;br /&gt;
'a string'.class.name 					# =&amp;gt; &amp;quot;String&amp;quot;&lt;br /&gt;
'a string'.class.superclass 				# =&amp;gt; Object&lt;br /&gt;
String.superclass 					# =&amp;gt; Object&lt;br /&gt;
String.class 						# =&amp;gt; Class&lt;br /&gt;
String.class.superclass 				# =&amp;gt; Module&lt;br /&gt;
'a string'.class.new 					# =&amp;gt; &amp;quot;&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52575</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4f sl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4f_sl&amp;diff=52575"/>
		<updated>2011-10-19T04:53:49Z</updated>

		<summary type="html">&lt;p&gt;Sarao: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;CSC/ECE 517 Fall 2010/ch4 4f sl&lt;br /&gt;
----&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
One of the many advantages of dynamic languages such as Ruby is the ability to introspect—to examine aspects of the program from within the program itself. Java, for one, calls this feature reflection.&lt;br /&gt;
Sometimes you need to create an instance of class depending upon the parameter passed to a function. This parameter could be the name of the class to be created.&lt;br /&gt;
&lt;br /&gt;
One way to do this is to write conditional loops and create the object. But if there are too many classes then this would become messy. Here comes reflection to rescue.&lt;br /&gt;
&lt;br /&gt;
The word “reflection” conjures up an image of looking at oneself in the mirror—perhaps investigating the relentless spread of that bald spot on the top of one's head. That's a pretty apt analogy: we use reflection to examine parts of our programs that aren't normally visible from where we stand.&lt;br /&gt;
&lt;br /&gt;
In this deeply introspective mood, while we are contemplating our navels and burning incense (being careful not to swap the two tasks), what can we learn about our program? We might discover:&lt;br /&gt;
&lt;br /&gt;
*	what objects it contains,&lt;br /&gt;
&lt;br /&gt;
*	the current class hierarchy,&lt;br /&gt;
&lt;br /&gt;
*	the contents and behaviors of objects, and&lt;br /&gt;
&lt;br /&gt;
*	Information on methods.&lt;br /&gt;
&lt;br /&gt;
=Definition of Reflection=&lt;br /&gt;
In languages like Ruby, the classes and Objects exhibit highly dynamic behavior. Probably the most interesting and useful aspect of the Ruby programming is the use of reflection and Meta-programming to save the designer from having to write repetitive code.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Ruby=&lt;br /&gt;
Reflection allows us to treat classes and methods as objects. Some of the important uses of reflection are:&lt;br /&gt;
a.	Which methods can be called from an Object?&lt;br /&gt;
b.	Grab one of the methods of an Object and pass it on to another as a code block.&lt;br /&gt;
c.	To know the inheritance structure.&lt;br /&gt;
d.	A reference to the module includes.&lt;br /&gt;
&lt;br /&gt;
These are some of the basic and important uses. Basically Reflection allows the user to interactively examine an unfamiliar object or structure.&lt;br /&gt;
&lt;br /&gt;
=Reflection in Java=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
Reflection&amp;lt;ref&amp;gt;http://download.oracle.com/javase/tutorial/reflect/&amp;lt;/ref&amp;gt; is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique&amp;lt;ref&amp;gt;http://java.sun.com/developer/technicalArticles/ALT/Reflection/&amp;lt;/ref&amp;gt; and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
'''Extensibility Features'''&lt;br /&gt;
&lt;br /&gt;
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.&lt;br /&gt;
&lt;br /&gt;
'''Class Browsers and Visual Development Environments'''&lt;br /&gt;
&lt;br /&gt;
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.&lt;br /&gt;
&lt;br /&gt;
'''Debuggers and Test Tools'''&lt;br /&gt;
&lt;br /&gt;
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.&lt;br /&gt;
&lt;br /&gt;
'''A Simple Example'''&lt;br /&gt;
 &lt;br /&gt;
To see how reflection works, consider this simple example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   import java.lang.reflect.*;&lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
      public static void main(String args[])&lt;br /&gt;
      {&lt;br /&gt;
         try {&lt;br /&gt;
            Class c = Class.forName(args[0]);&lt;br /&gt;
            Method m[] = c.getDeclaredMethods();&lt;br /&gt;
            for (int i = 0; i &amp;lt; m.length; i++)&lt;br /&gt;
            System.out.println(m[i].toString());&lt;br /&gt;
         }&lt;br /&gt;
         catch (Throwable e) {&lt;br /&gt;
            System.err.println(e);&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For an invocation of: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  java DumpMethods java.util.Stack&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
the output is: &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  public java.lang.Object java.util.Stack.push(&lt;br /&gt;
    java.lang.Object)&lt;br /&gt;
   public synchronized &lt;br /&gt;
     java.lang.Object java.util.Stack.pop()&lt;br /&gt;
   public synchronized&lt;br /&gt;
      java.lang.Object java.util.Stack.peek()&lt;br /&gt;
   public boolean java.util.Stack.empty()&lt;br /&gt;
   public synchronized &lt;br /&gt;
     int java.util.Stack.search(java.lang.Object) &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.&lt;br /&gt;
&lt;br /&gt;
'''Performance Overhead'''&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Security Restrictions'''&lt;br /&gt;
&lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.&lt;br /&gt;
&lt;br /&gt;
'''Exposure of Internals'''&lt;br /&gt;
&lt;br /&gt;
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.&lt;br /&gt;
&lt;br /&gt;
=Reflection in C++=&lt;br /&gt;
&lt;br /&gt;
==Use of Reflection==&lt;br /&gt;
&lt;br /&gt;
There exist a lot of applications where reflection comes in as a useful technique. So, quite a number of people think that some mechanisms supporting reflection should be added to the next version of C++. Here we present several different aspects of reflection to reflection in C++&amp;lt;ref&amp;gt;http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1751.html#APPROACHES&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
'''Compile-Time Reflection'''&lt;br /&gt;
&lt;br /&gt;
Compile-time reflection generally provides mechanisms at the source code level. These mechanisms provide information that can be directly derived from the source code, i.e. information about types and their definitions, variables and executable code like expressions or control structures. &lt;br /&gt;
&lt;br /&gt;
This information can then be used to inject or transform code, to instantiate templates or to generate external data: from simple metrics information to full-fledged representations of the source code entities. This external data together with some injected code can then be used to provide such information at runtime. &lt;br /&gt;
&lt;br /&gt;
'''Runtime Reflection'''&lt;br /&gt;
&lt;br /&gt;
Runtime reflection provides mechanisms during program execution. Information available generally includes what objects currently exist of a specific class or which variables have which values. But it also comprises typical debugging information like stack frames or even contents of processor registers. &lt;br /&gt;
&lt;br /&gt;
The manipulation mechanisms include changing values, calling functions or creation of new instances of a type, but also modification of existing functions or classes or addition of new ones. Related to debugging, runtime reflection also provides notification mechanisms for events like calling or leaving a function or creation or deletion of an object. &lt;br /&gt;
&lt;br /&gt;
'''For example:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;quot;reflect.h&amp;quot;&lt;br /&gt;
#include &amp;quot;typedecl.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
class A { &lt;br /&gt;
  public:&lt;br /&gt;
    int    i;&lt;br /&gt;
    char*  pc;&lt;br /&gt;
    double d;&lt;br /&gt;
  protected:&lt;br /&gt;
    long   larr[10];&lt;br /&gt;
    A**    ppa;    &lt;br /&gt;
    A*     pa;&lt;br /&gt;
  public:&lt;br /&gt;
    RTTI_DESCRIBE_STRUCT((RTTI_FIELD(i, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_PTR(pc, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_FIELD(d, RTTI_FLD_PUBLIC),&lt;br /&gt;
                          RTTI_ARRAY(larr, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR_TO_PTR(ppa, RTTI_FLD_PROTECTED),&lt;br /&gt;
                          RTTI_PTR(pa, RTTI_FLD_PROTECTED)));&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
RTTI_REGISTER_STRUCT(A, 0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Drawbacks of Reflection==&lt;br /&gt;
&lt;br /&gt;
In both models of using reflection (explicitly described by programmer and extracted from debugging information) there are some limitations&amp;lt;ref&amp;gt;http://www.garret.ru/cppreflection/docs/reflect.html&amp;lt;/ref&amp;gt;. Below we enumerate them grouping in three part - common for proposed reflection API and specific for each approach. &lt;br /&gt;
&lt;br /&gt;
'''Common limitation'''&lt;br /&gt;
&lt;br /&gt;
*It is possible to create instance of the object only using default constructor. &lt;br /&gt;
&lt;br /&gt;
*API doesn't support enums, unions and bit fields. &lt;br /&gt;
&lt;br /&gt;
*API provides type only information only about classes and structures used in the application - no information about global or static functions and variables is available. &lt;br /&gt;
&lt;br /&gt;
'''Limitation of extracting information from descriptors written by programmer'''&lt;br /&gt;
&lt;br /&gt;
*Only restricted set of field types is supported (scalars, classes, structures, array and pointers of those types, pointer to pointers to those types). &lt;br /&gt;
&lt;br /&gt;
*Methods can not have more than 5 parameters &lt;br /&gt;
&lt;br /&gt;
*It is not possible to describe static methods and fields. &lt;br /&gt;
&lt;br /&gt;
*Each class should have public default constructor or have no constructors at all. &lt;br /&gt;
&lt;br /&gt;
'''Limitations of extracting types from debug information'''&lt;br /&gt;
&lt;br /&gt;
*This approach works only under Unix and most probably only with GCC (because format of mangling symbols is specific to each compiler) &lt;br /&gt;
&lt;br /&gt;
*It is possible to invoke method only at platforms where parameters are passed through the stack (for example Intel). Unfortunately there is no portable way of invoking arbitrary method in C++. &lt;br /&gt;
&lt;br /&gt;
*There is no way to distinguish classes defined in application from system classes which are also extracted from debug information. &lt;br /&gt;
&lt;br /&gt;
*Virtual methods can be invoked only statically (i.e. if you have class A with virtual method foo and class B derived from class A which overrides foo method, get descriptor of A, find method foo in it and invoke this method, then A::foo will be called) &lt;br /&gt;
&lt;br /&gt;
*No field qualifiers (such as const, volatile,...) are currently extracted.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50999</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1f rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50999"/>
		<updated>2011-09-26T00:14:07Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Sample Workflow involving Clearcase */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=='''Comparison of Version Control Systems : The Programmer View'''==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Version control or Revision Control is indispensable in large projects. They make sure that projects are not spinning out of control by letting different programmers work on the project from a different perspective without getting in each other’s way and without doing any damage that cannot be undone.&lt;br /&gt;
&lt;br /&gt;
The article compares all the Version Control Systems like RCS, CVS and Distributed Version Control Systems.It extends the comparison to popular version control applications like RCS, SVN, CVS, Clearcase, Mercurial and Git.&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Systems==&lt;br /&gt;
[[Image:RCS.png|thumb|150px|alt=Local Version Control]]&lt;br /&gt;
==== Local Version Control ====&lt;br /&gt;
This is a primitive approach of version control. The system operates on single files only.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Revision_Control_System&amp;lt;/ref&amp;gt; It has no way of working with an entire project. Changes to a file are stored as 'deltas' with the aid of a diff utility. Since the approach maintain many copies of files, it is generally cumbersome with reduced efficiency.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:CVS.png|thumb|150px|alt=Centralized Version Control]]&lt;br /&gt;
&lt;br /&gt;
==== Centralized Version Control ====&lt;br /&gt;
CVS implements a Client-Server model to achieve version control. A central repository is maintained which is used by all the developers of a project. Programmers can only have a working copy of the project. The model allows a developer to checkout only the required files or branches. In this model all operations on the code (commit, exchange of code etc.)  involve communication with the central server via a network. This means that these operations are not available when the user is offline. Also  issues in communication with the server may disrupt the development activity.&lt;br /&gt;
Any changes to the project on the server involves an authentication process. Hence only users with certain privileges can commit code on the central server . A developer can checkpoint or create branches on his work only by committing his changes to the central repository.  Since all the developers commit changes on the same repository, it is generally challenging to resolve conflicts when merging or reconciling changes from other branches.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Distributed Version Control ====&lt;br /&gt;
[[Image:File-DVCS.png|thumb|150px|alt=Distributed Version Control]]&lt;br /&gt;
This system implements a peer to peer model to achieve version control. No reference copy of the codebase exist by default. However in practice, an official reference copy is indeed maintained. The model  requires the developers to possess a full blown backup of the repository , including the entire history. As a result, the developers can collaborate directly without the need of a central authority. Hence it allows developers to be productive even when offline. (e.g. when travelling). Also the overhead of communicating with a central repository is overcome in this approach.&lt;br /&gt;
Branching and merging fit much better in this approach as each user has his own branch. The administrator's major work is to merge appropriate changes from branches of different users. This is relatively simpler compared to  the centralized system. As a result, the distributed system scales much better with the number of people.&lt;br /&gt;
&lt;br /&gt;
==== Summary of Centralized versus Distributed Approach ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Centralized Version Control&lt;br /&gt;
! Distributed Version Control&lt;br /&gt;
|-&lt;br /&gt;
| Client - server model&lt;br /&gt;
| peer- to -peer model&lt;br /&gt;
|-&lt;br /&gt;
| Single central repository.&lt;br /&gt;
| Multiple repositories&lt;br /&gt;
|-&lt;br /&gt;
| Changes can be committed only Online&lt;br /&gt;
| Changes can be committed offline&lt;br /&gt;
|-&lt;br /&gt;
| Allows developer to checkout required files/branches.&lt;br /&gt;
| Complete repository has to be downloaded by the programmer &lt;br /&gt;
|-&lt;br /&gt;
| Merging and Branching operations are complicated as&lt;br /&gt;
multiple programmers work on the same repository at the same time.&lt;br /&gt;
| Branching is simple as each programmer gets a branch&lt;br /&gt;
|-&lt;br /&gt;
| Repository maintenance becomes more challenging with number of people&lt;br /&gt;
| Scales better with number of people&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Applications ==&lt;br /&gt;
==== Overview of Version Control Applications ====&lt;br /&gt;
&lt;br /&gt;
=====  RCS: The Revision Control System (RCS)=====&lt;br /&gt;
&lt;br /&gt;
RCS manages multiple revisions of files. RCS operates on a set of files. RCS automates the process of storing, retrieval, logging, identification, and merging of multiple revisions. It is useful for text that is modified frequently which includes source code, programs, documentation, graphics, papers, and letters. This is the most basic and most primitive Version Control System known to programmers.&lt;br /&gt;
&lt;br /&gt;
More information about RCS can be found at [http://www.gnu.org/s/rcs/ RCS]&lt;br /&gt;
&lt;br /&gt;
===== ClearCase Version Management System =====&lt;br /&gt;
&lt;br /&gt;
ClearCase is a comprehensive software configuration management system. It is used to manage multiple versions of evolving software systems, tracks which versions were used in software builds, used to perform builds of individual programs or complete releases according to user-defined version and system specifications.&lt;br /&gt;
&lt;br /&gt;
These capabilities enable ClearCase to address the issues and critical requirements of organizations that develop software:&lt;br /&gt;
&lt;br /&gt;
Effective development — ClearCase enables users to work efficiently, allowing them to fine-tune the balance between sharing each other's work and isolating themselves from destabilizing changes done. ClearCase automatically manages the sharing of both source files and the files produced by software builds.&lt;br /&gt;
&lt;br /&gt;
Effective management — ClearCase tracks the software build process, so that users can determine what was built, and how it was built. Further, ClearCase can instantly recreate the source base from which a software system was built, allowing it to be rebuilt, debugged, and updated — all without interfering with other programming work.&lt;br /&gt;
&lt;br /&gt;
Enforcement of development policies — ClearCase enables project administrators to define development policies and procedures, and to automate their enforcement.&lt;br /&gt;
&lt;br /&gt;
More information about ClearCase can be found at [http://www-01.ibm.com/software/awdtools/clearcase/ ClearCase]&lt;br /&gt;
&lt;br /&gt;
=====  CVS: The Concurrent Versions System (CVS)=====&lt;br /&gt;
&lt;br /&gt;
CVS is a Client-Server software revision control system in the field of software development. Version control software keeps track of all the changes in a set of files, and makes sure that several developers (potentially widely separated in space and/or time) can collaborate in real time. The CVS server runs on Unix-like systems with client software that runs on different operating systems on multiple client machines. It is regarded as the most mature version control system because it has been developed for such a long time and has stood the test of time. A fork project of CVS, CVSNT was created to run CVS on Windows servers, and it is currently being actively developed to increase functionality and make it more usable on Windows platform.&lt;br /&gt;
&lt;br /&gt;
More information about CVS can be found at [http://www.nongnu.org/cvs/ CVS]&lt;br /&gt;
&lt;br /&gt;
=====  SVN: Sub Version (SVN)=====&lt;br /&gt;
&lt;br /&gt;
SVN was created as an alternative to CVS that would be useful to fix some known issues in CVS and also maintaining high compatibility with it. SVN is free and open source with the difference of being distributed under the Apache license as opposed to GNU which distributes licenses for CVS. To prevent the database from being corrupted, SVN adopts a technique called 'Atomic Operations'. Either all of the modifications made to the source are committed or none are committed. Partial changes will not enter the original source. Many developers have switched to SVN as it is a newer technology that starts with the best features of CVS and improves upon them. While branch operations in CVS are expensive and do not really lend themselves to long-term forks in the project, SVN is designed to allow for it. Hence it lends itself better to large forked projects with many directions. Some known disadvantages of SVN includes slower operation speeds and the lack of distributed revision control. Distributed revision control uses a peer-to-peer model as compared to using a centralized server to store code modifications. Peer-to-peer model work better for open source projects which are spread over multiple far away physical locations. The downside to a dedicated server approach is the issue of not having redundancy in the case when the server is down leading to no access to clients.&lt;br /&gt;
&lt;br /&gt;
More information about SVN can be found at [http://subversion.apache.org/ SVN]&lt;br /&gt;
&lt;br /&gt;
=====  Git:=====&lt;br /&gt;
&lt;br /&gt;
Git takes a totally diverse approach that differs greatly in comparison to CVS and SVN. The original concepts for Git were to make a faster, distributed version control system that would openly invalidate conventions and practices used in CVS. It is primarily developed for Linux and has the highest speeds on there. It will also run on other Unix-like systems, and Windows agents are known as msysgit. As there is no centralized server, Git is not very suitable for single developer projects or small teams as the code may not necessarily be available when using a normal computer. Workarounds exist for this problem. Developers look out for Git’s improved speed as a decent trade off for the hassle.&lt;br /&gt;
&lt;br /&gt;
More information about Git can be found at [http://git-scm.com/ Git]&lt;br /&gt;
&lt;br /&gt;
=====  Mercurial:===== &lt;br /&gt;
&lt;br /&gt;
Mercurial is a distributed revision control tool which began close to the same time as Git. Mercurial was originally made to compete with Git for Linux kernel development. Mercurial is primarily implemented in Python as opposed to C, but there are some instances where C is used. This is a major difference compared to other version control systems in Developer's point of view. Users feel that Mercurial is similar to SVN with respect to certain features, as well as being a distributed system. This acts as an advantage for the tool as the learning curve for those already familiar with SVN will be less steep. The documentation for Mercurial is very well compiled and facilitates understanding the differences faster. One of the major drawback to Mercurial is that it does not allow for two parents to be merged. Unlike Git, it uses an extension system rather than using scripts. That may be ideal for some section of programmers, but many find the speed of operation of Git to be a feature they do not want to trade off for anything.&lt;br /&gt;
&lt;br /&gt;
More information about Mercurial can be found at [http://mercurial.selenic.com Mercurial]&lt;br /&gt;
&lt;br /&gt;
==== Basic Features ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Repository model|Repository model&lt;br /&gt;
! #Concurrency model|Concurrency model&lt;br /&gt;
! #Programming Languages|Programming Languages&lt;br /&gt;
! #Scope of Change|Scope of Change&lt;br /&gt;
! #Atomic commits|Atomic commits&lt;br /&gt;
! #File Renames|File Renames&lt;br /&gt;
! #Interactive Commits|Interactive Commits&lt;br /&gt;
! #Partial Checkout/clone|Partial Checkout/clone&lt;br /&gt;
! Platforms supported&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Set of files	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Clear Case	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C, Java, Perl &lt;br /&gt;
|  File &lt;br /&gt;
|  Partial&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Linux, Windows, AIX, Solaris, HP UX, i5/OS, OS/390, z/OS&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C,Perl &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Partial &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| POSIX, Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  Python, C&lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X	&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Feature&lt;br /&gt;
! Feature Description&lt;br /&gt;
|-&lt;br /&gt;
! Repository Model&lt;br /&gt;
| Describes the relationship between various copies of the source code repository.&lt;br /&gt;
|-&lt;br /&gt;
! Concurrency Model&lt;br /&gt;
| Describes how changes to the working copy are managed to prevent simultaneous edits from causing nonsensical data in the repository.&lt;br /&gt;
|-&lt;br /&gt;
! Programming Languages&lt;br /&gt;
| The coding language in which the application is being developed&lt;br /&gt;
|-&lt;br /&gt;
! Scope of Change&lt;br /&gt;
| Describes whether changes are recorded for individual files or for entire directory trees.&lt;br /&gt;
|-&lt;br /&gt;
! Atomic commits&lt;br /&gt;
| Refers to a guarantee that all changes made are merged, or that no change at all will be made.&lt;br /&gt;
|-&lt;br /&gt;
! File Renames&lt;br /&gt;
| Describes whether a system allows files to be renamed while retaining their version history.&lt;br /&gt;
|-&lt;br /&gt;
! Interactive Commits&lt;br /&gt;
| Interactive commits allow the user to cherrypick the patch-hunks that become part of a commit, instead of having only a file-level granularity.&lt;br /&gt;
|-&lt;br /&gt;
! Partial Checkout/clone&lt;br /&gt;
| Ability to check out or clone only a specified subdirectory from a repository.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Basic Commands ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Clone|Clone&lt;br /&gt;
! #Pull|Pull&lt;br /&gt;
! #Push|Push&lt;br /&gt;
! #Checkout|Checkout&lt;br /&gt;
! #Add|Add&lt;br /&gt;
! #Remove|Remove&lt;br /&gt;
! #Merge|Merge&lt;br /&gt;
! #Commit|Commit&lt;br /&gt;
! #Revert|Revert&lt;br /&gt;
! #Rebase|Rebase&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  co &lt;br /&gt;
| Not Supported&lt;br /&gt;
| rcsclean&lt;br /&gt;
| rcsmerge&lt;br /&gt;
|  ci&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| update -j&lt;br /&gt;
|  commit&lt;br /&gt;
| rm, update&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| svnadmin hotcopy	&lt;br /&gt;
| svnadmin load&lt;br /&gt;
| svnadmin dump&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
|  commit&lt;br /&gt;
| revert&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! Clearcase	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| mkelem&lt;br /&gt;
| rmelem&lt;br /&gt;
| merge&lt;br /&gt;
| checkin&lt;br /&gt;
| unco/rmver&lt;br /&gt;
| findmerge&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Fetch&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| checkout&lt;br /&gt;
| rebase&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Pull&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| revert&lt;br /&gt;
| rebase&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Command Description&lt;br /&gt;
|-&lt;br /&gt;
! Clone&lt;br /&gt;
| Create an identical instance of a repository&lt;br /&gt;
|-&lt;br /&gt;
! Pull&lt;br /&gt;
| Download revisions from a remote repository to a local repository&lt;br /&gt;
|-&lt;br /&gt;
! Push&lt;br /&gt;
| Upload revisions from a local repository to a remote repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Create a local working copy from a (remote) repository&lt;br /&gt;
|-&lt;br /&gt;
! Add an element&lt;br /&gt;
| Mark specified files to be added to repository at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Remove an element&lt;br /&gt;
| Mark specified files to be removed at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Merge&lt;br /&gt;
| Apply the differences between two sources to a working copy path&lt;br /&gt;
|-&lt;br /&gt;
! Commit&lt;br /&gt;
| Record changes in the repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Restore working copy file from repository&lt;br /&gt;
|-&lt;br /&gt;
! Rebase&lt;br /&gt;
| Forward-port local commits to the updated upstream head&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Sample Workflow involving Clearcase ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
This section provides an example of a sample workflow used by a programmer to maintain the code in Clearcase.&amp;lt;ref&amp;gt;http://www.ibm.com/developerworks/rational/library/836.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command to create a new view: cleartool mkview -tag atag  storage-location &lt;br /&gt;
&lt;br /&gt;
Command to create a new file: cleartool mkelem &amp;lt;file_name&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command to create a new branch: cleartool mkbranch -nc &amp;lt;version_selector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command to checkout a file for editing: cleartool co -nc &amp;lt;file_name&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command to check in a file after editing: cleartool ci -nc &amp;lt;file_name&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command to check the current working view: ct pwv&lt;br /&gt;
&lt;br /&gt;
Command to view/edit the current config spec for the view: ct catcs /edcs&lt;br /&gt;
&lt;br /&gt;
Command to merge and deliver the code changes: cleartool merge -to target-path -insert contributor-version-selector &amp;lt;name_version&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command to delete a file: cleartool rmelem &amp;lt;file_name&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command to delete a view: cleartool rmview &amp;lt;view_name&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1a_br Information about Version Control Systems]&lt;br /&gt;
&lt;br /&gt;
* [http://excess.org/article/2008/07/ogre-git-tutorial/ A Video presentation on &amp;quot;Git, The Basics&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
* [http://www.youtube.com/watch?v=4XpnKHJAok8 Linus Torvals on Git]&lt;br /&gt;
&lt;br /&gt;
* [http://changelog.complete.org/archives/698-if-version-control-systems-were-airlines If version control systems were airlines]&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [http://code.google.com/p/support/wiki/DVCSAnalysis Comparison between Git and Mercurial] by Google (summer 2008)&lt;br /&gt;
* [http://www.softeng.rl.ac.uk/media/uploads/publications/2010/03/cvs-svn.pdf Comparison of CVS and Subversion]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Comparison_of_revision_control_software Revision Control Systems Comparison]&lt;br /&gt;
* [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_wflow_cc_proj.htm IBM Software Information Center]&lt;br /&gt;
* [http://schacon.github.com/git/gittutorial.html Git Tutorial]&lt;br /&gt;
* [http://www.szakmeister.net/blog/2011/feb/17/choosing-new-version-control-system/ Choosing a Version Control System]&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50995</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1f rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50995"/>
		<updated>2011-09-26T00:12:15Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Sample Workflow involving Clearcase */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=='''Comparison of Version Control Systems : The Programmer View'''==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Choice of Version Control Systems ==&lt;br /&gt;
Version control also called Sub-Version Control or Revision Control is indispensable in large projects. They make sure that projects are not spinning out of control by letting different programmers work on the project from a different perspective without getting in each other’s way and without doing any damage that cannot be undone.&lt;br /&gt;
There are a number of solutions which are available for programmers.Some of the most popular version control systems are RCS, SVN, CVS, Mercurial and Git. This article intends to compare these version control systems in a programmer’s viewpoint.The main difference between Version Control Systems is whether they are Client-Server based or peer-to-peer based.  Either they have a centralized repository where code is checked out, edited and checked in with changes, or a setup where the code is frequently updated from different peer sources, a more decentralized network to keep your code updated with changes.&lt;br /&gt;
The article further compares all the Version Control Systems like RCS, CVS and Distributed Version Control Systems. Also, the article compares multiple Version Control applications like Mercurial, Git, Clearcase and CVS. Comparison is done in the view of programmer. Various commands used in different applications are discussed.&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Systems==&lt;br /&gt;
[[Image:RCS.png|thumb|150px|alt=Local Version Control]]&lt;br /&gt;
==== Local Version Control ====&lt;br /&gt;
This is a primitive approach of version control. The system operates on single files only.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Revision_Control_System&amp;lt;/ref&amp;gt; It has no way of working with an entire project. Changes to a file are stored as 'deltas' with the aid of a diff utility. Since the approach maintain many copies of files, it is generally cumbersome with reduced efficiency.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:CVS.png|thumb|150px|alt=Centralized Version Control]]&lt;br /&gt;
&lt;br /&gt;
==== Centralized Version Control ====&lt;br /&gt;
CVS implements a Client-Server model to achieve version control. A central repository is maintained which is used by all the developers of a project. Programmers can only have a working copy of the project. The model allows a developer to checkout only the required files or branches. In this model all operations on the code (commit, exchange of code etc.)  involve communication with the central server via a network. This means that these operations are not available when the user is offline. Also  issues in communication with the server may disrupt the development activity.&lt;br /&gt;
Any changes to the project on the server involves an authentication process. Hence only users with certain privileges can commit code on the central server . A developer can checkpoint or create branches on his work only by committing his changes to the central repository.  Since all the developers commit changes on the same repository, it is generally challenging to resolve conflicts when merging or reconciling changes from other branches.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Distributed Version Control ====&lt;br /&gt;
[[Image:File-DVCS.png|thumb|150px|alt=Distributed Version Control]]&lt;br /&gt;
This system implements a peer to peer model to achieve version control. No reference copy of the codebase exist by default. However in practice, an official reference copy is indeed maintained. The model  requires the developers to possess a full blown backup of the repository , including the entire history. As a result, the developers can collaborate directly without the need of a central authority. Hence it allows developers to be productive even when offline. (e.g. when travelling). Also the overhead of communicating with a central repository is overcome in this approach.&lt;br /&gt;
Branching and merging fit much better in this approach as each user has his own branch. The administrator's major work is to merge appropriate changes from branches of different users. This is relatively simpler compared to  the centralized system. As a result, the distributed system scales much better with the number of people.&lt;br /&gt;
&lt;br /&gt;
==== Summary of Centralized versus Distributed Approach ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Centralized Version Control&lt;br /&gt;
! Distributed Version Control&lt;br /&gt;
|-&lt;br /&gt;
| Client - server model&lt;br /&gt;
| peer- to -peer model&lt;br /&gt;
|-&lt;br /&gt;
| Single central repository.&lt;br /&gt;
| Multiple repositories&lt;br /&gt;
|-&lt;br /&gt;
| Changes can be committed only Online&lt;br /&gt;
| Changes can be committed offline&lt;br /&gt;
|-&lt;br /&gt;
| Allows developer to checkout required files/branches.&lt;br /&gt;
| Complete repository has to be downloaded by the programmer &lt;br /&gt;
|-&lt;br /&gt;
| Merging and Branching operations are complicated as&lt;br /&gt;
multiple programmers work on the same repository at the same time.&lt;br /&gt;
| Branching is simple as each programmer gets a branch&lt;br /&gt;
|-&lt;br /&gt;
| Repository maintenance becomes more challenging with number of people&lt;br /&gt;
| Scales better with number of people&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Applications ==&lt;br /&gt;
==== Overview of Version Control Applications ====&lt;br /&gt;
&lt;br /&gt;
=====  RCS: The Revision Control System (RCS)=====&lt;br /&gt;
&lt;br /&gt;
RCS manages multiple revisions of files. RCS operates on a set of files. RCS automates the process of storing, retrieval, logging, identification, and merging of multiple revisions. It is useful for text that is modified frequently which includes source code, programs, documentation, graphics, papers, and letters. This is the most basic and most primitive Version Control System known to programmers.&lt;br /&gt;
&lt;br /&gt;
More information about RCS can be found at [http://www.gnu.org/s/rcs/ RCS]&lt;br /&gt;
&lt;br /&gt;
===== ClearCase Version Management System =====&lt;br /&gt;
&lt;br /&gt;
ClearCase is a comprehensive software configuration management system. It is used to manage multiple versions of evolving software systems, tracks which versions were used in software builds, used to perform builds of individual programs or complete releases according to user-defined version and system specifications.&lt;br /&gt;
&lt;br /&gt;
These capabilities enable ClearCase to address the issues and critical requirements of organizations that develop software:&lt;br /&gt;
&lt;br /&gt;
Effective development — ClearCase enables users to work efficiently, allowing them to fine-tune the balance between sharing each other's work and isolating themselves from destabilizing changes done. ClearCase automatically manages the sharing of both source files and the files produced by software builds.&lt;br /&gt;
&lt;br /&gt;
Effective management — ClearCase tracks the software build process, so that users can determine what was built, and how it was built. Further, ClearCase can instantly recreate the source base from which a software system was built, allowing it to be rebuilt, debugged, and updated — all without interfering with other programming work.&lt;br /&gt;
&lt;br /&gt;
Enforcement of development policies — ClearCase enables project administrators to define development policies and procedures, and to automate their enforcement.&lt;br /&gt;
&lt;br /&gt;
More information about ClearCase can be found at [http://www-01.ibm.com/software/awdtools/clearcase/ ClearCase]&lt;br /&gt;
&lt;br /&gt;
=====  CVS: The Concurrent Versions System (CVS)=====&lt;br /&gt;
&lt;br /&gt;
CVS is a Client-Server software revision control system in the field of software development. Version control software keeps track of all the changes in a set of files, and makes sure that several developers (potentially widely separated in space and/or time) can collaborate in real time. The CVS server runs on Unix-like systems with client software that runs on different operating systems on multiple client machines. It is regarded as the most mature version control system because it has been developed for such a long time and has stood the test of time. A fork project of CVS, CVSNT was created to run CVS on Windows servers, and it is currently being actively developed to increase functionality and make it more usable on Windows platform.&lt;br /&gt;
&lt;br /&gt;
More information about CVS can be found at [http://www.nongnu.org/cvs/ CVS]&lt;br /&gt;
&lt;br /&gt;
=====  SVN: Sub Version (SVN)=====&lt;br /&gt;
&lt;br /&gt;
SVN was created as an alternative to CVS that would be useful to fix some known issues in CVS and also maintaining high compatibility with it. SVN is free and open source with the difference of being distributed under the Apache license as opposed to GNU which distributes licenses for CVS. To prevent the database from being corrupted, SVN adopts a technique called 'Atomic Operations'. Either all of the modifications made to the source are committed or none are committed. Partial changes will not enter the original source. Many developers have switched to SVN as it is a newer technology that starts with the best features of CVS and improves upon them. While branch operations in CVS are expensive and do not really lend themselves to long-term forks in the project, SVN is designed to allow for it. Hence it lends itself better to large forked projects with many directions. Some known disadvantages of SVN includes slower operation speeds and the lack of distributed revision control. Distributed revision control uses a peer-to-peer model as compared to using a centralized server to store code modifications. Peer-to-peer model work better for open source projects which are spread over multiple far away physical locations. The downside to a dedicated server approach is the issue of not having redundancy in the case when the server is down leading to no access to clients.&lt;br /&gt;
&lt;br /&gt;
More information about SVN can be found at [http://subversion.apache.org/ SVN]&lt;br /&gt;
&lt;br /&gt;
=====  Git:=====&lt;br /&gt;
&lt;br /&gt;
Git takes a totally diverse approach that differs greatly in comparison to CVS and SVN. The original concepts for Git were to make a faster, distributed version control system that would openly invalidate conventions and practices used in CVS. It is primarily developed for Linux and has the highest speeds on there. It will also run on other Unix-like systems, and Windows agents are known as msysgit. As there is no centralized server, Git is not very suitable for single developer projects or small teams as the code may not necessarily be available when using a normal computer. Workarounds exist for this problem. Developers look out for Git’s improved speed as a decent trade off for the hassle.&lt;br /&gt;
&lt;br /&gt;
More information about Git can be found at [http://git-scm.com/ Git]&lt;br /&gt;
&lt;br /&gt;
=====  Mercurial:===== &lt;br /&gt;
&lt;br /&gt;
Mercurial is a distributed revision control tool which began close to the same time as Git. Mercurial was originally made to compete with Git for Linux kernel development. Mercurial is primarily implemented in Python as opposed to C, but there are some instances where C is used. This is a major difference compared to other version control systems in Developer's point of view. Users feel that Mercurial is similar to SVN with respect to certain features, as well as being a distributed system. This acts as an advantage for the tool as the learning curve for those already familiar with SVN will be less steep. The documentation for Mercurial is very well compiled and facilitates understanding the differences faster. One of the major drawback to Mercurial is that it does not allow for two parents to be merged. Unlike Git, it uses an extension system rather than using scripts. That may be ideal for some section of programmers, but many find the speed of operation of Git to be a feature they do not want to trade off for anything.&lt;br /&gt;
&lt;br /&gt;
More information about Mercurial can be found at [http://mercurial.selenic.com Mercurial]&lt;br /&gt;
&lt;br /&gt;
==== Basic Features ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Repository model|Repository model&lt;br /&gt;
! #Concurrency model|Concurrency model&lt;br /&gt;
! #Programming Languages|Programming Languages&lt;br /&gt;
! #Scope of Change|Scope of Change&lt;br /&gt;
! #Atomic commits|Atomic commits&lt;br /&gt;
! #File Renames|File Renames&lt;br /&gt;
! #Interactive Commits|Interactive Commits&lt;br /&gt;
! #Partial Checkout/clone|Partial Checkout/clone&lt;br /&gt;
! Platforms supported&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Set of files	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Clear Case	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C, Java, Perl &lt;br /&gt;
|  File &lt;br /&gt;
|  Partial&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Linux, Windows, AIX, Solaris, HP UX, i5/OS, OS/390, z/OS&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C,Perl &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Partial &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| POSIX, Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  Python, C&lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X	&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Feature&lt;br /&gt;
! Feature Description&lt;br /&gt;
|-&lt;br /&gt;
! Repository Model&lt;br /&gt;
| Describes the relationship between various copies of the source code repository.&lt;br /&gt;
|-&lt;br /&gt;
! Concurrency Model&lt;br /&gt;
| Describes how changes to the working copy are managed to prevent simultaneous edits from causing nonsensical data in the repository.&lt;br /&gt;
|-&lt;br /&gt;
! Programming Languages&lt;br /&gt;
| The coding language in which the application is being developed&lt;br /&gt;
|-&lt;br /&gt;
! Scope of Change&lt;br /&gt;
| Describes whether changes are recorded for individual files or for entire directory trees.&lt;br /&gt;
|-&lt;br /&gt;
! Atomic commits&lt;br /&gt;
| Refers to a guarantee that all changes made are merged, or that no change at all will be made.&lt;br /&gt;
|-&lt;br /&gt;
! File Renames&lt;br /&gt;
| Describes whether a system allows files to be renamed while retaining their version history.&lt;br /&gt;
|-&lt;br /&gt;
! Interactive Commits&lt;br /&gt;
| Interactive commits allow the user to cherrypick the patch-hunks that become part of a commit, instead of having only a file-level granularity.&lt;br /&gt;
|-&lt;br /&gt;
! Partial Checkout/clone&lt;br /&gt;
| Ability to check out or clone only a specified subdirectory from a repository.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Basic Commands ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Clone|Clone&lt;br /&gt;
! #Pull|Pull&lt;br /&gt;
! #Push|Push&lt;br /&gt;
! #Checkout|Checkout&lt;br /&gt;
! #Add|Add&lt;br /&gt;
! #Remove|Remove&lt;br /&gt;
! #Merge|Merge&lt;br /&gt;
! #Commit|Commit&lt;br /&gt;
! #Revert|Revert&lt;br /&gt;
! #Rebase|Rebase&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  co &lt;br /&gt;
| Not Supported&lt;br /&gt;
| rcsclean&lt;br /&gt;
| rcsmerge&lt;br /&gt;
|  ci&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| update -j&lt;br /&gt;
|  commit&lt;br /&gt;
| rm, update&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| svnadmin hotcopy	&lt;br /&gt;
| svnadmin load&lt;br /&gt;
| svnadmin dump&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
|  commit&lt;br /&gt;
| revert&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! Clearcase	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| mkelem&lt;br /&gt;
| rmelem&lt;br /&gt;
| merge&lt;br /&gt;
| checkin&lt;br /&gt;
| unco/rmver&lt;br /&gt;
| findmerge&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Fetch&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| checkout&lt;br /&gt;
| rebase&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Pull&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| revert&lt;br /&gt;
| rebase&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Command Description&lt;br /&gt;
|-&lt;br /&gt;
! Clone&lt;br /&gt;
| Create an identical instance of a repository&lt;br /&gt;
|-&lt;br /&gt;
! Pull&lt;br /&gt;
| Download revisions from a remote repository to a local repository&lt;br /&gt;
|-&lt;br /&gt;
! Push&lt;br /&gt;
| Upload revisions from a local repository to a remote repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Create a local working copy from a (remote) repository&lt;br /&gt;
|-&lt;br /&gt;
! Add an element&lt;br /&gt;
| Mark specified files to be added to repository at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Remove an element&lt;br /&gt;
| Mark specified files to be removed at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Merge&lt;br /&gt;
| Apply the differences between two sources to a working copy path&lt;br /&gt;
|-&lt;br /&gt;
! Commit&lt;br /&gt;
| Record changes in the repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Restore working copy file from repository&lt;br /&gt;
|-&lt;br /&gt;
! Rebase&lt;br /&gt;
| Forward-port local commits to the updated upstream head&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Sample Workflow involving Clearcase ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
This section provides an example of a sample workflow used by a programmer to maintain the code in Clearcase.&lt;br /&gt;
&lt;br /&gt;
Command to create a new view: cleartool mkview -tag atag  storage-location &lt;br /&gt;
&lt;br /&gt;
Command to create a new file: cleartool mkelem &amp;lt;file_name&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command to create a new branch: cleartool mkbranch -nc &amp;lt;version_selector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command to checkout a file for editing: cleartool co -nc &amp;lt;file_name&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command to check in a file after editing: cleartool ci -nc &amp;lt;file_name&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command to check the current working view: ct pwv&lt;br /&gt;
&lt;br /&gt;
Command to view/edit the current config spec for the view: ct catcs /edcs&lt;br /&gt;
&lt;br /&gt;
Command to merge and deliver the code changes: cleartool merge -to target-path -insert contributor-version-selector &amp;lt;name_version&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command to delete a file: cleartool rmelem &amp;lt;file_name&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Command to delete a view: cleartool rmview &amp;lt;view_name&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1a_br Information about Version Control Systems]&lt;br /&gt;
&lt;br /&gt;
* [http://excess.org/article/2008/07/ogre-git-tutorial/ A Video presentation on &amp;quot;Git, The Basics&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
* [http://www.youtube.com/watch?v=4XpnKHJAok8 Linus Torvals on Git]&lt;br /&gt;
&lt;br /&gt;
* [http://changelog.complete.org/archives/698-if-version-control-systems-were-airlines If version control systems were airlines]&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [http://code.google.com/p/support/wiki/DVCSAnalysis Comparison between Git and Mercurial] by Google (summer 2008)&lt;br /&gt;
* [http://www.softeng.rl.ac.uk/media/uploads/publications/2010/03/cvs-svn.pdf Comparison of CVS and Subversion]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Comparison_of_revision_control_software Revision Control Systems Comparison]&lt;br /&gt;
* [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_wflow_cc_proj.htm IBM Software Information Center]&lt;br /&gt;
* [http://schacon.github.com/git/gittutorial.html Git Tutorial]&lt;br /&gt;
* [http://www.szakmeister.net/blog/2011/feb/17/choosing-new-version-control-system/ Choosing a Version Control System]&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50963</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1f rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50963"/>
		<updated>2011-09-25T23:39:20Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* ClearCase Version Management System */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=='''Comparison of Version Control Systems : The Programmer View'''==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Version control also called Sub-Version Control or Revision Control is indispensable in large projects. They make sure that projects are not spinning out of control by letting different programmers work on the project from a different perspective without getting in each other’s way and without doing any damage that cannot be undone.&lt;br /&gt;
&lt;br /&gt;
Some of the most popular version control systems are RCS, SVN, CVS, Mercurial and Git. This article intends to compare these version control systems in a programmer’s viewpoint.&lt;br /&gt;
&lt;br /&gt;
== Choice of Version Control Systems ==&lt;br /&gt;
There are a number of solutions which are available for programmers. We have put together a definitive feature comparison for reference on deciding about the best choice for a project.&lt;br /&gt;
&lt;br /&gt;
The main difference between Version Control Systems is whether they are Client-Server based or peer-to-peer based.  Either they have a centralized repository where code is checked out, edited and checked in with changes, or a setup where the code is frequently updated from different peer sources, a more decentralized network to keep your code updated with changes.&lt;br /&gt;
&lt;br /&gt;
The article further compares all the Version Control Systems like RCS, CVS and Distributed Version Control Systems. Also, the article compares multiple Version Control applications like Mercurial, Git, Clearcase and CVS. Comparison is done in the view of programmer. Various commands used in different applications are discussed.&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Systems==&lt;br /&gt;
[[Image:RCS.png|thumb|150px|alt=Local Version Control]]&lt;br /&gt;
==== Local Version Control ====&lt;br /&gt;
This is a primitive approach of version control. The system operates on single files only.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Revision_Control_System&amp;lt;/ref&amp;gt; It has no way of working with an entire project. Changes to a file are stored as 'deltas' with the aid of a diff utility. Since the approach maintain many copies of files, it is generally cumbersome with reduced efficiency.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:CVS.png|thumb|150px|alt=Centralized Version Control]]&lt;br /&gt;
&lt;br /&gt;
==== Centralized Version Control ====&lt;br /&gt;
CVS implements a Client-Server model to achieve version control. A central repository is maintained which is used by all the developers of a project. Programmers can only have a working copy of the project. The model allows a developer to checkout only the required files or branches. In this model all operations on the code (commit, exchange of code etc.)  involve communication with the central server via a network. This means that these operations are not available when the user is offline. Also  issues in communication with the server may disrupt the development activity.&lt;br /&gt;
Any changes to the project on the server involves an authentication process. Hence only users with certain privileges can commit code on the central server . A developer can checkpoint or create branches on his work only by committing his changes to the central repository.  Since all the developers commit changes on the same repository, it is generally challenging to resolve conflicts when merging or reconciling changes from other branches.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Distributed Version Control ====&lt;br /&gt;
[[Image:File-DVCS.png|thumb|150px|alt=Distributed Version Control]]&lt;br /&gt;
This system implements a peer to peer model to achieve version control. No reference copy of the codebase exist by default. However in practice, an official reference copy is indeed maintained. The model  requires the developers to possess a full blown backup of the repository , including the entire history. As a result, the developers can collaborate directly without the need of a central authority. Hence it allows developers to be productive even when offline. (e.g. when travelling). Also the overhead of communicating with a central repository is overcome in this approach.&lt;br /&gt;
Branching and merging fit much better in this approach as each user has his own branch. The administrator's major work is to merge appropriate changes from branches of different users. This is relatively simpler compared to  the centralized system. As a result, the distributed system scales much better with the number of people.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Summary of Centralized versus Distributed Approach ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Centralized Version Control&lt;br /&gt;
! Distributed Version Control&lt;br /&gt;
|-&lt;br /&gt;
| Client - server model&lt;br /&gt;
| peer- to -peer model&lt;br /&gt;
|-&lt;br /&gt;
| Single central repository.&lt;br /&gt;
| Multiple repositories&lt;br /&gt;
|-&lt;br /&gt;
| Changes can be committed only Online&lt;br /&gt;
| Changes can be committed offline&lt;br /&gt;
|-&lt;br /&gt;
| Allows developer to checkout required files/branches.&lt;br /&gt;
| Complete repository has to be downloaded by the programmer &lt;br /&gt;
|-&lt;br /&gt;
| Merging and Branching operations are complicated as&lt;br /&gt;
multiple programmers work on the same repository at the same time.&lt;br /&gt;
| Branching is simple as each programmer gets a branch&lt;br /&gt;
|-&lt;br /&gt;
| Repository maintenance becomes more challenging with number of people&lt;br /&gt;
| Scales better with number of people&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Applications ==&lt;br /&gt;
==== Overview of Version Control Applications ====&lt;br /&gt;
&lt;br /&gt;
=====  RCS: The Revision Control System (RCS)=====&lt;br /&gt;
&lt;br /&gt;
RCS manages multiple revisions of files. RCS operates on a set of files. RCS automates the process of storing, retrieval, logging, identification, and merging of multiple revisions. It is useful for text that is modified frequently which includes source code, programs, documentation, graphics, papers, and letters. This is the most basic and most primitive Version Control System known to programmers.&lt;br /&gt;
&lt;br /&gt;
More information about RCS can be found at [http://www.gnu.org/s/rcs/ RCS]&lt;br /&gt;
&lt;br /&gt;
===== ClearCase Version Management System =====&lt;br /&gt;
&lt;br /&gt;
ClearCase is a comprehensive software configuration management system. It is used to manage multiple versions of evolving software systems, tracks which versions were used in software builds, used to perform builds of individual programs or complete releases according to user-defined version and system specifications.&lt;br /&gt;
&lt;br /&gt;
These capabilities enable ClearCase to address the issues and critical requirements of organizations that develop software:&lt;br /&gt;
&lt;br /&gt;
Effective development — ClearCase enables users to work efficiently, allowing them to fine-tune the balance between sharing each other's work and isolating themselves from destabilizing changes done. ClearCase automatically manages the sharing of both source files and the files produced by software builds.&lt;br /&gt;
&lt;br /&gt;
Effective management — ClearCase tracks the software build process, so that users can determine what was built, and how it was built. Further, ClearCase can instantly recreate the source base from which a software system was built, allowing it to be rebuilt, debugged, and updated — all without interfering with other programming work.&lt;br /&gt;
&lt;br /&gt;
Enforcement of development policies — ClearCase enables project administrators to define development policies and procedures, and to automate their enforcement.&lt;br /&gt;
&lt;br /&gt;
More information about ClearCase can be found at [http://www-01.ibm.com/software/awdtools/clearcase/ ClearCase]&lt;br /&gt;
&lt;br /&gt;
=====  CVS: The Concurrent Versions System (CVS)=====&lt;br /&gt;
&lt;br /&gt;
CVS is a Client-Server software revision control system in the field of software development. Version control software keeps track of all the changes in a set of files, and makes sure that several developers (potentially widely separated in space and/or time) can collaborate in real time. The CVS server runs on Unix-like systems with client software that runs on different operating systems on multiple client machines. It is regarded as the most mature version control system because it has been developed for such a long time and has stood the test of time. A fork project of CVS, CVSNT was created to run CVS on Windows servers, and it is currently being actively developed to increase functionality and make it more usable on Windows platform.&lt;br /&gt;
&lt;br /&gt;
More information about CVS can be found at [http://www.nongnu.org/cvs/ CVS]&lt;br /&gt;
&lt;br /&gt;
=====  SVN: Sub Version (SVN)=====&lt;br /&gt;
&lt;br /&gt;
SVN was created as an alternative to CVS that would be useful to fix some known issues in CVS and also maintaining high compatibility with it. SVN is free and open source with the difference of being distributed under the Apache license as opposed to GNU which distributes licenses for CVS. To prevent the database from being corrupted, SVN adopts a technique called 'Atomic Operations'. Either all of the modifications made to the source are committed or none are committed. Partial changes will not enter the original source. Many developers have switched to SVN as it is a newer technology that starts with the best features of CVS and improves upon them. While branch operations in CVS are expensive and do not really lend themselves to long-term forks in the project, SVN is designed to allow for it. Hence it lends itself better to large forked projects with many directions. Some known disadvantages of SVN includes slower operation speeds and the lack of distributed revision control. Distributed revision control uses a peer-to-peer model as compared to using a centralized server to store code modifications. Peer-to-peer model work better for open source projects which are spread over multiple far away physical locations. The downside to a dedicated server approach is the issue of not having redundancy in the case when the server is down leading to no access to clients.&lt;br /&gt;
&lt;br /&gt;
More information about SVN can be found at [http://subversion.apache.org/ SVN]&lt;br /&gt;
&lt;br /&gt;
=====  Git:=====&lt;br /&gt;
&lt;br /&gt;
Git takes a totally diverse approach that differs greatly in comparison to CVS and SVN. The original concepts for Git were to make a faster, distributed version control system that would openly invalidate conventions and practices used in CVS. It is primarily developed for Linux and has the highest speeds on there. It will also run on other Unix-like systems, and Windows agents are known as msysgit. As there is no centralized server, Git is not very suitable for single developer projects or small teams as the code may not necessarily be available when using a normal computer. Workarounds exist for this problem. Developers look out for Git’s improved speed as a decent trade off for the hassle.&lt;br /&gt;
&lt;br /&gt;
More information about Git can be found at [http://git-scm.com/ Git]&lt;br /&gt;
&lt;br /&gt;
=====  Mercurial:===== &lt;br /&gt;
&lt;br /&gt;
Mercurial is a distributed revision control tool which began close to the same time as Git. Mercurial was originally made to compete with Git for Linux kernel development. Mercurial is primarily implemented in Python as opposed to C, but there are some instances where C is used. This is a major difference compared to other version control systems in Developer's point of view. Users feel that Mercurial is similar to SVN with respect to certain features, as well as being a distributed system. This acts as an advantage for the tool as the learning curve for those already familiar with SVN will be less steep. The documentation for Mercurial is very well compiled and facilitates understanding the differences faster. One of the major drawback to Mercurial is that it does not allow for two parents to be merged. Unlike Git, it uses an extension system rather than using scripts. That may be ideal for some section of programmers, but many find the speed of operation of Git to be a feature they do not want to trade off for anything.&lt;br /&gt;
&lt;br /&gt;
More information about Mercurial can be found at [http://mercurial.selenic.com Mercurial]&lt;br /&gt;
&lt;br /&gt;
==== Basic Features ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Repository model|Repository model&lt;br /&gt;
! #Concurrency model|Concurrency model&lt;br /&gt;
! #Programming Languages|Programming Languages&lt;br /&gt;
! #Scope of Change|Scope of Change&lt;br /&gt;
! #Atomic commits|Atomic commits&lt;br /&gt;
! #File Renames|File Renames&lt;br /&gt;
! #Interactive Commits|Interactive Commits&lt;br /&gt;
! #Partial Checkout/clone|Partial Checkout/clone&lt;br /&gt;
! Platforms supported&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Set of files	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Clear Case	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C, Java, Perl &lt;br /&gt;
|  File &lt;br /&gt;
|  Partial&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Linux, Windows, AIX, Solaris, HP UX, i5/OS, OS/390, z/OS&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C,Perl &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Partial &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| POSIX, Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  Python, C&lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X	&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Feature&lt;br /&gt;
! Feature Description&lt;br /&gt;
|-&lt;br /&gt;
! Repository Model&lt;br /&gt;
| Describes the relationship between various copies of the source code repository.&lt;br /&gt;
|-&lt;br /&gt;
! Concurrency Model&lt;br /&gt;
| Describes how changes to the working copy are managed to prevent simultaneous edits from causing nonsensical data in the repository.&lt;br /&gt;
|-&lt;br /&gt;
! Programming Languages&lt;br /&gt;
| The coding language in which the application is being developed&lt;br /&gt;
|-&lt;br /&gt;
! Scope of Change&lt;br /&gt;
| Describes whether changes are recorded for individual files or for entire directory trees.&lt;br /&gt;
|-&lt;br /&gt;
! Atomic commits&lt;br /&gt;
| Refers to a guarantee that all changes made are merged, or that no change at all will be made.&lt;br /&gt;
|-&lt;br /&gt;
! File Renames&lt;br /&gt;
| Describes whether a system allows files to be renamed while retaining their version history.&lt;br /&gt;
|-&lt;br /&gt;
! Interactive Commits&lt;br /&gt;
| Interactive commits allow the user to cherrypick the patch-hunks that become part of a commit, instead of having only a file-level granularity.&lt;br /&gt;
|-&lt;br /&gt;
! Partial Checkout/clone&lt;br /&gt;
| Ability to check out or clone only a specified subdirectory from a repository.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Basic Commands ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Clone|Clone&lt;br /&gt;
! #Pull|Pull&lt;br /&gt;
! #Push|Push&lt;br /&gt;
! #Checkout|Checkout&lt;br /&gt;
! #Add|Add&lt;br /&gt;
! #Remove|Remove&lt;br /&gt;
! #Merge|Merge&lt;br /&gt;
! #Commit|Commit&lt;br /&gt;
! #Revert|Revert&lt;br /&gt;
! #Rebase|Rebase&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  co &lt;br /&gt;
| Not Supported&lt;br /&gt;
| rcsclean&lt;br /&gt;
| rcsmerge&lt;br /&gt;
|  ci&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| update -j&lt;br /&gt;
|  commit&lt;br /&gt;
| rm, update&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| svnadmin hotcopy	&lt;br /&gt;
| svnadmin load&lt;br /&gt;
| svnadmin dump&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
|  commit&lt;br /&gt;
| revert&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! Clearcase	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| mkelem&lt;br /&gt;
| rmelem&lt;br /&gt;
| merge&lt;br /&gt;
| checkin&lt;br /&gt;
| unco/rmver&lt;br /&gt;
| findmerge&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Fetch&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| checkout&lt;br /&gt;
| rebase&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Pull&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| revert&lt;br /&gt;
| rebase&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Command Description&lt;br /&gt;
|-&lt;br /&gt;
! Clone&lt;br /&gt;
| Create an identical instance of a repository&lt;br /&gt;
|-&lt;br /&gt;
! Pull&lt;br /&gt;
| Download revisions from a remote repository to a local repository&lt;br /&gt;
|-&lt;br /&gt;
! Push&lt;br /&gt;
| Upload revisions from a local repository to a remote repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Create a local working copy from a (remote) repository&lt;br /&gt;
|-&lt;br /&gt;
! Add an element&lt;br /&gt;
| Mark specified files to be added to repository at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Remove an element&lt;br /&gt;
| Mark specified files to be removed at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Merge&lt;br /&gt;
| Apply the differences between two sources to a working copy path&lt;br /&gt;
|-&lt;br /&gt;
! Commit&lt;br /&gt;
| Record changes in the repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Restore working copy file from repository&lt;br /&gt;
|-&lt;br /&gt;
! Rebase&lt;br /&gt;
| Forward-port local commits to the updated upstream head&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Sample Workflow involving Clearcase ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
This section provides an example of a sample workflow used by a programmer to maintain the code in Clearcase.&lt;br /&gt;
&lt;br /&gt;
'''Joining a UCM Project''': After project managers have established the UCM project environment, developers can then set up their work areas.&lt;br /&gt;
This is accomplished by joining the UCM project. More information about this can be found   [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_join_ucm_proj.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Working with views''': A view is the mechanism Rational ClearCase uses to provide access to specific versions of elements or specific parts of code under source control. Rules are the basis to determine which version of each element is visible and accessible through the view for a developer. More information about views can be found [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_views_intro.htm here]&lt;br /&gt;
&lt;br /&gt;
Sample command to create a new view:''' ct mkview -tag &amp;lt;view_name&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Sample command to create a new file:'''ct mkelem &amp;lt;file_name&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
'''Checking in files and checking out files''': Checking files in and out are two basic operations you will perform on a regular basis by a developer. Whenever the developer needs to edit a file, he has to checkout the file. And after completion of coding, he needs to check in the file. More information about the process can be found  [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_file_ci_co.htm here]&lt;br /&gt;
&lt;br /&gt;
Sample command to check out a file: '''ct co -nc &amp;lt;file_name&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Sample command to check in a file:''' ct ci -nc &amp;lt;file_name&amp;gt;'''  (when you are working in the same branch where the file is checked out).&lt;br /&gt;
&lt;br /&gt;
'''Delivering your work''': After testing your work in your private workspace, and you are sure that your work is error-free, you are ready to submit your work to the integration stream so that other project members can use the latest version of your work. More information about the procedure of delivering code can be found  [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_deliver_wrk.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Merging your work''': After completion, the development branch must be merged to integration branch where software developed by different teams are merged and tested for seamless operation. More information about this procedure can be obtained [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_merge_wrk.htm here]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1a_br Information about Version Control Systems]&lt;br /&gt;
&lt;br /&gt;
* [http://excess.org/article/2008/07/ogre-git-tutorial/ A Video presentation on &amp;quot;Git, The Basics&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
* [http://www.youtube.com/watch?v=4XpnKHJAok8 Linus Torvals on Git]&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [http://code.google.com/p/support/wiki/DVCSAnalysis Comparison between Git and Mercurial] by Google (summer 2008)&lt;br /&gt;
* [http://www.softeng.rl.ac.uk/media/uploads/publications/2010/03/cvs-svn.pdf Comparison of CVS and Subversion]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Comparison_of_revision_control_software Revision Control Systems Comparison]&lt;br /&gt;
* [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_wflow_cc_proj.htm IBM Software Information Center]&lt;br /&gt;
* [http://schacon.github.com/git/gittutorial.html Git Tutorial]&lt;br /&gt;
* [http://www.szakmeister.net/blog/2011/feb/17/choosing-new-version-control-system/ Choosing a Version Control System]&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50962</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1f rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50962"/>
		<updated>2011-09-25T23:38:58Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Comparison of Version Control Applications */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=='''Comparison of Version Control Systems : The Programmer View'''==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Version control also called Sub-Version Control or Revision Control is indispensable in large projects. They make sure that projects are not spinning out of control by letting different programmers work on the project from a different perspective without getting in each other’s way and without doing any damage that cannot be undone.&lt;br /&gt;
&lt;br /&gt;
Some of the most popular version control systems are RCS, SVN, CVS, Mercurial and Git. This article intends to compare these version control systems in a programmer’s viewpoint.&lt;br /&gt;
&lt;br /&gt;
== Choice of Version Control Systems ==&lt;br /&gt;
There are a number of solutions which are available for programmers. We have put together a definitive feature comparison for reference on deciding about the best choice for a project.&lt;br /&gt;
&lt;br /&gt;
The main difference between Version Control Systems is whether they are Client-Server based or peer-to-peer based.  Either they have a centralized repository where code is checked out, edited and checked in with changes, or a setup where the code is frequently updated from different peer sources, a more decentralized network to keep your code updated with changes.&lt;br /&gt;
&lt;br /&gt;
The article further compares all the Version Control Systems like RCS, CVS and Distributed Version Control Systems. Also, the article compares multiple Version Control applications like Mercurial, Git, Clearcase and CVS. Comparison is done in the view of programmer. Various commands used in different applications are discussed.&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Systems==&lt;br /&gt;
[[Image:RCS.png|thumb|150px|alt=Local Version Control]]&lt;br /&gt;
==== Local Version Control ====&lt;br /&gt;
This is a primitive approach of version control. The system operates on single files only.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Revision_Control_System&amp;lt;/ref&amp;gt; It has no way of working with an entire project. Changes to a file are stored as 'deltas' with the aid of a diff utility. Since the approach maintain many copies of files, it is generally cumbersome with reduced efficiency.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:CVS.png|thumb|150px|alt=Centralized Version Control]]&lt;br /&gt;
&lt;br /&gt;
==== Centralized Version Control ====&lt;br /&gt;
CVS implements a Client-Server model to achieve version control. A central repository is maintained which is used by all the developers of a project. Programmers can only have a working copy of the project. The model allows a developer to checkout only the required files or branches. In this model all operations on the code (commit, exchange of code etc.)  involve communication with the central server via a network. This means that these operations are not available when the user is offline. Also  issues in communication with the server may disrupt the development activity.&lt;br /&gt;
Any changes to the project on the server involves an authentication process. Hence only users with certain privileges can commit code on the central server . A developer can checkpoint or create branches on his work only by committing his changes to the central repository.  Since all the developers commit changes on the same repository, it is generally challenging to resolve conflicts when merging or reconciling changes from other branches.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Distributed Version Control ====&lt;br /&gt;
[[Image:File-DVCS.png|thumb|150px|alt=Distributed Version Control]]&lt;br /&gt;
This system implements a peer to peer model to achieve version control. No reference copy of the codebase exist by default. However in practice, an official reference copy is indeed maintained. The model  requires the developers to possess a full blown backup of the repository , including the entire history. As a result, the developers can collaborate directly without the need of a central authority. Hence it allows developers to be productive even when offline. (e.g. when travelling). Also the overhead of communicating with a central repository is overcome in this approach.&lt;br /&gt;
Branching and merging fit much better in this approach as each user has his own branch. The administrator's major work is to merge appropriate changes from branches of different users. This is relatively simpler compared to  the centralized system. As a result, the distributed system scales much better with the number of people.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Summary of Centralized versus Distributed Approach ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Centralized Version Control&lt;br /&gt;
! Distributed Version Control&lt;br /&gt;
|-&lt;br /&gt;
| Client - server model&lt;br /&gt;
| peer- to -peer model&lt;br /&gt;
|-&lt;br /&gt;
| Single central repository.&lt;br /&gt;
| Multiple repositories&lt;br /&gt;
|-&lt;br /&gt;
| Changes can be committed only Online&lt;br /&gt;
| Changes can be committed offline&lt;br /&gt;
|-&lt;br /&gt;
| Allows developer to checkout required files/branches.&lt;br /&gt;
| Complete repository has to be downloaded by the programmer &lt;br /&gt;
|-&lt;br /&gt;
| Merging and Branching operations are complicated as&lt;br /&gt;
multiple programmers work on the same repository at the same time.&lt;br /&gt;
| Branching is simple as each programmer gets a branch&lt;br /&gt;
|-&lt;br /&gt;
| Repository maintenance becomes more challenging with number of people&lt;br /&gt;
| Scales better with number of people&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Applications ==&lt;br /&gt;
==== Overview of Version Control Applications ====&lt;br /&gt;
&lt;br /&gt;
=====  RCS: The Revision Control System (RCS)=====&lt;br /&gt;
&lt;br /&gt;
RCS manages multiple revisions of files. RCS operates on a set of files. RCS automates the process of storing, retrieval, logging, identification, and merging of multiple revisions. It is useful for text that is modified frequently which includes source code, programs, documentation, graphics, papers, and letters. This is the most basic and most primitive Version Control System known to programmers.&lt;br /&gt;
&lt;br /&gt;
More information about RCS can be found at [http://www.gnu.org/s/rcs/ RCS]&lt;br /&gt;
&lt;br /&gt;
===== ClearCase Version Management System =====&lt;br /&gt;
&lt;br /&gt;
ClearCase is a comprehensive software configuration management system. It is used to manage multiple versions of evolving software systems, tracks which versions were used in software builds, used to perform builds of individual programs or complete releases according to user-defined version and system specifications.&lt;br /&gt;
      These capabilities enable ClearCase to address the issues and critical requirements of organizations that develop software:&lt;br /&gt;
&lt;br /&gt;
Effective development — ClearCase enables users to work efficiently, allowing them to fine-tune the balance between sharing each other's work and isolating themselves from destabilizing changes done. ClearCase automatically manages the sharing of both source files and the files produced by software builds.&lt;br /&gt;
&lt;br /&gt;
Effective management — ClearCase tracks the software build process, so that users can determine what was built, and how it was built. Further, ClearCase can instantly recreate the source base from which a software system was built, allowing it to be rebuilt, debugged, and updated — all without interfering with other programming work.&lt;br /&gt;
&lt;br /&gt;
Enforcement of development policies — ClearCase enables project administrators to define development policies and procedures, and to automate their enforcement.&lt;br /&gt;
&lt;br /&gt;
More information about ClearCase can be found at [http://www-01.ibm.com/software/awdtools/clearcase/ ClearCase]&lt;br /&gt;
=====  CVS: The Concurrent Versions System (CVS)=====&lt;br /&gt;
&lt;br /&gt;
CVS is a Client-Server software revision control system in the field of software development. Version control software keeps track of all the changes in a set of files, and makes sure that several developers (potentially widely separated in space and/or time) can collaborate in real time. The CVS server runs on Unix-like systems with client software that runs on different operating systems on multiple client machines. It is regarded as the most mature version control system because it has been developed for such a long time and has stood the test of time. A fork project of CVS, CVSNT was created to run CVS on Windows servers, and it is currently being actively developed to increase functionality and make it more usable on Windows platform.&lt;br /&gt;
&lt;br /&gt;
More information about CVS can be found at [http://www.nongnu.org/cvs/ CVS]&lt;br /&gt;
&lt;br /&gt;
=====  SVN: Sub Version (SVN)=====&lt;br /&gt;
&lt;br /&gt;
SVN was created as an alternative to CVS that would be useful to fix some known issues in CVS and also maintaining high compatibility with it. SVN is free and open source with the difference of being distributed under the Apache license as opposed to GNU which distributes licenses for CVS. To prevent the database from being corrupted, SVN adopts a technique called 'Atomic Operations'. Either all of the modifications made to the source are committed or none are committed. Partial changes will not enter the original source. Many developers have switched to SVN as it is a newer technology that starts with the best features of CVS and improves upon them. While branch operations in CVS are expensive and do not really lend themselves to long-term forks in the project, SVN is designed to allow for it. Hence it lends itself better to large forked projects with many directions. Some known disadvantages of SVN includes slower operation speeds and the lack of distributed revision control. Distributed revision control uses a peer-to-peer model as compared to using a centralized server to store code modifications. Peer-to-peer model work better for open source projects which are spread over multiple far away physical locations. The downside to a dedicated server approach is the issue of not having redundancy in the case when the server is down leading to no access to clients.&lt;br /&gt;
&lt;br /&gt;
More information about SVN can be found at [http://subversion.apache.org/ SVN]&lt;br /&gt;
&lt;br /&gt;
=====  Git:=====&lt;br /&gt;
&lt;br /&gt;
Git takes a totally diverse approach that differs greatly in comparison to CVS and SVN. The original concepts for Git were to make a faster, distributed version control system that would openly invalidate conventions and practices used in CVS. It is primarily developed for Linux and has the highest speeds on there. It will also run on other Unix-like systems, and Windows agents are known as msysgit. As there is no centralized server, Git is not very suitable for single developer projects or small teams as the code may not necessarily be available when using a normal computer. Workarounds exist for this problem. Developers look out for Git’s improved speed as a decent trade off for the hassle.&lt;br /&gt;
&lt;br /&gt;
More information about Git can be found at [http://git-scm.com/ Git]&lt;br /&gt;
&lt;br /&gt;
=====  Mercurial:===== &lt;br /&gt;
&lt;br /&gt;
Mercurial is a distributed revision control tool which began close to the same time as Git. Mercurial was originally made to compete with Git for Linux kernel development. Mercurial is primarily implemented in Python as opposed to C, but there are some instances where C is used. This is a major difference compared to other version control systems in Developer's point of view. Users feel that Mercurial is similar to SVN with respect to certain features, as well as being a distributed system. This acts as an advantage for the tool as the learning curve for those already familiar with SVN will be less steep. The documentation for Mercurial is very well compiled and facilitates understanding the differences faster. One of the major drawback to Mercurial is that it does not allow for two parents to be merged. Unlike Git, it uses an extension system rather than using scripts. That may be ideal for some section of programmers, but many find the speed of operation of Git to be a feature they do not want to trade off for anything.&lt;br /&gt;
&lt;br /&gt;
More information about Mercurial can be found at [http://mercurial.selenic.com Mercurial]&lt;br /&gt;
&lt;br /&gt;
==== Basic Features ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Repository model|Repository model&lt;br /&gt;
! #Concurrency model|Concurrency model&lt;br /&gt;
! #Programming Languages|Programming Languages&lt;br /&gt;
! #Scope of Change|Scope of Change&lt;br /&gt;
! #Atomic commits|Atomic commits&lt;br /&gt;
! #File Renames|File Renames&lt;br /&gt;
! #Interactive Commits|Interactive Commits&lt;br /&gt;
! #Partial Checkout/clone|Partial Checkout/clone&lt;br /&gt;
! Platforms supported&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Set of files	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Clear Case	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C, Java, Perl &lt;br /&gt;
|  File &lt;br /&gt;
|  Partial&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Linux, Windows, AIX, Solaris, HP UX, i5/OS, OS/390, z/OS&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C,Perl &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Partial &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| POSIX, Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  Python, C&lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X	&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Feature&lt;br /&gt;
! Feature Description&lt;br /&gt;
|-&lt;br /&gt;
! Repository Model&lt;br /&gt;
| Describes the relationship between various copies of the source code repository.&lt;br /&gt;
|-&lt;br /&gt;
! Concurrency Model&lt;br /&gt;
| Describes how changes to the working copy are managed to prevent simultaneous edits from causing nonsensical data in the repository.&lt;br /&gt;
|-&lt;br /&gt;
! Programming Languages&lt;br /&gt;
| The coding language in which the application is being developed&lt;br /&gt;
|-&lt;br /&gt;
! Scope of Change&lt;br /&gt;
| Describes whether changes are recorded for individual files or for entire directory trees.&lt;br /&gt;
|-&lt;br /&gt;
! Atomic commits&lt;br /&gt;
| Refers to a guarantee that all changes made are merged, or that no change at all will be made.&lt;br /&gt;
|-&lt;br /&gt;
! File Renames&lt;br /&gt;
| Describes whether a system allows files to be renamed while retaining their version history.&lt;br /&gt;
|-&lt;br /&gt;
! Interactive Commits&lt;br /&gt;
| Interactive commits allow the user to cherrypick the patch-hunks that become part of a commit, instead of having only a file-level granularity.&lt;br /&gt;
|-&lt;br /&gt;
! Partial Checkout/clone&lt;br /&gt;
| Ability to check out or clone only a specified subdirectory from a repository.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Basic Commands ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Clone|Clone&lt;br /&gt;
! #Pull|Pull&lt;br /&gt;
! #Push|Push&lt;br /&gt;
! #Checkout|Checkout&lt;br /&gt;
! #Add|Add&lt;br /&gt;
! #Remove|Remove&lt;br /&gt;
! #Merge|Merge&lt;br /&gt;
! #Commit|Commit&lt;br /&gt;
! #Revert|Revert&lt;br /&gt;
! #Rebase|Rebase&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  co &lt;br /&gt;
| Not Supported&lt;br /&gt;
| rcsclean&lt;br /&gt;
| rcsmerge&lt;br /&gt;
|  ci&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| update -j&lt;br /&gt;
|  commit&lt;br /&gt;
| rm, update&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| svnadmin hotcopy	&lt;br /&gt;
| svnadmin load&lt;br /&gt;
| svnadmin dump&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
|  commit&lt;br /&gt;
| revert&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! Clearcase	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| mkelem&lt;br /&gt;
| rmelem&lt;br /&gt;
| merge&lt;br /&gt;
| checkin&lt;br /&gt;
| unco/rmver&lt;br /&gt;
| findmerge&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Fetch&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| checkout&lt;br /&gt;
| rebase&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Pull&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| revert&lt;br /&gt;
| rebase&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Command Description&lt;br /&gt;
|-&lt;br /&gt;
! Clone&lt;br /&gt;
| Create an identical instance of a repository&lt;br /&gt;
|-&lt;br /&gt;
! Pull&lt;br /&gt;
| Download revisions from a remote repository to a local repository&lt;br /&gt;
|-&lt;br /&gt;
! Push&lt;br /&gt;
| Upload revisions from a local repository to a remote repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Create a local working copy from a (remote) repository&lt;br /&gt;
|-&lt;br /&gt;
! Add an element&lt;br /&gt;
| Mark specified files to be added to repository at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Remove an element&lt;br /&gt;
| Mark specified files to be removed at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Merge&lt;br /&gt;
| Apply the differences between two sources to a working copy path&lt;br /&gt;
|-&lt;br /&gt;
! Commit&lt;br /&gt;
| Record changes in the repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Restore working copy file from repository&lt;br /&gt;
|-&lt;br /&gt;
! Rebase&lt;br /&gt;
| Forward-port local commits to the updated upstream head&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Sample Workflow involving Clearcase ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
This section provides an example of a sample workflow used by a programmer to maintain the code in Clearcase.&lt;br /&gt;
&lt;br /&gt;
'''Joining a UCM Project''': After project managers have established the UCM project environment, developers can then set up their work areas.&lt;br /&gt;
This is accomplished by joining the UCM project. More information about this can be found   [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_join_ucm_proj.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Working with views''': A view is the mechanism Rational ClearCase uses to provide access to specific versions of elements or specific parts of code under source control. Rules are the basis to determine which version of each element is visible and accessible through the view for a developer. More information about views can be found [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_views_intro.htm here]&lt;br /&gt;
&lt;br /&gt;
Sample command to create a new view:''' ct mkview -tag &amp;lt;view_name&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Sample command to create a new file:'''ct mkelem &amp;lt;file_name&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
'''Checking in files and checking out files''': Checking files in and out are two basic operations you will perform on a regular basis by a developer. Whenever the developer needs to edit a file, he has to checkout the file. And after completion of coding, he needs to check in the file. More information about the process can be found  [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_file_ci_co.htm here]&lt;br /&gt;
&lt;br /&gt;
Sample command to check out a file: '''ct co -nc &amp;lt;file_name&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Sample command to check in a file:''' ct ci -nc &amp;lt;file_name&amp;gt;'''  (when you are working in the same branch where the file is checked out).&lt;br /&gt;
&lt;br /&gt;
'''Delivering your work''': After testing your work in your private workspace, and you are sure that your work is error-free, you are ready to submit your work to the integration stream so that other project members can use the latest version of your work. More information about the procedure of delivering code can be found  [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_deliver_wrk.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Merging your work''': After completion, the development branch must be merged to integration branch where software developed by different teams are merged and tested for seamless operation. More information about this procedure can be obtained [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_merge_wrk.htm here]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1a_br Information about Version Control Systems]&lt;br /&gt;
&lt;br /&gt;
* [http://excess.org/article/2008/07/ogre-git-tutorial/ A Video presentation on &amp;quot;Git, The Basics&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
* [http://www.youtube.com/watch?v=4XpnKHJAok8 Linus Torvals on Git]&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [http://code.google.com/p/support/wiki/DVCSAnalysis Comparison between Git and Mercurial] by Google (summer 2008)&lt;br /&gt;
* [http://www.softeng.rl.ac.uk/media/uploads/publications/2010/03/cvs-svn.pdf Comparison of CVS and Subversion]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Comparison_of_revision_control_software Revision Control Systems Comparison]&lt;br /&gt;
* [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_wflow_cc_proj.htm IBM Software Information Center]&lt;br /&gt;
* [http://schacon.github.com/git/gittutorial.html Git Tutorial]&lt;br /&gt;
* [http://www.szakmeister.net/blog/2011/feb/17/choosing-new-version-control-system/ Choosing a Version Control System]&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50948</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1f rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50948"/>
		<updated>2011-09-25T23:31:05Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Sample Workflow involving Clearcase */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=='''Comparison of Version Control Systems : The Programmer View'''==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Version control also called Sub-Version Control or Revision Control is indispensable in large projects. They make sure that projects are not spinning out of control by letting different programmers work on the project from a different perspective without getting in each other’s way and without doing any damage that cannot be undone.&lt;br /&gt;
&lt;br /&gt;
Some of the most popular version control systems are RCS, SVN, CVS, Mercurial and Git. This article intends to compare these version control systems in a programmer’s viewpoint.&lt;br /&gt;
&lt;br /&gt;
== Choice of Version Control Systems ==&lt;br /&gt;
There are a number of solutions which are available for programmers. We have put together a definitive feature comparison for reference on deciding about the best choice for a project.&lt;br /&gt;
&lt;br /&gt;
The main difference between Version Control Systems is whether they are Client-Server based or peer-to-peer based.  Either they have a centralized repository where code is checked out, edited and checked in with changes, or a setup where the code is frequently updated from different peer sources, a more decentralized network to keep your code updated with changes.&lt;br /&gt;
&lt;br /&gt;
The article further compares all the Version Control Systems like RCS, CVS and Distributed Version Control Systems. Also, the article compares multiple Version Control applications like Mercurial, Git, Clearcase and CVS. Comparison is done in the view of programmer. Various commands used in different applications are discussed.&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Systems==&lt;br /&gt;
[[Image:RCS.png|thumb|150px|alt=Local Version Control]]&lt;br /&gt;
==== Local Version Control ====&lt;br /&gt;
This is a primitive approach of version control. The system operates on single files only.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Revision_Control_System&amp;lt;/ref&amp;gt; It has no way of working with an entire project. Changes to a file are stored as 'deltas' with the aid of a diff utility. Since the approach maintain many copies of files, it is generally cumbersome with reduced efficiency.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:CVS.png|thumb|150px|alt=Centralized Version Control]]&lt;br /&gt;
&lt;br /&gt;
==== Centralized Version Control ====&lt;br /&gt;
CVS implements a Client-Server model to achieve version control. A central repository is maintained which is used by all the developers of a project. Programmers can only have a working copy of the project. The model allows a developer to checkout only the required files or branches. In this model all operations on the code (commit, exchange of code etc.)  involve communication with the central server via a network. This means that these operations are not available when the user is offline. Also  issues in communication with the server may disrupt the development activity.&lt;br /&gt;
Any changes to the project on the server involves an authentication process. Hence only users with certain privileges can commit code on the central server . A developer can checkpoint or create branches on his work only by committing his changes to the central repository.  Since all the developers commit changes on the same repository, it is generally challenging to resolve conflicts when merging or reconciling changes from other branches.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Distributed Version Control ====&lt;br /&gt;
[[Image:File-DVCS.png|thumb|150px|alt=Distributed Version Control]]&lt;br /&gt;
This system implements a peer to peer model to achieve version control. No reference copy of the codebase exist by default. However in practice, an official reference copy is indeed maintained. The model  requires the developers to possess a full blown backup of the repository , including the entire history. As a result, the developers can collaborate directly without the need of a central authority. Hence it allows developers to be productive even when offline. (e.g. when travelling). Also the overhead of communicating with a central repository is overcome in this approach.&lt;br /&gt;
Branching and merging fit much better in this approach as each user has his own branch. The administrator's major work is to merge appropriate changes from branches of different users. This is relatively simpler compared to  the centralized system. As a result, the distributed system scales much better with the number of people.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Summary of Centralized versus Distributed Approach ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Centralized Version Control&lt;br /&gt;
! Distributed Version Control&lt;br /&gt;
|-&lt;br /&gt;
| Client - server model&lt;br /&gt;
| peer- to -peer model&lt;br /&gt;
|-&lt;br /&gt;
| Single central repository.&lt;br /&gt;
| Multiple repositories&lt;br /&gt;
|-&lt;br /&gt;
| Changes can be committed only Online&lt;br /&gt;
| Changes can be committed offline&lt;br /&gt;
|-&lt;br /&gt;
| Allows developer to checkout required files/branches.&lt;br /&gt;
| Complete repository has to be downloaded by the programmer &lt;br /&gt;
|-&lt;br /&gt;
| Merging and Branching operations are complicated as&lt;br /&gt;
multiple programmers work on the same repository at the same time.&lt;br /&gt;
| Branching is simple as each programmer gets a branch&lt;br /&gt;
|-&lt;br /&gt;
| Repository maintenance becomes more challenging with number of people&lt;br /&gt;
| Scales better with number of people&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Applications ==&lt;br /&gt;
==== Overview of Version Control Applications ====&lt;br /&gt;
&lt;br /&gt;
=====  RCS: The Revision Control System (RCS)=====&lt;br /&gt;
&lt;br /&gt;
RCS manages multiple revisions of files. RCS operates on a set of files. RCS automates the process of storing, retrieval, logging, identification, and merging of multiple revisions. It is useful for text that is modified frequently which includes source code, programs, documentation, graphics, papers, and letters. This is the most basic and most primitive Version Control System known to programmers.&lt;br /&gt;
&lt;br /&gt;
More information about RCS can be found at [http://www.gnu.org/s/rcs/ RCS]&lt;br /&gt;
&lt;br /&gt;
=====  CVS: The Concurrent Versions System (CVS)=====&lt;br /&gt;
&lt;br /&gt;
CVS is a Client-Server software revision control system in the field of software development. Version control software keeps track of all the changes in a set of files, and makes sure that several developers (potentially widely separated in space and/or time) can collaborate in real time. The CVS server runs on Unix-like systems with client software that runs on different operating systems on multiple client machines. It is regarded as the most mature version control system because it has been developed for such a long time and has stood the test of time. A fork project of CVS, CVSNT was created to run CVS on Windows servers, and it is currently being actively developed to increase functionality and make it more usable on Windows platform.&lt;br /&gt;
&lt;br /&gt;
More information about CVS can be found at [http://www.nongnu.org/cvs/ CVS]&lt;br /&gt;
&lt;br /&gt;
=====  SVN: Sub Version (SVN)=====&lt;br /&gt;
&lt;br /&gt;
SVN was created as an alternative to CVS that would be useful to fix some known issues in CVS and also maintaining high compatibility with it. SVN is free and open source with the difference of being distributed under the Apache license as opposed to GNU which distributes licenses for CVS. To prevent the database from being corrupted, SVN adopts a technique called 'Atomic Operations'. Either all of the modifications made to the source are committed or none are committed. Partial changes will not enter the original source. Many developers have switched to SVN as it is a newer technology that starts with the best features of CVS and improves upon them. While branch operations in CVS are expensive and do not really lend themselves to long-term forks in the project, SVN is designed to allow for it. Hence it lends itself better to large forked projects with many directions. Some known disadvantages of SVN includes slower operation speeds and the lack of distributed revision control. Distributed revision control uses a peer-to-peer model as compared to using a centralized server to store code modifications. Peer-to-peer model work better for open source projects which are spread over multiple far away physical locations. The downside to a dedicated server approach is the issue of not having redundancy in the case when the server is down leading to no access to clients.&lt;br /&gt;
&lt;br /&gt;
More information about SVN can be found at [http://subversion.apache.org/ SVN]&lt;br /&gt;
&lt;br /&gt;
=====  Git:=====&lt;br /&gt;
&lt;br /&gt;
Git takes a totally diverse approach that differs greatly in comparison to CVS and SVN. The original concepts for Git were to make a faster, distributed version control system that would openly invalidate conventions and practices used in CVS. It is primarily developed for Linux and has the highest speeds on there. It will also run on other Unix-like systems, and Windows agents are known as msysgit. As there is no centralized server, Git is not very suitable for single developer projects or small teams as the code may not necessarily be available when using a normal computer. Workarounds exist for this problem. Developers look out for Git’s improved speed as a decent trade off for the hassle.&lt;br /&gt;
&lt;br /&gt;
More information about Git can be found at [http://git-scm.com/ Git]&lt;br /&gt;
&lt;br /&gt;
=====  Mercurial:===== &lt;br /&gt;
&lt;br /&gt;
Mercurial is a distributed revision control tool which began close to the same time as Git. Mercurial was originally made to compete with Git for Linux kernel development. Mercurial is primarily implemented in Python as opposed to C, but there are some instances where C is used. This is a major difference compared to other version control systems in Developer's point of view. Users feel that Mercurial is similar to SVN with respect to certain features, as well as being a distributed system. This acts as an advantage for the tool as the learning curve for those already familiar with SVN will be less steep. The documentation for Mercurial is very well compiled and facilitates understanding the differences faster. One of the major drawback to Mercurial is that it does not allow for two parents to be merged. Unlike Git, it uses an extension system rather than using scripts. That may be ideal for some section of programmers, but many find the speed of operation of Git to be a feature they do not want to trade off for anything.&lt;br /&gt;
&lt;br /&gt;
More information about Mercurial can be found at [http://mercurial.selenic.com Mercurial]&lt;br /&gt;
&lt;br /&gt;
==== Basic Features ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Repository model|Repository model&lt;br /&gt;
! #Concurrency model|Concurrency model&lt;br /&gt;
! #Programming Languages|Programming Languages&lt;br /&gt;
! #Scope of Change|Scope of Change&lt;br /&gt;
! #Atomic commits|Atomic commits&lt;br /&gt;
! #File Renames|File Renames&lt;br /&gt;
! #Interactive Commits|Interactive Commits&lt;br /&gt;
! #Partial Checkout/clone|Partial Checkout/clone&lt;br /&gt;
! Platforms supported&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Set of files	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Clear Case	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C, Java, Perl &lt;br /&gt;
|  File &lt;br /&gt;
|  Partial&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Linux, Windows, AIX, Solaris, HP UX, i5/OS, OS/390, z/OS&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C,Perl &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Partial &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| POSIX, Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  Python, C&lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X	&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Feature&lt;br /&gt;
! Feature Description&lt;br /&gt;
|-&lt;br /&gt;
! Repository Model&lt;br /&gt;
| Describes the relationship between various copies of the source code repository.&lt;br /&gt;
|-&lt;br /&gt;
! Concurrency Model&lt;br /&gt;
| Describes how changes to the working copy are managed to prevent simultaneous edits from causing nonsensical data in the repository.&lt;br /&gt;
|-&lt;br /&gt;
! Programming Languages&lt;br /&gt;
| The coding language in which the application is being developed&lt;br /&gt;
|-&lt;br /&gt;
! Scope of Change&lt;br /&gt;
| Describes whether changes are recorded for individual files or for entire directory trees.&lt;br /&gt;
|-&lt;br /&gt;
! Atomic commits&lt;br /&gt;
| Refers to a guarantee that all changes made are merged, or that no change at all will be made.&lt;br /&gt;
|-&lt;br /&gt;
! File Renames&lt;br /&gt;
| Describes whether a system allows files to be renamed while retaining their version history.&lt;br /&gt;
|-&lt;br /&gt;
! Interactive Commits&lt;br /&gt;
| Interactive commits allow the user to cherrypick the patch-hunks that become part of a commit, instead of having only a file-level granularity.&lt;br /&gt;
|-&lt;br /&gt;
! Partial Checkout/clone&lt;br /&gt;
| Ability to check out or clone only a specified subdirectory from a repository.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Basic Commands ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Clone|Clone&lt;br /&gt;
! #Pull|Pull&lt;br /&gt;
! #Push|Push&lt;br /&gt;
! #Checkout|Checkout&lt;br /&gt;
! #Add|Add&lt;br /&gt;
! #Remove|Remove&lt;br /&gt;
! #Merge|Merge&lt;br /&gt;
! #Commit|Commit&lt;br /&gt;
! #Revert|Revert&lt;br /&gt;
! #Rebase|Rebase&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  co &lt;br /&gt;
| Not Supported&lt;br /&gt;
| rcsclean&lt;br /&gt;
| rcsmerge&lt;br /&gt;
|  ci&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| update -j&lt;br /&gt;
|  commit&lt;br /&gt;
| rm, update&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| svnadmin hotcopy	&lt;br /&gt;
| svnadmin load&lt;br /&gt;
| svnadmin dump&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
|  commit&lt;br /&gt;
| revert&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! Clearcase	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| mkelem&lt;br /&gt;
| rmelem&lt;br /&gt;
| merge&lt;br /&gt;
| checkin&lt;br /&gt;
| unco/rmver&lt;br /&gt;
| findmerge&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Fetch&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| checkout&lt;br /&gt;
| rebase&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Pull&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| revert&lt;br /&gt;
| rebase&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Command Description&lt;br /&gt;
|-&lt;br /&gt;
! Clone&lt;br /&gt;
| Create an identical instance of a repository&lt;br /&gt;
|-&lt;br /&gt;
! Pull&lt;br /&gt;
| Download revisions from a remote repository to a local repository&lt;br /&gt;
|-&lt;br /&gt;
! Push&lt;br /&gt;
| Upload revisions from a local repository to a remote repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Create a local working copy from a (remote) repository&lt;br /&gt;
|-&lt;br /&gt;
! Add an element&lt;br /&gt;
| Mark specified files to be added to repository at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Remove an element&lt;br /&gt;
| Mark specified files to be removed at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Merge&lt;br /&gt;
| Apply the differences between two sources to a working copy path&lt;br /&gt;
|-&lt;br /&gt;
! Commit&lt;br /&gt;
| Record changes in the repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Restore working copy file from repository&lt;br /&gt;
|-&lt;br /&gt;
! Rebase&lt;br /&gt;
| Forward-port local commits to the updated upstream head&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Sample Workflow involving Clearcase ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
This section provides an example of a sample workflow used by a programmer to maintain the code in Clearcase.&lt;br /&gt;
&lt;br /&gt;
'''Joining a UCM Project''': After project managers have established the UCM project environment, developers can then set up their work areas.&lt;br /&gt;
This is accomplished by joining the UCM project. More information about this can be found   [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_join_ucm_proj.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Working with views''': A view is the mechanism Rational ClearCase uses to provide access to specific versions of elements or specific parts of code under source control. Rules are the basis to determine which version of each element is visible and accessible through the view for a developer. More information about views can be found [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_views_intro.htm here]&lt;br /&gt;
&lt;br /&gt;
Sample command to create a new view:''' ct mkview -tag &amp;lt;view_name&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Sample command to create a new file:'''ct mkelem &amp;lt;file_name&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
'''Checking in files and checking out files''': Checking files in and out are two basic operations you will perform on a regular basis by a developer. Whenever the developer needs to edit a file, he has to checkout the file. And after completion of coding, he needs to check in the file. More information about the process can be found  [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_file_ci_co.htm here]&lt;br /&gt;
&lt;br /&gt;
Sample command to check out a file: '''ct co -nc &amp;lt;file_name&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
Sample command to check in a file:''' ct ci -nc &amp;lt;file_name&amp;gt;'''  (when you are working in the same branch where the file is checked out).&lt;br /&gt;
&lt;br /&gt;
'''Delivering your work''': After testing your work in your private workspace, and you are sure that your work is error-free, you are ready to submit your work to the integration stream so that other project members can use the latest version of your work. More information about the procedure of delivering code can be found  [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_deliver_wrk.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Merging your work''': After completion, the development branch must be merged to integration branch where software developed by different teams are merged and tested for seamless operation. More information about this procedure can be obtained [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_merge_wrk.htm here]&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1a_br Information about Version Control Systems]&lt;br /&gt;
&lt;br /&gt;
* [http://excess.org/article/2008/07/ogre-git-tutorial/ A Video presentation on &amp;quot;Git, The Basics&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
* [http://www.youtube.com/watch?v=4XpnKHJAok8 Linus Torvals on Git]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [http://code.google.com/p/support/wiki/DVCSAnalysis Comparison between Git and Mercurial] by Google (summer 2008)&lt;br /&gt;
* [http://www.softeng.rl.ac.uk/media/uploads/publications/2010/03/cvs-svn.pdf Comparison of CVS and Subversion]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Comparison_of_revision_control_software Revision Control Systems Comparison]&lt;br /&gt;
* [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_wflow_cc_proj.htm IBM Software Information Center]&lt;br /&gt;
* [http://schacon.github.com/git/gittutorial.html Git Tutorial]&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50946</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1f rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50946"/>
		<updated>2011-09-25T23:30:11Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Sample Workflow involving Clearcase */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=='''Comparison of Version Control Systems : The Programmer View'''==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Version control also called Sub-Version Control or Revision Control is indispensable in large projects. They make sure that projects are not spinning out of control by letting different programmers work on the project from a different perspective without getting in each other’s way and without doing any damage that cannot be undone.&lt;br /&gt;
&lt;br /&gt;
Some of the most popular version control systems are RCS, SVN, CVS, Mercurial and Git. This article intends to compare these version control systems in a programmer’s viewpoint.&lt;br /&gt;
&lt;br /&gt;
== Choice of Version Control Systems ==&lt;br /&gt;
There are a number of solutions which are available for programmers. We have put together a definitive feature comparison for reference on deciding about the best choice for a project.&lt;br /&gt;
&lt;br /&gt;
The main difference between Version Control Systems is whether they are Client-Server based or peer-to-peer based.  Either they have a centralized repository where code is checked out, edited and checked in with changes, or a setup where the code is frequently updated from different peer sources, a more decentralized network to keep your code updated with changes.&lt;br /&gt;
&lt;br /&gt;
The article further compares all the Version Control Systems like RCS, CVS and Distributed Version Control Systems. Also, the article compares multiple Version Control applications like Mercurial, Git, Clearcase and CVS. Comparison is done in the view of programmer. Various commands used in different applications are discussed.&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Systems==&lt;br /&gt;
[[Image:RCS.png|thumb|150px|alt=Local Version Control]]&lt;br /&gt;
==== Local Version Control ====&lt;br /&gt;
This is a primitive approach of version control. The system operates on single files only.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Revision_Control_System&amp;lt;/ref&amp;gt; It has no way of working with an entire project. Changes to a file are stored as 'deltas' with the aid of a diff utility. Since the approach maintain many copies of files, it is generally cumbersome with reduced efficiency.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:CVS.png|thumb|150px|alt=Centralized Version Control]]&lt;br /&gt;
&lt;br /&gt;
==== Centralized Version Control ====&lt;br /&gt;
CVS implements a Client-Server model to achieve version control. A central repository is maintained which is used by all the developers of a project. Programmers can only have a working copy of the project. The model allows a developer to checkout only the required files or branches. In this model all operations on the code (commit, exchange of code etc.)  involve communication with the central server via a network. This means that these operations are not available when the user is offline. Also  issues in communication with the server may disrupt the development activity.&lt;br /&gt;
Any changes to the project on the server involves an authentication process. Hence only users with certain privileges can commit code on the central server . A developer can checkpoint or create branches on his work only by committing his changes to the central repository.  Since all the developers commit changes on the same repository, it is generally challenging to resolve conflicts when merging or reconciling changes from other branches.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Distributed Version Control ====&lt;br /&gt;
[[Image:File-DVCS.png|thumb|150px|alt=Distributed Version Control]]&lt;br /&gt;
This system implements a peer to peer model to achieve version control. No reference copy of the codebase exist by default. However in practice, an official reference copy is indeed maintained. The model  requires the developers to possess a full blown backup of the repository , including the entire history. As a result, the developers can collaborate directly without the need of a central authority. Hence it allows developers to be productive even when offline. (e.g. when travelling). Also the overhead of communicating with a central repository is overcome in this approach.&lt;br /&gt;
Branching and merging fit much better in this approach as each user has his own branch. The administrator's major work is to merge appropriate changes from branches of different users. This is relatively simpler compared to  the centralized system. As a result, the distributed system scales much better with the number of people.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Summary of Centralized versus Distributed Approach ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Centralized Version Control&lt;br /&gt;
! Distributed Version Control&lt;br /&gt;
|-&lt;br /&gt;
| Client - server model&lt;br /&gt;
| peer- to -peer model&lt;br /&gt;
|-&lt;br /&gt;
| Single central repository.&lt;br /&gt;
| Multiple repositories&lt;br /&gt;
|-&lt;br /&gt;
| Changes can be committed only Online&lt;br /&gt;
| Changes can be committed offline&lt;br /&gt;
|-&lt;br /&gt;
| Allows developer to checkout required files/branches.&lt;br /&gt;
| Complete repository has to be downloaded by the programmer &lt;br /&gt;
|-&lt;br /&gt;
| Merging and Branching operations are complicated as&lt;br /&gt;
multiple programmers work on the same repository at the same time.&lt;br /&gt;
| Branching is simple as each programmer gets a branch&lt;br /&gt;
|-&lt;br /&gt;
| Repository maintenance becomes more challenging with number of people&lt;br /&gt;
| Scales better with number of people&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Applications ==&lt;br /&gt;
==== Overview of Version Control Applications ====&lt;br /&gt;
&lt;br /&gt;
=====  RCS: The Revision Control System (RCS)=====&lt;br /&gt;
&lt;br /&gt;
RCS manages multiple revisions of files. RCS operates on a set of files. RCS automates the process of storing, retrieval, logging, identification, and merging of multiple revisions. It is useful for text that is modified frequently which includes source code, programs, documentation, graphics, papers, and letters. This is the most basic and most primitive Version Control System known to programmers.&lt;br /&gt;
&lt;br /&gt;
More information about RCS can be found at [http://www.gnu.org/s/rcs/ RCS]&lt;br /&gt;
&lt;br /&gt;
=====  CVS: The Concurrent Versions System (CVS)=====&lt;br /&gt;
&lt;br /&gt;
CVS is a Client-Server software revision control system in the field of software development. Version control software keeps track of all the changes in a set of files, and makes sure that several developers (potentially widely separated in space and/or time) can collaborate in real time. The CVS server runs on Unix-like systems with client software that runs on different operating systems on multiple client machines. It is regarded as the most mature version control system because it has been developed for such a long time and has stood the test of time. A fork project of CVS, CVSNT was created to run CVS on Windows servers, and it is currently being actively developed to increase functionality and make it more usable on Windows platform.&lt;br /&gt;
&lt;br /&gt;
More information about CVS can be found at [http://www.nongnu.org/cvs/ CVS]&lt;br /&gt;
&lt;br /&gt;
=====  SVN: Sub Version (SVN)=====&lt;br /&gt;
&lt;br /&gt;
SVN was created as an alternative to CVS that would be useful to fix some known issues in CVS and also maintaining high compatibility with it. SVN is free and open source with the difference of being distributed under the Apache license as opposed to GNU which distributes licenses for CVS. To prevent the database from being corrupted, SVN adopts a technique called 'Atomic Operations'. Either all of the modifications made to the source are committed or none are committed. Partial changes will not enter the original source. Many developers have switched to SVN as it is a newer technology that starts with the best features of CVS and improves upon them. While branch operations in CVS are expensive and do not really lend themselves to long-term forks in the project, SVN is designed to allow for it. Hence it lends itself better to large forked projects with many directions. Some known disadvantages of SVN includes slower operation speeds and the lack of distributed revision control. Distributed revision control uses a peer-to-peer model as compared to using a centralized server to store code modifications. Peer-to-peer model work better for open source projects which are spread over multiple far away physical locations. The downside to a dedicated server approach is the issue of not having redundancy in the case when the server is down leading to no access to clients.&lt;br /&gt;
&lt;br /&gt;
More information about SVN can be found at [http://subversion.apache.org/ SVN]&lt;br /&gt;
&lt;br /&gt;
=====  Git:=====&lt;br /&gt;
&lt;br /&gt;
Git takes a totally diverse approach that differs greatly in comparison to CVS and SVN. The original concepts for Git were to make a faster, distributed version control system that would openly invalidate conventions and practices used in CVS. It is primarily developed for Linux and has the highest speeds on there. It will also run on other Unix-like systems, and Windows agents are known as msysgit. As there is no centralized server, Git is not very suitable for single developer projects or small teams as the code may not necessarily be available when using a normal computer. Workarounds exist for this problem. Developers look out for Git’s improved speed as a decent trade off for the hassle.&lt;br /&gt;
&lt;br /&gt;
More information about Git can be found at [http://git-scm.com/ Git]&lt;br /&gt;
&lt;br /&gt;
=====  Mercurial:===== &lt;br /&gt;
&lt;br /&gt;
Mercurial is a distributed revision control tool which began close to the same time as Git. Mercurial was originally made to compete with Git for Linux kernel development. Mercurial is primarily implemented in Python as opposed to C, but there are some instances where C is used. This is a major difference compared to other version control systems in Developer's point of view. Users feel that Mercurial is similar to SVN with respect to certain features, as well as being a distributed system. This acts as an advantage for the tool as the learning curve for those already familiar with SVN will be less steep. The documentation for Mercurial is very well compiled and facilitates understanding the differences faster. One of the major drawback to Mercurial is that it does not allow for two parents to be merged. Unlike Git, it uses an extension system rather than using scripts. That may be ideal for some section of programmers, but many find the speed of operation of Git to be a feature they do not want to trade off for anything.&lt;br /&gt;
&lt;br /&gt;
More information about Mercurial can be found at [http://mercurial.selenic.com Mercurial]&lt;br /&gt;
&lt;br /&gt;
==== Basic Features ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Repository model|Repository model&lt;br /&gt;
! #Concurrency model|Concurrency model&lt;br /&gt;
! #Programming Languages|Programming Languages&lt;br /&gt;
! #Scope of Change|Scope of Change&lt;br /&gt;
! #Atomic commits|Atomic commits&lt;br /&gt;
! #File Renames|File Renames&lt;br /&gt;
! #Interactive Commits|Interactive Commits&lt;br /&gt;
! #Partial Checkout/clone|Partial Checkout/clone&lt;br /&gt;
! Platforms supported&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Set of files	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Clear Case	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C, Java, Perl &lt;br /&gt;
|  File &lt;br /&gt;
|  Partial&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Linux, Windows, AIX, Solaris, HP UX, i5/OS, OS/390, z/OS&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C,Perl &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Partial &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| POSIX, Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  Python, C&lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X	&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Feature&lt;br /&gt;
! Feature Description&lt;br /&gt;
|-&lt;br /&gt;
! Repository Model&lt;br /&gt;
| Describes the relationship between various copies of the source code repository.&lt;br /&gt;
|-&lt;br /&gt;
! Concurrency Model&lt;br /&gt;
| Describes how changes to the working copy are managed to prevent simultaneous edits from causing nonsensical data in the repository.&lt;br /&gt;
|-&lt;br /&gt;
! Programming Languages&lt;br /&gt;
| The coding language in which the application is being developed&lt;br /&gt;
|-&lt;br /&gt;
! Scope of Change&lt;br /&gt;
| Describes whether changes are recorded for individual files or for entire directory trees.&lt;br /&gt;
|-&lt;br /&gt;
! Atomic commits&lt;br /&gt;
| Refers to a guarantee that all changes made are merged, or that no change at all will be made.&lt;br /&gt;
|-&lt;br /&gt;
! File Renames&lt;br /&gt;
| Describes whether a system allows files to be renamed while retaining their version history.&lt;br /&gt;
|-&lt;br /&gt;
! Interactive Commits&lt;br /&gt;
| Interactive commits allow the user to cherrypick the patch-hunks that become part of a commit, instead of having only a file-level granularity.&lt;br /&gt;
|-&lt;br /&gt;
! Partial Checkout/clone&lt;br /&gt;
| Ability to check out or clone only a specified subdirectory from a repository.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Basic Commands ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Clone|Clone&lt;br /&gt;
! #Pull|Pull&lt;br /&gt;
! #Push|Push&lt;br /&gt;
! #Checkout|Checkout&lt;br /&gt;
! #Add|Add&lt;br /&gt;
! #Remove|Remove&lt;br /&gt;
! #Merge|Merge&lt;br /&gt;
! #Commit|Commit&lt;br /&gt;
! #Revert|Revert&lt;br /&gt;
! #Rebase|Rebase&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  co &lt;br /&gt;
| Not Supported&lt;br /&gt;
| rcsclean&lt;br /&gt;
| rcsmerge&lt;br /&gt;
|  ci&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| update -j&lt;br /&gt;
|  commit&lt;br /&gt;
| rm, update&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| svnadmin hotcopy	&lt;br /&gt;
| svnadmin load&lt;br /&gt;
| svnadmin dump&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
|  commit&lt;br /&gt;
| revert&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! Clearcase	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| mkelem&lt;br /&gt;
| rmelem&lt;br /&gt;
| merge&lt;br /&gt;
| checkin&lt;br /&gt;
| unco/rmver&lt;br /&gt;
| findmerge&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Fetch&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| checkout&lt;br /&gt;
| rebase&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Pull&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| revert&lt;br /&gt;
| rebase&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Command Description&lt;br /&gt;
|-&lt;br /&gt;
! Clone&lt;br /&gt;
| Create an identical instance of a repository&lt;br /&gt;
|-&lt;br /&gt;
! Pull&lt;br /&gt;
| Download revisions from a remote repository to a local repository&lt;br /&gt;
|-&lt;br /&gt;
! Push&lt;br /&gt;
| Upload revisions from a local repository to a remote repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Create a local working copy from a (remote) repository&lt;br /&gt;
|-&lt;br /&gt;
! Add an element&lt;br /&gt;
| Mark specified files to be added to repository at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Remove an element&lt;br /&gt;
| Mark specified files to be removed at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Merge&lt;br /&gt;
| Apply the differences between two sources to a working copy path&lt;br /&gt;
|-&lt;br /&gt;
! Commit&lt;br /&gt;
| Record changes in the repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Restore working copy file from repository&lt;br /&gt;
|-&lt;br /&gt;
! Rebase&lt;br /&gt;
| Forward-port local commits to the updated upstream head&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Sample Workflow involving Clearcase ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
This section provides an example of a sample workflow used by a programmer to maintain the code in Clearcase.&lt;br /&gt;
&lt;br /&gt;
'''Joining a UCM Project''': After project managers have established the UCM project environment, developers can then set up their work areas.&lt;br /&gt;
This is accomplished by joining the UCM project. More information about this can be found   [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_join_ucm_proj.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Working with views''': A view is the mechanism Rational ClearCase uses to provide access to specific versions of elements or specific parts of code under source control. Rules are the basis to determine which version of each element is visible and accessible through the view for a developer. More information about views can be found [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_views_intro.htm here]&lt;br /&gt;
&lt;br /&gt;
Sample command to create a new view:''' ct mkview -tag &amp;lt;view_name&amp;gt;'''&lt;br /&gt;
Sample command to create a new file:'''ct mkelem &amp;lt;file_name&amp;gt;'''&lt;br /&gt;
&lt;br /&gt;
'''Checking in files and checking out files''': Checking files in and out are two basic operations you will perform on a regular basis by a developer. Whenever the developer needs to edit a file, he has to checkout the file. And after completion of coding, he needs to check in the file. More information about the process can be found  [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_file_ci_co.htm here]&lt;br /&gt;
&lt;br /&gt;
Sample command to check out a file: '''ct co -nc &amp;lt;file_name&amp;gt;'''&lt;br /&gt;
Sample command to check in a file:''' ct ci -nc &amp;lt;file_name&amp;gt;'''  (when you are working in the same branch where the file is checked out).&lt;br /&gt;
&lt;br /&gt;
'''Delivering your work''': After testing your work in your private workspace, and you are sure that your work is error-free, you are ready to submit your work to the integration stream so that other project members can use the latest version of your work. More information about the procedure of delivering code can be found  [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_deliver_wrk.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Merging your work''': After completion, the development branch must be merged to integration branch where software developed by different teams are merged and tested for seamless operation. More information about this procedure can be obtained [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_merge_wrk.htm here]&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
* [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1a_br Information about Version Control Systems]&lt;br /&gt;
&lt;br /&gt;
* [http://excess.org/article/2008/07/ogre-git-tutorial/ A Video presentation on &amp;quot;Git, The Basics&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
* [http://www.youtube.com/watch?v=4XpnKHJAok8 Linus Torvals on Git]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [http://code.google.com/p/support/wiki/DVCSAnalysis Comparison between Git and Mercurial] by Google (summer 2008)&lt;br /&gt;
* [http://www.softeng.rl.ac.uk/media/uploads/publications/2010/03/cvs-svn.pdf Comparison of CVS and Subversion]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Comparison_of_revision_control_software Revision Control Systems Comparison]&lt;br /&gt;
* [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_wflow_cc_proj.htm IBM Software Information Center]&lt;br /&gt;
* [http://schacon.github.com/git/gittutorial.html Git Tutorial]&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50944</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1f rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50944"/>
		<updated>2011-09-25T23:29:03Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Sample Workflow involving Clearcase */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=='''Comparison of Version Control Systems : The Programmer View'''==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Version control also called Sub-Version Control or Revision Control is indispensable in large projects. They make sure that projects are not spinning out of control by letting different programmers work on the project from a different perspective without getting in each other’s way and without doing any damage that cannot be undone.&lt;br /&gt;
&lt;br /&gt;
Some of the most popular version control systems are RCS, SVN, CVS, Mercurial and Git. This article intends to compare these version control systems in a programmer’s viewpoint.&lt;br /&gt;
&lt;br /&gt;
== Choice of Version Control Systems ==&lt;br /&gt;
There are a number of solutions which are available for programmers. We have put together a definitive feature comparison for reference on deciding about the best choice for a project.&lt;br /&gt;
&lt;br /&gt;
The main difference between Version Control Systems is whether they are Client-Server based or peer-to-peer based.  Either they have a centralized repository where code is checked out, edited and checked in with changes, or a setup where the code is frequently updated from different peer sources, a more decentralized network to keep your code updated with changes.&lt;br /&gt;
&lt;br /&gt;
The article further compares all the Version Control Systems like RCS, CVS and Distributed Version Control Systems. Also, the article compares multiple Version Control applications like Mercurial, Git, Clearcase and CVS. Comparison is done in the view of programmer. Various commands used in different applications are discussed.&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Systems==&lt;br /&gt;
[[Image:RCS.png|thumb|150px|alt=Local Version Control]]&lt;br /&gt;
==== Local Version Control ====&lt;br /&gt;
This is a primitive approach of version control. The system operates on single files only.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Revision_Control_System&amp;lt;/ref&amp;gt; It has no way of working with an entire project. Changes to a file are stored as 'deltas' with the aid of a diff utility. Since the approach maintain many copies of files, it is generally cumbersome with reduced efficiency.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:CVS.png|thumb|150px|alt=Centralized Version Control]]&lt;br /&gt;
&lt;br /&gt;
==== Centralized Version Control ====&lt;br /&gt;
CVS implements a Client-Server model to achieve version control. A central repository is maintained which is used by all the developers of a project. Programmers can only have a working copy of the project. The model allows a developer to checkout only the required files or branches. In this model all operations on the code (commit, exchange of code etc.)  involve communication with the central server via a network. This means that these operations are not available when the user is offline. Also  issues in communication with the server may disrupt the development activity.&lt;br /&gt;
Any changes to the project on the server involves an authentication process. Hence only users with certain privileges can commit code on the central server . A developer can checkpoint or create branches on his work only by committing his changes to the central repository.  Since all the developers commit changes on the same repository, it is generally challenging to resolve conflicts when merging or reconciling changes from other branches.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Distributed Version Control ====&lt;br /&gt;
[[Image:File-DVCS.png|thumb|150px|alt=Distributed Version Control]]&lt;br /&gt;
This system implements a peer to peer model to achieve version control. No reference copy of the codebase exist by default. However in practice, an official reference copy is indeed maintained. The model  requires the developers to possess a full blown backup of the repository , including the entire history. As a result, the developers can collaborate directly without the need of a central authority. Hence it allows developers to be productive even when offline. (e.g. when travelling). Also the overhead of communicating with a central repository is overcome in this approach.&lt;br /&gt;
Branching and merging fit much better in this approach as each user has his own branch. The administrator's major work is to merge appropriate changes from branches of different users. This is relatively simpler compared to  the centralized system. As a result, the distributed system scales much better with the number of people.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Summary of Centralized versus Distributed Approach ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Centralized Version Control&lt;br /&gt;
! Distributed Version Control&lt;br /&gt;
|-&lt;br /&gt;
| Client - server model&lt;br /&gt;
| peer- to -peer model&lt;br /&gt;
|-&lt;br /&gt;
| Single central repository.&lt;br /&gt;
| Multiple repositories&lt;br /&gt;
|-&lt;br /&gt;
| Changes can be committed only Online&lt;br /&gt;
| Changes can be committed offline&lt;br /&gt;
|-&lt;br /&gt;
| Allows developer to checkout required files/branches.&lt;br /&gt;
| Complete repository has to be downloaded by the programmer &lt;br /&gt;
|-&lt;br /&gt;
| Merging and Branching operations are complicated as&lt;br /&gt;
multiple programmers work on the same repository at the same time.&lt;br /&gt;
| Branching is simple as each programmer gets a branch&lt;br /&gt;
|-&lt;br /&gt;
| Repository maintenance becomes more challenging with number of people&lt;br /&gt;
| Scales better with number of people&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Applications ==&lt;br /&gt;
==== Overview of Version Control Applications ====&lt;br /&gt;
&lt;br /&gt;
=====  RCS: The Revision Control System (RCS)=====&lt;br /&gt;
&lt;br /&gt;
RCS manages multiple revisions of files. RCS operates on a set of files. RCS automates the process of storing, retrieval, logging, identification, and merging of multiple revisions. It is useful for text that is modified frequently which includes source code, programs, documentation, graphics, papers, and letters. This is the most basic and most primitive Version Control System known to programmers.&lt;br /&gt;
&lt;br /&gt;
More information about RCS can be found at [http://www.gnu.org/s/rcs/ RCS]&lt;br /&gt;
&lt;br /&gt;
=====  CVS: The Concurrent Versions System (CVS)=====&lt;br /&gt;
&lt;br /&gt;
CVS is a Client-Server software revision control system in the field of software development. Version control software keeps track of all the changes in a set of files, and makes sure that several developers (potentially widely separated in space and/or time) can collaborate in real time. The CVS server runs on Unix-like systems with client software that runs on different operating systems on multiple client machines. It is regarded as the most mature version control system because it has been developed for such a long time and has stood the test of time. A fork project of CVS, CVSNT was created to run CVS on Windows servers, and it is currently being actively developed to increase functionality and make it more usable on Windows platform.&lt;br /&gt;
&lt;br /&gt;
More information about CVS can be found at [http://www.nongnu.org/cvs/ CVS]&lt;br /&gt;
&lt;br /&gt;
=====  SVN: Sub Version (SVN)=====&lt;br /&gt;
&lt;br /&gt;
SVN was created as an alternative to CVS that would be useful to fix some known issues in CVS and also maintaining high compatibility with it. SVN is free and open source with the difference of being distributed under the Apache license as opposed to GNU which distributes licenses for CVS. To prevent the database from being corrupted, SVN adopts a technique called 'Atomic Operations'. Either all of the modifications made to the source are committed or none are committed. Partial changes will not enter the original source. Many developers have switched to SVN as it is a newer technology that starts with the best features of CVS and improves upon them. While branch operations in CVS are expensive and do not really lend themselves to long-term forks in the project, SVN is designed to allow for it. Hence it lends itself better to large forked projects with many directions. Some known disadvantages of SVN includes slower operation speeds and the lack of distributed revision control. Distributed revision control uses a peer-to-peer model as compared to using a centralized server to store code modifications. Peer-to-peer model work better for open source projects which are spread over multiple far away physical locations. The downside to a dedicated server approach is the issue of not having redundancy in the case when the server is down leading to no access to clients.&lt;br /&gt;
&lt;br /&gt;
More information about SVN can be found at [http://subversion.apache.org/ SVN]&lt;br /&gt;
&lt;br /&gt;
=====  Git:=====&lt;br /&gt;
&lt;br /&gt;
Git takes a totally diverse approach that differs greatly in comparison to CVS and SVN. The original concepts for Git were to make a faster, distributed version control system that would openly invalidate conventions and practices used in CVS. It is primarily developed for Linux and has the highest speeds on there. It will also run on other Unix-like systems, and Windows agents are known as msysgit. As there is no centralized server, Git is not very suitable for single developer projects or small teams as the code may not necessarily be available when using a normal computer. Workarounds exist for this problem. Developers look out for Git’s improved speed as a decent trade off for the hassle.&lt;br /&gt;
&lt;br /&gt;
More information about Git can be found at [http://git-scm.com/ Git]&lt;br /&gt;
&lt;br /&gt;
=====  Mercurial:===== &lt;br /&gt;
&lt;br /&gt;
Mercurial is a distributed revision control tool which began close to the same time as Git. Mercurial was originally made to compete with Git for Linux kernel development. Mercurial is primarily implemented in Python as opposed to C, but there are some instances where C is used. This is a major difference compared to other version control systems in Developer's point of view. Users feel that Mercurial is similar to SVN with respect to certain features, as well as being a distributed system. This acts as an advantage for the tool as the learning curve for those already familiar with SVN will be less steep. The documentation for Mercurial is very well compiled and facilitates understanding the differences faster. One of the major drawback to Mercurial is that it does not allow for two parents to be merged. Unlike Git, it uses an extension system rather than using scripts. That may be ideal for some section of programmers, but many find the speed of operation of Git to be a feature they do not want to trade off for anything.&lt;br /&gt;
&lt;br /&gt;
More information about Mercurial can be found at [http://mercurial.selenic.com Mercurial]&lt;br /&gt;
&lt;br /&gt;
==== Basic Features ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Repository model|Repository model&lt;br /&gt;
! #Concurrency model|Concurrency model&lt;br /&gt;
! #Programming Languages|Programming Languages&lt;br /&gt;
! #Scope of Change|Scope of Change&lt;br /&gt;
! #Atomic commits|Atomic commits&lt;br /&gt;
! #File Renames|File Renames&lt;br /&gt;
! #Interactive Commits|Interactive Commits&lt;br /&gt;
! #Partial Checkout/clone|Partial Checkout/clone&lt;br /&gt;
! Platforms supported&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Set of files	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Clear Case	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C, Java, Perl &lt;br /&gt;
|  File &lt;br /&gt;
|  Partial&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Linux, Windows, AIX, Solaris, HP UX, i5/OS, OS/390, z/OS&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C,Perl &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Partial &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| POSIX, Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  Python, C&lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X	&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Feature&lt;br /&gt;
! Feature Description&lt;br /&gt;
|-&lt;br /&gt;
! Repository Model&lt;br /&gt;
| Describes the relationship between various copies of the source code repository.&lt;br /&gt;
|-&lt;br /&gt;
! Concurrency Model&lt;br /&gt;
| Describes how changes to the working copy are managed to prevent simultaneous edits from causing nonsensical data in the repository.&lt;br /&gt;
|-&lt;br /&gt;
! Programming Languages&lt;br /&gt;
| The coding language in which the application is being developed&lt;br /&gt;
|-&lt;br /&gt;
! Scope of Change&lt;br /&gt;
| Describes whether changes are recorded for individual files or for entire directory trees.&lt;br /&gt;
|-&lt;br /&gt;
! Atomic commits&lt;br /&gt;
| Refers to a guarantee that all changes made are merged, or that no change at all will be made.&lt;br /&gt;
|-&lt;br /&gt;
! File Renames&lt;br /&gt;
| Describes whether a system allows files to be renamed while retaining their version history.&lt;br /&gt;
|-&lt;br /&gt;
! Interactive Commits&lt;br /&gt;
| Interactive commits allow the user to cherrypick the patch-hunks that become part of a commit, instead of having only a file-level granularity.&lt;br /&gt;
|-&lt;br /&gt;
! Partial Checkout/clone&lt;br /&gt;
| Ability to check out or clone only a specified subdirectory from a repository.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Basic Commands ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Clone|Clone&lt;br /&gt;
! #Pull|Pull&lt;br /&gt;
! #Push|Push&lt;br /&gt;
! #Checkout|Checkout&lt;br /&gt;
! #Add|Add&lt;br /&gt;
! #Remove|Remove&lt;br /&gt;
! #Merge|Merge&lt;br /&gt;
! #Commit|Commit&lt;br /&gt;
! #Revert|Revert&lt;br /&gt;
! #Rebase|Rebase&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  co &lt;br /&gt;
| Not Supported&lt;br /&gt;
| rcsclean&lt;br /&gt;
| rcsmerge&lt;br /&gt;
|  ci&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| update -j&lt;br /&gt;
|  commit&lt;br /&gt;
| rm, update&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| svnadmin hotcopy	&lt;br /&gt;
| svnadmin load&lt;br /&gt;
| svnadmin dump&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
|  commit&lt;br /&gt;
| revert&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! Clearcase	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| mkelem&lt;br /&gt;
| rmelem&lt;br /&gt;
| merge&lt;br /&gt;
| checkin&lt;br /&gt;
| unco/rmver&lt;br /&gt;
| findmerge&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Fetch&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| checkout&lt;br /&gt;
| rebase&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Pull&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| revert&lt;br /&gt;
| rebase&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Command Description&lt;br /&gt;
|-&lt;br /&gt;
! Clone&lt;br /&gt;
| Create an identical instance of a repository&lt;br /&gt;
|-&lt;br /&gt;
! Pull&lt;br /&gt;
| Download revisions from a remote repository to a local repository&lt;br /&gt;
|-&lt;br /&gt;
! Push&lt;br /&gt;
| Upload revisions from a local repository to a remote repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Create a local working copy from a (remote) repository&lt;br /&gt;
|-&lt;br /&gt;
! Add an element&lt;br /&gt;
| Mark specified files to be added to repository at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Remove an element&lt;br /&gt;
| Mark specified files to be removed at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Merge&lt;br /&gt;
| Apply the differences between two sources to a working copy path&lt;br /&gt;
|-&lt;br /&gt;
! Commit&lt;br /&gt;
| Record changes in the repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Restore working copy file from repository&lt;br /&gt;
|-&lt;br /&gt;
! Rebase&lt;br /&gt;
| Forward-port local commits to the updated upstream head&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Sample Workflow involving Clearcase ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
This section provides an example of a sample workflow used by a programmer to maintain the code in Clearcase.&lt;br /&gt;
&lt;br /&gt;
'''Joining a UCM Project''': After project managers have established the UCM project environment, developers can then set up their work areas.&lt;br /&gt;
This is accomplished by joining the UCM project. More information about this can be found   [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_join_ucm_proj.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Working with views''': A view is the mechanism Rational ClearCase uses to provide access to specific versions of elements or specific parts of code under source control. Rules are the basis to determine which version of each element is visible and accessible through the view for a developer. More information about views can be found [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_views_intro.htm here]&lt;br /&gt;
&lt;br /&gt;
Sample command to create a new view: ct mkview -tag &amp;lt;view_name&amp;gt;&lt;br /&gt;
Sample command to create a new file: ct mkelem &amp;lt;file_name&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Checking in files and checking out files''': Checking files in and out are two basic operations you will perform on a regular basis by a developer. Whenever the developer needs to edit a file, he has to checkout the file. And after completion of coding, he needs to check in the file. More information about the process can be found  [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_file_ci_co.htm here]&lt;br /&gt;
&lt;br /&gt;
Sample command to check out a file: ct co -nc &amp;lt;file_name&amp;gt;&lt;br /&gt;
Sample command to check in a file: ct ci -nc &amp;lt;file_name&amp;gt;  (when you are working in the same branch where the file is checked out).&lt;br /&gt;
&lt;br /&gt;
'''Delivering your work''': After testing your work in your private workspace, and you are sure that your work is error-free, you are ready to submit your work to the integration stream so that other project members can use the latest version of your work. More information about the procedure of delivering code can be found  [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_deliver_wrk.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Merging your work''': After completion, the development branch must be merged to integration branch where software developed by different teams are merged and tested for seamless operation. More information about this procedure can be obtained [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_merge_wrk.htm here]&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1a_br Information about Version Control Systems]&lt;br /&gt;
&lt;br /&gt;
[http://excess.org/article/2008/07/ogre-git-tutorial/ A Video presentation on &amp;quot;Git, The Basics&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://www.youtube.com/watch?v=4XpnKHJAok8 Linus Torvals on Git]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [http://code.google.com/p/support/wiki/DVCSAnalysis Comparison between Git and Mercurial] by Google (summer 2008)&lt;br /&gt;
* [http://www.softeng.rl.ac.uk/media/uploads/publications/2010/03/cvs-svn.pdf Comparison of CVS and Subversion]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Comparison_of_revision_control_software Revision Control Systems Comparison]&lt;br /&gt;
* [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_wflow_cc_proj.htm IBM Software Information Center]&lt;br /&gt;
* [http://schacon.github.com/git/gittutorial.html Git Tutorial]&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50936</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1f rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50936"/>
		<updated>2011-09-25T23:22:23Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Sample Workflow involving Clearcase */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=='''Comparison of Version Control Systems : The Programmer View'''==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Version control also called Sub-Version Control or Revision Control is indispensable in large projects. They make sure that projects are not spinning out of control by letting different programmers work on the project from a different perspective without getting in each other’s way and without doing any damage that cannot be undone.&lt;br /&gt;
&lt;br /&gt;
Some of the most popular version control systems are RCS, SVN, CVS, Mercurial and Git. This article intends to compare these version control systems in a programmer’s viewpoint.&lt;br /&gt;
&lt;br /&gt;
== Choice of Version Control Systems ==&lt;br /&gt;
There are a number of solutions which are available for programmers. We have put together a definitive feature comparison for reference on deciding about the best choice for a project.&lt;br /&gt;
&lt;br /&gt;
The main difference between Version Control Systems is whether they are Client-Server based or peer-to-peer based.  Either they have a centralized repository where code is checked out, edited and checked in with changes, or a setup where the code is frequently updated from different peer sources, a more decentralized network to keep your code updated with changes.&lt;br /&gt;
&lt;br /&gt;
The article further compares all the Version Control Systems like RCS, CVS and Distributed Version Control Systems. Also, the article compares multiple Version Control applications like Mercurial, Git, Clearcase and CVS. Comparison is done in the view of programmer. Various commands used in different applications are discussed.&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Systems==&lt;br /&gt;
[[Image:RCS.png|thumb|150px|alt=Local Version Control]]&lt;br /&gt;
==== Local Version Control ====&lt;br /&gt;
This is a primitive approach of version control. The system operates on single files only.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Revision_Control_System&amp;lt;/ref&amp;gt; It has no way of working with an entire project. Changes to a file are stored as 'deltas' with the aid of a diff utility. Since the approach maintain many copies of files, it is generally cumbersome with reduced efficiency.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:CVS.png|thumb|150px|alt=Centralized Version Control]]&lt;br /&gt;
&lt;br /&gt;
==== Centralized Version Control ====&lt;br /&gt;
CVS implements a Client-Server model to achieve version control. A central repository is maintained which is used by all the developers of a project. Programmers can only have a working copy of the project. The model allows a developer to checkout only the required files or branches. In this model all operations on the code (commit, exchange of code etc.)  involve communication with the central server via a network. This means that these operations are not available when the user is offline. Also  issues in communication with the server may disrupt the development activity.&lt;br /&gt;
Any changes to the project on the server involves an authentication process. Hence only users with certain privileges can commit code on the central server . A developer can checkpoint or create branches on his work only by committing his changes to the central repository.  Since all the developers commit changes on the same repository, it is generally challenging to resolve conflicts when merging or reconciling changes from other branches.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Distributed Version Control ====&lt;br /&gt;
[[Image:File-DVCS.png|thumb|150px|alt=Distributed Version Control]]&lt;br /&gt;
This system implements a peer to peer model to achieve version control. No reference copy of the codebase exist by default. However in practice, an official reference copy is indeed maintained. The model  requires the developers to possess a full blown backup of the repository , including the entire history. As a result, the developers can collaborate directly without the need of a central authority. Hence it allows developers to be productive even when offline. (e.g. when travelling). Also the overhead of communicating with a central repository is overcome in this approach.&lt;br /&gt;
Branching and merging fit much better in this approach as each user has his own branch. The administrator's major work is to merge appropriate changes from branches of different users. This is relatively simpler compared to  the centralized system. As a result, the distributed system scales much better with the number of people.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Summary of Centralized versus Distributed Approach ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Centralized Version Control&lt;br /&gt;
! Distributed Version Control&lt;br /&gt;
|-&lt;br /&gt;
| Client - server model&lt;br /&gt;
| peer- to -peer model&lt;br /&gt;
|-&lt;br /&gt;
| Single central repository.&lt;br /&gt;
| Multiple repositories&lt;br /&gt;
|-&lt;br /&gt;
| Changes can be committed only Online&lt;br /&gt;
| Changes can be committed offline&lt;br /&gt;
|-&lt;br /&gt;
| Allows developer to checkout required files/branches.&lt;br /&gt;
| Complete repository has to be downloaded by the programmer &lt;br /&gt;
|-&lt;br /&gt;
| Merging and Branching operations are complicated as&lt;br /&gt;
multiple programmers work on the same repository at the same time.&lt;br /&gt;
| Branching is simple as each programmer gets a branch&lt;br /&gt;
|-&lt;br /&gt;
| Repository maintenance becomes more challenging with number of people&lt;br /&gt;
| Scales better with number of people&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Applications ==&lt;br /&gt;
==== Overview of Version Control Applications ====&lt;br /&gt;
&lt;br /&gt;
=====  RCS: The Revision Control System (RCS)=====&lt;br /&gt;
&lt;br /&gt;
RCS manages multiple revisions of files. RCS operates on a set of files. RCS automates the process of storing, retrieval, logging, identification, and merging of multiple revisions. It is useful for text that is modified frequently which includes source code, programs, documentation, graphics, papers, and letters. This is the most basic and most primitive Version Control System known to programmers.&lt;br /&gt;
&lt;br /&gt;
More information about RCS can be found at [http://www.gnu.org/s/rcs/ RCS]&lt;br /&gt;
&lt;br /&gt;
=====  CVS: The Concurrent Versions System (CVS)=====&lt;br /&gt;
&lt;br /&gt;
CVS is a Client-Server software revision control system in the field of software development. Version control software keeps track of all the changes in a set of files, and makes sure that several developers (potentially widely separated in space and/or time) can collaborate in real time. The CVS server runs on Unix-like systems with client software that runs on different operating systems on multiple client machines. It is regarded as the most mature version control system because it has been developed for such a long time and has stood the test of time. A fork project of CVS, CVSNT was created to run CVS on Windows servers, and it is currently being actively developed to increase functionality and make it more usable on Windows platform.&lt;br /&gt;
&lt;br /&gt;
More information about CVS can be found at [http://www.nongnu.org/cvs/ CVS]&lt;br /&gt;
&lt;br /&gt;
=====  SVN: Sub Version (SVN)=====&lt;br /&gt;
&lt;br /&gt;
SVN was created as an alternative to CVS that would be useful to fix some known issues in CVS and also maintaining high compatibility with it. SVN is free and open source with the difference of being distributed under the Apache license as opposed to GNU which distributes licenses for CVS. To prevent the database from being corrupted, SVN adopts a technique called 'Atomic Operations'. Either all of the modifications made to the source are committed or none are committed. Partial changes will not enter the original source. Many developers have switched to SVN as it is a newer technology that starts with the best features of CVS and improves upon them. While branch operations in CVS are expensive and do not really lend themselves to long-term forks in the project, SVN is designed to allow for it. Hence it lends itself better to large forked projects with many directions. Some known disadvantages of SVN includes slower operation speeds and the lack of distributed revision control. Distributed revision control uses a peer-to-peer model as compared to using a centralized server to store code modifications. Peer-to-peer model work better for open source projects which are spread over multiple far away physical locations. The downside to a dedicated server approach is the issue of not having redundancy in the case when the server is down leading to no access to clients.&lt;br /&gt;
&lt;br /&gt;
More information about SVN can be found at [http://subversion.apache.org/ SVN]&lt;br /&gt;
&lt;br /&gt;
=====  Git:=====&lt;br /&gt;
&lt;br /&gt;
Git takes a totally diverse approach that differs greatly in comparison to CVS and SVN. The original concepts for Git were to make a faster, distributed version control system that would openly invalidate conventions and practices used in CVS. It is primarily developed for Linux and has the highest speeds on there. It will also run on other Unix-like systems, and Windows agents are known as msysgit. As there is no centralized server, Git is not very suitable for single developer projects or small teams as the code may not necessarily be available when using a normal computer. Workarounds exist for this problem. Developers look out for Git’s improved speed as a decent trade off for the hassle.&lt;br /&gt;
&lt;br /&gt;
More information about Git can be found at [http://git-scm.com/ Git]&lt;br /&gt;
&lt;br /&gt;
=====  Mercurial:===== &lt;br /&gt;
&lt;br /&gt;
Mercurial is a distributed revision control tool which began close to the same time as Git. Mercurial was originally made to compete with Git for Linux kernel development. Mercurial is primarily implemented in Python as opposed to C, but there are some instances where C is used. This is a major difference compared to other version control systems in Developer's point of view. Users feel that Mercurial is similar to SVN with respect to certain features, as well as being a distributed system. This acts as an advantage for the tool as the learning curve for those already familiar with SVN will be less steep. The documentation for Mercurial is very well compiled and facilitates understanding the differences faster. One of the major drawback to Mercurial is that it does not allow for two parents to be merged. Unlike Git, it uses an extension system rather than using scripts. That may be ideal for some section of programmers, but many find the speed of operation of Git to be a feature they do not want to trade off for anything.&lt;br /&gt;
&lt;br /&gt;
More information about Mercurial can be found at [http://mercurial.selenic.com Mercurial]&lt;br /&gt;
&lt;br /&gt;
==== Basic Features ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Repository model|Repository model&lt;br /&gt;
! #Concurrency model|Concurrency model&lt;br /&gt;
! #Programming Languages|Programming Languages&lt;br /&gt;
! #Scope of Change|Scope of Change&lt;br /&gt;
! #Atomic commits|Atomic commits&lt;br /&gt;
! #File Renames|File Renames&lt;br /&gt;
! #Interactive Commits|Interactive Commits&lt;br /&gt;
! #Partial Checkout/clone|Partial Checkout/clone&lt;br /&gt;
! Platforms supported&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Set of files	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Clear Case	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C, Java, Perl &lt;br /&gt;
|  File &lt;br /&gt;
|  Partial&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Linux, Windows, AIX, Solaris, HP UX, i5/OS, OS/390, z/OS&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C,Perl &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Partial &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| POSIX, Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  Python, C&lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X	&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Feature&lt;br /&gt;
! Feature Description&lt;br /&gt;
|-&lt;br /&gt;
! Repository Model&lt;br /&gt;
| Describes the relationship between various copies of the source code repository.&lt;br /&gt;
|-&lt;br /&gt;
! Concurrency Model&lt;br /&gt;
| Describes how changes to the working copy are managed to prevent simultaneous edits from causing nonsensical data in the repository.&lt;br /&gt;
|-&lt;br /&gt;
! Programming Languages&lt;br /&gt;
| The coding language in which the application is being developed&lt;br /&gt;
|-&lt;br /&gt;
! Scope of Change&lt;br /&gt;
| Describes whether changes are recorded for individual files or for entire directory trees.&lt;br /&gt;
|-&lt;br /&gt;
! Atomic commits&lt;br /&gt;
| Refers to a guarantee that all changes made are merged, or that no change at all will be made.&lt;br /&gt;
|-&lt;br /&gt;
! File Renames&lt;br /&gt;
| Describes whether a system allows files to be renamed while retaining their version history.&lt;br /&gt;
|-&lt;br /&gt;
! Interactive Commits&lt;br /&gt;
| Interactive commits allow the user to cherrypick the patch-hunks that become part of a commit, instead of having only a file-level granularity.&lt;br /&gt;
|-&lt;br /&gt;
! Partial Checkout/clone&lt;br /&gt;
| Ability to check out or clone only a specified subdirectory from a repository.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Basic Commands ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Clone|Clone&lt;br /&gt;
! #Pull|Pull&lt;br /&gt;
! #Push|Push&lt;br /&gt;
! #Checkout|Checkout&lt;br /&gt;
! #Add|Add&lt;br /&gt;
! #Remove|Remove&lt;br /&gt;
! #Merge|Merge&lt;br /&gt;
! #Commit|Commit&lt;br /&gt;
! #Revert|Revert&lt;br /&gt;
! #Rebase|Rebase&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  co &lt;br /&gt;
| Not Supported&lt;br /&gt;
| rcsclean&lt;br /&gt;
| rcsmerge&lt;br /&gt;
|  ci&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| update -j&lt;br /&gt;
|  commit&lt;br /&gt;
| rm, update&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| svnadmin hotcopy	&lt;br /&gt;
| svnadmin load&lt;br /&gt;
| svnadmin dump&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
|  commit&lt;br /&gt;
| revert&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! Clearcase	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| mkelem&lt;br /&gt;
| rmelem&lt;br /&gt;
| merge&lt;br /&gt;
| checkin&lt;br /&gt;
| unco/rmver&lt;br /&gt;
| findmerge&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Fetch&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| checkout&lt;br /&gt;
| rebase&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Pull&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| revert&lt;br /&gt;
| rebase&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Command Description&lt;br /&gt;
|-&lt;br /&gt;
! Clone&lt;br /&gt;
| Create an identical instance of a repository&lt;br /&gt;
|-&lt;br /&gt;
! Pull&lt;br /&gt;
| Download revisions from a remote repository to a local repository&lt;br /&gt;
|-&lt;br /&gt;
! Push&lt;br /&gt;
| Upload revisions from a local repository to a remote repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Create a local working copy from a (remote) repository&lt;br /&gt;
|-&lt;br /&gt;
! Add an element&lt;br /&gt;
| Mark specified files to be added to repository at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Remove an element&lt;br /&gt;
| Mark specified files to be removed at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Merge&lt;br /&gt;
| Apply the differences between two sources to a working copy path&lt;br /&gt;
|-&lt;br /&gt;
! Commit&lt;br /&gt;
| Record changes in the repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Restore working copy file from repository&lt;br /&gt;
|-&lt;br /&gt;
! Rebase&lt;br /&gt;
| Forward-port local commits to the updated upstream head&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Sample Workflow involving Clearcase ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
This section provides an example of a sample workflow used by a programmer to maintain the code in Clearcase.&lt;br /&gt;
&lt;br /&gt;
'''Joining a UCM Project''': After project managers have established the UCM project environment, developers can then set up their work areas.&lt;br /&gt;
This is accomplished by joining the UCM project. More information about this can be found   [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_join_ucm_proj.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Working with views''': A view is the mechanism Rational ClearCase uses to provide access to specific versions of elements or specific parts of &lt;br /&gt;
code under source control. Rules are the basis to determine which version of each element is visible and accessible through the view for a developer. More information about views can be found [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_views_intro.htm here]&lt;br /&gt;
Sample command to create a new view: ct mkview -tag &amp;lt;view_name&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Checking in files and checking out files''': Checking files in and out are two basic operations you will perform on a regular basis by a developer. Whenever the developer needs to edit a file, he has to checkout the file. And after completion of coding, he needs to check in the file. More information about the process can be found  [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_file_ci_co.htm here]&lt;br /&gt;
Sample command to check out a file: ct co -nc &amp;lt;file_name&amp;gt;&lt;br /&gt;
Sample command to check in a file: ct ci -nc &amp;lt;file_name&amp;gt;  (when you are working in the same branch where the file is checked out).&lt;br /&gt;
&lt;br /&gt;
'''Delivering your work''': After testing your work in your private workspace, and you are sure that your work is error-free, you are ready to submit your work to the integration stream so that other project members can use the latest version of your work. More information about the procedure of delivering code can be found  [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_deliver_wrk.htm here]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Merging your work''': After completion, the development branch must be merged to integration branch where software developed by different teams are merged and tested for seamless operation. More information about this procedure can be obtained [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_merge_wrk.htm here]&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1a_br Information about Version Control Systems]&lt;br /&gt;
&lt;br /&gt;
[http://excess.org/article/2008/07/ogre-git-tutorial/ A Video presentation on &amp;quot;Git, The Basics&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://www.youtube.com/watch?v=4XpnKHJAok8 Linus Torvals on Git]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [http://code.google.com/p/support/wiki/DVCSAnalysis Comparison between Git and Mercurial] by Google (summer 2008)&lt;br /&gt;
* [http://www.softeng.rl.ac.uk/media/uploads/publications/2010/03/cvs-svn.pdf Comparison of CVS and Subversion]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Comparison_of_revision_control_software Revision Control Systems Comparison]&lt;br /&gt;
* [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_wflow_cc_proj.htm IBM Software Information Center]&lt;br /&gt;
* [http://schacon.github.com/git/gittutorial.html Git Tutorial]&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50935</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1f rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50935"/>
		<updated>2011-09-25T23:21:42Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* Sample Workflow involving Clearcase */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=='''Comparison of Version Control Systems : The Programmer View'''==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Version control also called Sub-Version Control or Revision Control is indispensable in large projects. They make sure that projects are not spinning out of control by letting different programmers work on the project from a different perspective without getting in each other’s way and without doing any damage that cannot be undone.&lt;br /&gt;
&lt;br /&gt;
Some of the most popular version control systems are RCS, SVN, CVS, Mercurial and Git. This article intends to compare these version control systems in a programmer’s viewpoint.&lt;br /&gt;
&lt;br /&gt;
== Choice of Version Control Systems ==&lt;br /&gt;
There are a number of solutions which are available for programmers. We have put together a definitive feature comparison for reference on deciding about the best choice for a project.&lt;br /&gt;
&lt;br /&gt;
The main difference between Version Control Systems is whether they are Client-Server based or peer-to-peer based.  Either they have a centralized repository where code is checked out, edited and checked in with changes, or a setup where the code is frequently updated from different peer sources, a more decentralized network to keep your code updated with changes.&lt;br /&gt;
&lt;br /&gt;
The article further compares all the Version Control Systems like RCS, CVS and Distributed Version Control Systems. Also, the article compares multiple Version Control applications like Mercurial, Git, Clearcase and CVS. Comparison is done in the view of programmer. Various commands used in different applications are discussed.&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Systems==&lt;br /&gt;
[[Image:RCS.png|thumb|150px|alt=Local Version Control]]&lt;br /&gt;
==== Local Version Control ====&lt;br /&gt;
This is a primitive approach of version control. The system operates on single files only.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Revision_Control_System&amp;lt;/ref&amp;gt; It has no way of working with an entire project. Changes to a file are stored as 'deltas' with the aid of a diff utility. Since the approach maintain many copies of files, it is generally cumbersome with reduced efficiency.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:CVS.png|thumb|150px|alt=Centralized Version Control]]&lt;br /&gt;
&lt;br /&gt;
==== Centralized Version Control ====&lt;br /&gt;
CVS implements a Client-Server model to achieve version control. A central repository is maintained which is used by all the developers of a project. Programmers can only have a working copy of the project. The model allows a developer to checkout only the required files or branches. In this model all operations on the code (commit, exchange of code etc.)  involve communication with the central server via a network. This means that these operations are not available when the user is offline. Also  issues in communication with the server may disrupt the development activity.&lt;br /&gt;
Any changes to the project on the server involves an authentication process. Hence only users with certain privileges can commit code on the central server . A developer can checkpoint or create branches on his work only by committing his changes to the central repository.  Since all the developers commit changes on the same repository, it is generally challenging to resolve conflicts when merging or reconciling changes from other branches.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Distributed Version Control ====&lt;br /&gt;
[[Image:File-DVCS.png|thumb|150px|alt=Distributed Version Control]]&lt;br /&gt;
This system implements a peer to peer model to achieve version control. No reference copy of the codebase exist by default. However in practice, an official reference copy is indeed maintained. The model  requires the developers to possess a full blown backup of the repository , including the entire history. As a result, the developers can collaborate directly without the need of a central authority. Hence it allows developers to be productive even when offline. (e.g. when travelling). Also the overhead of communicating with a central repository is overcome in this approach.&lt;br /&gt;
Branching and merging fit much better in this approach as each user has his own branch. The administrator's major work is to merge appropriate changes from branches of different users. This is relatively simpler compared to  the centralized system. As a result, the distributed system scales much better with the number of people.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Summary of Centralized versus Distributed Approach ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Centralized Version Control&lt;br /&gt;
! Distributed Version Control&lt;br /&gt;
|-&lt;br /&gt;
| Client - server model&lt;br /&gt;
| peer- to -peer model&lt;br /&gt;
|-&lt;br /&gt;
| Single central repository.&lt;br /&gt;
| Multiple repositories&lt;br /&gt;
|-&lt;br /&gt;
| Changes can be committed only Online&lt;br /&gt;
| Changes can be committed offline&lt;br /&gt;
|-&lt;br /&gt;
| Allows developer to checkout required files/branches.&lt;br /&gt;
| Complete repository has to be downloaded by the programmer &lt;br /&gt;
|-&lt;br /&gt;
| Merging and Branching operations are complicated as&lt;br /&gt;
multiple programmers work on the same repository at the same time.&lt;br /&gt;
| Branching is simple as each programmer gets a branch&lt;br /&gt;
|-&lt;br /&gt;
| Repository maintenance becomes more challenging with number of people&lt;br /&gt;
| Scales better with number of people&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Applications ==&lt;br /&gt;
==== Overview of Version Control Applications ====&lt;br /&gt;
&lt;br /&gt;
=====  RCS: The Revision Control System (RCS)=====&lt;br /&gt;
&lt;br /&gt;
RCS manages multiple revisions of files. RCS operates on a set of files. RCS automates the process of storing, retrieval, logging, identification, and merging of multiple revisions. It is useful for text that is modified frequently which includes source code, programs, documentation, graphics, papers, and letters. This is the most basic and most primitive Version Control System known to programmers.&lt;br /&gt;
&lt;br /&gt;
More information about RCS can be found at [http://www.gnu.org/s/rcs/ RCS]&lt;br /&gt;
&lt;br /&gt;
=====  CVS: The Concurrent Versions System (CVS)=====&lt;br /&gt;
&lt;br /&gt;
CVS is a Client-Server software revision control system in the field of software development. Version control software keeps track of all the changes in a set of files, and makes sure that several developers (potentially widely separated in space and/or time) can collaborate in real time. The CVS server runs on Unix-like systems with client software that runs on different operating systems on multiple client machines. It is regarded as the most mature version control system because it has been developed for such a long time and has stood the test of time. A fork project of CVS, CVSNT was created to run CVS on Windows servers, and it is currently being actively developed to increase functionality and make it more usable on Windows platform.&lt;br /&gt;
&lt;br /&gt;
More information about CVS can be found at [http://www.nongnu.org/cvs/ CVS]&lt;br /&gt;
&lt;br /&gt;
=====  SVN: Sub Version (SVN)=====&lt;br /&gt;
&lt;br /&gt;
SVN was created as an alternative to CVS that would be useful to fix some known issues in CVS and also maintaining high compatibility with it. SVN is free and open source with the difference of being distributed under the Apache license as opposed to GNU which distributes licenses for CVS. To prevent the database from being corrupted, SVN adopts a technique called 'Atomic Operations'. Either all of the modifications made to the source are committed or none are committed. Partial changes will not enter the original source. Many developers have switched to SVN as it is a newer technology that starts with the best features of CVS and improves upon them. While branch operations in CVS are expensive and do not really lend themselves to long-term forks in the project, SVN is designed to allow for it. Hence it lends itself better to large forked projects with many directions. Some known disadvantages of SVN includes slower operation speeds and the lack of distributed revision control. Distributed revision control uses a peer-to-peer model as compared to using a centralized server to store code modifications. Peer-to-peer model work better for open source projects which are spread over multiple far away physical locations. The downside to a dedicated server approach is the issue of not having redundancy in the case when the server is down leading to no access to clients.&lt;br /&gt;
&lt;br /&gt;
More information about SVN can be found at [http://subversion.apache.org/ SVN]&lt;br /&gt;
&lt;br /&gt;
=====  Git:=====&lt;br /&gt;
&lt;br /&gt;
Git takes a totally diverse approach that differs greatly in comparison to CVS and SVN. The original concepts for Git were to make a faster, distributed version control system that would openly invalidate conventions and practices used in CVS. It is primarily developed for Linux and has the highest speeds on there. It will also run on other Unix-like systems, and Windows agents are known as msysgit. As there is no centralized server, Git is not very suitable for single developer projects or small teams as the code may not necessarily be available when using a normal computer. Workarounds exist for this problem. Developers look out for Git’s improved speed as a decent trade off for the hassle.&lt;br /&gt;
&lt;br /&gt;
More information about Git can be found at [http://git-scm.com/ Git]&lt;br /&gt;
&lt;br /&gt;
=====  Mercurial:===== &lt;br /&gt;
&lt;br /&gt;
Mercurial is a distributed revision control tool which began close to the same time as Git. Mercurial was originally made to compete with Git for Linux kernel development. Mercurial is primarily implemented in Python as opposed to C, but there are some instances where C is used. This is a major difference compared to other version control systems in Developer's point of view. Users feel that Mercurial is similar to SVN with respect to certain features, as well as being a distributed system. This acts as an advantage for the tool as the learning curve for those already familiar with SVN will be less steep. The documentation for Mercurial is very well compiled and facilitates understanding the differences faster. One of the major drawback to Mercurial is that it does not allow for two parents to be merged. Unlike Git, it uses an extension system rather than using scripts. That may be ideal for some section of programmers, but many find the speed of operation of Git to be a feature they do not want to trade off for anything.&lt;br /&gt;
&lt;br /&gt;
More information about Mercurial can be found at [http://mercurial.selenic.com Mercurial]&lt;br /&gt;
&lt;br /&gt;
==== Basic Features ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Repository model|Repository model&lt;br /&gt;
! #Concurrency model|Concurrency model&lt;br /&gt;
! #Programming Languages|Programming Languages&lt;br /&gt;
! #Scope of Change|Scope of Change&lt;br /&gt;
! #Atomic commits|Atomic commits&lt;br /&gt;
! #File Renames|File Renames&lt;br /&gt;
! #Interactive Commits|Interactive Commits&lt;br /&gt;
! #Partial Checkout/clone|Partial Checkout/clone&lt;br /&gt;
! Platforms supported&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Set of files	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Clear Case	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C, Java, Perl &lt;br /&gt;
|  File &lt;br /&gt;
|  Partial&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Linux, Windows, AIX, Solaris, HP UX, i5/OS, OS/390, z/OS&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C,Perl &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Partial &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| POSIX, Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  Python, C&lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X	&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Feature&lt;br /&gt;
! Feature Description&lt;br /&gt;
|-&lt;br /&gt;
! Repository Model&lt;br /&gt;
| Describes the relationship between various copies of the source code repository.&lt;br /&gt;
|-&lt;br /&gt;
! Concurrency Model&lt;br /&gt;
| Describes how changes to the working copy are managed to prevent simultaneous edits from causing nonsensical data in the repository.&lt;br /&gt;
|-&lt;br /&gt;
! Programming Languages&lt;br /&gt;
| The coding language in which the application is being developed&lt;br /&gt;
|-&lt;br /&gt;
! Scope of Change&lt;br /&gt;
| Describes whether changes are recorded for individual files or for entire directory trees.&lt;br /&gt;
|-&lt;br /&gt;
! Atomic commits&lt;br /&gt;
| Refers to a guarantee that all changes made are merged, or that no change at all will be made.&lt;br /&gt;
|-&lt;br /&gt;
! File Renames&lt;br /&gt;
| Describes whether a system allows files to be renamed while retaining their version history.&lt;br /&gt;
|-&lt;br /&gt;
! Interactive Commits&lt;br /&gt;
| Interactive commits allow the user to cherrypick the patch-hunks that become part of a commit, instead of having only a file-level granularity.&lt;br /&gt;
|-&lt;br /&gt;
! Partial Checkout/clone&lt;br /&gt;
| Ability to check out or clone only a specified subdirectory from a repository.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Basic Commands ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Clone|Clone&lt;br /&gt;
! #Pull|Pull&lt;br /&gt;
! #Push|Push&lt;br /&gt;
! #Checkout|Checkout&lt;br /&gt;
! #Add|Add&lt;br /&gt;
! #Remove|Remove&lt;br /&gt;
! #Merge|Merge&lt;br /&gt;
! #Commit|Commit&lt;br /&gt;
! #Revert|Revert&lt;br /&gt;
! #Rebase|Rebase&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  co &lt;br /&gt;
| Not Supported&lt;br /&gt;
| rcsclean&lt;br /&gt;
| rcsmerge&lt;br /&gt;
|  ci&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| update -j&lt;br /&gt;
|  commit&lt;br /&gt;
| rm, update&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| svnadmin hotcopy	&lt;br /&gt;
| svnadmin load&lt;br /&gt;
| svnadmin dump&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
|  commit&lt;br /&gt;
| revert&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! Clearcase	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| mkelem&lt;br /&gt;
| rmelem&lt;br /&gt;
| merge&lt;br /&gt;
| checkin&lt;br /&gt;
| unco/rmver&lt;br /&gt;
| findmerge&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Fetch&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| checkout&lt;br /&gt;
| rebase&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Pull&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| revert&lt;br /&gt;
| rebase&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Command Description&lt;br /&gt;
|-&lt;br /&gt;
! Clone&lt;br /&gt;
| Create an identical instance of a repository&lt;br /&gt;
|-&lt;br /&gt;
! Pull&lt;br /&gt;
| Download revisions from a remote repository to a local repository&lt;br /&gt;
|-&lt;br /&gt;
! Push&lt;br /&gt;
| Upload revisions from a local repository to a remote repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Create a local working copy from a (remote) repository&lt;br /&gt;
|-&lt;br /&gt;
! Add an element&lt;br /&gt;
| Mark specified files to be added to repository at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Remove an element&lt;br /&gt;
| Mark specified files to be removed at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Merge&lt;br /&gt;
| Apply the differences between two sources to a working copy path&lt;br /&gt;
|-&lt;br /&gt;
! Commit&lt;br /&gt;
| Record changes in the repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Restore working copy file from repository&lt;br /&gt;
|-&lt;br /&gt;
! Rebase&lt;br /&gt;
| Forward-port local commits to the updated upstream head&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Sample Workflow involving Clearcase ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
This section provides an example of a sample workflow used by a programmer to maintain the code in Clearcase.&lt;br /&gt;
&lt;br /&gt;
'''Joining a UCM Project''': After project managers have established the UCM project environment, developers can then set up their work areas.&lt;br /&gt;
This is accomplished by joining the UCM project. More information about this can be found   [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_join_ucm_proj.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Working with views''': A view is the mechanism Rational ClearCase uses to provide access to specific versions of elements or specific parts of &lt;br /&gt;
code under source control. Rules are the basis to determine which version of each element is visible and accessible through the view for a developer. More information about views can be found [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_views_intro.htm here]&lt;br /&gt;
Sample command to create a new view: ct mkview -tag &amp;lt;view_name&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Checking in files and checking out files''': Checking files in and out are two basic operations you will perform on a regular basis by a developer. Whenever the developer needs to edit a file, he has to checkout the file. And after completion of coding, he needs to check in the file. More information about the process can be found  [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_file_ci_co.htm here]&lt;br /&gt;
Sample command to check out a file: ct co -nc &amp;lt;file_name&amp;gt;&lt;br /&gt;
Sample command to check in a file: ct ci -nc &amp;lt;file_name&amp;gt;  (when you are working in the same branch where the file is checked out).&lt;br /&gt;
&lt;br /&gt;
'''Delivering your work''': After testing your work in your private workspace, and you are sure that your work is error-free, you are ready to submit your work to the integration stream so that other project members can use the latest version of your work. More information about the procedure of delivering code can be found  [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_deliver_wrk.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Merging your work''': After completion, the development branch must be merged to integration branch where software developed by different teams are merged and tested for seamless operation. More information about this procedure can be obtained [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_merge_wrk.htm here]&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1a_br Information about Version Control Systems]&lt;br /&gt;
&lt;br /&gt;
[http://excess.org/article/2008/07/ogre-git-tutorial/ A Video presentation on &amp;quot;Git, The Basics&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://www.youtube.com/watch?v=4XpnKHJAok8 Linus Torvals on Git]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [http://code.google.com/p/support/wiki/DVCSAnalysis Comparison between Git and Mercurial] by Google (summer 2008)&lt;br /&gt;
* [http://www.softeng.rl.ac.uk/media/uploads/publications/2010/03/cvs-svn.pdf Comparison of CVS and Subversion]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Comparison_of_revision_control_software Revision Control Systems Comparison]&lt;br /&gt;
* [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_wflow_cc_proj.htm IBM Software Information Center]&lt;br /&gt;
* [http://schacon.github.com/git/gittutorial.html Git Tutorial]&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50895</id>
		<title>CSC/ECE 517 Fall 2011/ch1 1f rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_1f_rs&amp;diff=50895"/>
		<updated>2011-09-25T23:02:54Z</updated>

		<summary type="html">&lt;p&gt;Sarao: /* See also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=='''Comparison of Version Control Systems : The Programmer View'''==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Version control also called Sub-Version Control or Revision Control is indispensable in large projects. They make sure that projects are not spinning out of control by letting different programmers work on the project from a different perspective without getting in each other’s way and without doing any damage that cannot be undone.&lt;br /&gt;
&lt;br /&gt;
Some of the most popular version control systems are RCS, SVN, CVS, Mercurial and Git. This article intends to compare these version control systems in a programmer’s viewpoint.&lt;br /&gt;
&lt;br /&gt;
== Choice of Version Control Systems ==&lt;br /&gt;
There are a number of solutions which are available for programmers. We have put together a definitive feature comparison for reference on deciding about the best choice for a project.&lt;br /&gt;
&lt;br /&gt;
The main difference between Version Control Systems is whether they are Client-Server based or peer-to-peer based.  Either they have a centralized repository where code is checked out, edited and checked in with changes, or a setup where the code is frequently updated from different peer sources, a more decentralized network to keep your code updated with changes.&lt;br /&gt;
&lt;br /&gt;
The article further compares all the Version Control Systems like RCS, CVS and Distributed Version Control Systems. Also, the article compares multiple Version Control applications like Mercurial, Git, Clearcase and CVS. Comparison is done in the view of programmer. Various commands used in different applications are discussed.&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Systems==&lt;br /&gt;
==== Local Version Control ====&lt;br /&gt;
This is a primitive approach of version control. The system operates on single files only.&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Revision_Control_System&amp;lt;/ref&amp;gt; It has no way of working with an entire project. Changes to a file are stored as 'deltas' with the aid of a diff utility. Since the approach maintain many copies of files, it is generally cumbersome with reduced efficiency.&lt;br /&gt;
&lt;br /&gt;
[[Image:CVS.png|thumb|150px|alt=Distributed Version Control]]&lt;br /&gt;
==== Centralized Version Control ====&lt;br /&gt;
CVS implements a Client-Server model to achieve version control. A central repository is maintained which is used by all the developers of a project. Programmers can only have a working copy of the project. The model allows a developer to checkout only the required files or branches. In this model all operations on the code (commit, exchange of code etc.)  involve communication with the central server via a network. This means that these operations are not available when the user is offline. Also  issues in communication with the server may disrupt the development activity.&lt;br /&gt;
Any changes to the project on the server involves an authentication process. Hence only users with certain privileges can commit code on the central server . A developer can checkpoint or create branches on his work only by committing his changes to the central repository.  Since all the developers commit changes on the same repository, it is generally challenging to resolve conflicts when merging or reconciling changes from other branches.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Distributed Version Control ====&lt;br /&gt;
[[Image:File-DVCS.png|thumb|150px|alt=Distributed Version Control]]&lt;br /&gt;
This system implements a peer to peer model to achieve version control. No reference copy of the codebase exist by default. However in practice, an official reference copy is indeed maintained. The model  requires the developers to possess a full blown backup of the repository , including the entire history. As a result, the developers can collaborate directly without the need of a central authority. Hence it allows developers to be productive even when offline. (e.g. when travelling). Also the overhead of communicating with a central repository is overcome in this approach.&lt;br /&gt;
Branching and merging fit much better in this approach as each user has his own branch. The administrator's major work is to merge appropriate changes from branches of different users. This is relatively simpler compared to  the centralized system. As a result, the distributed system scales much better with the number of people.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Summary of Centralized versus Distributed Approach ====&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;font-size: 100%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Centralized Version Control&lt;br /&gt;
! Distributed Version Control&lt;br /&gt;
|-&lt;br /&gt;
| Client - server model&lt;br /&gt;
| peer- to -peer model&lt;br /&gt;
|-&lt;br /&gt;
| Single central repository.&lt;br /&gt;
| Multiple repositories&lt;br /&gt;
|-&lt;br /&gt;
| Changes can be committed only Online&lt;br /&gt;
| Changes can be committed offline&lt;br /&gt;
|-&lt;br /&gt;
| Allows developer to checkout required files/branches.&lt;br /&gt;
| Complete repository has to be downloaded by the programmer &lt;br /&gt;
|-&lt;br /&gt;
| Merging and Branching operations are complicated as&lt;br /&gt;
multiple programmers work on the same repository at the same time.&lt;br /&gt;
| Branching is simple as each programmer gets a branch&lt;br /&gt;
|-&lt;br /&gt;
| Repository maintenance becomes more challenging with number of people&lt;br /&gt;
| Scales better with number of people&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Comparison of Version Control Applications ==&lt;br /&gt;
==== Overview of Version Control Applications ====&lt;br /&gt;
&lt;br /&gt;
=====  RCS: The Revision Control System (RCS)=====&lt;br /&gt;
&lt;br /&gt;
RCS manages multiple revisions of files. RCS operates on a set of files. RCS automates the process of storing, retrieval, logging, identification, and merging of multiple revisions. It is useful for text that is modified frequently which includes source code, programs, documentation, graphics, papers, and letters. This is the most basic and most primitive Version Control System known to programmers.&lt;br /&gt;
&lt;br /&gt;
More information about RCS can be found at [http://www.gnu.org/s/rcs/ RCS]&lt;br /&gt;
&lt;br /&gt;
=====  CVS: The Concurrent Versions System (CVS)=====&lt;br /&gt;
&lt;br /&gt;
CVS is a Client-Server software revision control system in the field of software development. Version control software keeps track of all the changes in a set of files, and makes sure that several developers (potentially widely separated in space and/or time) can collaborate in real time. The CVS server runs on Unix-like systems with client software that runs on different operating systems on multiple client machines. It is regarded as the most mature version control system because it has been developed for such a long time and has stood the test of time. A fork project of CVS, CVSNT was created to run CVS on Windows servers, and it is currently being actively developed to increase functionality and make it more usable on Windows platform.&lt;br /&gt;
&lt;br /&gt;
More information about CVS can be found at [http://www.nongnu.org/cvs/ CVS]&lt;br /&gt;
&lt;br /&gt;
=====  SVN: Sub Version (SVN)=====&lt;br /&gt;
&lt;br /&gt;
SVN was created as an alternative to CVS that would be useful to fix some known issues in CVS and also maintaining high compatibility with it. SVN is free and open source with the difference of being distributed under the Apache license as opposed to GNU which distributes licenses for CVS. To prevent the database from being corrupted, SVN adopts a technique called 'Atomic Operations'. Either all of the modifications made to the source are committed or none are committed. Partial changes will not enter the original source. Many developers have switched to SVN as it is a newer technology that starts with the best features of CVS and improves upon them. While branch operations in CVS are expensive and do not really lend themselves to long-term forks in the project, SVN is designed to allow for it. Hence it lends itself better to large forked projects with many directions. Some known disadvantages of SVN includes slower operation speeds and the lack of distributed revision control. Distributed revision control uses a peer-to-peer model as compared to using a centralized server to store code modifications. Peer-to-peer model work better for open source projects which are spread over multiple far away physical locations. The downside to a dedicated server approach is the issue of not having redundancy in the case when the server is down leading to no access to clients.&lt;br /&gt;
&lt;br /&gt;
More information about SVN can be found at [http://subversion.apache.org/ SVN]&lt;br /&gt;
&lt;br /&gt;
=====  Git:=====&lt;br /&gt;
&lt;br /&gt;
Git takes a totally diverse approach that differs greatly in comparison to CVS and SVN. The original concepts for Git were to make a faster, distributed version control system that would openly invalidate conventions and practices used in CVS. It is primarily developed for Linux and has the highest speeds on there. It will also run on other Unix-like systems, and Windows agents are known as msysgit. As there is no centralized server, Git is not very suitable for single developer projects or small teams as the code may not necessarily be available when using a normal computer. Workarounds exist for this problem. Developers look out for Git’s improved speed as a decent trade off for the hassle.&lt;br /&gt;
&lt;br /&gt;
More information about Git can be found at [http://git-scm.com/ Git]&lt;br /&gt;
&lt;br /&gt;
=====  Mercurial:===== &lt;br /&gt;
&lt;br /&gt;
Mercurial is a distributed revision control tool which began close to the same time as Git. Mercurial was originally made to compete with Git for Linux kernel development. Mercurial is primarily implemented in Python as opposed to C, but there are some instances where C is used. This is a major difference compared to other version control systems in Developer's point of view. Users feel that Mercurial is similar to SVN with respect to certain features, as well as being a distributed system. This acts as an advantage for the tool as the learning curve for those already familiar with SVN will be less steep. The documentation for Mercurial is very well compiled and facilitates understanding the differences faster. One of the major drawback to Mercurial is that it does not allow for two parents to be merged. Unlike Git, it uses an extension system rather than using scripts. That may be ideal for some section of programmers, but many find the speed of operation of Git to be a feature they do not want to trade off for anything.&lt;br /&gt;
&lt;br /&gt;
More information about Mercurial can be found at [http://mercurial.selenic.com Mercurial]&lt;br /&gt;
&lt;br /&gt;
==== Basic Features ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Repository model|Repository model&lt;br /&gt;
! #Concurrency model|Concurrency model&lt;br /&gt;
! #Programming Languages|Programming Languages&lt;br /&gt;
! #Scope of Change|Scope of Change&lt;br /&gt;
! #Atomic commits|Atomic commits&lt;br /&gt;
! #File Renames|File Renames&lt;br /&gt;
! #Interactive Commits|Interactive Commits&lt;br /&gt;
! #Partial Checkout/clone|Partial Checkout/clone&lt;br /&gt;
! Platforms supported&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Set of files	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C &lt;br /&gt;
|  File &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Clear Case	&lt;br /&gt;
| Client-Server	&lt;br /&gt;
| Merge or Lock&lt;br /&gt;
|  C, Java, Perl &lt;br /&gt;
|  File &lt;br /&gt;
|  Partial&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
|  Yes &lt;br /&gt;
| Linux, Windows, AIX, Solaris, HP UX, i5/OS, OS/390, z/OS&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  C,Perl &lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Partial &lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| POSIX, Windows, Mac OS X&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Distributed	&lt;br /&gt;
| Merge&lt;br /&gt;
|  Python, C&lt;br /&gt;
|  Tree &lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes&lt;br /&gt;
|  Yes &lt;br /&gt;
|  No &lt;br /&gt;
| Unix-like, Microsoft Windows|Windows, Mac OS X	&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Feature&lt;br /&gt;
! Feature Description&lt;br /&gt;
|-&lt;br /&gt;
! Repository Model&lt;br /&gt;
| Describes the relationship between various copies of the source code repository.&lt;br /&gt;
|-&lt;br /&gt;
! Concurrency Model&lt;br /&gt;
| Describes how changes to the working copy are managed to prevent simultaneous edits from causing nonsensical data in the repository.&lt;br /&gt;
|-&lt;br /&gt;
! Programming Languages&lt;br /&gt;
| The coding language in which the application is being developed&lt;br /&gt;
|-&lt;br /&gt;
! Scope of Change&lt;br /&gt;
| Describes whether changes are recorded for individual files or for entire directory trees.&lt;br /&gt;
|-&lt;br /&gt;
! Atomic commits&lt;br /&gt;
| Refers to a guarantee that all changes made are merged, or that no change at all will be made.&lt;br /&gt;
|-&lt;br /&gt;
! File Renames&lt;br /&gt;
| Describes whether a system allows files to be renamed while retaining their version history.&lt;br /&gt;
|-&lt;br /&gt;
! Interactive Commits&lt;br /&gt;
| Interactive commits allow the user to cherrypick the patch-hunks that become part of a commit, instead of having only a file-level granularity.&lt;br /&gt;
|-&lt;br /&gt;
! Partial Checkout/clone&lt;br /&gt;
| Ability to check out or clone only a specified subdirectory from a repository.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Basic Commands ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Software&lt;br /&gt;
! #Clone|Clone&lt;br /&gt;
! #Pull|Pull&lt;br /&gt;
! #Push|Push&lt;br /&gt;
! #Checkout|Checkout&lt;br /&gt;
! #Add|Add&lt;br /&gt;
! #Remove|Remove&lt;br /&gt;
! #Merge|Merge&lt;br /&gt;
! #Commit|Commit&lt;br /&gt;
! #Revert|Revert&lt;br /&gt;
! #Rebase|Rebase&lt;br /&gt;
|-&lt;br /&gt;
! RCS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  co &lt;br /&gt;
| Not Supported&lt;br /&gt;
| rcsclean&lt;br /&gt;
| rcsmerge&lt;br /&gt;
|  ci&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! CVS	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| update -j&lt;br /&gt;
|  commit&lt;br /&gt;
| rm, update&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! SVN	&lt;br /&gt;
| svnadmin hotcopy	&lt;br /&gt;
| svnadmin load&lt;br /&gt;
| svnadmin dump&lt;br /&gt;
|  checkout &lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
|  commit&lt;br /&gt;
| revert&lt;br /&gt;
| Not Supported&lt;br /&gt;
|-&lt;br /&gt;
! Clearcase	&lt;br /&gt;
| Not Supported	&lt;br /&gt;
| Not Supported&lt;br /&gt;
| Not Supported&lt;br /&gt;
|  checkout &lt;br /&gt;
| mkelem&lt;br /&gt;
| rmelem&lt;br /&gt;
| merge&lt;br /&gt;
| checkin&lt;br /&gt;
| unco/rmver&lt;br /&gt;
| findmerge&lt;br /&gt;
|-&lt;br /&gt;
! Git	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Fetch&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| checkout&lt;br /&gt;
| rebase&lt;br /&gt;
|-&lt;br /&gt;
! Mercurial	&lt;br /&gt;
| Clone	&lt;br /&gt;
| Pull&lt;br /&gt;
| Push&lt;br /&gt;
| Clone&lt;br /&gt;
| add&lt;br /&gt;
| rm&lt;br /&gt;
| merge&lt;br /&gt;
| commit&lt;br /&gt;
| revert&lt;br /&gt;
| rebase&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 95%; text-align: left; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Command&lt;br /&gt;
! Command Description&lt;br /&gt;
|-&lt;br /&gt;
! Clone&lt;br /&gt;
| Create an identical instance of a repository&lt;br /&gt;
|-&lt;br /&gt;
! Pull&lt;br /&gt;
| Download revisions from a remote repository to a local repository&lt;br /&gt;
|-&lt;br /&gt;
! Push&lt;br /&gt;
| Upload revisions from a local repository to a remote repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Create a local working copy from a (remote) repository&lt;br /&gt;
|-&lt;br /&gt;
! Add an element&lt;br /&gt;
| Mark specified files to be added to repository at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Remove an element&lt;br /&gt;
| Mark specified files to be removed at next commit&lt;br /&gt;
|-&lt;br /&gt;
! Merge&lt;br /&gt;
| Apply the differences between two sources to a working copy path&lt;br /&gt;
|-&lt;br /&gt;
! Commit&lt;br /&gt;
| Record changes in the repository&lt;br /&gt;
|-&lt;br /&gt;
! Checkout&lt;br /&gt;
| Restore working copy file from repository&lt;br /&gt;
|-&lt;br /&gt;
! Rebase&lt;br /&gt;
| Forward-port local commits to the updated upstream head&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Sample Workflow involving Clearcase ====&lt;br /&gt;
------------------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
This section provides an example of a sample workflow used by a programmer to maintain the code in Clearcase.&lt;br /&gt;
&lt;br /&gt;
'''Joining a UCM Project''': After project managers have established the UCM project environment, developers can then set up their work areas.&lt;br /&gt;
This is accomplished by joining the UCM project. More information about this can be found   [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_join_ucm_proj.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Working with views''': A view is the mechanism Rational ClearCase uses to provide access to specific versions of elements or specific parts of &lt;br /&gt;
code under source control. Rules are the basis to determine which version of each element is visible and accessible through the view for a developer. More information about views can be found [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_views_intro.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Checking in files and checking out files''': Checking files in and out are two basic operations you will perform on a regular basis by a developer. Whenever the developer needs to edit a file, he has to checkout the file. And after completion of coding, he needs to check in the file. More information about the process can be found  [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_file_ci_co.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Delivering your work''': After testing your work in your private workspace, and you are sure that your work is error-free, you are ready to submit your work to the integration stream so that other project members can use the latest version of your work. More information about the procedure of delivering code can be found  [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_deliver_wrk.htm here]&lt;br /&gt;
&lt;br /&gt;
'''Merging your work''': After completion, the development branch must be merged to integration branch where software developed by different teams are merged and tested for seamless operation. More information about this procedure can be obtained [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/topic/com.ibm.rational.clearcase.tutorial.doc/a_merge_wrk.htm here]&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
[http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1a_br Information about Version Control Systems]&lt;br /&gt;
&lt;br /&gt;
[http://excess.org/article/2008/07/ogre-git-tutorial/ A Video presentation on &amp;quot;Git, The Basics&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://www.youtube.com/watch?v=4XpnKHJAok8 Linus Torvals on Git]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [http://code.google.com/p/support/wiki/DVCSAnalysis Comparison between Git and Mercurial] by Google (summer 2008)&lt;br /&gt;
* [http://www.softeng.rl.ac.uk/media/uploads/publications/2010/03/cvs-svn.pdf Comparison of CVS and Subversion]&lt;br /&gt;
* [http://en.wikipedia.org/wiki/Comparison_of_revision_control_software Revision Control Systems Comparison]&lt;br /&gt;
* [http://publib.boulder.ibm.com/infocenter/cchelp/v7r0m1/index.jsp?topic=/com.ibm.rational.clearcase.tutorial.doc/a_wflow_cc_proj.htm IBM Software Information Center]&lt;br /&gt;
* [http://schacon.github.com/git/gittutorial.html Git Tutorial]&lt;/div&gt;</summary>
		<author><name>Sarao</name></author>
	</entry>
</feed>