<?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=Amosti</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=Amosti"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Amosti"/>
	<updated>2026-06-03T16:49:03Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15900</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 6 esb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15900"/>
		<updated>2008-07-26T23:37:24Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Why use Protected Variation? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Protected Variation==&lt;br /&gt;
&lt;br /&gt;
The term protected variations comes from the need for the programmer to predict what in the code is likely vary in future adaptions of the code and prevent those variations from damaging the intended functionality of the code.  At some time in the future the code is likely going to need additional functionality.  The adding of functionality should not prevent the sections of code that currently work from continuing to do so while also allowing the new code to integrate and use the existing structure efficiently.&lt;br /&gt;
&lt;br /&gt;
Protected variation is often linked to the Open-Closed Principle (OCP) and Information Hiding which all tend to have a final goal of code that is adaptable without damaging the integrity of original code and without alterations beyond additions to the original code.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
When programming code, care should be made to protect against change to the existing code.  Information should be kept out of the grasp of components that could damage integrity.  New design should be implemented at designated &amp;quot;variation&amp;quot; and &amp;quot;evolution&amp;quot; points.  The pattern of ''Protected Variation'' involves identifying points of predicted variation and creating a stable interface around them.&lt;br /&gt;
&lt;br /&gt;
Protected Variation is closely knitted with the Open-Closed Principle.  When points of predicted variation are identified, the code becomes easy to extend/adapt (&amp;quot;open&amp;quot;), while the stable interfaces around these points provides for &amp;quot;closed&amp;quot; code, wherein modification will not affect clients.  This stable interface surrounding the points of change also promotes the concept of Information Hiding, in that design information is hidden from other modules at the points of likely change.&lt;br /&gt;
&lt;br /&gt;
The term Open-Closed Principal is credited to having orginated from Dr. Bertrand Meyer and appears in his 1988 book &amp;lt;u&amp;gt;Object Oriented Software Construction&amp;lt;/u&amp;gt;.  This original version of the Open-Closed Principle achieved protection from variation by relying heavily on original class inheritance.  This is often referred to as the Meyer's Open-Closed Principle.&lt;br /&gt;
&lt;br /&gt;
In more recent years the Open-Closed principle is linked more closely to the use of polymorphism to achieve protection from variations.  In this system abstract interfaces are commonly used and multitudes of implementations are used polymorphically as substitutes for the existing implementation which leaves the original code intact and operating as intended.&lt;br /&gt;
&lt;br /&gt;
== Why use Protected Variation? ==&lt;br /&gt;
Protected Variation is a principle which can save much time and money.  It is important to reduce the introduction of new defects, and in order to do so, the impact of change to a piece of code on the overall program must be minimized.  Protected Variations help to introduce low coupling (i.e. minimizing the degree to which each program module relies on each one of the other modules), which in turn reduce change impact.&lt;br /&gt;
&lt;br /&gt;
==How would you classify it ==&lt;br /&gt;
&lt;br /&gt;
==Coding Examples==&lt;br /&gt;
&lt;br /&gt;
The following are typical mechanisms which illustrate protected variations:&lt;br /&gt;
&lt;br /&gt;
'''Polymorphism''':&lt;br /&gt;
&lt;br /&gt;
In the below example from Larman's book, protected variations are illustrated via the use of polymorphism.  Many tax calculator systems need to be integrated, and these external tax calculators provide for a point of instability in the program.  Polymorphism allows for various ''ITaxCalculatorAdapter'' implementations to be created and collaborated with while protecting the system from variations in the external APIs.&lt;br /&gt;
&lt;br /&gt;
[[Image:wiki3polymorphism.JPG]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Inheritance''':&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Indirection''':&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Confusion with Data Encapsulation==&lt;br /&gt;
While the concept of protected variations is similar to the concept of information hiding, it should not be confused with data encapsulation.  Data encapsulation is merely a technique used to hide design information.  Protected variations can involve many techniques wherein the overall goal is to protect code from other areas of the code which are decided to be highly volatile and likely to have the capability of adversely affecting the program.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
==''References''==&lt;br /&gt;
Larman, Craig.  [http://books.google.com/books?id=r8i-4En_aa4C Applying UML and Patterns].  Prentice Hall PTR, 2001.&lt;br /&gt;
&lt;br /&gt;
Larman, Craig.  [http://ieeexplore.ieee.org.www.lib.ncsu.edu:2048/iel5/52/19948/00922731.pdf?tp=&amp;amp;arnumber=922731&amp;amp;isnumber=19948 Protected Variation: The Importance of Being Closed].  &amp;lt;u&amp;gt;IEEE Software&amp;lt;/u&amp;gt; 18.3 (2001): 89-91.&lt;br /&gt;
&lt;br /&gt;
==''External Links''==&lt;br /&gt;
&lt;br /&gt;
http://codecourse.sourceforge.net/materials/The-Importance-of-Being-Closed.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/Files/ooprimer.ppt#288,9,OCP Example &lt;br /&gt;
&lt;br /&gt;
http://www.cs.wright.edu/~tkprasad/courses/cs480/L3OOP.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.csci.csusb.edu/dick/cs375/16q.txt&lt;br /&gt;
&lt;br /&gt;
http://www.cs.wustl.edu/~schmidt/PDF/design-principles4.pdf&lt;br /&gt;
&lt;br /&gt;
http://davidhayden.com/blog/dave/archive/2005/06/04/1096.aspx&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Open/closed_principle&lt;br /&gt;
&lt;br /&gt;
http://www.openmymind.net/FoundationsOfProgramming.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.cs.umu.se/kurser/TDBC31/Overheads/L8-10_Advanced.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Open-Closed-Principle&lt;br /&gt;
&lt;br /&gt;
Good General link:&lt;br /&gt;
http://www.netobjectives.com/resources/books/design-patterns-explained/review-questions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next very good:&lt;br /&gt;
http://www.augustana.ab.ca/~mohrj/courses/2007.fall/csc220/presentations/25_GRASP2.ppt#270,5,Fig. 25.2  Applying polymorphism to Monopoly&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Information-hiding&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/nblog/CategoryView,category,ruby.aspx&lt;br /&gt;
&lt;br /&gt;
http://www.cse.ohio-state.edu/~rountev/757/pdf/Principles.pdf&lt;br /&gt;
&lt;br /&gt;
[[CSC/ECE 517 Summer 2008/wiki3 Assignment|Back to the assignment page]]&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15899</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 6 esb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15899"/>
		<updated>2008-07-26T23:36:29Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Coding Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Protected Variation==&lt;br /&gt;
&lt;br /&gt;
The term protected variations comes from the need for the programmer to predict what in the code is likely vary in future adaptions of the code and prevent those variations from damaging the intended functionality of the code.  At some time in the future the code is likely going to need additional functionality.  The adding of functionality should not prevent the sections of code that currently work from continuing to do so while also allowing the new code to integrate and use the existing structure efficiently.&lt;br /&gt;
&lt;br /&gt;
Protected variation is often linked to the Open-Closed Principle (OCP) and Information Hiding which all tend to have a final goal of code that is adaptable without damaging the integrity of original code and without alterations beyond additions to the original code.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
When programming code, care should be made to protect against change to the existing code.  Information should be kept out of the grasp of components that could damage integrity.  New design should be implemented at designated &amp;quot;variation&amp;quot; and &amp;quot;evolution&amp;quot; points.  The pattern of ''Protected Variation'' involves identifying points of predicted variation and creating a stable interface around them.&lt;br /&gt;
&lt;br /&gt;
Protected Variation is closely knitted with the Open-Closed Principle.  When points of predicted variation are identified, the code becomes easy to extend/adapt (&amp;quot;open&amp;quot;), while the stable interfaces around these points provides for &amp;quot;closed&amp;quot; code, wherein modification will not affect clients.  This stable interface surrounding the points of change also promotes the concept of Information Hiding, in that design information is hidden from other modules at the points of likely change.&lt;br /&gt;
&lt;br /&gt;
The term Open-Closed Principal is credited to having orginated from Dr. Bertrand Meyer and appears in his 1988 book &amp;lt;u&amp;gt;Object Oriented Software Construction&amp;lt;/u&amp;gt;.  This original version of the Open-Closed Principle achieved protection from variation by relying heavily on original class inheritance.  This is often referred to as the Meyer's Open-Closed Principle.&lt;br /&gt;
&lt;br /&gt;
In more recent years the Open-Closed principle is linked more closely to the use of polymorphism to achieve protection from variations.  In this system abstract interfaces are commonly used and multitudes of implementations are used polymorphically as substitutes for the existing implementation which leaves the original code intact and operating as intended.&lt;br /&gt;
&lt;br /&gt;
== Why use Protected Variation? ==&lt;br /&gt;
Protected Variation is a principle which can save much time and money.  It is important to reduce the introduction of new defects, and in order to do so, the impact of change to a piece of code on the overall program must be minimized.  Protected Variations help to introduce low coupling (i.e. minimizing the degree to which each program module relies on each one of the other modules), which in turn reduce change impact.&lt;br /&gt;
&lt;br /&gt;
* Example 1 &lt;br /&gt;
&lt;br /&gt;
* Example 2&lt;br /&gt;
&lt;br /&gt;
==How would you classify it ==&lt;br /&gt;
&lt;br /&gt;
==Coding Examples==&lt;br /&gt;
&lt;br /&gt;
The following are typical mechanisms which illustrate protected variations:&lt;br /&gt;
&lt;br /&gt;
'''Polymorphism''':&lt;br /&gt;
&lt;br /&gt;
In the below example from Larman's book, protected variations are illustrated via the use of polymorphism.  Many tax calculator systems need to be integrated, and these external tax calculators provide for a point of instability in the program.  Polymorphism allows for various ''ITaxCalculatorAdapter'' implementations to be created and collaborated with while protecting the system from variations in the external APIs.&lt;br /&gt;
&lt;br /&gt;
[[Image:wiki3polymorphism.JPG]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Inheritance''':&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Indirection''':&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Confusion with Data Encapsulation==&lt;br /&gt;
While the concept of protected variations is similar to the concept of information hiding, it should not be confused with data encapsulation.  Data encapsulation is merely a technique used to hide design information.  Protected variations can involve many techniques wherein the overall goal is to protect code from other areas of the code which are decided to be highly volatile and likely to have the capability of adversely affecting the program.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
==''References''==&lt;br /&gt;
Larman, Craig.  [http://books.google.com/books?id=r8i-4En_aa4C Applying UML and Patterns].  Prentice Hall PTR, 2001.&lt;br /&gt;
&lt;br /&gt;
Larman, Craig.  [http://ieeexplore.ieee.org.www.lib.ncsu.edu:2048/iel5/52/19948/00922731.pdf?tp=&amp;amp;arnumber=922731&amp;amp;isnumber=19948 Protected Variation: The Importance of Being Closed].  &amp;lt;u&amp;gt;IEEE Software&amp;lt;/u&amp;gt; 18.3 (2001): 89-91.&lt;br /&gt;
&lt;br /&gt;
==''External Links''==&lt;br /&gt;
&lt;br /&gt;
http://codecourse.sourceforge.net/materials/The-Importance-of-Being-Closed.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/Files/ooprimer.ppt#288,9,OCP Example &lt;br /&gt;
&lt;br /&gt;
http://www.cs.wright.edu/~tkprasad/courses/cs480/L3OOP.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.csci.csusb.edu/dick/cs375/16q.txt&lt;br /&gt;
&lt;br /&gt;
http://www.cs.wustl.edu/~schmidt/PDF/design-principles4.pdf&lt;br /&gt;
&lt;br /&gt;
http://davidhayden.com/blog/dave/archive/2005/06/04/1096.aspx&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Open/closed_principle&lt;br /&gt;
&lt;br /&gt;
http://www.openmymind.net/FoundationsOfProgramming.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.cs.umu.se/kurser/TDBC31/Overheads/L8-10_Advanced.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Open-Closed-Principle&lt;br /&gt;
&lt;br /&gt;
Good General link:&lt;br /&gt;
http://www.netobjectives.com/resources/books/design-patterns-explained/review-questions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next very good:&lt;br /&gt;
http://www.augustana.ab.ca/~mohrj/courses/2007.fall/csc220/presentations/25_GRASP2.ppt#270,5,Fig. 25.2  Applying polymorphism to Monopoly&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Information-hiding&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/nblog/CategoryView,category,ruby.aspx&lt;br /&gt;
&lt;br /&gt;
http://www.cse.ohio-state.edu/~rountev/757/pdf/Principles.pdf&lt;br /&gt;
&lt;br /&gt;
[[CSC/ECE 517 Summer 2008/wiki3 Assignment|Back to the assignment page]]&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15886</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 6 esb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15886"/>
		<updated>2008-07-26T23:19:17Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Coding Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Protected Variation==&lt;br /&gt;
&lt;br /&gt;
The term protected variations comes from the need for the programmer to predict what in the code is likely vary in future adaptions of the code and prevent those variations from damaging the intended functionality of the code.  At some time in the future the code is likely going to need additional functionality.  The adding of functionality should not prevent the sections of code that currently work from continuing to do so while also allowing the new code to integrate and use the existing structure efficiently.&lt;br /&gt;
&lt;br /&gt;
Protected variation is often linked to the Open-Closed Principle (OCP) and Information Hiding which all tend to have a final goal of code that is adaptable without damaging the integrity of original code and without alterations beyond additions to the original code.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
When programming code, care should be made to protect against change to the existing code.  Information should be kept out of the grasp of components that could damage integrity.  New design should be implemented at designated &amp;quot;variation&amp;quot; and &amp;quot;evolution&amp;quot; points.  The pattern of ''Protected Variation'' involves identifying points of predicted variation and creating a stable interface around them.&lt;br /&gt;
&lt;br /&gt;
Protected Variation is closely knitted with the Open-Closed Principle.  When points of predicted variation are identified, the code becomes easy to extend/adapt (&amp;quot;open&amp;quot;), while the stable interfaces around these points provides for &amp;quot;closed&amp;quot; code, wherein modification will not affect clients.  This stable interface surrounding the points of change also promotes the concept of Information Hiding, in that design information is hidden from other modules at the points of likely change.&lt;br /&gt;
&lt;br /&gt;
The term Open-Closed Principal is credited to having orginated from Dr. Bertrand Meyer and appears in his 1988 book &amp;lt;u&amp;gt;Object Oriented Software Construction&amp;lt;/u&amp;gt;.  This original version of the Open-Closed Principle achieved protection from variation by relying heavily on original class inheritance.  This is often referred to as the Meyer's Open-Closed Principle.&lt;br /&gt;
&lt;br /&gt;
In more recent years the Open-Closed principle is linked more closely to the use of polymorphism to achieve protection from variations.  In this system abstract interfaces are commonly used and multitudes of implementations are used polymorphically as substitutes for the existing implementation which leaves the original code intact and operating as intended.&lt;br /&gt;
&lt;br /&gt;
== Why use Protected Variation? ==&lt;br /&gt;
Protected Variation is a principle which can save much time and money.  It is important to reduce the introduction of new defects, and in order to do so, the impact of change to a piece of code on the overall program must be minimized.  Protected Variations help to introduce low coupling (i.e. minimizing the degree to which each program module relies on each one of the other modules), which in turn reduce change impact.&lt;br /&gt;
&lt;br /&gt;
* Example 1 &lt;br /&gt;
&lt;br /&gt;
* Example 2&lt;br /&gt;
&lt;br /&gt;
==How would you classify it ==&lt;br /&gt;
&lt;br /&gt;
==Coding Examples==&lt;br /&gt;
&lt;br /&gt;
The following are typical mechanisms which illustrate protected variations:&lt;br /&gt;
&lt;br /&gt;
'''Polymorphism''':&lt;br /&gt;
&lt;br /&gt;
In the below example from Larman's book, protected variations are illustrated via the use of polymorphism.  Many tax calculator systems need to be integrated, and these external tax calculators provide for a point of instability in the program.  Polymorphism allows for various ''ITaxCalculatorAdapter'' implementations to be created and collaborated with while protecting the system from variations in the external APIs.&lt;br /&gt;
&lt;br /&gt;
[[Image:wiki3polymorphism.JPG]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Inheritance''':&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Indirection''':&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
==''References''==&lt;br /&gt;
Larman, Craig.  [http://books.google.com/books?id=r8i-4En_aa4C Applying UML and Patterns].  Prentice Hall PTR, 2001.&lt;br /&gt;
&lt;br /&gt;
Larman, Craig.  [http://ieeexplore.ieee.org.www.lib.ncsu.edu:2048/iel5/52/19948/00922731.pdf?tp=&amp;amp;arnumber=922731&amp;amp;isnumber=19948 Protected Variation: The Importance of Being Closed].  &amp;lt;u&amp;gt;IEEE Software&amp;lt;/u&amp;gt; 18.3 (2001): 89-91.&lt;br /&gt;
&lt;br /&gt;
==''External Links''==&lt;br /&gt;
&lt;br /&gt;
http://codecourse.sourceforge.net/materials/The-Importance-of-Being-Closed.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/Files/ooprimer.ppt#288,9,OCP Example &lt;br /&gt;
&lt;br /&gt;
http://www.cs.wright.edu/~tkprasad/courses/cs480/L3OOP.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.csci.csusb.edu/dick/cs375/16q.txt&lt;br /&gt;
&lt;br /&gt;
http://www.cs.wustl.edu/~schmidt/PDF/design-principles4.pdf&lt;br /&gt;
&lt;br /&gt;
http://davidhayden.com/blog/dave/archive/2005/06/04/1096.aspx&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Open/closed_principle&lt;br /&gt;
&lt;br /&gt;
http://www.openmymind.net/FoundationsOfProgramming.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.cs.umu.se/kurser/TDBC31/Overheads/L8-10_Advanced.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Open-Closed-Principle&lt;br /&gt;
&lt;br /&gt;
Good General link:&lt;br /&gt;
http://www.netobjectives.com/resources/books/design-patterns-explained/review-questions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next very good:&lt;br /&gt;
http://www.augustana.ab.ca/~mohrj/courses/2007.fall/csc220/presentations/25_GRASP2.ppt#270,5,Fig. 25.2  Applying polymorphism to Monopoly&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Information-hiding&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/nblog/CategoryView,category,ruby.aspx&lt;br /&gt;
&lt;br /&gt;
http://www.cse.ohio-state.edu/~rountev/757/pdf/Principles.pdf&lt;br /&gt;
&lt;br /&gt;
[[CSC/ECE 517 Summer 2008/wiki3 Assignment|Back to the assignment page]]&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15876</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 6 esb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15876"/>
		<updated>2008-07-26T22:55:43Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Coding Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Protected Variation==&lt;br /&gt;
&lt;br /&gt;
The term protected variations comes from the need for the programmer to predict what in the code is likely vary in future adaptions of the code and prevent those variations from damaging the intended functionality of the code.  At some time in the future the code is likely going to need additional functionality.  The adding of functionality should not prevent the sections of code that currently work from continuing to do so while also allowing the new code to integrate and use the existing structure efficiently.&lt;br /&gt;
&lt;br /&gt;
Protected variation is often linked to the Open-Closed Principle (OCP) and Information Hiding which all tend to have a final goal of code that is adaptable without damaging the integrity of original code and without alterations beyond additions to the original code.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
When programming code, care should be made to protect against change to the existing code.  Information should be kept out of the grasp of components that could damage integrity.  New design should be implemented at designated &amp;quot;variation&amp;quot; and &amp;quot;evolution&amp;quot; points.  The pattern of ''Protected Variation'' involves identifying points of predicted variation and creating a stable interface around them.&lt;br /&gt;
&lt;br /&gt;
Protected Variation is closely knitted with the Open-Closed Principle.  When points of predicted variation are identified, the code becomes easy to extend/adapt (&amp;quot;open&amp;quot;), while the stable interfaces around these points provides for &amp;quot;closed&amp;quot; code, wherein modification will not affect clients.  This stable interface surrounding the points of change also promotes the concept of Information Hiding, in that design information is hidden from other modules at the points of likely change.&lt;br /&gt;
&lt;br /&gt;
The term Open-Closed Principal is credited to having orginated from Dr. Bertrand Meyer and appears in his 1988 book &amp;lt;u&amp;gt;Object Oriented Software Construction&amp;lt;/u&amp;gt;.  This original version of the Open-Closed Principle achieved protection from variation by relying heavily on original class inheritance.  This is often referred to as the Meyer's Open-Closed Principle.&lt;br /&gt;
&lt;br /&gt;
In more recent years the Open-Closed principle is linked more closely to the use of polymorphism to achieve protection from variations.  In this system abstract interfaces are commonly used and multitudes of implementations are used polymorphically as substitutes for the existing implementation which leaves the original code intact and operating as intended.&lt;br /&gt;
&lt;br /&gt;
== Why use Protected Variation? ==&lt;br /&gt;
Protected Variation is a principle which can save much time and money.  It is important to reduce the introduction of new defects, and in order to do so, the impact of change to a piece of code on the overall program must be minimized.  Protected Variations help to introduce low coupling (i.e. minimizing the degree to which each program module relies on each one of the other modules), which in turn reduce change impact.&lt;br /&gt;
&lt;br /&gt;
* Example 1 &lt;br /&gt;
&lt;br /&gt;
* Example 2&lt;br /&gt;
&lt;br /&gt;
==How would you classify it ==&lt;br /&gt;
&lt;br /&gt;
==Coding Examples==&lt;br /&gt;
&lt;br /&gt;
The following are typical mechanisms which illustrate protected variations:&lt;br /&gt;
&lt;br /&gt;
'''Polymorphism''':&lt;br /&gt;
&lt;br /&gt;
In the below example from Larman's book, protected variations are illustrated via the use of polymorphism.  Many tax calculator systems need to be integrated, and these external tax calculators provide for a point of instability in the program.  Polymorphism allows for various ''ITaxCalculatorAdapter'' implementations to be created and collaborated with while protecting the system from variations in the external APIs.&lt;br /&gt;
&lt;br /&gt;
[[Image:wiki3polymorphism.JPG]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Inheritance''':&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Encapsulation''':&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Indirection''':&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
==''References''==&lt;br /&gt;
Larman, Craig.  [http://books.google.com/books?id=r8i-4En_aa4C Applying UML and Patterns].  Prentice Hall PTR, 2001.&lt;br /&gt;
&lt;br /&gt;
Larman, Craig.  [http://ieeexplore.ieee.org.www.lib.ncsu.edu:2048/iel5/52/19948/00922731.pdf?tp=&amp;amp;arnumber=922731&amp;amp;isnumber=19948 Protected Variation: The Importance of Being Closed].  &amp;lt;u&amp;gt;IEEE Software&amp;lt;/u&amp;gt; 18.3 (2001): 89-91.&lt;br /&gt;
&lt;br /&gt;
==''External Links''==&lt;br /&gt;
&lt;br /&gt;
http://codecourse.sourceforge.net/materials/The-Importance-of-Being-Closed.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/Files/ooprimer.ppt#288,9,OCP Example &lt;br /&gt;
&lt;br /&gt;
http://www.cs.wright.edu/~tkprasad/courses/cs480/L3OOP.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.csci.csusb.edu/dick/cs375/16q.txt&lt;br /&gt;
&lt;br /&gt;
http://www.cs.wustl.edu/~schmidt/PDF/design-principles4.pdf&lt;br /&gt;
&lt;br /&gt;
http://davidhayden.com/blog/dave/archive/2005/06/04/1096.aspx&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Open/closed_principle&lt;br /&gt;
&lt;br /&gt;
http://www.openmymind.net/FoundationsOfProgramming.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.cs.umu.se/kurser/TDBC31/Overheads/L8-10_Advanced.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Open-Closed-Principle&lt;br /&gt;
&lt;br /&gt;
Good General link:&lt;br /&gt;
http://www.netobjectives.com/resources/books/design-patterns-explained/review-questions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next very good:&lt;br /&gt;
http://www.augustana.ab.ca/~mohrj/courses/2007.fall/csc220/presentations/25_GRASP2.ppt#270,5,Fig. 25.2  Applying polymorphism to Monopoly&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Information-hiding&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/nblog/CategoryView,category,ruby.aspx&lt;br /&gt;
&lt;br /&gt;
http://www.cse.ohio-state.edu/~rountev/757/pdf/Principles.pdf&lt;br /&gt;
&lt;br /&gt;
[[CSC/ECE 517 Summer 2008/wiki3 Assignment|Back to the assignment page]]&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15875</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 6 esb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15875"/>
		<updated>2008-07-26T22:53:18Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Coding Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Protected Variation==&lt;br /&gt;
&lt;br /&gt;
The term protected variations comes from the need for the programmer to predict what in the code is likely vary in future adaptions of the code and prevent those variations from damaging the intended functionality of the code.  At some time in the future the code is likely going to need additional functionality.  The adding of functionality should not prevent the sections of code that currently work from continuing to do so while also allowing the new code to integrate and use the existing structure efficiently.&lt;br /&gt;
&lt;br /&gt;
Protected variation is often linked to the Open-Closed Principle (OCP) and Information Hiding which all tend to have a final goal of code that is adaptable without damaging the integrity of original code and without alterations beyond additions to the original code.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
When programming code, care should be made to protect against change to the existing code.  Information should be kept out of the grasp of components that could damage integrity.  New design should be implemented at designated &amp;quot;variation&amp;quot; and &amp;quot;evolution&amp;quot; points.  The pattern of ''Protected Variation'' involves identifying points of predicted variation and creating a stable interface around them.&lt;br /&gt;
&lt;br /&gt;
Protected Variation is closely knitted with the Open-Closed Principle.  When points of predicted variation are identified, the code becomes easy to extend/adapt (&amp;quot;open&amp;quot;), while the stable interfaces around these points provides for &amp;quot;closed&amp;quot; code, wherein modification will not affect clients.  This stable interface surrounding the points of change also promotes the concept of Information Hiding, in that design information is hidden from other modules at the points of likely change.&lt;br /&gt;
&lt;br /&gt;
The term Open-Closed Principal is credited to having orginated from Dr. Bertrand Meyer and appears in his 1988 book &amp;lt;u&amp;gt;Object Oriented Software Construction&amp;lt;/u&amp;gt;.  This original version of the Open-Closed Principle achieved protection from variation by relying heavily on original class inheritance.  This is often referred to as the Meyer's Open-Closed Principle.&lt;br /&gt;
&lt;br /&gt;
In more recent years the Open-Closed principle is linked more closely to the use of polymorphism to achieve protection from variations.  In this system abstract interfaces are commonly used and multitudes of implementations are used polymorphically as substitutes for the existing implementation which leaves the original code intact and operating as intended.&lt;br /&gt;
&lt;br /&gt;
== Why use Protected Variation? ==&lt;br /&gt;
Protected Variation is a principle which can save much time and money.  It is important to reduce the introduction of new defects, and in order to do so, the impact of change to a piece of code on the overall program must be minimized.  Protected Variations help to introduce low coupling (i.e. minimizing the degree to which each program module relies on each one of the other modules), which in turn reduce change impact.&lt;br /&gt;
&lt;br /&gt;
* Example 1 &lt;br /&gt;
&lt;br /&gt;
* Example 2&lt;br /&gt;
&lt;br /&gt;
==How would you classify it ==&lt;br /&gt;
&lt;br /&gt;
==Coding Examples==&lt;br /&gt;
The typical mechanisms for protecting a system against variations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Polymorphism:&lt;br /&gt;
&lt;br /&gt;
In the below example from Larman's book, protected variations are illustrated via the use of polymorphism.  Many tax calculator systems need to be integrated, and these external tax calculators provide for a point of instability in the program.  Polymorphism allows for various ''ITaxCalculatorAdapter'' implementations to be created and collaborated with while protecting the system from variations in the external APIs.&lt;br /&gt;
&lt;br /&gt;
[[Image:wiki3polymorphism.JPG]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Inheritance example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Encapsulation example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Indirection example:&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
==''References''==&lt;br /&gt;
Larman, Craig.  [http://books.google.com/books?id=r8i-4En_aa4C Applying UML and Patterns].  Prentice Hall PTR, 2001.&lt;br /&gt;
&lt;br /&gt;
Larman, Craig.  [http://ieeexplore.ieee.org.www.lib.ncsu.edu:2048/iel5/52/19948/00922731.pdf?tp=&amp;amp;arnumber=922731&amp;amp;isnumber=19948 Protected Variation: The Importance of Being Closed].  &amp;lt;u&amp;gt;IEEE Software&amp;lt;/u&amp;gt; 18.3 (2001): 89-91.&lt;br /&gt;
&lt;br /&gt;
==''External Links''==&lt;br /&gt;
&lt;br /&gt;
http://codecourse.sourceforge.net/materials/The-Importance-of-Being-Closed.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/Files/ooprimer.ppt#288,9,OCP Example &lt;br /&gt;
&lt;br /&gt;
http://www.cs.wright.edu/~tkprasad/courses/cs480/L3OOP.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.csci.csusb.edu/dick/cs375/16q.txt&lt;br /&gt;
&lt;br /&gt;
http://www.cs.wustl.edu/~schmidt/PDF/design-principles4.pdf&lt;br /&gt;
&lt;br /&gt;
http://davidhayden.com/blog/dave/archive/2005/06/04/1096.aspx&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Open/closed_principle&lt;br /&gt;
&lt;br /&gt;
http://www.openmymind.net/FoundationsOfProgramming.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.cs.umu.se/kurser/TDBC31/Overheads/L8-10_Advanced.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Open-Closed-Principle&lt;br /&gt;
&lt;br /&gt;
Good General link:&lt;br /&gt;
http://www.netobjectives.com/resources/books/design-patterns-explained/review-questions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next very good:&lt;br /&gt;
http://www.augustana.ab.ca/~mohrj/courses/2007.fall/csc220/presentations/25_GRASP2.ppt#270,5,Fig. 25.2  Applying polymorphism to Monopoly&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Information-hiding&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/nblog/CategoryView,category,ruby.aspx&lt;br /&gt;
&lt;br /&gt;
http://www.cse.ohio-state.edu/~rountev/757/pdf/Principles.pdf&lt;br /&gt;
&lt;br /&gt;
[[CSC/ECE 517 Summer 2008/wiki3 Assignment|Back to the assignment page]]&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15874</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 6 esb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15874"/>
		<updated>2008-07-26T22:52:40Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Coding Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Protected Variation==&lt;br /&gt;
&lt;br /&gt;
The term protected variations comes from the need for the programmer to predict what in the code is likely vary in future adaptions of the code and prevent those variations from damaging the intended functionality of the code.  At some time in the future the code is likely going to need additional functionality.  The adding of functionality should not prevent the sections of code that currently work from continuing to do so while also allowing the new code to integrate and use the existing structure efficiently.&lt;br /&gt;
&lt;br /&gt;
Protected variation is often linked to the Open-Closed Principle (OCP) and Information Hiding which all tend to have a final goal of code that is adaptable without damaging the integrity of original code and without alterations beyond additions to the original code.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
When programming code, care should be made to protect against change to the existing code.  Information should be kept out of the grasp of components that could damage integrity.  New design should be implemented at designated &amp;quot;variation&amp;quot; and &amp;quot;evolution&amp;quot; points.  The pattern of ''Protected Variation'' involves identifying points of predicted variation and creating a stable interface around them.&lt;br /&gt;
&lt;br /&gt;
Protected Variation is closely knitted with the Open-Closed Principle.  When points of predicted variation are identified, the code becomes easy to extend/adapt (&amp;quot;open&amp;quot;), while the stable interfaces around these points provides for &amp;quot;closed&amp;quot; code, wherein modification will not affect clients.  This stable interface surrounding the points of change also promotes the concept of Information Hiding, in that design information is hidden from other modules at the points of likely change.&lt;br /&gt;
&lt;br /&gt;
The term Open-Closed Principal is credited to having orginated from Dr. Bertrand Meyer and appears in his 1988 book &amp;lt;u&amp;gt;Object Oriented Software Construction&amp;lt;/u&amp;gt;.  This original version of the Open-Closed Principle achieved protection from variation by relying heavily on original class inheritance.  This is often referred to as the Meyer's Open-Closed Principle.&lt;br /&gt;
&lt;br /&gt;
In more recent years the Open-Closed principle is linked more closely to the use of polymorphism to achieve protection from variations.  In this system abstract interfaces are commonly used and multitudes of implementations are used polymorphically as substitutes for the existing implementation which leaves the original code intact and operating as intended.&lt;br /&gt;
&lt;br /&gt;
== Why use Protected Variation? ==&lt;br /&gt;
Protected Variation is a principle which can save much time and money.  It is important to reduce the introduction of new defects, and in order to do so, the impact of change to a piece of code on the overall program must be minimized.  Protected Variations help to introduce low coupling (i.e. minimizing the degree to which each program module relies on each one of the other modules), which in turn reduce change impact.&lt;br /&gt;
&lt;br /&gt;
* Example 1 &lt;br /&gt;
&lt;br /&gt;
* Example 2&lt;br /&gt;
&lt;br /&gt;
==How would you classify it ==&lt;br /&gt;
&lt;br /&gt;
==Coding Examples==&lt;br /&gt;
The typical mechanisms for protecting a system against variations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Polymorphism:&lt;br /&gt;
In the below example from Larman's book, protected variations are illustrated via the use of polymorphism.  Many tax calculator systems need to be integrated, and these external tax calculators provide for a point of instability in the program.  Polymorphism allows for various ''ITaxCalculatorAdapter'' implementations to be created and collaborated with while protecting the system from variations in the external APIs.&lt;br /&gt;
&lt;br /&gt;
[[Image:wiki3polymorphism.JPG]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Inheritance example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Encapsulation example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Indirection example:&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
==''References''==&lt;br /&gt;
Larman, Craig.  [http://books.google.com/books?id=r8i-4En_aa4C Applying UML and Patterns].  Prentice Hall PTR, 2001.&lt;br /&gt;
&lt;br /&gt;
Larman, Craig.  [http://ieeexplore.ieee.org.www.lib.ncsu.edu:2048/iel5/52/19948/00922731.pdf?tp=&amp;amp;arnumber=922731&amp;amp;isnumber=19948 Protected Variation: The Importance of Being Closed].  &amp;lt;u&amp;gt;IEEE Software&amp;lt;/u&amp;gt; 18.3 (2001): 89-91.&lt;br /&gt;
&lt;br /&gt;
==''External Links''==&lt;br /&gt;
&lt;br /&gt;
http://codecourse.sourceforge.net/materials/The-Importance-of-Being-Closed.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/Files/ooprimer.ppt#288,9,OCP Example &lt;br /&gt;
&lt;br /&gt;
http://www.cs.wright.edu/~tkprasad/courses/cs480/L3OOP.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.csci.csusb.edu/dick/cs375/16q.txt&lt;br /&gt;
&lt;br /&gt;
http://www.cs.wustl.edu/~schmidt/PDF/design-principles4.pdf&lt;br /&gt;
&lt;br /&gt;
http://davidhayden.com/blog/dave/archive/2005/06/04/1096.aspx&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Open/closed_principle&lt;br /&gt;
&lt;br /&gt;
http://www.openmymind.net/FoundationsOfProgramming.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.cs.umu.se/kurser/TDBC31/Overheads/L8-10_Advanced.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Open-Closed-Principle&lt;br /&gt;
&lt;br /&gt;
Good General link:&lt;br /&gt;
http://www.netobjectives.com/resources/books/design-patterns-explained/review-questions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next very good:&lt;br /&gt;
http://www.augustana.ab.ca/~mohrj/courses/2007.fall/csc220/presentations/25_GRASP2.ppt#270,5,Fig. 25.2  Applying polymorphism to Monopoly&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Information-hiding&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/nblog/CategoryView,category,ruby.aspx&lt;br /&gt;
&lt;br /&gt;
http://www.cse.ohio-state.edu/~rountev/757/pdf/Principles.pdf&lt;br /&gt;
&lt;br /&gt;
[[CSC/ECE 517 Summer 2008/wiki3 Assignment|Back to the assignment page]]&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Wiki3polymorphism.JPG&amp;diff=15871</id>
		<title>File:Wiki3polymorphism.JPG</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Wiki3polymorphism.JPG&amp;diff=15871"/>
		<updated>2008-07-26T22:31:46Z</updated>

		<summary type="html">&lt;p&gt;Amosti: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15870</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 6 esb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15870"/>
		<updated>2008-07-26T22:29:51Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* ''References'' */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Protected Variation==&lt;br /&gt;
&lt;br /&gt;
The term protected variations comes from the need for the programmer to predict what in the code is likely vary in future adaptions of the code and prevent those variations from damaging the intended functionality of the code.  At some time in the future the code is likely going to need additional functionality.  The adding of functionality should not prevent the sections of code that currently work from continuing to do so while also allowing the new code to integrate and use the existing structure efficiently.&lt;br /&gt;
&lt;br /&gt;
Protected variation is often linked to the Open-Closed Principle (OCP) and Information Hiding which all tend to have a final goal of code that is adaptable without damaging the integrity of original code and without alterations beyond additions to the original code.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
When programming code, care should be made to protect against change to the existing code.  Information should be kept out of the grasp of components that could damage integrity.  New design should be implemented at designated &amp;quot;variation&amp;quot; and &amp;quot;evolution&amp;quot; points.  The pattern of ''Protected Variation'' involves identifying points of predicted variation and creating a stable interface around them.&lt;br /&gt;
&lt;br /&gt;
Protected Variation is closely knitted with the Open-Closed Principle.  When points of predicted variation are identified, the code becomes easy to extend/adapt (&amp;quot;open&amp;quot;), while the stable interfaces around these points provides for &amp;quot;closed&amp;quot; code, wherein modification will not affect clients.  This stable interface surrounding the points of change also promotes the concept of Information Hiding, in that design information is hidden from other modules at the points of likely change.&lt;br /&gt;
&lt;br /&gt;
The term Open-Closed Principal is credited to having orginated from Dr. Bertrand Meyer and appears in his 1988 book &amp;lt;u&amp;gt;Object Oriented Software Construction&amp;lt;/u&amp;gt;.  This original version of the Open-Closed Principle achieved protection from variation by relying heavily on original class inheritance.  This is often referred to as the Meyer's Open-Closed Principle.&lt;br /&gt;
&lt;br /&gt;
In more recent years the Open-Closed principle is linked more closely to the use of polymorphism to achieve protection from variations.  In this system abstract interfaces are commonly used and multitudes of implementations are used polymorphically as substitutes for the existing implementation which leaves the original code intact and operating as intended.&lt;br /&gt;
&lt;br /&gt;
== Why use Protected Variation? ==&lt;br /&gt;
Protected Variation is a principle which can save much time and money.  It is important to reduce the introduction of new defects, and in order to do so, the impact of change to a piece of code on the overall program must be minimized.  Protected Variations help to introduce low coupling (i.e. minimizing the degree to which each program module relies on each one of the other modules), which in turn reduce change impact.&lt;br /&gt;
&lt;br /&gt;
* Example 1 &lt;br /&gt;
&lt;br /&gt;
* Example 2&lt;br /&gt;
&lt;br /&gt;
==How would you classify it ==&lt;br /&gt;
&lt;br /&gt;
==Coding Examples==&lt;br /&gt;
The typical mechanisms for protecting a system against variations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Polymorphism example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Inheritance example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Encapsulation example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Indirection example:&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
==''References''==&lt;br /&gt;
Larman, Craig.  [http://books.google.com/books?id=r8i-4En_aa4C Applying UML and Patterns].  Prentice Hall PTR, 2001.&lt;br /&gt;
&lt;br /&gt;
Larman, Craig.  [http://ieeexplore.ieee.org.www.lib.ncsu.edu:2048/iel5/52/19948/00922731.pdf?tp=&amp;amp;arnumber=922731&amp;amp;isnumber=19948 Protected Variation: The Importance of Being Closed].  &amp;lt;u&amp;gt;IEEE Software&amp;lt;/u&amp;gt; 18.3 (2001): 89-91.&lt;br /&gt;
&lt;br /&gt;
==''External Links''==&lt;br /&gt;
&lt;br /&gt;
http://codecourse.sourceforge.net/materials/The-Importance-of-Being-Closed.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/Files/ooprimer.ppt#288,9,OCP Example &lt;br /&gt;
&lt;br /&gt;
http://www.cs.wright.edu/~tkprasad/courses/cs480/L3OOP.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.csci.csusb.edu/dick/cs375/16q.txt&lt;br /&gt;
&lt;br /&gt;
http://www.cs.wustl.edu/~schmidt/PDF/design-principles4.pdf&lt;br /&gt;
&lt;br /&gt;
http://davidhayden.com/blog/dave/archive/2005/06/04/1096.aspx&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Open/closed_principle&lt;br /&gt;
&lt;br /&gt;
http://www.openmymind.net/FoundationsOfProgramming.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.cs.umu.se/kurser/TDBC31/Overheads/L8-10_Advanced.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Open-Closed-Principle&lt;br /&gt;
&lt;br /&gt;
Good General link:&lt;br /&gt;
http://www.netobjectives.com/resources/books/design-patterns-explained/review-questions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next very good:&lt;br /&gt;
http://www.augustana.ab.ca/~mohrj/courses/2007.fall/csc220/presentations/25_GRASP2.ppt#270,5,Fig. 25.2  Applying polymorphism to Monopoly&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Information-hiding&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/nblog/CategoryView,category,ruby.aspx&lt;br /&gt;
&lt;br /&gt;
http://www.cse.ohio-state.edu/~rountev/757/pdf/Principles.pdf&lt;br /&gt;
&lt;br /&gt;
[[CSC/ECE 517 Summer 2008/wiki3 Assignment|Back to the assignment page]]&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15869</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 6 esb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15869"/>
		<updated>2008-07-26T22:29:19Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* ''References'' */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Protected Variation==&lt;br /&gt;
&lt;br /&gt;
The term protected variations comes from the need for the programmer to predict what in the code is likely vary in future adaptions of the code and prevent those variations from damaging the intended functionality of the code.  At some time in the future the code is likely going to need additional functionality.  The adding of functionality should not prevent the sections of code that currently work from continuing to do so while also allowing the new code to integrate and use the existing structure efficiently.&lt;br /&gt;
&lt;br /&gt;
Protected variation is often linked to the Open-Closed Principle (OCP) and Information Hiding which all tend to have a final goal of code that is adaptable without damaging the integrity of original code and without alterations beyond additions to the original code.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
When programming code, care should be made to protect against change to the existing code.  Information should be kept out of the grasp of components that could damage integrity.  New design should be implemented at designated &amp;quot;variation&amp;quot; and &amp;quot;evolution&amp;quot; points.  The pattern of ''Protected Variation'' involves identifying points of predicted variation and creating a stable interface around them.&lt;br /&gt;
&lt;br /&gt;
Protected Variation is closely knitted with the Open-Closed Principle.  When points of predicted variation are identified, the code becomes easy to extend/adapt (&amp;quot;open&amp;quot;), while the stable interfaces around these points provides for &amp;quot;closed&amp;quot; code, wherein modification will not affect clients.  This stable interface surrounding the points of change also promotes the concept of Information Hiding, in that design information is hidden from other modules at the points of likely change.&lt;br /&gt;
&lt;br /&gt;
The term Open-Closed Principal is credited to having orginated from Dr. Bertrand Meyer and appears in his 1988 book &amp;lt;u&amp;gt;Object Oriented Software Construction&amp;lt;/u&amp;gt;.  This original version of the Open-Closed Principle achieved protection from variation by relying heavily on original class inheritance.  This is often referred to as the Meyer's Open-Closed Principle.&lt;br /&gt;
&lt;br /&gt;
In more recent years the Open-Closed principle is linked more closely to the use of polymorphism to achieve protection from variations.  In this system abstract interfaces are commonly used and multitudes of implementations are used polymorphically as substitutes for the existing implementation which leaves the original code intact and operating as intended.&lt;br /&gt;
&lt;br /&gt;
== Why use Protected Variation? ==&lt;br /&gt;
Protected Variation is a principle which can save much time and money.  It is important to reduce the introduction of new defects, and in order to do so, the impact of change to a piece of code on the overall program must be minimized.  Protected Variations help to introduce low coupling (i.e. minimizing the degree to which each program module relies on each one of the other modules), which in turn reduce change impact.&lt;br /&gt;
&lt;br /&gt;
* Example 1 &lt;br /&gt;
&lt;br /&gt;
* Example 2&lt;br /&gt;
&lt;br /&gt;
==How would you classify it ==&lt;br /&gt;
&lt;br /&gt;
==Coding Examples==&lt;br /&gt;
The typical mechanisms for protecting a system against variations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Polymorphism example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Inheritance example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Encapsulation example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Indirection example:&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
==''References''==&lt;br /&gt;
Larman, Craig.  [http://books.google.com/books?id=r8i-4En_aa4C Applying UML and Patterns].  Prentice Hall PTR, 2001.&lt;br /&gt;
Larman, Craig.  [http://ieeexplore.ieee.org.www.lib.ncsu.edu:2048/iel5/52/19948/00922731.pdf?tp=&amp;amp;arnumber=922731&amp;amp;isnumber=19948 Protected Variation: The Importance of Being Closed].  &amp;lt;u&amp;gt;IEEE Software&amp;lt;/u&amp;gt; 18.3 (2001): 89-91.&lt;br /&gt;
&lt;br /&gt;
==''External Links''==&lt;br /&gt;
&lt;br /&gt;
http://codecourse.sourceforge.net/materials/The-Importance-of-Being-Closed.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/Files/ooprimer.ppt#288,9,OCP Example &lt;br /&gt;
&lt;br /&gt;
http://www.cs.wright.edu/~tkprasad/courses/cs480/L3OOP.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.csci.csusb.edu/dick/cs375/16q.txt&lt;br /&gt;
&lt;br /&gt;
http://www.cs.wustl.edu/~schmidt/PDF/design-principles4.pdf&lt;br /&gt;
&lt;br /&gt;
http://davidhayden.com/blog/dave/archive/2005/06/04/1096.aspx&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Open/closed_principle&lt;br /&gt;
&lt;br /&gt;
http://www.openmymind.net/FoundationsOfProgramming.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.cs.umu.se/kurser/TDBC31/Overheads/L8-10_Advanced.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Open-Closed-Principle&lt;br /&gt;
&lt;br /&gt;
Good General link:&lt;br /&gt;
http://www.netobjectives.com/resources/books/design-patterns-explained/review-questions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next very good:&lt;br /&gt;
http://www.augustana.ab.ca/~mohrj/courses/2007.fall/csc220/presentations/25_GRASP2.ppt#270,5,Fig. 25.2  Applying polymorphism to Monopoly&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Information-hiding&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/nblog/CategoryView,category,ruby.aspx&lt;br /&gt;
&lt;br /&gt;
http://www.cse.ohio-state.edu/~rountev/757/pdf/Principles.pdf&lt;br /&gt;
&lt;br /&gt;
[[CSC/ECE 517 Summer 2008/wiki3 Assignment|Back to the assignment page]]&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15857</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 6 esb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15857"/>
		<updated>2008-07-26T22:10:00Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Protected Variation==&lt;br /&gt;
&lt;br /&gt;
The term protected variations comes from the need for the programmer to predict what in the code is likely vary in future adaptions of the code and prevent those variations from damaging the intended functionality of the code.  At some time in the future the code is likely going to need additional functionality.  The adding of functionality should not prevent the sections of code that currently work from continuing to do so while also allowing the new code to integrate and use the existing structure efficiently.&lt;br /&gt;
&lt;br /&gt;
Protected variation is often linked to the Open-Closed Principle (OCP) and Information Hiding which all tend to have a final goal of code that is adaptable without damaging the integrity of original code and without alterations beyond additions to the original code.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
When programming code, care should be made to protect against change to the existing code.  Information should be kept out of the grasp of components that could damage integrity.  New design should be implemented at designated &amp;quot;variation&amp;quot; and &amp;quot;evolution&amp;quot; points.  The pattern of ''Protected Variation'' involves identifying points of predicted variation and creating a stable interface around them.&lt;br /&gt;
&lt;br /&gt;
Protected Variation is closely knitted with the Open-Closed Principle.  When points of predicted variation are identified, the code becomes easy to extend/adapt (&amp;quot;open&amp;quot;), while the stable interfaces around these points provides for &amp;quot;closed&amp;quot; code, wherein modification will not affect clients.  This stable interface surrounding the points of change also promotes the concept of Information Hiding, in that design information is hidden from other modules at the points of likely change.&lt;br /&gt;
&lt;br /&gt;
The term Open-Closed Principal is credited to having orginated from Dr. Bertrand Meyer and appears in his 1988 book &amp;lt;u&amp;gt;Object Oriented Software Construction&amp;lt;/u&amp;gt;.  This original version of the Open-Closed Principle achieved protection from variation by relying heavily on original class inheritance.  This is often referred to as the Meyer's Open-Closed Principle.&lt;br /&gt;
&lt;br /&gt;
In more recent years the Open-Closed principle is linked more closely to the use of polymorphism to achieve protection from variations.  In this system abstract interfaces are commonly used and multitudes of implementations are used polymorphically as substitutes for the existing implementation which leaves the original code intact and operating as intended.&lt;br /&gt;
&lt;br /&gt;
== Why use Protected Variation? ==&lt;br /&gt;
Protected Variation is a principle which can save much time and money.  It is important to reduce the introduction of new defects, and in order to do so, the impact of change to a piece of code on the overall program must be minimized.  Protected Variations help to introduce low coupling (i.e. minimizing the degree to which each program module relies on each one of the other modules), which in turn reduce change impact.&lt;br /&gt;
&lt;br /&gt;
* Example 1 &lt;br /&gt;
&lt;br /&gt;
* Example 2&lt;br /&gt;
&lt;br /&gt;
==How would you classify it ==&lt;br /&gt;
&lt;br /&gt;
==Coding Examples==&lt;br /&gt;
The typical mechanisms for protecting a system against variations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Polymorphism example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Inheritance example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Encapsulation example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Indirection example:&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
==''References''==&lt;br /&gt;
Larman, Craig.  [http://ieeexplore.ieee.org.www.lib.ncsu.edu:2048/iel5/52/19948/00922731.pdf?tp=&amp;amp;arnumber=922731&amp;amp;isnumber=19948 Protected Variation: The Importance of Being Closed].  &amp;lt;u&amp;gt;IEEE Software&amp;lt;/u&amp;gt; 18.3 (2001): 89-91.&lt;br /&gt;
&lt;br /&gt;
==''External Links''==&lt;br /&gt;
&lt;br /&gt;
http://codecourse.sourceforge.net/materials/The-Importance-of-Being-Closed.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/Files/ooprimer.ppt#288,9,OCP Example &lt;br /&gt;
&lt;br /&gt;
http://www.cs.wright.edu/~tkprasad/courses/cs480/L3OOP.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.csci.csusb.edu/dick/cs375/16q.txt&lt;br /&gt;
&lt;br /&gt;
http://www.cs.wustl.edu/~schmidt/PDF/design-principles4.pdf&lt;br /&gt;
&lt;br /&gt;
http://davidhayden.com/blog/dave/archive/2005/06/04/1096.aspx&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Open/closed_principle&lt;br /&gt;
&lt;br /&gt;
http://www.openmymind.net/FoundationsOfProgramming.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.cs.umu.se/kurser/TDBC31/Overheads/L8-10_Advanced.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Open-Closed-Principle&lt;br /&gt;
&lt;br /&gt;
Good General link:&lt;br /&gt;
http://www.netobjectives.com/resources/books/design-patterns-explained/review-questions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next very good:&lt;br /&gt;
http://www.augustana.ab.ca/~mohrj/courses/2007.fall/csc220/presentations/25_GRASP2.ppt#270,5,Fig. 25.2  Applying polymorphism to Monopoly&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Information-hiding&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/nblog/CategoryView,category,ruby.aspx&lt;br /&gt;
&lt;br /&gt;
http://www.cse.ohio-state.edu/~rountev/757/pdf/Principles.pdf&lt;br /&gt;
&lt;br /&gt;
[[CSC/ECE 517 Summer 2008/wiki3 Assignment|Back to the assignment page]]&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15832</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 6 esb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15832"/>
		<updated>2008-07-26T15:59:14Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Why use Protected Variation? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Protected Variation==&lt;br /&gt;
Open-Closed Principle&lt;br /&gt;
&amp;quot;Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification [Martin, p.99]&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Description of protected variation&lt;br /&gt;
&lt;br /&gt;
Protected variation and Polymorphism&lt;br /&gt;
Protected Variation and Polymorphism seem related.  What is the difference&lt;br /&gt;
between the two?  Where would you apply one pattern over the other?&lt;br /&gt;
&lt;br /&gt;
Polymorphism is a powerful technology that is very useful for&lt;br /&gt;
handling Protected Variation.   Protected Variation gives one reason &amp;quot;why&amp;quot;&lt;br /&gt;
for a particular use of Polymorphism.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An example of an&lt;br /&gt;
.Key Open-Closed Principle&lt;br /&gt;
is... X can be opened to Y and always Z. And it's only open to Y if it Y &lt;br /&gt;
needs to access something and has permission to. While Z can always access&lt;br /&gt;
it because it has permission to.&lt;br /&gt;
That look right?&lt;br /&gt;
&lt;br /&gt;
I'd express it like this: if X uses Y in some way or other you don't want&lt;br /&gt;
changes to Y to effect X, but you also want to be able to change Y in ways&lt;br /&gt;
that don't change X.&lt;br /&gt;
&lt;br /&gt;
Open-closed principle&lt;br /&gt;
“Software should be open to extension but closed to modification” (Bertrand Meyer). The idea is to&lt;br /&gt;
enhance functionality by making non-intrusive changes. Intrusive changes are changes that alter code that&lt;br /&gt;
has been previously written; changing inside of classes. Rather, interfaces should be changed/extended&lt;br /&gt;
rather than the implementation of classes.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
When programming code, care should be made to protect against change to the existing code.  Information should be kept out of the grasp of components that could damage integrity.  New design should be implemented at designated &amp;quot;variation&amp;quot; and &amp;quot;evolution&amp;quot; points.  The pattern of ''Protected Variation'' involves identifying points of predicted variation and creating a stable interface around them.&lt;br /&gt;
&lt;br /&gt;
Protected Variation is closely knitted with the Open-Closed Principle.  When points of predicted variation are identified, the code becomes easy to extend/adapt (&amp;quot;open&amp;quot;), while the stable interfaces around these points provides for &amp;quot;closed&amp;quot; code, wherein modification will not affect clients.  This stable interface surrounding the points of change also promotes the concept of Information Hiding, in that design information is hidden from other modules at the points of likely change.&lt;br /&gt;
&lt;br /&gt;
== Why use Protected Variation? ==&lt;br /&gt;
Protected Variation is a principle which can save much time and money.  It is important to reduce the introduction of new defects, and in order to do so, the impact of change to a piece of code on the overall program must be minimized.  Protected Variations help to introduce low coupling (i.e. minimizing the degree to which each program module relies on each one of the other modules), which in turn reduce change impact.&lt;br /&gt;
&lt;br /&gt;
* Example 1 &lt;br /&gt;
&lt;br /&gt;
* Example 2&lt;br /&gt;
&lt;br /&gt;
==How would you classify it ==&lt;br /&gt;
&lt;br /&gt;
==Coding Examples==&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
==''References''==&lt;br /&gt;
Larman, Craig.  [http://ieeexplore.ieee.org.www.lib.ncsu.edu:2048/iel5/52/19948/00922731.pdf?tp=&amp;amp;arnumber=922731&amp;amp;isnumber=19948 Protected Variation: The Importance of Being Closed].  &amp;lt;u&amp;gt;IEEE Software&amp;lt;/u&amp;gt; 18.3 (2001): 89-91.&lt;br /&gt;
&lt;br /&gt;
==''External Links''==&lt;br /&gt;
&lt;br /&gt;
http://codecourse.sourceforge.net/materials/The-Importance-of-Being-Closed.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/Files/ooprimer.ppt#288,9,OCP Example &lt;br /&gt;
&lt;br /&gt;
http://www.cs.wright.edu/~tkprasad/courses/cs480/L3OOP.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.csci.csusb.edu/dick/cs375/16q.txt&lt;br /&gt;
&lt;br /&gt;
http://www.cs.wustl.edu/~schmidt/PDF/design-principles4.pdf&lt;br /&gt;
&lt;br /&gt;
http://davidhayden.com/blog/dave/archive/2005/06/04/1096.aspx&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Open/closed_principle&lt;br /&gt;
&lt;br /&gt;
http://www.openmymind.net/FoundationsOfProgramming.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.cs.umu.se/kurser/TDBC31/Overheads/L8-10_Advanced.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Open-Closed-Principle&lt;br /&gt;
&lt;br /&gt;
Good General link:&lt;br /&gt;
http://www.netobjectives.com/resources/books/design-patterns-explained/review-questions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next very good:&lt;br /&gt;
http://www.augustana.ab.ca/~mohrj/courses/2007.fall/csc220/presentations/25_GRASP2.ppt#270,5,Fig. 25.2  Applying polymorphism to Monopoly&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Information-hiding&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/nblog/CategoryView,category,ruby.aspx&lt;br /&gt;
&lt;br /&gt;
http://www.cse.ohio-state.edu/~rountev/757/pdf/Principles.pdf&lt;br /&gt;
&lt;br /&gt;
[[CSC/ECE 517 Summer 2008/wiki3 Assignment|Back to the assignment page]]&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15830</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 6 esb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15830"/>
		<updated>2008-07-26T15:36:35Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Protected Variation==&lt;br /&gt;
Open-Closed Principle&lt;br /&gt;
&amp;quot;Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification [Martin, p.99]&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Description of protected variation&lt;br /&gt;
&lt;br /&gt;
Protected variation and Polymorphism&lt;br /&gt;
Protected Variation and Polymorphism seem related.  What is the difference&lt;br /&gt;
between the two?  Where would you apply one pattern over the other?&lt;br /&gt;
&lt;br /&gt;
Polymorphism is a powerful technology that is very useful for&lt;br /&gt;
handling Protected Variation.   Protected Variation gives one reason &amp;quot;why&amp;quot;&lt;br /&gt;
for a particular use of Polymorphism.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An example of an&lt;br /&gt;
.Key Open-Closed Principle&lt;br /&gt;
is... X can be opened to Y and always Z. And it's only open to Y if it Y &lt;br /&gt;
needs to access something and has permission to. While Z can always access&lt;br /&gt;
it because it has permission to.&lt;br /&gt;
That look right?&lt;br /&gt;
&lt;br /&gt;
I'd express it like this: if X uses Y in some way or other you don't want&lt;br /&gt;
changes to Y to effect X, but you also want to be able to change Y in ways&lt;br /&gt;
that don't change X.&lt;br /&gt;
&lt;br /&gt;
Open-closed principle&lt;br /&gt;
“Software should be open to extension but closed to modification” (Bertrand Meyer). The idea is to&lt;br /&gt;
enhance functionality by making non-intrusive changes. Intrusive changes are changes that alter code that&lt;br /&gt;
has been previously written; changing inside of classes. Rather, interfaces should be changed/extended&lt;br /&gt;
rather than the implementation of classes.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
When programming code, care should be made to protect against change to the existing code.  Information should be kept out of the grasp of components that could damage integrity.  New design should be implemented at designated &amp;quot;variation&amp;quot; and &amp;quot;evolution&amp;quot; points.  The pattern of ''Protected Variation'' involves identifying points of predicted variation and creating a stable interface around them.&lt;br /&gt;
&lt;br /&gt;
Protected Variation is closely knitted with the Open-Closed Principle.  When points of predicted variation are identified, the code becomes easy to extend/adapt (&amp;quot;open&amp;quot;), while the stable interfaces around these points provides for &amp;quot;closed&amp;quot; code, wherein modification will not affect clients.  This stable interface surrounding the points of change also promotes the concept of Information Hiding, in that design information is hidden from other modules at the points of likely change.&lt;br /&gt;
&lt;br /&gt;
== Why use Protected Variation? ==&lt;br /&gt;
General overview&lt;br /&gt;
&lt;br /&gt;
* Example 1 &lt;br /&gt;
&lt;br /&gt;
* Example 2&lt;br /&gt;
==How would you classify it ==&lt;br /&gt;
&lt;br /&gt;
==Coding Examples==&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
==''References''==&lt;br /&gt;
Larman, Craig.  [http://ieeexplore.ieee.org.www.lib.ncsu.edu:2048/iel5/52/19948/00922731.pdf?tp=&amp;amp;arnumber=922731&amp;amp;isnumber=19948 Protected Variation: The Importance of Being Closed].  &amp;lt;u&amp;gt;IEEE Software&amp;lt;/u&amp;gt; 18.3 (2001): 89-91.&lt;br /&gt;
&lt;br /&gt;
==''External Links''==&lt;br /&gt;
&lt;br /&gt;
http://codecourse.sourceforge.net/materials/The-Importance-of-Being-Closed.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/Files/ooprimer.ppt#288,9,OCP Example &lt;br /&gt;
&lt;br /&gt;
http://www.cs.wright.edu/~tkprasad/courses/cs480/L3OOP.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.csci.csusb.edu/dick/cs375/16q.txt&lt;br /&gt;
&lt;br /&gt;
http://www.cs.wustl.edu/~schmidt/PDF/design-principles4.pdf&lt;br /&gt;
&lt;br /&gt;
http://davidhayden.com/blog/dave/archive/2005/06/04/1096.aspx&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Open/closed_principle&lt;br /&gt;
&lt;br /&gt;
http://www.openmymind.net/FoundationsOfProgramming.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.cs.umu.se/kurser/TDBC31/Overheads/L8-10_Advanced.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Open-Closed-Principle&lt;br /&gt;
&lt;br /&gt;
Good General link:&lt;br /&gt;
http://www.netobjectives.com/resources/books/design-patterns-explained/review-questions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next very good:&lt;br /&gt;
http://www.augustana.ab.ca/~mohrj/courses/2007.fall/csc220/presentations/25_GRASP2.ppt#270,5,Fig. 25.2  Applying polymorphism to Monopoly&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Information-hiding&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/nblog/CategoryView,category,ruby.aspx&lt;br /&gt;
&lt;br /&gt;
http://www.cse.ohio-state.edu/~rountev/757/pdf/Principles.pdf&lt;br /&gt;
&lt;br /&gt;
[[CSC/ECE 517 Summer 2008/wiki3 Assignment|Back to the assignment page]]&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15828</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 6 esb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15828"/>
		<updated>2008-07-26T15:09:19Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Protected Variation==&lt;br /&gt;
Open-Closed Principle&lt;br /&gt;
&amp;quot;Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification [Martin, p.99]&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Description of protected variation&lt;br /&gt;
&lt;br /&gt;
Protected variation and Polymorphism&lt;br /&gt;
Protected Variation and Polymorphism seem related.  What is the difference&lt;br /&gt;
between the two?  Where would you apply one pattern over the other?&lt;br /&gt;
&lt;br /&gt;
Polymorphism is a powerful technology that is very useful for&lt;br /&gt;
handling Protected Variation.   Protected Variation gives one reason &amp;quot;why&amp;quot;&lt;br /&gt;
for a particular use of Polymorphism.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An example of an&lt;br /&gt;
.Key Open-Closed Principle&lt;br /&gt;
is... X can be opened to Y and always Z. And it's only open to Y if it Y &lt;br /&gt;
needs to access something and has permission to. While Z can always access&lt;br /&gt;
it because it has permission to.&lt;br /&gt;
That look right?&lt;br /&gt;
&lt;br /&gt;
I'd express it like this: if X uses Y in some way or other you don't want&lt;br /&gt;
changes to Y to effect X, but you also want to be able to change Y in ways&lt;br /&gt;
that don't change X.&lt;br /&gt;
&lt;br /&gt;
Open-closed principle&lt;br /&gt;
“Software should be open to extension but closed to modification” (Bertrand Meyer). The idea is to&lt;br /&gt;
enhance functionality by making non-intrusive changes. Intrusive changes are changes that alter code that&lt;br /&gt;
has been previously written; changing inside of classes. Rather, interfaces should be changed/extended&lt;br /&gt;
rather than the implementation of classes.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Keep information out of the grasp of components that could damage integrity.&lt;br /&gt;
Introduce reader to protected variation -Open/Closed Principle and Information Hiding&lt;br /&gt;
&lt;br /&gt;
== Why use Protected Variation? ==&lt;br /&gt;
General overview&lt;br /&gt;
&lt;br /&gt;
* Example 1 &lt;br /&gt;
&lt;br /&gt;
* Example 2&lt;br /&gt;
==How would you classify it ==&lt;br /&gt;
&lt;br /&gt;
==Coding Examples==&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
==''References''==&lt;br /&gt;
Larman, Craig.  [http://ieeexplore.ieee.org.www.lib.ncsu.edu:2048/iel5/52/19948/00922731.pdf?tp=&amp;amp;arnumber=922731&amp;amp;isnumber=19948 Protected Variation: The Importance of Being Closed].  &amp;lt;u&amp;gt;IEEE Software&amp;lt;/u&amp;gt; 18.3 (2001): 89-91.&lt;br /&gt;
&lt;br /&gt;
==''External Links''==&lt;br /&gt;
&lt;br /&gt;
http://codecourse.sourceforge.net/materials/The-Importance-of-Being-Closed.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/Files/ooprimer.ppt#288,9,OCP Example &lt;br /&gt;
&lt;br /&gt;
http://www.cs.wright.edu/~tkprasad/courses/cs480/L3OOP.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.csci.csusb.edu/dick/cs375/16q.txt&lt;br /&gt;
&lt;br /&gt;
http://www.cs.wustl.edu/~schmidt/PDF/design-principles4.pdf&lt;br /&gt;
&lt;br /&gt;
http://davidhayden.com/blog/dave/archive/2005/06/04/1096.aspx&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Open/closed_principle&lt;br /&gt;
&lt;br /&gt;
http://www.openmymind.net/FoundationsOfProgramming.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.cs.umu.se/kurser/TDBC31/Overheads/L8-10_Advanced.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Open-Closed-Principle&lt;br /&gt;
&lt;br /&gt;
Good General link:&lt;br /&gt;
http://www.netobjectives.com/resources/books/design-patterns-explained/review-questions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next very good:&lt;br /&gt;
http://www.augustana.ab.ca/~mohrj/courses/2007.fall/csc220/presentations/25_GRASP2.ppt#270,5,Fig. 25.2  Applying polymorphism to Monopoly&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Information-hiding&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/nblog/CategoryView,category,ruby.aspx&lt;br /&gt;
&lt;br /&gt;
http://www.cse.ohio-state.edu/~rountev/757/pdf/Principles.pdf&lt;br /&gt;
&lt;br /&gt;
[[CSC/ECE 517 Summer 2008/wiki3 Assignment|Back to the assignment page]]&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15822</id>
		<title>CSC/ECE 517 Summer 2008/wiki3 6 esb</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki3_6_esb&amp;diff=15822"/>
		<updated>2008-07-26T14:42:58Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Protected Variation==&lt;br /&gt;
Open-Closed Principle&lt;br /&gt;
&amp;quot;Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification [Martin, p.99]&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Description of protected variation&lt;br /&gt;
&lt;br /&gt;
Protected variation and Polymorphism&lt;br /&gt;
Protected Variation and Polymorphism seem related.  What is the difference&lt;br /&gt;
between the two?  Where would you apply one pattern over the other?&lt;br /&gt;
&lt;br /&gt;
Polymorphism is a powerful technology that is very useful for&lt;br /&gt;
handling Protected Variation.   Protected Variation gives one reason &amp;quot;why&amp;quot;&lt;br /&gt;
for a particular use of Polymorphism.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
An example of an&lt;br /&gt;
.Key Open-Closed Principle&lt;br /&gt;
is... X can be opened to Y and always Z. And it's only open to Y if it Y &lt;br /&gt;
needs to access something and has permission to. While Z can always access&lt;br /&gt;
it because it has permission to.&lt;br /&gt;
That look right?&lt;br /&gt;
&lt;br /&gt;
I'd express it like this: if X uses Y in some way or other you don't want&lt;br /&gt;
changes to Y to effect X, but you also want to be able to change Y in ways&lt;br /&gt;
that don't change X.&lt;br /&gt;
&lt;br /&gt;
Open-closed principle&lt;br /&gt;
“Software should be open to extension but closed to modification” (Bertrand Meyer). The idea is to&lt;br /&gt;
enhance functionality by making non-intrusive changes. Intrusive changes are changes that alter code that&lt;br /&gt;
has been previously written; changing inside of classes. Rather, interfaces should be changed/extended&lt;br /&gt;
rather than the implementation of classes.&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
Keep information out of the grasp of components that could damage integrity.&lt;br /&gt;
Introduce reader to protected variation -Open/Closed Principle and Information Hiding&lt;br /&gt;
&lt;br /&gt;
== Why use Protected Variation? ==&lt;br /&gt;
General overview&lt;br /&gt;
&lt;br /&gt;
* Example 1 &lt;br /&gt;
&lt;br /&gt;
* Example 2&lt;br /&gt;
==How would you classify it ==&lt;br /&gt;
&lt;br /&gt;
==Coding Examples==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
Larman, Craig.  [http://ieeexplore.ieee.org.www.lib.ncsu.edu:2048/iel5/52/19948/00922731.pdf?tp=&amp;amp;arnumber=922731&amp;amp;isnumber=19948 Protected Variation: The Importance of Being Closed].  &amp;lt;u&amp;gt;IEEE Software&amp;lt;/u&amp;gt; 18.3 (2001): 89-91.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
&lt;br /&gt;
http://codecourse.sourceforge.net/materials/The-Importance-of-Being-Closed.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/Files/ooprimer.ppt#288,9,OCP Example &lt;br /&gt;
&lt;br /&gt;
http://www.cs.wright.edu/~tkprasad/courses/cs480/L3OOP.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.csci.csusb.edu/dick/cs375/16q.txt&lt;br /&gt;
&lt;br /&gt;
http://www.cs.wustl.edu/~schmidt/PDF/design-principles4.pdf&lt;br /&gt;
&lt;br /&gt;
http://davidhayden.com/blog/dave/archive/2005/06/04/1096.aspx&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Open/closed_principle&lt;br /&gt;
&lt;br /&gt;
http://www.openmymind.net/FoundationsOfProgramming.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.cs.umu.se/kurser/TDBC31/Overheads/L8-10_Advanced.pdf&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Open-Closed-Principle&lt;br /&gt;
&lt;br /&gt;
Good General link:&lt;br /&gt;
http://www.netobjectives.com/resources/books/design-patterns-explained/review-questions&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next very good:&lt;br /&gt;
http://www.augustana.ab.ca/~mohrj/courses/2007.fall/csc220/presentations/25_GRASP2.ppt#270,5,Fig. 25.2  Applying polymorphism to Monopoly&lt;br /&gt;
&lt;br /&gt;
http://www.nationmaster.com/encyclopedia/Information-hiding&lt;br /&gt;
&lt;br /&gt;
http://www.rgoarchitects.com/nblog/CategoryView,category,ruby.aspx&lt;br /&gt;
&lt;br /&gt;
http://www.cse.ohio-state.edu/~rountev/757/pdf/Principles.pdf&lt;br /&gt;
&lt;br /&gt;
[[CSC/ECE 517 Summer 2008/wiki3 Assignment|Back to the assignment page]]&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=14098</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=14098"/>
		<updated>2008-06-26T02:52:36Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Hungarian notation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  It's always important to choose descriptive names with straightforward meanings.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions advantages ===&lt;br /&gt;
* to provide additional information (ie, metadata) about the use to which an identifier is put&lt;br /&gt;
* to help formalize expectations and promote consistency within a development team&lt;br /&gt;
* to enable the use of automated refactoring or search and replace tools with minimal potential for error&lt;br /&gt;
* to enhance clarity in cases of potential ambiguity&lt;br /&gt;
* to enhance the aesthetic and professional appearance of work product (for example, by disallowing overly long names, comical or &amp;quot;cute&amp;quot; names, or abbreviations)&lt;br /&gt;
* to help avoid &amp;quot;naming collisions&amp;quot; that might occur when the work product of different organizations is combined&lt;br /&gt;
* to provide meaningful data to be used in project handovers which require submission of program source code and all relevant documentation&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions disadvantages ===&lt;br /&gt;
* When a variable type is changed, you may have to go through and change ''all'' the instances of that variable within your code.  The find-and-replace feature of many editors alleviates this issue a bit, but it may still be tedious.&lt;br /&gt;
* If your language changes, i.e. is updated, code may need to be changed so the naming convention still fits.&lt;br /&gt;
* Naming conventions may defeat the purpose of encapsulation.  The &amp;quot;black box&amp;quot; aspect is eliminated when the programmer needs to know what specific type is being returned.&lt;br /&gt;
&lt;br /&gt;
=== Common conventions ===&lt;br /&gt;
* Packages should have prefixes always written in all-lowercase ASCII letters and they should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.  Examples: &amp;lt;tt&amp;gt;com.sun.eng&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com.apple.quicktime.v2&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;edu.cmu.cs.bovik.cheese&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* Class names should be nouns, in mixed case with the first letter of each word capitalized.  They should be simple and descriptive, and whole words should be used (avoid acronyms and abbreviations).  Examples: &amp;lt;tt&amp;gt;class Roster;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;class ImageSprite;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* Interface names should be capitalized like class names.  Examples: &amp;lt;tt&amp;gt;interface RosterDelegate;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;interface Storing;&amp;lt;/tt&amp;gt;. &lt;br /&gt;
* Lowercase letters should be used for variable and method names.  A name consisting of several words should be concatenated into one word (take out the spaces).  The first word should be all lowercase, and each subsequent word (the internal words) should have its first letter capitalized.  Methods should be verbs.  Examples: &amp;lt;tt&amp;gt;run();&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;runFast();&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;getBackground();&amp;lt;/tt&amp;gt;&lt;br /&gt;
* Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary &amp;quot;throwaway&amp;quot; variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.  Variable names should not start with underscore (&amp;quot;&amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt;&amp;quot;) or dollar sign (&amp;quot;&amp;lt;tt&amp;gt;$&amp;lt;/tt&amp;gt;&amp;quot;) characters, even though both are allowed.  Examples: &amp;lt;tt&amp;gt;int i;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;char c;&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;float myWidth;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* The names of variables declared ''class constants'' and of ''ANSI constants'' should be all uppercase with words separated by underscores (&amp;quot;&amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt;&amp;quot;).  Examples: &amp;lt;tt&amp;gt;static final int MIN_WIDTH = 4;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;static final int MAX_WIDTH = 999;&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;static final int GET_THE_CPU = 1;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Hungarian notation ===&lt;br /&gt;
&lt;br /&gt;
One of the most popular naming conventions is Hungarian notation, which encodes either the purpose (&amp;quot;Apps Hungarian&amp;quot;) or the type (&amp;quot;Systems Hungarian&amp;quot;) of a variable in its name.  Some examples of &amp;quot;Apps Hungarian&amp;quot; are: &lt;br /&gt;
* &amp;lt;tt&amp;gt;rwPosition&amp;lt;/tt&amp;gt;: variable represents a row (&amp;quot;rw&amp;quot;)&lt;br /&gt;
* &amp;lt;tt&amp;gt;usName&amp;lt;/tt&amp;gt;: variable represents an unsafe string (&amp;quot;us&amp;quot;), which needs to be &amp;quot;sanitized&amp;quot; before it is used&lt;br /&gt;
* &amp;lt;tt&amp;gt;strName&amp;lt;/tt&amp;gt;: Variable represents a string (&amp;quot;str&amp;quot;) containing the name, but does not specify how that string is implemented&lt;br /&gt;
&lt;br /&gt;
Some examples of &amp;quot;Apps Hungarian&amp;quot; are as follows:&lt;br /&gt;
* &amp;lt;tt&amp;gt;lAccountNum&amp;lt;/tt&amp;gt;: variable is a long integer (&amp;quot;l&amp;quot;)&lt;br /&gt;
* &amp;lt;tt&amp;gt;arru8NumberList&amp;lt;/tt&amp;gt;: variable is an array of unsigned 8-bit integers (&amp;quot;arru8&amp;quot;)&lt;br /&gt;
* &amp;lt;tt&amp;gt;szName&amp;lt;/tt&amp;gt;: variable is a zero-terminated string (&amp;quot;sz&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
=== Final conclusions ===&lt;br /&gt;
The advantages of using naming conventions outweigh the disadvantages.  Readability is the biggest derived benefit, and its importance cannot be overstated.  Variable types, as well as their purpose (for classes and modules too) can be determined at a quick glance.  Readers unfamiliar with the code can derive meaning and purpose quicker, without having to study large portions of the code and refer to various places within.  The following tips should be used when choosing names for variables, classes, modules, etc.:&lt;br /&gt;
&lt;br /&gt;
* Prefix the variable names with their scope and their data types &lt;br /&gt;
* Use meaningful variable names &lt;br /&gt;
* Be wary of long names &lt;br /&gt;
* Use your conventions consistently&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
* Liang, Y. Daniel.  [http://books.google.com/books?id=juuPxtSCg50C Introduction to Java Programming: Comprehensive].  Pearson Prentice Hall, 2008.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
* http://frontier.userland.com/tutorial/variableNaming&lt;br /&gt;
* http://www.programmers-corner.com/article/18&lt;br /&gt;
* http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html&lt;br /&gt;
* http://www.cprogramming.com/tutorial/style_naming_conventions.html&lt;br /&gt;
* http://www.uwe-sauerland.de/richtlinien/naming.html&lt;br /&gt;
* http://www.kamath.com/columns/squareone/so001_whatname1.asp&lt;br /&gt;
* http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html&lt;br /&gt;
* http://en.wikipedia.org/wiki/Identifier_naming_convention&lt;br /&gt;
* http://www.erlang.se/doc/programming_rules.shtml&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=14095</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=14095"/>
		<updated>2008-06-26T02:50:32Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Variable, Class, and Module Naming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  It's always important to choose descriptive names with straightforward meanings.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions advantages ===&lt;br /&gt;
* to provide additional information (ie, metadata) about the use to which an identifier is put&lt;br /&gt;
* to help formalize expectations and promote consistency within a development team&lt;br /&gt;
* to enable the use of automated refactoring or search and replace tools with minimal potential for error&lt;br /&gt;
* to enhance clarity in cases of potential ambiguity&lt;br /&gt;
* to enhance the aesthetic and professional appearance of work product (for example, by disallowing overly long names, comical or &amp;quot;cute&amp;quot; names, or abbreviations)&lt;br /&gt;
* to help avoid &amp;quot;naming collisions&amp;quot; that might occur when the work product of different organizations is combined&lt;br /&gt;
* to provide meaningful data to be used in project handovers which require submission of program source code and all relevant documentation&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions disadvantages ===&lt;br /&gt;
* When a variable type is changed, you may have to go through and change ''all'' the instances of that variable within your code.  The find-and-replace feature of many editors alleviates this issue a bit, but it may still be tedious.&lt;br /&gt;
* If your language changes, i.e. is updated, code may need to be changed so the naming convention still fits.&lt;br /&gt;
* Naming conventions may defeat the purpose of encapsulation.  The &amp;quot;black box&amp;quot; aspect is eliminated when the programmer needs to know what specific type is being returned.&lt;br /&gt;
&lt;br /&gt;
=== Common conventions ===&lt;br /&gt;
* Packages should have prefixes always written in all-lowercase ASCII letters and they should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.  Examples: &amp;lt;tt&amp;gt;com.sun.eng&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com.apple.quicktime.v2&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;edu.cmu.cs.bovik.cheese&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* Class names should be nouns, in mixed case with the first letter of each word capitalized.  They should be simple and descriptive, and whole words should be used (avoid acronyms and abbreviations).  Examples: &amp;lt;tt&amp;gt;class Roster;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;class ImageSprite;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* Interface names should be capitalized like class names.  Examples: &amp;lt;tt&amp;gt;interface RosterDelegate;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;interface Storing;&amp;lt;/tt&amp;gt;. &lt;br /&gt;
* Lowercase letters should be used for variable and method names.  A name consisting of several words should be concatenated into one word (take out the spaces).  The first word should be all lowercase, and each subsequent word (the internal words) should have its first letter capitalized.  Methods should be verbs.  Examples: &amp;lt;tt&amp;gt;run();&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;runFast();&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;getBackground();&amp;lt;/tt&amp;gt;&lt;br /&gt;
* Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary &amp;quot;throwaway&amp;quot; variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.  Variable names should not start with underscore (&amp;quot;&amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt;&amp;quot;) or dollar sign (&amp;quot;&amp;lt;tt&amp;gt;$&amp;lt;/tt&amp;gt;&amp;quot;) characters, even though both are allowed.  Examples: &amp;lt;tt&amp;gt;int i;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;char c;&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;float myWidth;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* The names of variables declared ''class constants'' and of ''ANSI constants'' should be all uppercase with words separated by underscores (&amp;quot;&amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt;&amp;quot;).  Examples: &amp;lt;tt&amp;gt;static final int MIN_WIDTH = 4;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;static final int MAX_WIDTH = 999;&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;static final int GET_THE_CPU = 1;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Hungarian notation ===&lt;br /&gt;
&lt;br /&gt;
One of the most popular naming conventions is Hungarian notation, which encodes either the purpose (&amp;quot;Apps Hungarian&amp;quot;) or the type (&amp;quot;Systems Hungarian&amp;quot;) of a variable in its name.  Some examples of &amp;quot;Systems Hungarian&amp;quot; are: &lt;br /&gt;
* &amp;lt;tt&amp;gt;lAccountNum&amp;lt;/tt&amp;gt;: variable is a long integer (&amp;quot;l&amp;quot;)&lt;br /&gt;
* &amp;lt;tt&amp;gt;arru8NumberList&amp;lt;/tt&amp;gt;: variable is an array of unsigned 8-bit integers (&amp;quot;arru8&amp;quot;)&lt;br /&gt;
* &amp;lt;tt&amp;gt;szName&amp;lt;/tt&amp;gt;: variable is a zero-terminated string (&amp;quot;sz&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Some examples of &amp;quot;Apps Hungarian&amp;quot; are as follows:&lt;br /&gt;
* &amp;lt;tt&amp;gt;rwPosition&amp;lt;/tt&amp;gt;: variable represents a row (&amp;quot;rw&amp;quot;)&lt;br /&gt;
* &amp;lt;tt&amp;gt;usName&amp;lt;/tt&amp;gt;: variable represents an unsafe string (&amp;quot;us&amp;quot;), which needs to be &amp;quot;sanitized&amp;quot; before it is used&lt;br /&gt;
* &amp;lt;tt&amp;gt;strName&amp;lt;/tt&amp;gt;: Variable represents a string (&amp;quot;str&amp;quot;) containing the name, but does not specify how that string is implemented&lt;br /&gt;
&lt;br /&gt;
=== Final conclusions ===&lt;br /&gt;
The advantages of using naming conventions outweigh the disadvantages.  Readability is the biggest derived benefit, and its importance cannot be overstated.  Variable types, as well as their purpose (for classes and modules too) can be determined at a quick glance.  Readers unfamiliar with the code can derive meaning and purpose quicker, without having to study large portions of the code and refer to various places within.  The following tips should be used when choosing names for variables, classes, modules, etc.:&lt;br /&gt;
&lt;br /&gt;
* Prefix the variable names with their scope and their data types &lt;br /&gt;
* Use meaningful variable names &lt;br /&gt;
* Be wary of long names &lt;br /&gt;
* Use your conventions consistently&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
* Liang, Y. Daniel.  [http://books.google.com/books?id=juuPxtSCg50C Introduction to Java Programming: Comprehensive].  Pearson Prentice Hall, 2008.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
* http://frontier.userland.com/tutorial/variableNaming&lt;br /&gt;
* http://www.programmers-corner.com/article/18&lt;br /&gt;
* http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html&lt;br /&gt;
* http://www.cprogramming.com/tutorial/style_naming_conventions.html&lt;br /&gt;
* http://www.uwe-sauerland.de/richtlinien/naming.html&lt;br /&gt;
* http://www.kamath.com/columns/squareone/so001_whatname1.asp&lt;br /&gt;
* http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html&lt;br /&gt;
* http://en.wikipedia.org/wiki/Identifier_naming_convention&lt;br /&gt;
* http://www.erlang.se/doc/programming_rules.shtml&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=14080</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=14080"/>
		<updated>2008-06-26T02:36:31Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Common conventions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  It's always important to choose descriptive names with straightforward meanings.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions advantages ===&lt;br /&gt;
* to provide additional information (ie, metadata) about the use to which an identifier is put&lt;br /&gt;
* to help formalize expectations and promote consistency within a development team&lt;br /&gt;
* to enable the use of automated refactoring or search and replace tools with minimal potential for error&lt;br /&gt;
* to enhance clarity in cases of potential ambiguity&lt;br /&gt;
* to enhance the aesthetic and professional appearance of work product (for example, by disallowing overly long names, comical or &amp;quot;cute&amp;quot; names, or abbreviations)&lt;br /&gt;
* to help avoid &amp;quot;naming collisions&amp;quot; that might occur when the work product of different organizations is combined&lt;br /&gt;
* to provide meaningful data to be used in project handovers which require submission of program source code and all relevant documentation&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions disadvantages ===&lt;br /&gt;
* When a variable type is changed, you may have to go through and change ''all'' the instances of that variable within your code.  The find-and-replace feature of many editors alleviates this issue a bit, but it may still be tedious.&lt;br /&gt;
* If your language changes, i.e. is updated, code may need to be changed so the naming convention still fits.&lt;br /&gt;
* Naming conventions may defeat the purpose of encapsulation.  The &amp;quot;black box&amp;quot; aspect is eliminated when the programmer needs to know what specific type is being returned.&lt;br /&gt;
&lt;br /&gt;
=== Common conventions ===&lt;br /&gt;
* Packages should have prefixes always written in all-lowercase ASCII letters and they should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.  Examples: &amp;lt;tt&amp;gt;com.sun.eng&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com.apple.quicktime.v2&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;edu.cmu.cs.bovik.cheese&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* Class names should be nouns, in mixed case with the first letter of each word capitalized.  They should be simple and descriptive, and whole words should be used (avoid acronyms and abbreviations).  Examples: &amp;lt;tt&amp;gt;class Roster;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;class ImageSprite;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* Interface names should be capitalized like class names.  Examples: &amp;lt;tt&amp;gt;interface RosterDelegate;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;interface Storing;&amp;lt;/tt&amp;gt;. &lt;br /&gt;
* Lowercase letters should be used for variable and method names.  A name consisting of several words should be concatenated into one word (take out the spaces).  The first word should be all lowercase, and each subsequent word (the internal words) should have its first letter capitalized.  Methods should be verbs.  Examples: &amp;lt;tt&amp;gt;run();&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;runFast();&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;getBackground();&amp;lt;/tt&amp;gt;&lt;br /&gt;
* Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary &amp;quot;throwaway&amp;quot; variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.  Variable names should not start with underscore (&amp;quot;&amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt;&amp;quot;) or dollar sign (&amp;quot;&amp;lt;tt&amp;gt;$&amp;lt;/tt&amp;gt;&amp;quot;) characters, even though both are allowed.  Examples: &amp;lt;tt&amp;gt;int i;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;char c;&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;float myWidth;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* The names of variables declared ''class constants'' and of ''ANSI constants'' should be all uppercase with words separated by underscores (&amp;quot;&amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt;&amp;quot;).  Examples: &amp;lt;tt&amp;gt;static final int MIN_WIDTH = 4;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;static final int MAX_WIDTH = 999;&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;static final int GET_THE_CPU = 1;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Final conclusions ===&lt;br /&gt;
The advantages of using naming conventions outweigh the disadvantages.  Readability is the biggest derived benefit, and its importance cannot be overstated.  Variable types, as well as their purpose (for classes and modules too) can be determined at a quick glance.  Readers unfamiliar with the code can derive meaning and purpose quicker, without having to study large portions of the code and refer to various places within.  The following tips should be used when choosing names for variables, classes, modules, etc.:&lt;br /&gt;
&lt;br /&gt;
* Prefix the variable names with their scope and their data types &lt;br /&gt;
* Use meaningful variable names &lt;br /&gt;
* Be wary of long names &lt;br /&gt;
* Use your conventions consistently&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
* Liang, Y. Daniel.  [http://books.google.com/books?id=juuPxtSCg50C Introduction to Java Programming: Comprehensive].  Pearson Prentice Hall, 2008.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
* http://frontier.userland.com/tutorial/variableNaming&lt;br /&gt;
* http://www.programmers-corner.com/article/18&lt;br /&gt;
* http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html&lt;br /&gt;
* http://www.cprogramming.com/tutorial/style_naming_conventions.html&lt;br /&gt;
* http://www.uwe-sauerland.de/richtlinien/naming.html&lt;br /&gt;
* http://www.kamath.com/columns/squareone/so001_whatname1.asp&lt;br /&gt;
* http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html&lt;br /&gt;
* http://en.wikipedia.org/wiki/Identifier_naming_convention&lt;br /&gt;
* http://www.erlang.se/doc/programming_rules.shtml&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=14077</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=14077"/>
		<updated>2008-06-26T02:35:02Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Common conventions */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  It's always important to choose descriptive names with straightforward meanings.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions advantages ===&lt;br /&gt;
* to provide additional information (ie, metadata) about the use to which an identifier is put&lt;br /&gt;
* to help formalize expectations and promote consistency within a development team&lt;br /&gt;
* to enable the use of automated refactoring or search and replace tools with minimal potential for error&lt;br /&gt;
* to enhance clarity in cases of potential ambiguity&lt;br /&gt;
* to enhance the aesthetic and professional appearance of work product (for example, by disallowing overly long names, comical or &amp;quot;cute&amp;quot; names, or abbreviations)&lt;br /&gt;
* to help avoid &amp;quot;naming collisions&amp;quot; that might occur when the work product of different organizations is combined&lt;br /&gt;
* to provide meaningful data to be used in project handovers which require submission of program source code and all relevant documentation&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions disadvantages ===&lt;br /&gt;
* When a variable type is changed, you may have to go through and change ''all'' the instances of that variable within your code.  The find-and-replace feature of many editors alleviates this issue a bit, but it may still be tedious.&lt;br /&gt;
* If your language changes, i.e. is updated, code may need to be changed so the naming convention still fits.&lt;br /&gt;
* Naming conventions may defeat the purpose of encapsulation.  The &amp;quot;black box&amp;quot; aspect is eliminated when the programmer needs to know what specific type is being returned.&lt;br /&gt;
&lt;br /&gt;
=== Common conventions ===&lt;br /&gt;
* Packages should have prefixes always written in all-lowercase ASCII letters and they should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.  Examples: &amp;lt;tt&amp;gt;com.sun.eng&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;com.apple.quicktime.v2&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;edu.cmu.cs.bovik.cheese&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* Class names should be nouns, in mixed case with the first letter of each word capitalized.  They should be simple and descriptive, and whole words should be used (avoid acronyms and abbreviations).  Examples: &amp;lt;tt&amp;gt;class Roster;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;class ImageSprite;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* Interface names should be capitalized like class names.  Examples: &amp;lt;tt&amp;gt;interface RosterDelegate;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;interface Storing;&amp;lt;/tt&amp;gt;. &lt;br /&gt;
* Lowercase letters should be used for variable and method names.  A name consisting of several words should be concatenated into one word (take out the spaces).  The first word should be all lowercase, and each subsequent word (the internal words) should have its first letter capitalized.  Methods should be verbs.  Examples: &amp;lt;tt&amp;gt;run();&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;runFast();&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;getBackground();&amp;lt;/tt&amp;gt;&lt;br /&gt;
* Variable names should be short yet meaningful. The choice of a variable name should be mnemonic- that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary &amp;quot;throwaway&amp;quot; variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.  Variable names should not start with underscore (&amp;quot;&amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt;&amp;quot;) or dollar sign (&amp;quot;&amp;lt;tt&amp;gt;$&amp;lt;/tt&amp;gt;&amp;quot;) characters, even though both are allowed.  Examples: &amp;lt;tt&amp;gt;int i;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;char c;&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;float myWidth;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
* The names of variables declared ''class constants'' and of ''ANSI constants'' should be all uppercase with words separated by underscores (&amp;quot;&amp;lt;tt&amp;gt;_&amp;lt;/tt&amp;gt;&amp;quot;). (ANSI constants should be avoided, for ease of debugging.)  Examples: &amp;lt;tt&amp;gt;static final int MIN_WIDTH = 4;&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;static final int MAX_WIDTH = 999;&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;static final int GET_THE_CPU = 1;&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Final conclusions ===&lt;br /&gt;
The advantages of using naming conventions outweigh the disadvantages.  Readability is the biggest derived benefit, and its importance cannot be overstated.  Variable types, as well as their purpose (for classes and modules too) can be determined at a quick glance.  Readers unfamiliar with the code can derive meaning and purpose quicker, without having to study large portions of the code and refer to various places within.  The following tips should be used when choosing names for variables, classes, modules, etc.:&lt;br /&gt;
&lt;br /&gt;
* Prefix the variable names with their scope and their data types &lt;br /&gt;
* Use meaningful variable names &lt;br /&gt;
* Be wary of long names &lt;br /&gt;
* Use your conventions consistently&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
* Liang, Y. Daniel.  [http://books.google.com/books?id=juuPxtSCg50C Introduction to Java Programming: Comprehensive].  Pearson Prentice Hall, 2008.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
* http://frontier.userland.com/tutorial/variableNaming&lt;br /&gt;
* http://www.programmers-corner.com/article/18&lt;br /&gt;
* http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html&lt;br /&gt;
* http://www.cprogramming.com/tutorial/style_naming_conventions.html&lt;br /&gt;
* http://www.uwe-sauerland.de/richtlinien/naming.html&lt;br /&gt;
* http://www.kamath.com/columns/squareone/so001_whatname1.asp&lt;br /&gt;
* http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html&lt;br /&gt;
* http://en.wikipedia.org/wiki/Identifier_naming_convention&lt;br /&gt;
* http://www.erlang.se/doc/programming_rules.shtml&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=14012</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=14012"/>
		<updated>2008-06-26T01:47:39Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Final conclusions and tips */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  It's always important to choose descriptive names with straightforward meanings.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions advantages ===&lt;br /&gt;
* to provide additional information (ie, metadata) about the use to which an identifier is put&lt;br /&gt;
* to help formalize expectations and promote consistency within a development team&lt;br /&gt;
* to enable the use of automated refactoring or search and replace tools with minimal potential for error&lt;br /&gt;
* to enhance clarity in cases of potential ambiguity&lt;br /&gt;
* to enhance the aesthetic and professional appearance of work product (for example, by disallowing overly long names, comical or &amp;quot;cute&amp;quot; names, or abbreviations)&lt;br /&gt;
* to help avoid &amp;quot;naming collisions&amp;quot; that might occur when the work product of different organizations is combined&lt;br /&gt;
* to provide meaningful data to be used in project handovers which require submission of program source code and all relevant documentation&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions disadvantages ===&lt;br /&gt;
* When a variable type is changed, you may have to go through and change ''all'' the instances of that variable within your code.  The find-and-replace feature of many editors alleviates this issue a bit, but it may still be tedious.&lt;br /&gt;
* If your language changes, i.e. is updated, code may need to be changed so the naming convention still fits.&lt;br /&gt;
* Naming conventions may defeat the purpose of encapsulation.  The &amp;quot;black box&amp;quot; aspect is eliminated when the programmer needs to know what specific type is being returned.&lt;br /&gt;
&lt;br /&gt;
=== Common conventions ===&lt;br /&gt;
* Lowercase letters should be used for variable and method names.  A name consisting of several words should be concatenated into one word (take out the spaces).  The first word should be all lowercase, and each subsequent word should have its first letter capitalized.&lt;br /&gt;
* Class names should have the first letter of each word capitalized.  Multi-worded names should follow the rule above, except that the first word should also be capitalized.&lt;br /&gt;
* Contants should have every letter capitalized, with underscores between words.&lt;br /&gt;
&lt;br /&gt;
=== Final conclusions ===&lt;br /&gt;
The advantages of using naming conventions outweigh the disadvantages.  Readability is the biggest derived benefit, and its importance cannot be overstated.  Variable types, as well as their purpose (for classes and modules too) can be determined at a quick glance.  Readers unfamiliar with the code can derive meaning and purpose quicker, without having to study large portions of the code and refer to various places within.  The following tips should be used when choosing names for variables, classes, modules, etc.:&lt;br /&gt;
&lt;br /&gt;
* Prefix the variable names with their scope and their data types &lt;br /&gt;
* Use meaningful variable names &lt;br /&gt;
* Be wary of long names &lt;br /&gt;
* Use your conventions consistently&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
* Liang, Y. Daniel.  [http://books.google.com/books?id=juuPxtSCg50C Introduction to Java Programming: Comprehensive].  Pearson Prentice Hall, 2008.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
* http://frontier.userland.com/tutorial/variableNaming&lt;br /&gt;
* http://www.programmers-corner.com/article/18&lt;br /&gt;
* http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html&lt;br /&gt;
* http://www.cprogramming.com/tutorial/style_naming_conventions.html&lt;br /&gt;
* http://www.uwe-sauerland.de/richtlinien/naming.html&lt;br /&gt;
* http://www.kamath.com/columns/squareone/so001_whatname1.asp&lt;br /&gt;
* http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html&lt;br /&gt;
* http://en.wikipedia.org/wiki/Identifier_naming_convention&lt;br /&gt;
* http://www.erlang.se/doc/programming_rules.shtml&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=14005</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=14005"/>
		<updated>2008-06-26T01:40:53Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Variable, Class, and Module Naming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  It's always important to choose descriptive names with straightforward meanings.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions advantages ===&lt;br /&gt;
* to provide additional information (ie, metadata) about the use to which an identifier is put&lt;br /&gt;
* to help formalize expectations and promote consistency within a development team&lt;br /&gt;
* to enable the use of automated refactoring or search and replace tools with minimal potential for error&lt;br /&gt;
* to enhance clarity in cases of potential ambiguity&lt;br /&gt;
* to enhance the aesthetic and professional appearance of work product (for example, by disallowing overly long names, comical or &amp;quot;cute&amp;quot; names, or abbreviations)&lt;br /&gt;
* to help avoid &amp;quot;naming collisions&amp;quot; that might occur when the work product of different organizations is combined&lt;br /&gt;
* to provide meaningful data to be used in project handovers which require submission of program source code and all relevant documentation&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions disadvantages ===&lt;br /&gt;
* When a variable type is changed, you may have to go through and change ''all'' the instances of that variable within your code.  The find-and-replace feature of many editors alleviates this issue a bit, but it may still be tedious.&lt;br /&gt;
* If your language changes, i.e. is updated, code may need to be changed so the naming convention still fits.&lt;br /&gt;
* Naming conventions may defeat the purpose of encapsulation.  The &amp;quot;black box&amp;quot; aspect is eliminated when the programmer needs to know what specific type is being returned.&lt;br /&gt;
&lt;br /&gt;
=== Common conventions ===&lt;br /&gt;
* Lowercase letters should be used for variable and method names.  A name consisting of several words should be concatenated into one word (take out the spaces).  The first word should be all lowercase, and each subsequent word should have its first letter capitalized.&lt;br /&gt;
* Class names should have the first letter of each word capitalized.  Multi-worded names should follow the rule above, except that the first word should also be capitalized.&lt;br /&gt;
* Contants should have every letter capitalized, with underscores between words.&lt;br /&gt;
&lt;br /&gt;
=== Final conclusions and tips ===&lt;br /&gt;
&lt;br /&gt;
* Prefix the variable names with their scope and their data types &lt;br /&gt;
* Use meaningful variable names &lt;br /&gt;
* Be wary of long names &lt;br /&gt;
* Use your conventions consistently&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
* Liang, Y. Daniel.  [http://books.google.com/books?id=juuPxtSCg50C Introduction to Java Programming: Comprehensive].  Pearson Prentice Hall, 2008.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
* http://frontier.userland.com/tutorial/variableNaming&lt;br /&gt;
* http://www.programmers-corner.com/article/18&lt;br /&gt;
* http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html&lt;br /&gt;
* http://www.cprogramming.com/tutorial/style_naming_conventions.html&lt;br /&gt;
* http://www.uwe-sauerland.de/richtlinien/naming.html&lt;br /&gt;
* http://www.kamath.com/columns/squareone/so001_whatname1.asp&lt;br /&gt;
* http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html&lt;br /&gt;
* http://en.wikipedia.org/wiki/Identifier_naming_convention&lt;br /&gt;
* http://www.erlang.se/doc/programming_rules.shtml&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13993</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13993"/>
		<updated>2008-06-26T01:33:46Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Naming conventions disadvantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  It's always important to choose descriptive names with straightforward meanings.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions advantages ===&lt;br /&gt;
* to provide additional information (ie, metadata) about the use to which an identifier is put&lt;br /&gt;
* to help formalize expectations and promote consistency within a development team&lt;br /&gt;
* to enable the use of automated refactoring or search and replace tools with minimal potential for error&lt;br /&gt;
* to enhance clarity in cases of potential ambiguity&lt;br /&gt;
* to enhance the aesthetic and professional appearance of work product (for example, by disallowing overly long names, comical or &amp;quot;cute&amp;quot; names, or abbreviations)&lt;br /&gt;
* to help avoid &amp;quot;naming collisions&amp;quot; that might occur when the work product of different organizations is combined&lt;br /&gt;
* to provide meaningful data to be used in project handovers which require submission of program source code and all relevant documentation&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions disadvantages ===&lt;br /&gt;
* When a variable type is changed, you may have to go through and change ''all'' the instances of that variable within your code.  The find-and-replace feature of many editors alleviates this issue a bit, but it may still be tedious.&lt;br /&gt;
&lt;br /&gt;
* If your language changes, i.e. is updated, code may need to be changed so the naming convention still fits.&lt;br /&gt;
&lt;br /&gt;
* Naming conventions may defeat the purpose of encapsulation.  The &amp;quot;black box&amp;quot; aspect is eliminated when the programmer needs to know what specific type is being returned.&lt;br /&gt;
&lt;br /&gt;
=== Common conventions ===&lt;br /&gt;
* Lowercase letters should be used for variable and method names.  A name consisting of several words should be concatenated into one word (take out the spaces).  The first word should be all lowercase, and each subsequent word should have its first letter capitalized.&lt;br /&gt;
* Class names should have the first letter of each word capitalized.  Multi-worded names should follow the rule above, except that the first word should also be capitalized.&lt;br /&gt;
* Contants should have every letter capitalized, with underscores between words.&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
* Liang, Y. Daniel.  [http://books.google.com/books?id=juuPxtSCg50C Introduction to Java Programming: Comprehensive].  Pearson Prentice Hall, 2008.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
* http://frontier.userland.com/tutorial/variableNaming&lt;br /&gt;
* http://www.programmers-corner.com/article/18&lt;br /&gt;
* http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html&lt;br /&gt;
* http://www.cprogramming.com/tutorial/style_naming_conventions.html&lt;br /&gt;
* http://www.uwe-sauerland.de/richtlinien/naming.html&lt;br /&gt;
* http://www.kamath.com/columns/squareone/so001_whatname1.asp&lt;br /&gt;
* http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html&lt;br /&gt;
* http://en.wikipedia.org/wiki/Identifier_naming_convention&lt;br /&gt;
* http://www.erlang.se/doc/programming_rules.shtml&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13981</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13981"/>
		<updated>2008-06-26T01:24:01Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Variable, Class, and Module Naming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  It's always important to choose descriptive names with straightforward meanings.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions advantages ===&lt;br /&gt;
* to provide additional information (ie, metadata) about the use to which an identifier is put&lt;br /&gt;
* to help formalize expectations and promote consistency within a development team&lt;br /&gt;
* to enable the use of automated refactoring or search and replace tools with minimal potential for error&lt;br /&gt;
* to enhance clarity in cases of potential ambiguity&lt;br /&gt;
* to enhance the aesthetic and professional appearance of work product (for example, by disallowing overly long names, comical or &amp;quot;cute&amp;quot; names, or abbreviations)&lt;br /&gt;
* to help avoid &amp;quot;naming collisions&amp;quot; that might occur when the work product of different organizations is combined&lt;br /&gt;
* to provide meaningful data to be used in project handovers which require submission of program source code and all relevant documentation&lt;br /&gt;
&lt;br /&gt;
=== Naming conventions disadvantages ===&lt;br /&gt;
* First, what happens when you change a variable type? You have to go change ALL the instances of that variable within your code. Some editors have some pretty sophisticated find-and-replace tools, so this wouldn't be that big of an issue... but it still needs to be done.&lt;br /&gt;
&lt;br /&gt;
Second, what happens if you upgrade your language, and what was a 16-bit integer is now a 32-bit integer? Now you have to go change portions of your code so your naming convention still fits...&lt;br /&gt;
&lt;br /&gt;
Third, naming conventions can defeat the purpose of encapsulation. Encapsulation means &amp;quot;black box&amp;quot; right? So why does the programmer need to know what specific type is being returned? They just create a variable of that class, and then use it. Who cares if it’s a long, or a string, or an object.&lt;br /&gt;
&lt;br /&gt;
So, do the disadvantages outweigh the advantages?  No... I would still use naming conventions for the simple fact of readability.  As I stated before, using naming conventions in your programming allows you to read your variable types (and their purpose) at a glance... No need to go looking for a declaration.&lt;br /&gt;
&lt;br /&gt;
=== Common conventions ===&lt;br /&gt;
* Lowercase letters should be used for variable and method names.  A name consisting of several words should be concatenated into one word (take out the spaces).  The first word should be all lowercase, and each subsequent word should have its first letter capitalized.&lt;br /&gt;
* Class names should have the first letter of each word capitalized.  Multi-worded names should follow the rule above, except that the first word should also be capitalized.&lt;br /&gt;
* Contants should have every letter capitalized, with underscores between words.&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
* Liang, Y. Daniel.  [http://books.google.com/books?id=juuPxtSCg50C Introduction to Java Programming: Comprehensive].  Pearson Prentice Hall, 2008.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
* http://frontier.userland.com/tutorial/variableNaming&lt;br /&gt;
* http://www.programmers-corner.com/article/18&lt;br /&gt;
* http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html&lt;br /&gt;
* http://www.cprogramming.com/tutorial/style_naming_conventions.html&lt;br /&gt;
* http://www.uwe-sauerland.de/richtlinien/naming.html&lt;br /&gt;
* http://www.kamath.com/columns/squareone/so001_whatname1.asp&lt;br /&gt;
* http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html&lt;br /&gt;
* http://en.wikipedia.org/wiki/Identifier_naming_convention&lt;br /&gt;
* http://www.erlang.se/doc/programming_rules.shtml&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13859</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13859"/>
		<updated>2008-06-25T18:42:07Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* ''External Links'' */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  It's always important to choose descriptive names with straightforward meanings.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;br /&gt;
&lt;br /&gt;
=== Common conventions ===&lt;br /&gt;
* Lowercase letters should be used for variable and method names.  A name consisting of several words should be concatenated into one word (take out the spaces).  The first word should be all lowercase, and each subsequent word should have its first letter capitalized.&lt;br /&gt;
* Class names should have the first letter of each word capitalized.  Multi-worded names should follow the rule above, except that the first word should also be capitalized.&lt;br /&gt;
* Contants should have every letter capitalized, with underscores between words.&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
* Liang, Y. Daniel.  [http://books.google.com/books?id=juuPxtSCg50C Introduction to Java Programming: Comprehensive].  Pearson Prentice Hall, 2008.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
* http://frontier.userland.com/tutorial/variableNaming&lt;br /&gt;
* http://www.programmers-corner.com/article/18&lt;br /&gt;
* http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html&lt;br /&gt;
* http://www.cprogramming.com/tutorial/style_naming_conventions.html&lt;br /&gt;
* http://www.uwe-sauerland.de/richtlinien/naming.html&lt;br /&gt;
* http://www.kamath.com/columns/squareone/so001_whatname1.asp&lt;br /&gt;
* http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html&lt;br /&gt;
* http://www.erlang.se/doc/programming_rules.shtml&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13858</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13858"/>
		<updated>2008-06-25T18:40:54Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Variable, Class, and Module Naming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  It's always important to choose descriptive names with straightforward meanings.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;br /&gt;
&lt;br /&gt;
=== Common conventions ===&lt;br /&gt;
* Lowercase letters should be used for variable and method names.  A name consisting of several words should be concatenated into one word (take out the spaces).  The first word should be all lowercase, and each subsequent word should have its first letter capitalized.&lt;br /&gt;
* Class names should have the first letter of each word capitalized.  Multi-worded names should follow the rule above, except that the first word should also be capitalized.&lt;br /&gt;
* Contants should have every letter capitalized, with underscores between words.&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
* Liang, Y. Daniel.  [http://books.google.com/books?id=juuPxtSCg50C Introduction to Java Programming: Comprehensive].  Pearson Prentice Hall, 2008.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
* http://frontier.userland.com/tutorial/variableNaming&lt;br /&gt;
* http://www.programmers-corner.com/article/18&lt;br /&gt;
* http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html&lt;br /&gt;
* http://www.cprogramming.com/tutorial/style_naming_conventions.html&lt;br /&gt;
* http://www.uwe-sauerland.de/richtlinien/naming.html&lt;br /&gt;
* http://www.kamath.com/columns/squareone/so001_whatname1.asp&lt;br /&gt;
* http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13852</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13852"/>
		<updated>2008-06-25T18:28:55Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
* Liang, Y. Daniel.  [http://books.google.com/books?id=juuPxtSCg50C Introduction to Java Programming: Comprehensive].  Pearson Prentice Hall, 2008.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
* http://frontier.userland.com/tutorial/variableNaming&lt;br /&gt;
* http://www.programmers-corner.com/article/18&lt;br /&gt;
* http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html&lt;br /&gt;
* http://www.cprogramming.com/tutorial/style_naming_conventions.html&lt;br /&gt;
* http://www.uwe-sauerland.de/richtlinien/naming.html&lt;br /&gt;
* http://www.kamath.com/columns/squareone/so001_whatname1.asp&lt;br /&gt;
* http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13851</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13851"/>
		<updated>2008-06-25T18:28:20Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Variable, Class, and Module Naming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;br /&gt;
&lt;br /&gt;
=== References ===&lt;br /&gt;
* Liang, Y. Daniel.  [http://books.google.com/books?id=juuPxtSCg50C Introduction to Java Programming: Comprehensive].  Pearson Prentice Hall, 2008.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
* http://frontier.userland.com/tutorial/variableNaming&lt;br /&gt;
* http://www.programmers-corner.com/article/18&lt;br /&gt;
* http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html&lt;br /&gt;
* http://www.cprogramming.com/tutorial/style_naming_conventions.html&lt;br /&gt;
* http://www.uwe-sauerland.de/richtlinien/naming.html&lt;br /&gt;
* http://www.kamath.com/columns/squareone/so001_whatname1.asp&lt;br /&gt;
* http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13843</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13843"/>
		<updated>2008-06-25T18:13:22Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* ''External Links'' */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
* http://frontier.userland.com/tutorial/variableNaming&lt;br /&gt;
* http://www.programmers-corner.com/article/18&lt;br /&gt;
* http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html&lt;br /&gt;
* http://www.cprogramming.com/tutorial/style_naming_conventions.html&lt;br /&gt;
* http://www.uwe-sauerland.de/richtlinien/naming.html&lt;br /&gt;
* http://www.kamath.com/columns/squareone/so001_whatname1.asp&lt;br /&gt;
* http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13839</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13839"/>
		<updated>2008-06-25T17:52:39Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* ''External Links'' */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
* http://frontier.userland.com/tutorial/variableNaming&lt;br /&gt;
* http://www.programmers-corner.com/article/18&lt;br /&gt;
* http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html&lt;br /&gt;
* http://www.cprogramming.com/tutorial/style_naming_conventions.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13837</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13837"/>
		<updated>2008-06-25T17:51:02Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Variable, Class, and Module Naming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
* http://frontier.userland.com/tutorial/variableNaming&lt;br /&gt;
* http://www.programmers-corner.com/article/18&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13836</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13836"/>
		<updated>2008-06-25T17:49:47Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Variable, Class, and Module Naming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
* http://frontier.userland.com/tutorial/variableNaming *&lt;br /&gt;
* http://www.programmers-corner.com/article/18 *&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13832</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13832"/>
		<updated>2008-06-25T17:43:52Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Variable, Class, and Module Naming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
When programming, choosing appropriate names for variables, classes, modules, etc. can often be taken too lightly.  Certain conventions should be followed to ensure code can be read, followed, and edited easily.&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13829</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13829"/>
		<updated>2008-06-25T17:29:47Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Variable, Class, and Module Naming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Variable, Class, and Module Naming ==&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13828</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13828"/>
		<updated>2008-06-25T17:29:31Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Variable, Class, and Module Naming =&lt;br /&gt;
&lt;br /&gt;
=== Introduction ===&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13825</id>
		<title>CSC/ECE 517 Summer 2008/wiki2 2 ao</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki2_2_ao&amp;diff=13825"/>
		<updated>2008-06-25T17:26:07Z</updated>

		<summary type="html">&lt;p&gt;Amosti: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=13137</id>
		<title>CSC/ECE 517 Summer 2008/wiki1 3 aobk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=13137"/>
		<updated>2008-06-12T03:02:18Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* What is ''Reflection'' in computer science? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection in Ruby and Java ==&lt;br /&gt;
;&lt;br /&gt;
===What is ''Reflection'' in computer science?=== &lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;0&amp;quot;&lt;br /&gt;
|width=&amp;quot;550&amp;quot;|Reflection, in the realm of Computer Science, is the process of introspection by a program.  Introspection allows program entities to discover things about themselves.  Through the technique of ''metaprogramming'', aspects of a program are examined from within the program itself, which enables the program to modify itself at runtime.  This provides for a very liberated and dynamic programming environment.&lt;br /&gt;
|width=&amp;quot;50&amp;quot;|&lt;br /&gt;
|width=&amp;quot;200&amp;quot;|[[Image:linear.png|Reflection]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By looking at and inside classes and objects, much can be discovered, including the following:&lt;br /&gt;
* what objects are contained in a program&lt;br /&gt;
* the class hierarchy&lt;br /&gt;
* the attributes and methods of the objects contained in the program&lt;br /&gt;
* various method information&lt;br /&gt;
&lt;br /&gt;
One of the design challenges when using Reflection is that you need to clarify what services you wish to provide at runtime.&lt;br /&gt;
&lt;br /&gt;
=== Java Reflection Sequences ===&lt;br /&gt;
;''Reflection in Java''&lt;br /&gt;
:Java uses a '''Reflection API''' to inspect and manipulate itself.  It includes the expanded ''class'' class in java.lang and the java.lang.reflect package, which represents the members of a class with ''Method'', ''Constructor'' and ''Field'' objects.Reflection can be used to obtain information about a class and its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
*This example uses the &amp;lt;tt&amp;gt;Class.forName()&amp;lt;/tt&amp;gt; method to dynamically load the named class, and then calls various methods of the ''class'' object to look up the Superclass, Interfaces, and Members of the class. The example uses ''Constructor'', ''Field'' and ''Method'' objects to obtain information about each member of the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
&lt;br /&gt;
/** The main method.  Print info about the named class */&lt;br /&gt;
&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Display the modifiers, name, superclass and interfaces of a class&lt;br /&gt;
   *  or interface. Then go and list all constructors, fields, and methods. */&lt;br /&gt;
&lt;br /&gt;
  public static void print_class(Class c)&lt;br /&gt;
  {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot;+c.getName());&lt;br /&gt;
    }&lt;br /&gt;
    else if (c.getSuperclass() != null)&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName() +&lt;br /&gt;
                       &amp;quot; extends &amp;quot; + c.getSuperclass().getName());&lt;br /&gt;
    else&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName());&lt;br /&gt;
        &lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface()) System.out.println(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for(int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(interfaces[i].getName());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;);            // Begin class member listing.&lt;br /&gt;
&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for(int i = 0; i &amp;lt; constructors.length; i++)      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields();           // Look up fields.&lt;br /&gt;
    for(int i = 0; i &amp;lt; fields.length; i++)            // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods();        // Look up methods.&lt;br /&gt;
    for(int i = 0; i &amp;lt; methods.length; i++)           // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;);             // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  &lt;br /&gt;
public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while(t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    return t.getName() + brackets;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
 &lt;br /&gt;
 public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0) return &amp;quot;&amp;quot;;&lt;br /&gt;
    else return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Print the modifiers, type, and name of a field */&lt;br /&gt;
&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; +&lt;br /&gt;
                       modifiers(f.getModifiers()) +&lt;br /&gt;
                       typename(f.getType()) + &amp;quot; &amp;quot; + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, return type, name, parameter types and exception&lt;br /&gt;
   *  type of a method or constructor.  Note the use of the Member interface&lt;br /&gt;
   *  to allow this method to work with both Method and Constructor objects */&lt;br /&gt;
&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype=null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) +&lt;br /&gt;
                     ((returntype!=null)? typename(returntype)+&amp;quot; &amp;quot; : &amp;quot;&amp;quot;) +&lt;br /&gt;
                     member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0) System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''This code is then compiled and run, with the following simple test class passed in:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput {&lt;br /&gt;
    public static void main(String[] args) {}&lt;br /&gt;
    public int method1() {&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public String method2() {&lt;br /&gt;
        return &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    public void method3() {}&lt;br /&gt;
    public void method4(int num) {}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''The output generated is as follows:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput extends java.lang.Object {&lt;br /&gt;
 &lt;br /&gt;
// Constructors&lt;br /&gt;
  public testInput();&lt;br /&gt;
 // Fields&lt;br /&gt;
 // Methods&lt;br /&gt;
  public int method1();&lt;br /&gt;
  public java.lang.String method2();&lt;br /&gt;
  public void method3();&lt;br /&gt;
  public void method4(int);&lt;br /&gt;
  public static void main(java.lang.String[]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection Sequences ===&lt;br /&gt;
The Java code above, which displays constructors, methods, and other information pertinent to the ''testInput'' class, is somewhat convoluted and difficult to trace.  In Ruby, the methods below can be called on a module/class to return similar information:&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.private_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.protected_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.public_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.singleton_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
For example, using the &amp;lt;tt&amp;gt;Demo&amp;lt;/tt&amp;gt; class below, all of the class' methods (private, protected, public, and singleton) can be easily displayed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; class Demo&lt;br /&gt;
irb(main):002:1&amp;gt;   public&lt;br /&gt;
irb(main):003:1&amp;gt;     def method1&lt;br /&gt;
irb(main):004:2&amp;gt;   end&lt;br /&gt;
irb(main):005:1&amp;gt;   private&lt;br /&gt;
irb(main):006:1&amp;gt;     def method2&lt;br /&gt;
irb(main):007:2&amp;gt;   end&lt;br /&gt;
irb(main):008:1&amp;gt;   public&lt;br /&gt;
irb(main):009:1&amp;gt;     def method3&lt;br /&gt;
irb(main):010:2&amp;gt;   end&lt;br /&gt;
irb(main):011:1&amp;gt;   protected&lt;br /&gt;
irb(main):012:1&amp;gt;     def method4&lt;br /&gt;
irb(main):013:2&amp;gt;   end&lt;br /&gt;
irb(main):014:1&amp;gt;   def Demo.method5&lt;br /&gt;
irb(main):015:2&amp;gt;   end&lt;br /&gt;
irb(main):016:1&amp;gt;   public&lt;br /&gt;
irb(main):017:1&amp;gt;     def method6&lt;br /&gt;
irb(main):018:2&amp;gt;   end&lt;br /&gt;
irb(main):019:1&amp;gt; end&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):020:0&amp;gt; Demo.private_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method2&amp;quot;]&lt;br /&gt;
irb(main):021:0&amp;gt; Demo.protected_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method4&amp;quot;]&lt;br /&gt;
irb(main):022:0&amp;gt; Demo.public_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method3&amp;quot;, &amp;quot;method6&amp;quot;, &amp;quot;method1&amp;quot;]&lt;br /&gt;
irb(main):023:0&amp;gt; Demo.singleton_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method5&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to looking inside classes, reflection in Ruby provides the ability to dynamically call methods.  An arbitrary method of some object can be called using &amp;lt;tt&amp;gt;Object#send&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;Method#call&amp;lt;/tt&amp;gt;, and also various versions of the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method (including &amp;lt;tt&amp;gt;class_eval&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;module_eval&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;instance_eval&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;send&amp;lt;/tt&amp;gt; works on any object, and it calls the passed in method on that object.  It will also find new methods added at runtime.  In the below example, the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; method is called on the object, which is a 14 character string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):105:0&amp;gt; &amp;quot;This is a test&amp;quot;.send(:length)&lt;br /&gt;
=&amp;gt; 14&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; objects represent a chunk of code and a context in which it executes.  The &amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; object is created, and it can be executed whenever desired by sending it the &amp;lt;tt&amp;gt;call&amp;lt;/tt&amp;gt; message:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):106:0&amp;gt; test_message = &amp;quot;Test message&amp;quot;.method(:length)&lt;br /&gt;
=&amp;gt; #&amp;lt;Method: String#length&amp;gt;&lt;br /&gt;
irb(main):107:0&amp;gt; test_message.call&lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A third way methods can be invoked dynamically using reflection is with the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method.  &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; provides the ability to parse and execute an arbitrary string of legal Ruby source code.  The string of code can be stored as an object, and then evaluated at a later time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):111:0&amp;gt; eval_test = %q{&amp;quot;A third test&amp;quot;.length}&lt;br /&gt;
=&amp;gt; &amp;quot;\&amp;quot;A third test\&amp;quot;.length&amp;quot;&lt;br /&gt;
irb(main):112:0&amp;gt; eval eval_test  &lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection vs. Java Reflection ===&lt;br /&gt;
&lt;br /&gt;
*By coding and comparing we find that Reflection in Java tends to be more verbose than Ruby code which accomplishes the same task.  Also, exceptions such as &amp;lt;tt&amp;gt;NoSuchMethodError&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;InvocationTargetException&amp;lt;/tt&amp;gt; tend to create further complications and confusion when using Java's reflection API.&lt;br /&gt;
**In order to print out all the methods contained in a class in Java, we approached 100 lines of code.  It took merely 4 lines of code in Ruby to print all private, protected, public, and singleton methods of a class.  This illustrates Ruby efficiency when looking inside classes.&lt;br /&gt;
&lt;br /&gt;
*Language facilities such as ''aliasing'', ''reflection'', and ''metaprogramming'' make Aspect Oriented Programming much easier to perform in Ruby than in Java.&lt;br /&gt;
&lt;br /&gt;
=== Uses of Reflection ===&lt;br /&gt;
Reflection is used by programs which require the ability to analyze or modify the runtime behavior of applications.This is a relatively advanced feature.Thus, reflection is a powerful technique which can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
*Extensibility Features: &lt;br /&gt;
An application may utilize 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;
A class browser could 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;
Debuggers could examine private members on classes. Test harnesses could use 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;
*Reflection API in java is also used in Database Access with SQL.&lt;br /&gt;
&lt;br /&gt;
=== Drawbacks of Reflection ===&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately.  It is preferable to avoid using reflection, if possible.  The following concerns should be kept in mind when accessing code via reflection:&lt;br /&gt;
&lt;br /&gt;
*Performance Overhead &lt;br /&gt;
Certain Java virtual machine optimizations, which greatly increase performance in Java applications, cannot be performed when reflective operations are used due to the dynamically resolved types involved in reflection.  Therefore, reflection should be avoided in sections of code called frequently in performance-sensitive applications.&lt;br /&gt;
&lt;br /&gt;
*Security Restrictions &lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager.  This is important for code running in a restricted security contect, e.g. in an Applet.&lt;br /&gt;
&lt;br /&gt;
*Exposure of Internals&lt;br /&gt;
Operations which would be illegal in a non-reflective environment, such as accessing private fields and methods, are allowed in reflective code.  Consequently, unexpected side-effects, which may render code dysfunctional and destroy portability, may result from the use of reflection.  Abstractions may also be broken, thus changing behavior with platform upgrades.&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
#Flanagan, David.  [http://books.google.com/books?id=kADTIsgeLh4C Java Examples in a Nutshell].  O'Reilly, 2004.&lt;br /&gt;
#Thomas, Dave.  [http://books.google.com/books?id=8LXOAAAACAAJ Programming Ruby].  Raleigh: Pragmatic Bookshelf, 2004.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
*http://phrogz.net/programmingruby/ospace.html&lt;br /&gt;
*http://java.sun.com/docs/books/tutorial/reflect/&lt;br /&gt;
*http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html&lt;br /&gt;
*http://www.javaworld.com/javaworld/jw-07-2006/jw-0717-ruby.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=13134</id>
		<title>CSC/ECE 517 Summer 2008/wiki1 3 aobk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=13134"/>
		<updated>2008-06-12T03:00:53Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* What is ''Reflection'' in computer science? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection in Ruby and Java ==&lt;br /&gt;
;&lt;br /&gt;
===What is ''Reflection'' in computer science?=== &lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;0&amp;quot; cellpadding=&amp;quot;0&amp;quot;&lt;br /&gt;
|width=&amp;quot;550&amp;quot;|Reflection, in the realm of Computer Science, is the process of introspection by a program.  Introspection allows program entities to discover things about themselves.  Through the technique of ''metaprogramming'', aspects of a program are examined from within the program itself, which enables the program to modify itself at runtime.  This provides for a very liberated and dynamic programming environment.&lt;br /&gt;
|width=&amp;quot;50&amp;quot;|&lt;br /&gt;
|width=&amp;quot;200&amp;quot;|[[Image:linear.png]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
By looking at and inside classes and objects, much can be discovered, including the following:&lt;br /&gt;
* what objects are contained in a program&lt;br /&gt;
* the class hierarchy&lt;br /&gt;
* the attributes and methods of the objects contained in the program&lt;br /&gt;
* various method information&lt;br /&gt;
&lt;br /&gt;
One of the design challenges when using Reflection is that you need to clarify what services you wish to provide at runtime.&lt;br /&gt;
&lt;br /&gt;
=== Java Reflection Sequences ===&lt;br /&gt;
;''Reflection in Java''&lt;br /&gt;
:Java uses a '''Reflection API''' to inspect and manipulate itself.  It includes the expanded ''class'' class in java.lang and the java.lang.reflect package, which represents the members of a class with ''Method'', ''Constructor'' and ''Field'' objects.Reflection can be used to obtain information about a class and its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
*This example uses the &amp;lt;tt&amp;gt;Class.forName()&amp;lt;/tt&amp;gt; method to dynamically load the named class, and then calls various methods of the ''class'' object to look up the Superclass, Interfaces, and Members of the class. The example uses ''Constructor'', ''Field'' and ''Method'' objects to obtain information about each member of the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
&lt;br /&gt;
/** The main method.  Print info about the named class */&lt;br /&gt;
&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Display the modifiers, name, superclass and interfaces of a class&lt;br /&gt;
   *  or interface. Then go and list all constructors, fields, and methods. */&lt;br /&gt;
&lt;br /&gt;
  public static void print_class(Class c)&lt;br /&gt;
  {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot;+c.getName());&lt;br /&gt;
    }&lt;br /&gt;
    else if (c.getSuperclass() != null)&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName() +&lt;br /&gt;
                       &amp;quot; extends &amp;quot; + c.getSuperclass().getName());&lt;br /&gt;
    else&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName());&lt;br /&gt;
        &lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface()) System.out.println(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for(int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(interfaces[i].getName());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;);            // Begin class member listing.&lt;br /&gt;
&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for(int i = 0; i &amp;lt; constructors.length; i++)      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields();           // Look up fields.&lt;br /&gt;
    for(int i = 0; i &amp;lt; fields.length; i++)            // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods();        // Look up methods.&lt;br /&gt;
    for(int i = 0; i &amp;lt; methods.length; i++)           // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;);             // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  &lt;br /&gt;
public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while(t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    return t.getName() + brackets;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
 &lt;br /&gt;
 public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0) return &amp;quot;&amp;quot;;&lt;br /&gt;
    else return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Print the modifiers, type, and name of a field */&lt;br /&gt;
&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; +&lt;br /&gt;
                       modifiers(f.getModifiers()) +&lt;br /&gt;
                       typename(f.getType()) + &amp;quot; &amp;quot; + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, return type, name, parameter types and exception&lt;br /&gt;
   *  type of a method or constructor.  Note the use of the Member interface&lt;br /&gt;
   *  to allow this method to work with both Method and Constructor objects */&lt;br /&gt;
&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype=null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) +&lt;br /&gt;
                     ((returntype!=null)? typename(returntype)+&amp;quot; &amp;quot; : &amp;quot;&amp;quot;) +&lt;br /&gt;
                     member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0) System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''This code is then compiled and run, with the following simple test class passed in:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput {&lt;br /&gt;
    public static void main(String[] args) {}&lt;br /&gt;
    public int method1() {&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public String method2() {&lt;br /&gt;
        return &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    public void method3() {}&lt;br /&gt;
    public void method4(int num) {}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''The output generated is as follows:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput extends java.lang.Object {&lt;br /&gt;
 &lt;br /&gt;
// Constructors&lt;br /&gt;
  public testInput();&lt;br /&gt;
 // Fields&lt;br /&gt;
 // Methods&lt;br /&gt;
  public int method1();&lt;br /&gt;
  public java.lang.String method2();&lt;br /&gt;
  public void method3();&lt;br /&gt;
  public void method4(int);&lt;br /&gt;
  public static void main(java.lang.String[]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection Sequences ===&lt;br /&gt;
The Java code above, which displays constructors, methods, and other information pertinent to the ''testInput'' class, is somewhat convoluted and difficult to trace.  In Ruby, the methods below can be called on a module/class to return similar information:&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.private_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.protected_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.public_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.singleton_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
For example, using the &amp;lt;tt&amp;gt;Demo&amp;lt;/tt&amp;gt; class below, all of the class' methods (private, protected, public, and singleton) can be easily displayed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; class Demo&lt;br /&gt;
irb(main):002:1&amp;gt;   public&lt;br /&gt;
irb(main):003:1&amp;gt;     def method1&lt;br /&gt;
irb(main):004:2&amp;gt;   end&lt;br /&gt;
irb(main):005:1&amp;gt;   private&lt;br /&gt;
irb(main):006:1&amp;gt;     def method2&lt;br /&gt;
irb(main):007:2&amp;gt;   end&lt;br /&gt;
irb(main):008:1&amp;gt;   public&lt;br /&gt;
irb(main):009:1&amp;gt;     def method3&lt;br /&gt;
irb(main):010:2&amp;gt;   end&lt;br /&gt;
irb(main):011:1&amp;gt;   protected&lt;br /&gt;
irb(main):012:1&amp;gt;     def method4&lt;br /&gt;
irb(main):013:2&amp;gt;   end&lt;br /&gt;
irb(main):014:1&amp;gt;   def Demo.method5&lt;br /&gt;
irb(main):015:2&amp;gt;   end&lt;br /&gt;
irb(main):016:1&amp;gt;   public&lt;br /&gt;
irb(main):017:1&amp;gt;     def method6&lt;br /&gt;
irb(main):018:2&amp;gt;   end&lt;br /&gt;
irb(main):019:1&amp;gt; end&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):020:0&amp;gt; Demo.private_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method2&amp;quot;]&lt;br /&gt;
irb(main):021:0&amp;gt; Demo.protected_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method4&amp;quot;]&lt;br /&gt;
irb(main):022:0&amp;gt; Demo.public_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method3&amp;quot;, &amp;quot;method6&amp;quot;, &amp;quot;method1&amp;quot;]&lt;br /&gt;
irb(main):023:0&amp;gt; Demo.singleton_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method5&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to looking inside classes, reflection in Ruby provides the ability to dynamically call methods.  An arbitrary method of some object can be called using &amp;lt;tt&amp;gt;Object#send&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;Method#call&amp;lt;/tt&amp;gt;, and also various versions of the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method (including &amp;lt;tt&amp;gt;class_eval&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;module_eval&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;instance_eval&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;send&amp;lt;/tt&amp;gt; works on any object, and it calls the passed in method on that object.  It will also find new methods added at runtime.  In the below example, the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; method is called on the object, which is a 14 character string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):105:0&amp;gt; &amp;quot;This is a test&amp;quot;.send(:length)&lt;br /&gt;
=&amp;gt; 14&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; objects represent a chunk of code and a context in which it executes.  The &amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; object is created, and it can be executed whenever desired by sending it the &amp;lt;tt&amp;gt;call&amp;lt;/tt&amp;gt; message:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):106:0&amp;gt; test_message = &amp;quot;Test message&amp;quot;.method(:length)&lt;br /&gt;
=&amp;gt; #&amp;lt;Method: String#length&amp;gt;&lt;br /&gt;
irb(main):107:0&amp;gt; test_message.call&lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A third way methods can be invoked dynamically using reflection is with the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method.  &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; provides the ability to parse and execute an arbitrary string of legal Ruby source code.  The string of code can be stored as an object, and then evaluated at a later time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):111:0&amp;gt; eval_test = %q{&amp;quot;A third test&amp;quot;.length}&lt;br /&gt;
=&amp;gt; &amp;quot;\&amp;quot;A third test\&amp;quot;.length&amp;quot;&lt;br /&gt;
irb(main):112:0&amp;gt; eval eval_test  &lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection vs. Java Reflection ===&lt;br /&gt;
&lt;br /&gt;
*By coding and comparing we find that Reflection in Java tends to be more verbose than Ruby code which accomplishes the same task.  Also, exceptions such as &amp;lt;tt&amp;gt;NoSuchMethodError&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;InvocationTargetException&amp;lt;/tt&amp;gt; tend to create further complications and confusion when using Java's reflection API.&lt;br /&gt;
**In order to print out all the methods contained in a class in Java, we approached 100 lines of code.  It took merely 4 lines of code in Ruby to print all private, protected, public, and singleton methods of a class.  This illustrates Ruby efficiency when looking inside classes.&lt;br /&gt;
&lt;br /&gt;
*Language facilities such as ''aliasing'', ''reflection'', and ''metaprogramming'' make Aspect Oriented Programming much easier to perform in Ruby than in Java.&lt;br /&gt;
&lt;br /&gt;
=== Uses of Reflection ===&lt;br /&gt;
Reflection is used by programs which require the ability to analyze or modify the runtime behavior of applications.This is a relatively advanced feature.Thus, reflection is a powerful technique which can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
*Extensibility Features: &lt;br /&gt;
An application may utilize 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;
A class browser could 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;
Debuggers could examine private members on classes. Test harnesses could use 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;
*Reflection API in java is also used in Database Access with SQL.&lt;br /&gt;
&lt;br /&gt;
=== Drawbacks of Reflection ===&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately.  It is preferable to avoid using reflection, if possible.  The following concerns should be kept in mind when accessing code via reflection:&lt;br /&gt;
&lt;br /&gt;
*Performance Overhead &lt;br /&gt;
Certain Java virtual machine optimizations, which greatly increase performance in Java applications, cannot be performed when reflective operations are used due to the dynamically resolved types involved in reflection.  Therefore, reflection should be avoided in sections of code called frequently in performance-sensitive applications.&lt;br /&gt;
&lt;br /&gt;
*Security Restrictions &lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager.  This is important for code running in a restricted security contect, e.g. in an Applet.&lt;br /&gt;
&lt;br /&gt;
*Exposure of Internals&lt;br /&gt;
Operations which would be illegal in a non-reflective environment, such as accessing private fields and methods, are allowed in reflective code.  Consequently, unexpected side-effects, which may render code dysfunctional and destroy portability, may result from the use of reflection.  Abstractions may also be broken, thus changing behavior with platform upgrades.&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
#Flanagan, David.  [http://books.google.com/books?id=kADTIsgeLh4C Java Examples in a Nutshell].  O'Reilly, 2004.&lt;br /&gt;
#Thomas, Dave.  [http://books.google.com/books?id=8LXOAAAACAAJ Programming Ruby].  Raleigh: Pragmatic Bookshelf, 2004.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
*http://phrogz.net/programmingruby/ospace.html&lt;br /&gt;
*http://java.sun.com/docs/books/tutorial/reflect/&lt;br /&gt;
*http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html&lt;br /&gt;
*http://www.javaworld.com/javaworld/jw-07-2006/jw-0717-ruby.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=13114</id>
		<title>CSC/ECE 517 Summer 2008/wiki1 3 aobk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=13114"/>
		<updated>2008-06-12T02:42:52Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* What is ''Reflection'' in computer science? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection in Ruby and Java ==&lt;br /&gt;
;&lt;br /&gt;
===What is ''Reflection'' in computer science?=== &lt;br /&gt;
&amp;lt;center&amp;gt;[[Image:linear.png]]&amp;lt;/center&amp;gt;&lt;br /&gt;
Reflection, in the realm of Computer Science, is the process of introspection by a program.  Introspection allows program entities to discover things about themselves.  Through the technique of ''metaprogramming'', aspects of a program are examined from within the program itself, which enables the program to modify itself at runtime.  This provides for a very liberated and dynamic programming environment.&lt;br /&gt;
&lt;br /&gt;
By looking at and inside classes and objects, much can be discovered, including the following:&lt;br /&gt;
* what objects are contained in a program&lt;br /&gt;
* the class hierarchy&lt;br /&gt;
* the attributes and methods of the objects contained in the program&lt;br /&gt;
* various method information&lt;br /&gt;
&lt;br /&gt;
One of the design challenges when using Reflection is that you need to clarify what services you wish to provide at runtime.&lt;br /&gt;
&lt;br /&gt;
=== Java Reflection Sequences ===&lt;br /&gt;
;''Reflection in Java''&lt;br /&gt;
:Java uses a '''Reflection API''' to inspect and manipulate itself.  It includes the expanded ''class'' class in java.lang and the java.lang.reflect package, which represents the members of a class with ''Method'', ''Constructor'' and ''Field'' objects.Reflection can be used to obtain information about a class and its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
*This example uses the &amp;lt;tt&amp;gt;Class.forName()&amp;lt;/tt&amp;gt; method to dynamically load the named class, and then calls various methods of the ''class'' object to look up the Superclass, Interfaces, and Members of the class. The example uses ''Constructor'', ''Field'' and ''Method'' objects to obtain information about each member of the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
&lt;br /&gt;
/** The main method.  Print info about the named class */&lt;br /&gt;
&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Display the modifiers, name, superclass and interfaces of a class&lt;br /&gt;
   *  or interface. Then go and list all constructors, fields, and methods. */&lt;br /&gt;
&lt;br /&gt;
  public static void print_class(Class c)&lt;br /&gt;
  {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot;+c.getName());&lt;br /&gt;
    }&lt;br /&gt;
    else if (c.getSuperclass() != null)&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName() +&lt;br /&gt;
                       &amp;quot; extends &amp;quot; + c.getSuperclass().getName());&lt;br /&gt;
    else&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName());&lt;br /&gt;
        &lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface()) System.out.println(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for(int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(interfaces[i].getName());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;);            // Begin class member listing.&lt;br /&gt;
&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for(int i = 0; i &amp;lt; constructors.length; i++)      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields();           // Look up fields.&lt;br /&gt;
    for(int i = 0; i &amp;lt; fields.length; i++)            // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods();        // Look up methods.&lt;br /&gt;
    for(int i = 0; i &amp;lt; methods.length; i++)           // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;);             // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  &lt;br /&gt;
public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while(t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    return t.getName() + brackets;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
 &lt;br /&gt;
 public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0) return &amp;quot;&amp;quot;;&lt;br /&gt;
    else return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Print the modifiers, type, and name of a field */&lt;br /&gt;
&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; +&lt;br /&gt;
                       modifiers(f.getModifiers()) +&lt;br /&gt;
                       typename(f.getType()) + &amp;quot; &amp;quot; + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, return type, name, parameter types and exception&lt;br /&gt;
   *  type of a method or constructor.  Note the use of the Member interface&lt;br /&gt;
   *  to allow this method to work with both Method and Constructor objects */&lt;br /&gt;
&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype=null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) +&lt;br /&gt;
                     ((returntype!=null)? typename(returntype)+&amp;quot; &amp;quot; : &amp;quot;&amp;quot;) +&lt;br /&gt;
                     member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0) System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''This code is then compiled and run, with the following simple test class passed in:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput {&lt;br /&gt;
    public static void main(String[] args) {}&lt;br /&gt;
    public int method1() {&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public String method2() {&lt;br /&gt;
        return &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    public void method3() {}&lt;br /&gt;
    public void method4(int num) {}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''The output generated is as follows:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput extends java.lang.Object {&lt;br /&gt;
 &lt;br /&gt;
// Constructors&lt;br /&gt;
  public testInput();&lt;br /&gt;
 // Fields&lt;br /&gt;
 // Methods&lt;br /&gt;
  public int method1();&lt;br /&gt;
  public java.lang.String method2();&lt;br /&gt;
  public void method3();&lt;br /&gt;
  public void method4(int);&lt;br /&gt;
  public static void main(java.lang.String[]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection Sequences ===&lt;br /&gt;
The Java code above, which displays constructors, methods, and other information pertinent to the ''testInput'' class, is somewhat convoluted and difficult to trace.  In Ruby, the methods below can be called on a module/class to return similar information:&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.private_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.protected_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.public_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.singleton_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
For example, using the &amp;lt;tt&amp;gt;Demo&amp;lt;/tt&amp;gt; class below, all of the class' methods (private, protected, public, and singleton) can be easily displayed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; class Demo&lt;br /&gt;
irb(main):002:1&amp;gt;   public&lt;br /&gt;
irb(main):003:1&amp;gt;     def method1&lt;br /&gt;
irb(main):004:2&amp;gt;   end&lt;br /&gt;
irb(main):005:1&amp;gt;   private&lt;br /&gt;
irb(main):006:1&amp;gt;     def method2&lt;br /&gt;
irb(main):007:2&amp;gt;   end&lt;br /&gt;
irb(main):008:1&amp;gt;   public&lt;br /&gt;
irb(main):009:1&amp;gt;     def method3&lt;br /&gt;
irb(main):010:2&amp;gt;   end&lt;br /&gt;
irb(main):011:1&amp;gt;   protected&lt;br /&gt;
irb(main):012:1&amp;gt;     def method4&lt;br /&gt;
irb(main):013:2&amp;gt;   end&lt;br /&gt;
irb(main):014:1&amp;gt;   def Demo.method5&lt;br /&gt;
irb(main):015:2&amp;gt;   end&lt;br /&gt;
irb(main):016:1&amp;gt;   public&lt;br /&gt;
irb(main):017:1&amp;gt;     def method6&lt;br /&gt;
irb(main):018:2&amp;gt;   end&lt;br /&gt;
irb(main):019:1&amp;gt; end&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):020:0&amp;gt; Demo.private_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method2&amp;quot;]&lt;br /&gt;
irb(main):021:0&amp;gt; Demo.protected_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method4&amp;quot;]&lt;br /&gt;
irb(main):022:0&amp;gt; Demo.public_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method3&amp;quot;, &amp;quot;method6&amp;quot;, &amp;quot;method1&amp;quot;]&lt;br /&gt;
irb(main):023:0&amp;gt; Demo.singleton_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method5&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to looking inside classes, reflection in Ruby provides the ability to dynamically call methods.  An arbitrary method of some object can be called using &amp;lt;tt&amp;gt;Object#send&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;Method#call&amp;lt;/tt&amp;gt;, and also various versions of the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method (including &amp;lt;tt&amp;gt;class_eval&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;module_eval&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;instance_eval&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;send&amp;lt;/tt&amp;gt; works on any object, and it calls the passed in method on that object.  It will also find new methods added at runtime.  In the below example, the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; method is called on the object, which is a 14 character string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):105:0&amp;gt; &amp;quot;This is a test&amp;quot;.send(:length)&lt;br /&gt;
=&amp;gt; 14&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; objects represent a chunk of code and a context in which it executes.  The &amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; object is created, and it can be executed whenever desired by sending it the &amp;lt;tt&amp;gt;call&amp;lt;/tt&amp;gt; message:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):106:0&amp;gt; test_message = &amp;quot;Test message&amp;quot;.method(:length)&lt;br /&gt;
=&amp;gt; #&amp;lt;Method: String#length&amp;gt;&lt;br /&gt;
irb(main):107:0&amp;gt; test_message.call&lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A third way methods can be invoked dynamically using reflection is with the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method.  &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; provides the ability to parse and execute an arbitrary string of legal Ruby source code.  The string of code can be stored as an object, and then evaluated at a later time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):111:0&amp;gt; eval_test = %q{&amp;quot;A third test&amp;quot;.length}&lt;br /&gt;
=&amp;gt; &amp;quot;\&amp;quot;A third test\&amp;quot;.length&amp;quot;&lt;br /&gt;
irb(main):112:0&amp;gt; eval eval_test  &lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection vs. Java Reflection ===&lt;br /&gt;
&lt;br /&gt;
*By coding and comparing we find that Reflection in Java tends to be more verbose than Ruby code which accomplishes the same task.  Also, exceptions such as &amp;lt;tt&amp;gt;NoSuchMethodError&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;InvocationTargetException&amp;lt;/tt&amp;gt; tend to create further complications and confusion when using Java's reflection API.&lt;br /&gt;
**In order to print out all the methods contained in a class in Java, we approached 100 lines of code.  It took merely 4 lines of code in Ruby to print all private, protected, public, and singleton methods of a class.  This illustrates Ruby efficiency when looking inside classes.&lt;br /&gt;
&lt;br /&gt;
*Language facilities such as ''aliasing'', ''reflection'', and ''metaprogramming'' make Aspect Oriented Programming much easier to perform in Ruby than in Java.&lt;br /&gt;
&lt;br /&gt;
=== Uses of Reflection ===&lt;br /&gt;
Reflection is used by programs which require the ability to analyze or modify the runtime behavior of applications.This is a relatively advanced feature.Thus, reflection is a powerful technique which can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
*Extensibility Features: &lt;br /&gt;
An application may utilize 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;
A class browser could 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;
Debuggers could examine private members on classes. Test harnesses could use 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;
*Reflection API in java is also used in Database Access with SQL.&lt;br /&gt;
&lt;br /&gt;
=== Drawbacks of Reflection ===&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately.  It is preferable to avoid using reflection, if possible.  The following concerns should be kept in mind when accessing code via reflection:&lt;br /&gt;
&lt;br /&gt;
*Performance Overhead &lt;br /&gt;
Certain Java virtual machine optimizations, which greatly increase performance in Java applications, cannot be performed when reflective operations are used due to the dynamically resolved types involved in reflection.  Therefore, reflection should be avoided in sections of code called frequently in performance-sensitive applications.&lt;br /&gt;
&lt;br /&gt;
*Security Restrictions &lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager.  This is important for code running in a restricted security contect, e.g. in an Applet.&lt;br /&gt;
&lt;br /&gt;
*Exposure of Internals&lt;br /&gt;
Operations which would be illegal in a non-reflective environment, such as accessing private fields and methods, are allowed in reflective code.  Consequently, unexpected side-effects, which may render code dysfunctional and destroy portability, may result from the use of reflection.  Abstractions may also be broken, thus changing behavior with platform upgrades.&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
#Flanagan, David.  [http://books.google.com/books?id=kADTIsgeLh4C Java Examples in a Nutshell].  O'Reilly, 2004.&lt;br /&gt;
#Thomas, Dave.  [http://books.google.com/books?id=8LXOAAAACAAJ Programming Ruby].  Raleigh: Pragmatic Bookshelf, 2004.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
*http://phrogz.net/programmingruby/ospace.html&lt;br /&gt;
*http://java.sun.com/docs/books/tutorial/reflect/&lt;br /&gt;
*http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html&lt;br /&gt;
*http://www.javaworld.com/javaworld/jw-07-2006/jw-0717-ruby.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=13112</id>
		<title>CSC/ECE 517 Summer 2008/wiki1 3 aobk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=13112"/>
		<updated>2008-06-12T02:42:26Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* What is ''Reflection'' in computer science? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection in Ruby and Java ==&lt;br /&gt;
;&lt;br /&gt;
===What is ''Reflection'' in computer science?=== &lt;br /&gt;
                                    [[Image:linear.png]]&lt;br /&gt;
&amp;lt;center&amp;gt;[[Image:linear.png]]&amp;lt;/center&amp;gt;&lt;br /&gt;
Reflection, in the realm of Computer Science, is the process of introspection by a program.  Introspection allows program entities to discover things about themselves.  Through the technique of ''metaprogramming'', aspects of a program are examined from within the program itself, which enables the program to modify itself at runtime.  This provides for a very liberated and dynamic programming environment.&lt;br /&gt;
&lt;br /&gt;
By looking at and inside classes and objects, much can be discovered, including the following:&lt;br /&gt;
* what objects are contained in a program&lt;br /&gt;
* the class hierarchy&lt;br /&gt;
* the attributes and methods of the objects contained in the program&lt;br /&gt;
* various method information&lt;br /&gt;
&lt;br /&gt;
One of the design challenges when using Reflection is that you need to clarify what services you wish to provide at runtime.&lt;br /&gt;
&lt;br /&gt;
=== Java Reflection Sequences ===&lt;br /&gt;
;''Reflection in Java''&lt;br /&gt;
:Java uses a '''Reflection API''' to inspect and manipulate itself.  It includes the expanded ''class'' class in java.lang and the java.lang.reflect package, which represents the members of a class with ''Method'', ''Constructor'' and ''Field'' objects.Reflection can be used to obtain information about a class and its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
*This example uses the &amp;lt;tt&amp;gt;Class.forName()&amp;lt;/tt&amp;gt; method to dynamically load the named class, and then calls various methods of the ''class'' object to look up the Superclass, Interfaces, and Members of the class. The example uses ''Constructor'', ''Field'' and ''Method'' objects to obtain information about each member of the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
&lt;br /&gt;
/** The main method.  Print info about the named class */&lt;br /&gt;
&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Display the modifiers, name, superclass and interfaces of a class&lt;br /&gt;
   *  or interface. Then go and list all constructors, fields, and methods. */&lt;br /&gt;
&lt;br /&gt;
  public static void print_class(Class c)&lt;br /&gt;
  {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot;+c.getName());&lt;br /&gt;
    }&lt;br /&gt;
    else if (c.getSuperclass() != null)&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName() +&lt;br /&gt;
                       &amp;quot; extends &amp;quot; + c.getSuperclass().getName());&lt;br /&gt;
    else&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName());&lt;br /&gt;
        &lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface()) System.out.println(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for(int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(interfaces[i].getName());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;);            // Begin class member listing.&lt;br /&gt;
&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for(int i = 0; i &amp;lt; constructors.length; i++)      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields();           // Look up fields.&lt;br /&gt;
    for(int i = 0; i &amp;lt; fields.length; i++)            // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods();        // Look up methods.&lt;br /&gt;
    for(int i = 0; i &amp;lt; methods.length; i++)           // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;);             // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  &lt;br /&gt;
public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while(t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    return t.getName() + brackets;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
 &lt;br /&gt;
 public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0) return &amp;quot;&amp;quot;;&lt;br /&gt;
    else return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Print the modifiers, type, and name of a field */&lt;br /&gt;
&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; +&lt;br /&gt;
                       modifiers(f.getModifiers()) +&lt;br /&gt;
                       typename(f.getType()) + &amp;quot; &amp;quot; + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, return type, name, parameter types and exception&lt;br /&gt;
   *  type of a method or constructor.  Note the use of the Member interface&lt;br /&gt;
   *  to allow this method to work with both Method and Constructor objects */&lt;br /&gt;
&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype=null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) +&lt;br /&gt;
                     ((returntype!=null)? typename(returntype)+&amp;quot; &amp;quot; : &amp;quot;&amp;quot;) +&lt;br /&gt;
                     member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0) System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''This code is then compiled and run, with the following simple test class passed in:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput {&lt;br /&gt;
    public static void main(String[] args) {}&lt;br /&gt;
    public int method1() {&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public String method2() {&lt;br /&gt;
        return &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    public void method3() {}&lt;br /&gt;
    public void method4(int num) {}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''The output generated is as follows:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput extends java.lang.Object {&lt;br /&gt;
 &lt;br /&gt;
// Constructors&lt;br /&gt;
  public testInput();&lt;br /&gt;
 // Fields&lt;br /&gt;
 // Methods&lt;br /&gt;
  public int method1();&lt;br /&gt;
  public java.lang.String method2();&lt;br /&gt;
  public void method3();&lt;br /&gt;
  public void method4(int);&lt;br /&gt;
  public static void main(java.lang.String[]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection Sequences ===&lt;br /&gt;
The Java code above, which displays constructors, methods, and other information pertinent to the ''testInput'' class, is somewhat convoluted and difficult to trace.  In Ruby, the methods below can be called on a module/class to return similar information:&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.private_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.protected_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.public_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.singleton_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
For example, using the &amp;lt;tt&amp;gt;Demo&amp;lt;/tt&amp;gt; class below, all of the class' methods (private, protected, public, and singleton) can be easily displayed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; class Demo&lt;br /&gt;
irb(main):002:1&amp;gt;   public&lt;br /&gt;
irb(main):003:1&amp;gt;     def method1&lt;br /&gt;
irb(main):004:2&amp;gt;   end&lt;br /&gt;
irb(main):005:1&amp;gt;   private&lt;br /&gt;
irb(main):006:1&amp;gt;     def method2&lt;br /&gt;
irb(main):007:2&amp;gt;   end&lt;br /&gt;
irb(main):008:1&amp;gt;   public&lt;br /&gt;
irb(main):009:1&amp;gt;     def method3&lt;br /&gt;
irb(main):010:2&amp;gt;   end&lt;br /&gt;
irb(main):011:1&amp;gt;   protected&lt;br /&gt;
irb(main):012:1&amp;gt;     def method4&lt;br /&gt;
irb(main):013:2&amp;gt;   end&lt;br /&gt;
irb(main):014:1&amp;gt;   def Demo.method5&lt;br /&gt;
irb(main):015:2&amp;gt;   end&lt;br /&gt;
irb(main):016:1&amp;gt;   public&lt;br /&gt;
irb(main):017:1&amp;gt;     def method6&lt;br /&gt;
irb(main):018:2&amp;gt;   end&lt;br /&gt;
irb(main):019:1&amp;gt; end&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):020:0&amp;gt; Demo.private_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method2&amp;quot;]&lt;br /&gt;
irb(main):021:0&amp;gt; Demo.protected_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method4&amp;quot;]&lt;br /&gt;
irb(main):022:0&amp;gt; Demo.public_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method3&amp;quot;, &amp;quot;method6&amp;quot;, &amp;quot;method1&amp;quot;]&lt;br /&gt;
irb(main):023:0&amp;gt; Demo.singleton_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method5&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to looking inside classes, reflection in Ruby provides the ability to dynamically call methods.  An arbitrary method of some object can be called using &amp;lt;tt&amp;gt;Object#send&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;Method#call&amp;lt;/tt&amp;gt;, and also various versions of the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method (including &amp;lt;tt&amp;gt;class_eval&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;module_eval&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;instance_eval&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;send&amp;lt;/tt&amp;gt; works on any object, and it calls the passed in method on that object.  It will also find new methods added at runtime.  In the below example, the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; method is called on the object, which is a 14 character string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):105:0&amp;gt; &amp;quot;This is a test&amp;quot;.send(:length)&lt;br /&gt;
=&amp;gt; 14&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; objects represent a chunk of code and a context in which it executes.  The &amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; object is created, and it can be executed whenever desired by sending it the &amp;lt;tt&amp;gt;call&amp;lt;/tt&amp;gt; message:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):106:0&amp;gt; test_message = &amp;quot;Test message&amp;quot;.method(:length)&lt;br /&gt;
=&amp;gt; #&amp;lt;Method: String#length&amp;gt;&lt;br /&gt;
irb(main):107:0&amp;gt; test_message.call&lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A third way methods can be invoked dynamically using reflection is with the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method.  &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; provides the ability to parse and execute an arbitrary string of legal Ruby source code.  The string of code can be stored as an object, and then evaluated at a later time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):111:0&amp;gt; eval_test = %q{&amp;quot;A third test&amp;quot;.length}&lt;br /&gt;
=&amp;gt; &amp;quot;\&amp;quot;A third test\&amp;quot;.length&amp;quot;&lt;br /&gt;
irb(main):112:0&amp;gt; eval eval_test  &lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection vs. Java Reflection ===&lt;br /&gt;
&lt;br /&gt;
*By coding and comparing we find that Reflection in Java tends to be more verbose than Ruby code which accomplishes the same task.  Also, exceptions such as &amp;lt;tt&amp;gt;NoSuchMethodError&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;InvocationTargetException&amp;lt;/tt&amp;gt; tend to create further complications and confusion when using Java's reflection API.&lt;br /&gt;
**In order to print out all the methods contained in a class in Java, we approached 100 lines of code.  It took merely 4 lines of code in Ruby to print all private, protected, public, and singleton methods of a class.  This illustrates Ruby efficiency when looking inside classes.&lt;br /&gt;
&lt;br /&gt;
*Language facilities such as ''aliasing'', ''reflection'', and ''metaprogramming'' make Aspect Oriented Programming much easier to perform in Ruby than in Java.&lt;br /&gt;
&lt;br /&gt;
=== Uses of Reflection ===&lt;br /&gt;
Reflection is used by programs which require the ability to analyze or modify the runtime behavior of applications.This is a relatively advanced feature.Thus, reflection is a powerful technique which can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
*Extensibility Features: &lt;br /&gt;
An application may utilize 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;
A class browser could 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;
Debuggers could examine private members on classes. Test harnesses could use 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;
*Reflection API in java is also used in Database Access with SQL.&lt;br /&gt;
&lt;br /&gt;
=== Drawbacks of Reflection ===&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately.  It is preferable to avoid using reflection, if possible.  The following concerns should be kept in mind when accessing code via reflection:&lt;br /&gt;
&lt;br /&gt;
*Performance Overhead &lt;br /&gt;
Certain Java virtual machine optimizations, which greatly increase performance in Java applications, cannot be performed when reflective operations are used due to the dynamically resolved types involved in reflection.  Therefore, reflection should be avoided in sections of code called frequently in performance-sensitive applications.&lt;br /&gt;
&lt;br /&gt;
*Security Restrictions &lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager.  This is important for code running in a restricted security contect, e.g. in an Applet.&lt;br /&gt;
&lt;br /&gt;
*Exposure of Internals&lt;br /&gt;
Operations which would be illegal in a non-reflective environment, such as accessing private fields and methods, are allowed in reflective code.  Consequently, unexpected side-effects, which may render code dysfunctional and destroy portability, may result from the use of reflection.  Abstractions may also be broken, thus changing behavior with platform upgrades.&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
#Flanagan, David.  [http://books.google.com/books?id=kADTIsgeLh4C Java Examples in a Nutshell].  O'Reilly, 2004.&lt;br /&gt;
#Thomas, Dave.  [http://books.google.com/books?id=8LXOAAAACAAJ Programming Ruby].  Raleigh: Pragmatic Bookshelf, 2004.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
*http://phrogz.net/programmingruby/ospace.html&lt;br /&gt;
*http://java.sun.com/docs/books/tutorial/reflect/&lt;br /&gt;
*http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html&lt;br /&gt;
*http://www.javaworld.com/javaworld/jw-07-2006/jw-0717-ruby.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=13076</id>
		<title>CSC/ECE 517 Summer 2008/wiki1 3 aobk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=13076"/>
		<updated>2008-06-12T02:18:01Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Ruby Reflection vs. Java Reflection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection in Ruby and Java ==&lt;br /&gt;
;[[Image:Example.jpg]]What is ''Reflection''?&lt;br /&gt;
Reflection, in the realm of Computer Science, is the process of introspection by a program.  Introspection allows program entities to discover things about themselves.  Through the technique of ''metaprogramming'', aspects of a program are examined from within the program itself, which enables the program to modify itself at runtime.  This provides for a very liberated and dynamic programming environment.&lt;br /&gt;
&lt;br /&gt;
By looking at and inside classes and objects, much can be discovered, including the following:&lt;br /&gt;
* what objects are contained in a program&lt;br /&gt;
* the class hierarchy&lt;br /&gt;
* the attributes and methods of the objects contained in the program&lt;br /&gt;
* various method information&lt;br /&gt;
&lt;br /&gt;
One of the design challenges when using Reflection is that you need to clarify what services you wish to provide at runtime. &lt;br /&gt;
&lt;br /&gt;
=== Java Reflection Sequences ===&lt;br /&gt;
;''Reflection in Java''&lt;br /&gt;
:Java uses a '''Reflection API''' to inspect and manipulate itself.  It includes the expanded ''class'' class in java.lang and the java.lang.reflect package, which represents the members of a class with ''Method'', ''Constructor'' and ''Field'' objects.Reflection can be used to obtain information about a class and its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
*This example uses the &amp;lt;tt&amp;gt;Class.forName()&amp;lt;/tt&amp;gt; method to dynamically load the named class, and then calls various methods of the ''class'' object to look up the Superclass, Interfaces, and Members of the class. The example uses ''Constructor'', ''Field'' and ''Method'' objects to obtain information about each member of the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
&lt;br /&gt;
/** The main method.  Print info about the named class */&lt;br /&gt;
&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Display the modifiers, name, superclass and interfaces of a class&lt;br /&gt;
   *  or interface. Then go and list all constructors, fields, and methods. */&lt;br /&gt;
&lt;br /&gt;
  public static void print_class(Class c)&lt;br /&gt;
  {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot;+c.getName());&lt;br /&gt;
    }&lt;br /&gt;
    else if (c.getSuperclass() != null)&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName() +&lt;br /&gt;
                       &amp;quot; extends &amp;quot; + c.getSuperclass().getName());&lt;br /&gt;
    else&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName());&lt;br /&gt;
        &lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface()) System.out.println(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for(int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(interfaces[i].getName());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;);            // Begin class member listing.&lt;br /&gt;
&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for(int i = 0; i &amp;lt; constructors.length; i++)      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields();           // Look up fields.&lt;br /&gt;
    for(int i = 0; i &amp;lt; fields.length; i++)            // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods();        // Look up methods.&lt;br /&gt;
    for(int i = 0; i &amp;lt; methods.length; i++)           // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;);             // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  &lt;br /&gt;
public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while(t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    return t.getName() + brackets;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
 &lt;br /&gt;
 public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0) return &amp;quot;&amp;quot;;&lt;br /&gt;
    else return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
/** Print the modifiers, type, and name of a field */&lt;br /&gt;
&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; +&lt;br /&gt;
                       modifiers(f.getModifiers()) +&lt;br /&gt;
                       typename(f.getType()) + &amp;quot; &amp;quot; + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, return type, name, parameter types and exception&lt;br /&gt;
   *  type of a method or constructor.  Note the use of the Member interface&lt;br /&gt;
   *  to allow this method to work with both Method and Constructor objects */&lt;br /&gt;
&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype=null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) +&lt;br /&gt;
                     ((returntype!=null)? typename(returntype)+&amp;quot; &amp;quot; : &amp;quot;&amp;quot;) +&lt;br /&gt;
                     member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0) System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''This code is then compiled and run, with the following simple test class passed in:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput {&lt;br /&gt;
    public static void main(String[] args) {}&lt;br /&gt;
    public int method1() {&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public String method2() {&lt;br /&gt;
        return &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    public void method3() {}&lt;br /&gt;
    public void method4(int num) {}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''The output generated is as follows:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput extends java.lang.Object {&lt;br /&gt;
 &lt;br /&gt;
// Constructors&lt;br /&gt;
  public testInput();&lt;br /&gt;
 // Fields&lt;br /&gt;
 // Methods&lt;br /&gt;
  public int method1();&lt;br /&gt;
  public java.lang.String method2();&lt;br /&gt;
  public void method3();&lt;br /&gt;
  public void method4(int);&lt;br /&gt;
  public static void main(java.lang.String[]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection Sequences ===&lt;br /&gt;
The Java code above, which displays constructors, methods, and other information pertinent to the ''testInput'' class, is somewhat convoluted and difficult to trace.  In Ruby, the methods below can be called on a module/class to return similar information:&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.private_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.protected_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.public_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.singleton_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
For example, using the &amp;lt;tt&amp;gt;Demo&amp;lt;/tt&amp;gt; class below, all of the class' methods (private, protected, public, and singleton) can be easily displayed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; class Demo&lt;br /&gt;
irb(main):002:1&amp;gt;   public&lt;br /&gt;
irb(main):003:1&amp;gt;     def method1&lt;br /&gt;
irb(main):004:2&amp;gt;   end&lt;br /&gt;
irb(main):005:1&amp;gt;   private&lt;br /&gt;
irb(main):006:1&amp;gt;     def method2&lt;br /&gt;
irb(main):007:2&amp;gt;   end&lt;br /&gt;
irb(main):008:1&amp;gt;   public&lt;br /&gt;
irb(main):009:1&amp;gt;     def method3&lt;br /&gt;
irb(main):010:2&amp;gt;   end&lt;br /&gt;
irb(main):011:1&amp;gt;   protected&lt;br /&gt;
irb(main):012:1&amp;gt;     def method4&lt;br /&gt;
irb(main):013:2&amp;gt;   end&lt;br /&gt;
irb(main):014:1&amp;gt;   def Demo.method5&lt;br /&gt;
irb(main):015:2&amp;gt;   end&lt;br /&gt;
irb(main):016:1&amp;gt;   public&lt;br /&gt;
irb(main):017:1&amp;gt;     def method6&lt;br /&gt;
irb(main):018:2&amp;gt;   end&lt;br /&gt;
irb(main):019:1&amp;gt; end&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):020:0&amp;gt; Demo.private_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method2&amp;quot;]&lt;br /&gt;
irb(main):021:0&amp;gt; Demo.protected_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method4&amp;quot;]&lt;br /&gt;
irb(main):022:0&amp;gt; Demo.public_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method3&amp;quot;, &amp;quot;method6&amp;quot;, &amp;quot;method1&amp;quot;]&lt;br /&gt;
irb(main):023:0&amp;gt; Demo.singleton_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method5&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to looking inside classes, reflection in Ruby provides the ability to dynamically call methods.  An arbitrary method of some object can be called using &amp;lt;tt&amp;gt;Object#send&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;Method#call&amp;lt;/tt&amp;gt;, and also various versions of the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method (including &amp;lt;tt&amp;gt;class_eval&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;module_eval&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;instance_eval&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;send&amp;lt;/tt&amp;gt; works on any object, and it calls the passed in method on that object.  It will also find new methods added at runtime.  In the below example, the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; method is called on the object, which is a 14 character string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):105:0&amp;gt; &amp;quot;This is a test&amp;quot;.send(:length)&lt;br /&gt;
=&amp;gt; 14&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; objects represent a chunk of code and a context in which it executes.  The &amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; object is created, and it can be executed whenever desired by sending it the &amp;lt;tt&amp;gt;call&amp;lt;/tt&amp;gt; message:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):106:0&amp;gt; test_message = &amp;quot;Test message&amp;quot;.method(:length)&lt;br /&gt;
=&amp;gt; #&amp;lt;Method: String#length&amp;gt;&lt;br /&gt;
irb(main):107:0&amp;gt; test_message.call&lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A third way methods can be invoked dynamically using reflection is with the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method.  &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; provides the ability to parse and execute an arbitrary string of legal Ruby source code.  The string of code can be stored as an object, and then evaluated at a later time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):111:0&amp;gt; eval_test = %q{&amp;quot;A third test&amp;quot;.length}&lt;br /&gt;
=&amp;gt; &amp;quot;\&amp;quot;A third test\&amp;quot;.length&amp;quot;&lt;br /&gt;
irb(main):112:0&amp;gt; eval eval_test  &lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection vs. Java Reflection ===&lt;br /&gt;
&lt;br /&gt;
*By coding and comparing we find that Reflection in Java tends to be more verbose than Ruby code which accomplishes the same task.  Also, exceptions such as &amp;lt;tt&amp;gt;NoSuchMethodError&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;InvocationTargetException&amp;lt;/tt&amp;gt; tend to create further complications and confusion when using Java's reflection API.&lt;br /&gt;
**In order to print out all the methods contained in a class in Java, we approached 100 lines of code.  It took merely 4 lines of code in Ruby to print all private, protected, public, and singleton methods of a class.  This illustrates Ruby efficiency when looking inside classes.&lt;br /&gt;
&lt;br /&gt;
*Language facilities such as ''aliasing'', ''reflection'', and ''metaprogramming'' make Aspect Oriented Programming much easier to perform in Ruby than in Java.&lt;br /&gt;
&lt;br /&gt;
=== Uses of Reflection ===&lt;br /&gt;
Reflection is used by programs which require the ability to analyze or modify the runtime behavior of applications.This is a relatively advanced feature.Thus, reflection is a powerful technique which can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
*Extensibility Features: &lt;br /&gt;
An application may utilize 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;
A class browser could 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;
Debuggers could examine private members on classes. Test harnesses could use 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;
*Reflection API in java is also used in Database Access with SQL.&lt;br /&gt;
&lt;br /&gt;
=== Drawbacks of Reflection ===&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately.  It is preferable to avoid using reflection, if possible.  The following concerns should be kept in mind when accessing code via reflection:&lt;br /&gt;
&lt;br /&gt;
*Performance Overhead &lt;br /&gt;
Certain Java virtual machine optimizations, which greatly increase performance in Java applications, cannot be performed when reflective operations are used due to the dynamically resolved types involved in reflection.  Therefore, reflection should be avoided in sections of code called frequently in performance-sensitive applications.&lt;br /&gt;
&lt;br /&gt;
*Security Restrictions &lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager.  This is important for code running in a restricted security contect, e.g. in an Applet.&lt;br /&gt;
&lt;br /&gt;
*Exposure of Internals&lt;br /&gt;
Operations which would be illegal in a non-reflective environment, such as accessing private fields and methods, are allowed in reflective code.  Consequently, unexpected side-effects, which may render code dysfunctional and destroy portability, may result from the use of reflection.  Abstractions may also be broken, thus changing behavior with platform upgrades.&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
#Flanagan, David.  [http://books.google.com/books?id=kADTIsgeLh4C Java Examples in a Nutshell].  O'Reilly, 2004.&lt;br /&gt;
#Thomas, Dave.  [http://books.google.com/books?id=8LXOAAAACAAJ Programming Ruby].  Raleigh: Pragmatic Bookshelf, 2004.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
*http://phrogz.net/programmingruby/ospace.html&lt;br /&gt;
*http://java.sun.com/docs/books/tutorial/reflect/&lt;br /&gt;
*http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html&lt;br /&gt;
*http://www.javaworld.com/javaworld/jw-07-2006/jw-0717-ruby.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=13066</id>
		<title>CSC/ECE 517 Summer 2008/wiki1 3 aobk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=13066"/>
		<updated>2008-06-12T01:28:27Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Drawbacks of Reflection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection in Ruby and Java ==&lt;br /&gt;
;What is ''Reflection''?&lt;br /&gt;
Reflection, in the realm of Computer Science, is the process of introspection by a program.  Introspection allows program entities to discover things about themselves.  Through the technique of ''metaprogramming'', aspects of a program are examined from within the program itself, which enables the program to modify itself at runtime.  This provides for a very liberated and dynamic programming environment.&lt;br /&gt;
&lt;br /&gt;
By looking at and inside classes and objects, much can be discovered, including the following:&lt;br /&gt;
* what objects are contained in a program&lt;br /&gt;
* the class hierarchy&lt;br /&gt;
* the attributes and methods of the objects contained in the program&lt;br /&gt;
* various method information&lt;br /&gt;
&lt;br /&gt;
One of the design challenges when using Reflection is that you need to clarify what services you wish to provide at runtime, i.e. ones that you don't want to implement statically. The provision of reusable services that vary based upon the runtime environment is one of the reasons that frameworks such as Struts and JAAS are created.  However, even small applications can face design issues that make a dynamic solution preferable over a static one. A word of caution, though, over-reliance on Reflection can lead to performance issues due to the extra overhead when determining what class(es) need to be loaded.&lt;br /&gt;
&lt;br /&gt;
=== Java Reflection Sequences ===&lt;br /&gt;
;''Reflection in Java''&lt;br /&gt;
:Java uses a '''Reflection API''' to inspect and manipulate itself.  It includes the expanded ''class'' class in java.lang and the java.lang.reflect package, which represents the members of a class with ''Method'', ''Constructor'' and ''Field'' objects.Reflection can be used to obtain information about a class and its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
*This example uses the &amp;lt;tt&amp;gt;Class.forName()&amp;lt;/tt&amp;gt; method to dynamically load the named class, and then calls various methods of the ''class'' object to look up the Superclass, Interfaces, and Members of the class. The example uses ''Constructor'', ''Field'' and ''Method'' objects to obtain information about each member of the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
  /** The main method.  Print info about the named class */&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Display the modifiers, name, superclass and interfaces of a class&lt;br /&gt;
   *  or interface. Then go and list all constructors, fields, and methods. */&lt;br /&gt;
  public static void print_class(Class c)&lt;br /&gt;
  {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot;+c.getName());&lt;br /&gt;
    }&lt;br /&gt;
    else if (c.getSuperclass() != null)&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName() +&lt;br /&gt;
                       &amp;quot; extends &amp;quot; + c.getSuperclass().getName());&lt;br /&gt;
    else&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName());&lt;br /&gt;
        &lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface()) System.out.println(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for(int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(interfaces[i].getName());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;);            // Begin class member listing.&lt;br /&gt;
&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
    System.out.println(&amp;quot; // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for(int i = 0; i &amp;lt; constructors.length; i++)      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields();           // Look up fields.&lt;br /&gt;
    for(int i = 0; i &amp;lt; fields.length; i++)            // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods();        // Look up methods.&lt;br /&gt;
    for(int i = 0; i &amp;lt; methods.length; i++)           // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;);             // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while(t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    return t.getName() + brackets;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
  public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0) return &amp;quot;&amp;quot;;&lt;br /&gt;
    else return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, type, and name of a field */&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; +&lt;br /&gt;
                       modifiers(f.getModifiers()) +&lt;br /&gt;
                       typename(f.getType()) + &amp;quot; &amp;quot; + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, return type, name, parameter types and exception&lt;br /&gt;
   *  type of a method or constructor.  Note the use of the Member interface&lt;br /&gt;
   *  to allow this method to work with both Method and Constructor objects */&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype=null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) +&lt;br /&gt;
                     ((returntype!=null)? typename(returntype)+&amp;quot; &amp;quot; : &amp;quot;&amp;quot;) +&lt;br /&gt;
                     member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0) System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code is then compiled and run, with the following simple test class passed in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput {&lt;br /&gt;
    public static void main(String[] args) {}&lt;br /&gt;
    public int method1() {&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public String method2() {&lt;br /&gt;
        return &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    public void method3() {}&lt;br /&gt;
    public void method4(int num) {}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output generated is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput extends java.lang.Object {&lt;br /&gt;
 // Constructors&lt;br /&gt;
  public testInput();&lt;br /&gt;
 // Fields&lt;br /&gt;
 // Methods&lt;br /&gt;
  public int method1();&lt;br /&gt;
  public java.lang.String method2();&lt;br /&gt;
  public void method3();&lt;br /&gt;
  public void method4(int);&lt;br /&gt;
  public static void main(java.lang.String[]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection Sequences ===&lt;br /&gt;
The Java code above, which displays constructors, methods, and other information pertinent to the ''testInput'' class, is somewhat convoluted and difficult to trace.  In Ruby, the methods below can be called on a module/class to return similar information:&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.private_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.protected_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.public_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.singleton_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
For example, using the &amp;lt;tt&amp;gt;Demo&amp;lt;/tt&amp;gt; class below, all of the class' methods (private, protected, public, and singleton) can be easily displayed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; class Demo&lt;br /&gt;
irb(main):002:1&amp;gt;   public&lt;br /&gt;
irb(main):003:1&amp;gt;     def method1&lt;br /&gt;
irb(main):004:2&amp;gt;   end&lt;br /&gt;
irb(main):005:1&amp;gt;   private&lt;br /&gt;
irb(main):006:1&amp;gt;     def method2&lt;br /&gt;
irb(main):007:2&amp;gt;   end&lt;br /&gt;
irb(main):008:1&amp;gt;   public&lt;br /&gt;
irb(main):009:1&amp;gt;     def method3&lt;br /&gt;
irb(main):010:2&amp;gt;   end&lt;br /&gt;
irb(main):011:1&amp;gt;   protected&lt;br /&gt;
irb(main):012:1&amp;gt;     def method4&lt;br /&gt;
irb(main):013:2&amp;gt;   end&lt;br /&gt;
irb(main):014:1&amp;gt;   def Demo.method5&lt;br /&gt;
irb(main):015:2&amp;gt;   end&lt;br /&gt;
irb(main):016:1&amp;gt;   public&lt;br /&gt;
irb(main):017:1&amp;gt;     def method6&lt;br /&gt;
irb(main):018:2&amp;gt;   end&lt;br /&gt;
irb(main):019:1&amp;gt; end&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):020:0&amp;gt; Demo.private_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method2&amp;quot;]&lt;br /&gt;
irb(main):021:0&amp;gt; Demo.protected_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method4&amp;quot;]&lt;br /&gt;
irb(main):022:0&amp;gt; Demo.public_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method3&amp;quot;, &amp;quot;method6&amp;quot;, &amp;quot;method1&amp;quot;]&lt;br /&gt;
irb(main):023:0&amp;gt; Demo.singleton_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method5&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to looking inside classes, reflection in Ruby provides the ability to dynamically call methods.  An arbitrary method of some object can be called using &amp;lt;tt&amp;gt;Object#send&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;Method#call&amp;lt;/tt&amp;gt;, and also various versions of the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method (including &amp;lt;tt&amp;gt;class_eval&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;module_eval&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;instance_eval&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;send&amp;lt;/tt&amp;gt; works on any object, and it calls the passed in method on that object.  It will also find new methods added at runtime.  In the below example, the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; method is called on the object, which is a 14 character string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):105:0&amp;gt; &amp;quot;This is a test&amp;quot;.send(:length)&lt;br /&gt;
=&amp;gt; 14&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; objects represent a chunk of code and a context in which it executes.  The &amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; object is created, and it can be executed whenever desired by sending it the &amp;lt;tt&amp;gt;call&amp;lt;/tt&amp;gt; message:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):106:0&amp;gt; test_message = &amp;quot;Test message&amp;quot;.method(:length)&lt;br /&gt;
=&amp;gt; #&amp;lt;Method: String#length&amp;gt;&lt;br /&gt;
irb(main):107:0&amp;gt; test_message.call&lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A third way methods can be invoked dynamically using reflection is with the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method.  &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; provides the ability to parse and execute an arbitrary string of legal Ruby source code.  The string of code can be stored as an object, and then evaluated at a later time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):111:0&amp;gt; eval_test = %q{&amp;quot;A third test&amp;quot;.length}&lt;br /&gt;
=&amp;gt; &amp;quot;\&amp;quot;A third test\&amp;quot;.length&amp;quot;&lt;br /&gt;
irb(main):112:0&amp;gt; eval eval_test  &lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection vs. Java Reflection ===&lt;br /&gt;
&lt;br /&gt;
*By coding and comparing we find that Reflection in Java tends to be more verbose than Ruby code which accomplishes the same task.  Also, exceptions such as &amp;lt;tt&amp;gt;NoSuchMethodError&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;InvocationTargetException&amp;lt;/tt&amp;gt; tend to create further complications and confusion when using Java's reflection API.&lt;br /&gt;
**In order to print out all the methods contained in a class in Java, we approached 100 lines of code.  It took merely 4 lines of code in Ruby to print all private, protected, public, and singleton methods of a class.  This illustrates Ruby efficiency when looking inside classes.&lt;br /&gt;
&lt;br /&gt;
*In Ruby, Aspect Oriented Programming(AOP) can be performed using ''aliasing'', ''reflection'', and ''metaprogramming''.  These language facilities make it much easier to perform AOP than in Java.&lt;br /&gt;
&lt;br /&gt;
=== Uses of Reflection ===&lt;br /&gt;
Reflection 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 and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
*Extensibility Features &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;
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;
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;
*Reflection API in java is also used in Database Access with SQL.&lt;br /&gt;
&lt;br /&gt;
=== Drawbacks of Reflection ===&lt;br /&gt;
Reflection is powerful, but should not be used indiscriminately.  It is preferable to avoid using reflection, if possible.  The following concerns should be kept in mind when accessing code via reflection:&lt;br /&gt;
&lt;br /&gt;
*Performance Overhead &lt;br /&gt;
Certain Java virtual machine optimizations, which greatly increase performance in Java applications, cannot be performed when reflective operations are used due to the dynamically resolved types involved in reflection.  Therefore, reflection should be avoided in sections of code called frequently in performance-sensitive applications.&lt;br /&gt;
&lt;br /&gt;
*Security Restrictions &lt;br /&gt;
Reflection requires a runtime permission which may not be present when running under a security manager.  This is important for code running in a restricted security contect, e.g. in an Applet.&lt;br /&gt;
&lt;br /&gt;
*Exposure of Internals&lt;br /&gt;
Operations which would be illegal in a non-reflective environment, such as accessing private fields and methods, are allowed in reflective code.  Consequently, unexpected side-effects, which may render code dysfunctional and destroy portability, may result from the use of reflection.  Abstractions may also be broken, thus changing behavior with platform upgrades.&lt;br /&gt;
&lt;br /&gt;
=== ''References'' ===&lt;br /&gt;
#Flanagan, David.  [http://books.google.com/books?id=kADTIsgeLh4C Java Examples in a Nutshell].  O'Reilly, 2004.&lt;br /&gt;
#Thomas, Dave.  [http://books.google.com/books?id=8LXOAAAACAAJ Programming Ruby].  Raleigh: Pragmatic Bookshelf, 2004.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
*http://phrogz.net/programmingruby/ospace.html&lt;br /&gt;
*http://java.sun.com/docs/books/tutorial/reflect/&lt;br /&gt;
*http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html&lt;br /&gt;
*http://www.javaworld.com/javaworld/jw-07-2006/jw-0717-ruby.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12463</id>
		<title>CSC/ECE 517 Summer 2008/wiki1 3 aobk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12463"/>
		<updated>2008-06-06T17:48:12Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Ruby Reflection vs. Java Reflection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection in Ruby and Java ==&lt;br /&gt;
;What is ''Reflection''?&lt;br /&gt;
Reflection, in the realm of Computer Science, is the process of introspection by a program.  Introspection allows program entities to discover things about themselves.  Through the technique of ''metaprogramming'', aspects of a program are examined from within the program itself, which enables the program to modify itself at runtime.  This provides for a very liberated and dynamic programming environment.&lt;br /&gt;
&lt;br /&gt;
By looking at and inside classes and objects, much can be discovered, including the following:&lt;br /&gt;
* what objects are contained in a program&lt;br /&gt;
* the class hierarchy&lt;br /&gt;
* the attributes and methods of the objects contained in the program&lt;br /&gt;
* various method information&lt;br /&gt;
&lt;br /&gt;
One of the design challenges when using Reflection is that you need to clarify what services you wish to provide at runtime, i.e. ones that you don't want to implement statically. The provision of reusable services that vary based upon the runtime environment is one of the reasons that frameworks such as Struts and JAAS are created.  However, even small applications can face design issues that make a dynamic solution preferable over a static one. A word of caution, though, over-reliance on Reflection can lead to performance issues due to the extra overhead when determining what class(es) need to be loaded.&lt;br /&gt;
&lt;br /&gt;
=== Java Reflection Sequences ===&lt;br /&gt;
;''Reflection in Java''&lt;br /&gt;
:Java uses a '''Reflection API''' to inspect and manipulate itself.  It includes the expanded ''class'' class in java.lang and the java.lang.reflect package, which represents the members of a class with ''Method'', ''Constructor'' and ''Field'' objects.Reflection can be used to obtain information about a class and its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
*This example uses the &amp;lt;tt&amp;gt;Class.forName()&amp;lt;/tt&amp;gt; method to dynamically load the named class, and then calls various methods of the ''class'' object to look up the Superclass, Interfaces, and Members of the class. The example uses ''Constructor'', ''Field'' and ''Method'' objects to obtain information about each member of the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
  /** The main method.  Print info about the named class */&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Display the modifiers, name, superclass and interfaces of a class&lt;br /&gt;
   *  or interface. Then go and list all constructors, fields, and methods. */&lt;br /&gt;
  public static void print_class(Class c)&lt;br /&gt;
  {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot;+c.getName());&lt;br /&gt;
    }&lt;br /&gt;
    else if (c.getSuperclass() != null)&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName() +&lt;br /&gt;
                       &amp;quot; extends &amp;quot; + c.getSuperclass().getName());&lt;br /&gt;
    else&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName());&lt;br /&gt;
        &lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface()) System.out.println(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for(int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(interfaces[i].getName());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;);            // Begin class member listing.&lt;br /&gt;
&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
    System.out.println(&amp;quot; // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for(int i = 0; i &amp;lt; constructors.length; i++)      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields();           // Look up fields.&lt;br /&gt;
    for(int i = 0; i &amp;lt; fields.length; i++)            // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods();        // Look up methods.&lt;br /&gt;
    for(int i = 0; i &amp;lt; methods.length; i++)           // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;);             // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while(t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    return t.getName() + brackets;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
  public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0) return &amp;quot;&amp;quot;;&lt;br /&gt;
    else return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, type, and name of a field */&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; +&lt;br /&gt;
                       modifiers(f.getModifiers()) +&lt;br /&gt;
                       typename(f.getType()) + &amp;quot; &amp;quot; + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, return type, name, parameter types and exception&lt;br /&gt;
   *  type of a method or constructor.  Note the use of the Member interface&lt;br /&gt;
   *  to allow this method to work with both Method and Constructor objects */&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype=null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) +&lt;br /&gt;
                     ((returntype!=null)? typename(returntype)+&amp;quot; &amp;quot; : &amp;quot;&amp;quot;) +&lt;br /&gt;
                     member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0) System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code is then compiled and run, with the following simple test class passed in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput {&lt;br /&gt;
    public static void main(String[] args) {}&lt;br /&gt;
    public int method1() {&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public String method2() {&lt;br /&gt;
        return &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    public void method3() {}&lt;br /&gt;
    public void method4(int num) {}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output generated is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput extends java.lang.Object {&lt;br /&gt;
 // Constructors&lt;br /&gt;
  public testInput();&lt;br /&gt;
 // Fields&lt;br /&gt;
 // Methods&lt;br /&gt;
  public int method1();&lt;br /&gt;
  public java.lang.String method2();&lt;br /&gt;
  public void method3();&lt;br /&gt;
  public void method4(int);&lt;br /&gt;
  public static void main(java.lang.String[]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection Sequences ===&lt;br /&gt;
The Java code above, which displays constructors, methods, and other information pertinent to the ''testInput'' class, is somewhat convoluted and difficult to trace.  In Ruby, the methods below can be called on a module/class to return similar information:&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.private_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.protected_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.public_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.singleton_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
For example, using the &amp;lt;tt&amp;gt;Demo&amp;lt;/tt&amp;gt; class below, all of the class' methods (private, protected, public, and singleton) can be easily displayed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; class Demo&lt;br /&gt;
irb(main):002:1&amp;gt;   public&lt;br /&gt;
irb(main):003:1&amp;gt;     def method1&lt;br /&gt;
irb(main):004:2&amp;gt;   end&lt;br /&gt;
irb(main):005:1&amp;gt;   private&lt;br /&gt;
irb(main):006:1&amp;gt;     def method2&lt;br /&gt;
irb(main):007:2&amp;gt;   end&lt;br /&gt;
irb(main):008:1&amp;gt;   public&lt;br /&gt;
irb(main):009:1&amp;gt;     def method3&lt;br /&gt;
irb(main):010:2&amp;gt;   end&lt;br /&gt;
irb(main):011:1&amp;gt;   protected&lt;br /&gt;
irb(main):012:1&amp;gt;     def method4&lt;br /&gt;
irb(main):013:2&amp;gt;   end&lt;br /&gt;
irb(main):014:1&amp;gt;   def Demo.method5&lt;br /&gt;
irb(main):015:2&amp;gt;   end&lt;br /&gt;
irb(main):016:1&amp;gt;   public&lt;br /&gt;
irb(main):017:1&amp;gt;     def method6&lt;br /&gt;
irb(main):018:2&amp;gt;   end&lt;br /&gt;
irb(main):019:1&amp;gt; end&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):020:0&amp;gt; Demo.private_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method2&amp;quot;]&lt;br /&gt;
irb(main):021:0&amp;gt; Demo.protected_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method4&amp;quot;]&lt;br /&gt;
irb(main):022:0&amp;gt; Demo.public_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method3&amp;quot;, &amp;quot;method6&amp;quot;, &amp;quot;method1&amp;quot;]&lt;br /&gt;
irb(main):023:0&amp;gt; Demo.singleton_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method5&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to looking inside classes, reflection in Ruby provides the ability to dynamically call methods.  An arbitrary method of some object can be called using &amp;lt;tt&amp;gt;Object#send&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;Method#call&amp;lt;/tt&amp;gt;, and also various versions of the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method (including &amp;lt;tt&amp;gt;class_eval&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;module_eval&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;instance_eval&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;send&amp;lt;/tt&amp;gt; works on any object, and it calls the passed in method on that object.  It will also find new methods added at runtime.  In the below example, the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; method is called on the object, which is a 14 character string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):105:0&amp;gt; &amp;quot;This is a test&amp;quot;.send(:length)&lt;br /&gt;
=&amp;gt; 14&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; objects represent a chunk of code and a context in which it executes.  The &amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; object is created, and it can be executed whenever desired by sending it the &amp;lt;tt&amp;gt;call&amp;lt;/tt&amp;gt; message:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):106:0&amp;gt; test_message = &amp;quot;Test message&amp;quot;.method(:length)&lt;br /&gt;
=&amp;gt; #&amp;lt;Method: String#length&amp;gt;&lt;br /&gt;
irb(main):107:0&amp;gt; test_message.call&lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A third way methods can be invoked dynamically using reflection is with the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method.  &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; provides the ability to parse and execute an arbitrary string of legal Ruby source code.  The string of code can be stored as an object, and then evaluated at a later time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):111:0&amp;gt; eval_test = %q{&amp;quot;A third test&amp;quot;.length}&lt;br /&gt;
=&amp;gt; &amp;quot;\&amp;quot;A third test\&amp;quot;.length&amp;quot;&lt;br /&gt;
irb(main):112:0&amp;gt; eval eval_test  &lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection vs. Java Reflection ===&lt;br /&gt;
&lt;br /&gt;
*By coding and comparing we find that Reflection in Java tends to be more verbose than Ruby code which accomplishes the same task.  Also, exceptions such as &amp;lt;tt&amp;gt;NoSuchMethodError&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;InvocationTargetException&amp;lt;/tt&amp;gt; tend to create further complications and confusion when using Java's reflection API.&lt;br /&gt;
**In order to print out all the methods contained in a class in Java, we approached 100 lines of code.  It took merely 4 lines of code in Ruby to print all private, protected, public, and singleton methods of a class.  This illustrates Ruby efficiency when looking inside classes.&lt;br /&gt;
&lt;br /&gt;
*In Ruby, Aspect Oriented Programming(AOP) can be performed using ''aliasing'', ''reflection'', and ''metaprogramming''.  These language facilities make it much easier to perform AOP than in Java.&lt;br /&gt;
&lt;br /&gt;
=== Uses of Reflection ===&lt;br /&gt;
Reflection 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 and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
*Extensibility Features &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;
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;
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;
*Reflection API in java is also used in Database Access with SQL.&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;
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;
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;
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;
=== ''References'' ===&lt;br /&gt;
#Flanagan, David.  [http://books.google.com/books?id=kADTIsgeLh4C Java Examples in a Nutshell].  O'Reilly, 2004.&lt;br /&gt;
#Thomas, Dave.  [http://books.google.com/books?id=8LXOAAAACAAJ Programming Ruby].  Raleigh: Pragmatic Bookshelf, 2004.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
*http://phrogz.net/programmingruby/ospace.html&lt;br /&gt;
*http://java.sun.com/docs/books/tutorial/reflect/&lt;br /&gt;
*http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html&lt;br /&gt;
*http://www.javaworld.com/javaworld/jw-07-2006/jw-0717-ruby.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12462</id>
		<title>CSC/ECE 517 Summer 2008/wiki1 3 aobk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12462"/>
		<updated>2008-06-06T17:47:58Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Ruby Reflection vs. Java Reflection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection in Ruby and Java ==&lt;br /&gt;
;What is ''Reflection''?&lt;br /&gt;
Reflection, in the realm of Computer Science, is the process of introspection by a program.  Introspection allows program entities to discover things about themselves.  Through the technique of ''metaprogramming'', aspects of a program are examined from within the program itself, which enables the program to modify itself at runtime.  This provides for a very liberated and dynamic programming environment.&lt;br /&gt;
&lt;br /&gt;
By looking at and inside classes and objects, much can be discovered, including the following:&lt;br /&gt;
* what objects are contained in a program&lt;br /&gt;
* the class hierarchy&lt;br /&gt;
* the attributes and methods of the objects contained in the program&lt;br /&gt;
* various method information&lt;br /&gt;
&lt;br /&gt;
One of the design challenges when using Reflection is that you need to clarify what services you wish to provide at runtime, i.e. ones that you don't want to implement statically. The provision of reusable services that vary based upon the runtime environment is one of the reasons that frameworks such as Struts and JAAS are created.  However, even small applications can face design issues that make a dynamic solution preferable over a static one. A word of caution, though, over-reliance on Reflection can lead to performance issues due to the extra overhead when determining what class(es) need to be loaded.&lt;br /&gt;
&lt;br /&gt;
=== Java Reflection Sequences ===&lt;br /&gt;
;''Reflection in Java''&lt;br /&gt;
:Java uses a '''Reflection API''' to inspect and manipulate itself.  It includes the expanded ''class'' class in java.lang and the java.lang.reflect package, which represents the members of a class with ''Method'', ''Constructor'' and ''Field'' objects.Reflection can be used to obtain information about a class and its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
*This example uses the &amp;lt;tt&amp;gt;Class.forName()&amp;lt;/tt&amp;gt; method to dynamically load the named class, and then calls various methods of the ''class'' object to look up the Superclass, Interfaces, and Members of the class. The example uses ''Constructor'', ''Field'' and ''Method'' objects to obtain information about each member of the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
  /** The main method.  Print info about the named class */&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Display the modifiers, name, superclass and interfaces of a class&lt;br /&gt;
   *  or interface. Then go and list all constructors, fields, and methods. */&lt;br /&gt;
  public static void print_class(Class c)&lt;br /&gt;
  {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot;+c.getName());&lt;br /&gt;
    }&lt;br /&gt;
    else if (c.getSuperclass() != null)&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName() +&lt;br /&gt;
                       &amp;quot; extends &amp;quot; + c.getSuperclass().getName());&lt;br /&gt;
    else&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName());&lt;br /&gt;
        &lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface()) System.out.println(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for(int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(interfaces[i].getName());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;);            // Begin class member listing.&lt;br /&gt;
&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
    System.out.println(&amp;quot; // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for(int i = 0; i &amp;lt; constructors.length; i++)      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields();           // Look up fields.&lt;br /&gt;
    for(int i = 0; i &amp;lt; fields.length; i++)            // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods();        // Look up methods.&lt;br /&gt;
    for(int i = 0; i &amp;lt; methods.length; i++)           // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;);             // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while(t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    return t.getName() + brackets;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
  public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0) return &amp;quot;&amp;quot;;&lt;br /&gt;
    else return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, type, and name of a field */&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; +&lt;br /&gt;
                       modifiers(f.getModifiers()) +&lt;br /&gt;
                       typename(f.getType()) + &amp;quot; &amp;quot; + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, return type, name, parameter types and exception&lt;br /&gt;
   *  type of a method or constructor.  Note the use of the Member interface&lt;br /&gt;
   *  to allow this method to work with both Method and Constructor objects */&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype=null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) +&lt;br /&gt;
                     ((returntype!=null)? typename(returntype)+&amp;quot; &amp;quot; : &amp;quot;&amp;quot;) +&lt;br /&gt;
                     member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0) System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code is then compiled and run, with the following simple test class passed in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput {&lt;br /&gt;
    public static void main(String[] args) {}&lt;br /&gt;
    public int method1() {&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public String method2() {&lt;br /&gt;
        return &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    public void method3() {}&lt;br /&gt;
    public void method4(int num) {}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output generated is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput extends java.lang.Object {&lt;br /&gt;
 // Constructors&lt;br /&gt;
  public testInput();&lt;br /&gt;
 // Fields&lt;br /&gt;
 // Methods&lt;br /&gt;
  public int method1();&lt;br /&gt;
  public java.lang.String method2();&lt;br /&gt;
  public void method3();&lt;br /&gt;
  public void method4(int);&lt;br /&gt;
  public static void main(java.lang.String[]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection Sequences ===&lt;br /&gt;
The Java code above, which displays constructors, methods, and other information pertinent to the ''testInput'' class, is somewhat convoluted and difficult to trace.  In Ruby, the methods below can be called on a module/class to return similar information:&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.private_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.protected_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.public_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.singleton_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
For example, using the &amp;lt;tt&amp;gt;Demo&amp;lt;/tt&amp;gt; class below, all of the class' methods (private, protected, public, and singleton) can be easily displayed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; class Demo&lt;br /&gt;
irb(main):002:1&amp;gt;   public&lt;br /&gt;
irb(main):003:1&amp;gt;     def method1&lt;br /&gt;
irb(main):004:2&amp;gt;   end&lt;br /&gt;
irb(main):005:1&amp;gt;   private&lt;br /&gt;
irb(main):006:1&amp;gt;     def method2&lt;br /&gt;
irb(main):007:2&amp;gt;   end&lt;br /&gt;
irb(main):008:1&amp;gt;   public&lt;br /&gt;
irb(main):009:1&amp;gt;     def method3&lt;br /&gt;
irb(main):010:2&amp;gt;   end&lt;br /&gt;
irb(main):011:1&amp;gt;   protected&lt;br /&gt;
irb(main):012:1&amp;gt;     def method4&lt;br /&gt;
irb(main):013:2&amp;gt;   end&lt;br /&gt;
irb(main):014:1&amp;gt;   def Demo.method5&lt;br /&gt;
irb(main):015:2&amp;gt;   end&lt;br /&gt;
irb(main):016:1&amp;gt;   public&lt;br /&gt;
irb(main):017:1&amp;gt;     def method6&lt;br /&gt;
irb(main):018:2&amp;gt;   end&lt;br /&gt;
irb(main):019:1&amp;gt; end&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):020:0&amp;gt; Demo.private_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method2&amp;quot;]&lt;br /&gt;
irb(main):021:0&amp;gt; Demo.protected_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method4&amp;quot;]&lt;br /&gt;
irb(main):022:0&amp;gt; Demo.public_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method3&amp;quot;, &amp;quot;method6&amp;quot;, &amp;quot;method1&amp;quot;]&lt;br /&gt;
irb(main):023:0&amp;gt; Demo.singleton_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method5&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to looking inside classes, reflection in Ruby provides the ability to dynamically call methods.  An arbitrary method of some object can be called using &amp;lt;tt&amp;gt;Object#send&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;Method#call&amp;lt;/tt&amp;gt;, and also various versions of the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method (including &amp;lt;tt&amp;gt;class_eval&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;module_eval&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;instance_eval&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;send&amp;lt;/tt&amp;gt; works on any object, and it calls the passed in method on that object.  It will also find new methods added at runtime.  In the below example, the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; method is called on the object, which is a 14 character string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):105:0&amp;gt; &amp;quot;This is a test&amp;quot;.send(:length)&lt;br /&gt;
=&amp;gt; 14&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; objects represent a chunk of code and a context in which it executes.  The &amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; object is created, and it can be executed whenever desired by sending it the &amp;lt;tt&amp;gt;call&amp;lt;/tt&amp;gt; message:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):106:0&amp;gt; test_message = &amp;quot;Test message&amp;quot;.method(:length)&lt;br /&gt;
=&amp;gt; #&amp;lt;Method: String#length&amp;gt;&lt;br /&gt;
irb(main):107:0&amp;gt; test_message.call&lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A third way methods can be invoked dynamically using reflection is with the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method.  &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; provides the ability to parse and execute an arbitrary string of legal Ruby source code.  The string of code can be stored as an object, and then evaluated at a later time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):111:0&amp;gt; eval_test = %q{&amp;quot;A third test&amp;quot;.length}&lt;br /&gt;
=&amp;gt; &amp;quot;\&amp;quot;A third test\&amp;quot;.length&amp;quot;&lt;br /&gt;
irb(main):112:0&amp;gt; eval eval_test  &lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection vs. Java Reflection ===&lt;br /&gt;
&lt;br /&gt;
*By coding and comparing we find that Reflection in Java tends to be more verbose than Ruby code which accomplishes the same task.  Also, exceptions such as &amp;lt;tt&amp;gt;NoSuchMethodError&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;InvocationTargetException&amp;lt;/tt&amp;gt; tend to create further complications and confusion when using Java's reflection API.&lt;br /&gt;
**In order to print out all the methods contained in a class in Java, we approached 100 lines of code.  It took merely 4 lines of code in Ruby to print all private, protected, public, and singleton methods of a class.  This illustrates Ruby coding efficiency when looking inside classes.&lt;br /&gt;
&lt;br /&gt;
*In Ruby, Aspect Oriented Programming(AOP) can be performed using ''aliasing'', ''reflection'', and ''metaprogramming''.  These language facilities make it much easier to perform AOP than in Java.&lt;br /&gt;
&lt;br /&gt;
=== Uses of Reflection ===&lt;br /&gt;
Reflection 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 and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
*Extensibility Features &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;
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;
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;
*Reflection API in java is also used in Database Access with SQL.&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;
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;
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;
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;
=== ''References'' ===&lt;br /&gt;
#Flanagan, David.  [http://books.google.com/books?id=kADTIsgeLh4C Java Examples in a Nutshell].  O'Reilly, 2004.&lt;br /&gt;
#Thomas, Dave.  [http://books.google.com/books?id=8LXOAAAACAAJ Programming Ruby].  Raleigh: Pragmatic Bookshelf, 2004.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
*http://phrogz.net/programmingruby/ospace.html&lt;br /&gt;
*http://java.sun.com/docs/books/tutorial/reflect/&lt;br /&gt;
*http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html&lt;br /&gt;
*http://www.javaworld.com/javaworld/jw-07-2006/jw-0717-ruby.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12460</id>
		<title>CSC/ECE 517 Summer 2008/wiki1 3 aobk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12460"/>
		<updated>2008-06-06T17:44:02Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* ''Refrences'' */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection in Ruby and Java ==&lt;br /&gt;
;What is ''Reflection''?&lt;br /&gt;
Reflection, in the realm of Computer Science, is the process of introspection by a program.  Introspection allows program entities to discover things about themselves.  Through the technique of ''metaprogramming'', aspects of a program are examined from within the program itself, which enables the program to modify itself at runtime.  This provides for a very liberated and dynamic programming environment.&lt;br /&gt;
&lt;br /&gt;
By looking at and inside classes and objects, much can be discovered, including the following:&lt;br /&gt;
* what objects are contained in a program&lt;br /&gt;
* the class hierarchy&lt;br /&gt;
* the attributes and methods of the objects contained in the program&lt;br /&gt;
* various method information&lt;br /&gt;
&lt;br /&gt;
One of the design challenges when using Reflection is that you need to clarify what services you wish to provide at runtime, i.e. ones that you don't want to implement statically. The provision of reusable services that vary based upon the runtime environment is one of the reasons that frameworks such as Struts and JAAS are created.  However, even small applications can face design issues that make a dynamic solution preferable over a static one. A word of caution, though, over-reliance on Reflection can lead to performance issues due to the extra overhead when determining what class(es) need to be loaded.&lt;br /&gt;
&lt;br /&gt;
=== Java Reflection Sequences ===&lt;br /&gt;
;''Reflection in Java''&lt;br /&gt;
:Java uses a '''Reflection API''' to inspect and manipulate itself.  It includes the expanded ''class'' class in java.lang and the java.lang.reflect package, which represents the members of a class with ''Method'', ''Constructor'' and ''Field'' objects.Reflection can be used to obtain information about a class and its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
*This example uses the &amp;lt;tt&amp;gt;Class.forName()&amp;lt;/tt&amp;gt; method to dynamically load the named class, and then calls various methods of the ''class'' object to look up the Superclass, Interfaces, and Members of the class. The example uses ''Constructor'', ''Field'' and ''Method'' objects to obtain information about each member of the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
  /** The main method.  Print info about the named class */&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Display the modifiers, name, superclass and interfaces of a class&lt;br /&gt;
   *  or interface. Then go and list all constructors, fields, and methods. */&lt;br /&gt;
  public static void print_class(Class c)&lt;br /&gt;
  {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot;+c.getName());&lt;br /&gt;
    }&lt;br /&gt;
    else if (c.getSuperclass() != null)&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName() +&lt;br /&gt;
                       &amp;quot; extends &amp;quot; + c.getSuperclass().getName());&lt;br /&gt;
    else&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName());&lt;br /&gt;
        &lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface()) System.out.println(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for(int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(interfaces[i].getName());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;);            // Begin class member listing.&lt;br /&gt;
&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
    System.out.println(&amp;quot; // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for(int i = 0; i &amp;lt; constructors.length; i++)      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields();           // Look up fields.&lt;br /&gt;
    for(int i = 0; i &amp;lt; fields.length; i++)            // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods();        // Look up methods.&lt;br /&gt;
    for(int i = 0; i &amp;lt; methods.length; i++)           // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;);             // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while(t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    return t.getName() + brackets;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
  public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0) return &amp;quot;&amp;quot;;&lt;br /&gt;
    else return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, type, and name of a field */&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; +&lt;br /&gt;
                       modifiers(f.getModifiers()) +&lt;br /&gt;
                       typename(f.getType()) + &amp;quot; &amp;quot; + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, return type, name, parameter types and exception&lt;br /&gt;
   *  type of a method or constructor.  Note the use of the Member interface&lt;br /&gt;
   *  to allow this method to work with both Method and Constructor objects */&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype=null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) +&lt;br /&gt;
                     ((returntype!=null)? typename(returntype)+&amp;quot; &amp;quot; : &amp;quot;&amp;quot;) +&lt;br /&gt;
                     member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0) System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code is then compiled and run, with the following simple test class passed in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput {&lt;br /&gt;
    public static void main(String[] args) {}&lt;br /&gt;
    public int method1() {&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public String method2() {&lt;br /&gt;
        return &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    public void method3() {}&lt;br /&gt;
    public void method4(int num) {}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output generated is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput extends java.lang.Object {&lt;br /&gt;
 // Constructors&lt;br /&gt;
  public testInput();&lt;br /&gt;
 // Fields&lt;br /&gt;
 // Methods&lt;br /&gt;
  public int method1();&lt;br /&gt;
  public java.lang.String method2();&lt;br /&gt;
  public void method3();&lt;br /&gt;
  public void method4(int);&lt;br /&gt;
  public static void main(java.lang.String[]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection Sequences ===&lt;br /&gt;
The Java code above, which displays constructors, methods, and other information pertinent to the ''testInput'' class, is somewhat convoluted and difficult to trace.  In Ruby, the methods below can be called on a module/class to return similar information:&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.private_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.protected_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.public_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.singleton_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
For example, using the &amp;lt;tt&amp;gt;Demo&amp;lt;/tt&amp;gt; class below, all of the class' methods (private, protected, public, and singleton) can be easily displayed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; class Demo&lt;br /&gt;
irb(main):002:1&amp;gt;   public&lt;br /&gt;
irb(main):003:1&amp;gt;     def method1&lt;br /&gt;
irb(main):004:2&amp;gt;   end&lt;br /&gt;
irb(main):005:1&amp;gt;   private&lt;br /&gt;
irb(main):006:1&amp;gt;     def method2&lt;br /&gt;
irb(main):007:2&amp;gt;   end&lt;br /&gt;
irb(main):008:1&amp;gt;   public&lt;br /&gt;
irb(main):009:1&amp;gt;     def method3&lt;br /&gt;
irb(main):010:2&amp;gt;   end&lt;br /&gt;
irb(main):011:1&amp;gt;   protected&lt;br /&gt;
irb(main):012:1&amp;gt;     def method4&lt;br /&gt;
irb(main):013:2&amp;gt;   end&lt;br /&gt;
irb(main):014:1&amp;gt;   def Demo.method5&lt;br /&gt;
irb(main):015:2&amp;gt;   end&lt;br /&gt;
irb(main):016:1&amp;gt;   public&lt;br /&gt;
irb(main):017:1&amp;gt;     def method6&lt;br /&gt;
irb(main):018:2&amp;gt;   end&lt;br /&gt;
irb(main):019:1&amp;gt; end&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):020:0&amp;gt; Demo.private_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method2&amp;quot;]&lt;br /&gt;
irb(main):021:0&amp;gt; Demo.protected_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method4&amp;quot;]&lt;br /&gt;
irb(main):022:0&amp;gt; Demo.public_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method3&amp;quot;, &amp;quot;method6&amp;quot;, &amp;quot;method1&amp;quot;]&lt;br /&gt;
irb(main):023:0&amp;gt; Demo.singleton_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method5&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to looking inside classes, reflection in Ruby provides the ability to dynamically call methods.  An arbitrary method of some object can be called using &amp;lt;tt&amp;gt;Object#send&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;Method#call&amp;lt;/tt&amp;gt;, and also various versions of the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method (including &amp;lt;tt&amp;gt;class_eval&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;module_eval&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;instance_eval&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;send&amp;lt;/tt&amp;gt; works on any object, and it calls the passed in method on that object.  It will also find new methods added at runtime.  In the below example, the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; method is called on the object, which is a 14 character string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):105:0&amp;gt; &amp;quot;This is a test&amp;quot;.send(:length)&lt;br /&gt;
=&amp;gt; 14&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; objects represent a chunk of code and a context in which it executes.  The &amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; object is created, and it can be executed whenever desired by sending it the &amp;lt;tt&amp;gt;call&amp;lt;/tt&amp;gt; message:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):106:0&amp;gt; test_message = &amp;quot;Test message&amp;quot;.method(:length)&lt;br /&gt;
=&amp;gt; #&amp;lt;Method: String#length&amp;gt;&lt;br /&gt;
irb(main):107:0&amp;gt; test_message.call&lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A third way methods can be invoked dynamically using reflection is with the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method.  &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; provides the ability to parse and execute an arbitrary string of legal Ruby source code.  The string of code can be stored as an object, and then evaluated at a later time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):111:0&amp;gt; eval_test = %q{&amp;quot;A third test&amp;quot;.length}&lt;br /&gt;
=&amp;gt; &amp;quot;\&amp;quot;A third test\&amp;quot;.length&amp;quot;&lt;br /&gt;
irb(main):112:0&amp;gt; eval eval_test  &lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection vs. Java Reflection ===&lt;br /&gt;
&lt;br /&gt;
*By coding and comparing we find that Reflection in Java tends to be more verbose than Ruby code which accomplishes the same task.  Also, exceptions such as &amp;lt;tt&amp;gt;NoSuchMethodError&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;InvocationTargetException&amp;lt;/tt&amp;gt; tend to create further complications and confusion when using Java's reflection API.&lt;br /&gt;
**In order to print out all the methods contained in a class in Java, we approached 100 lines of code.  It took merely 4 lines of code in Ruby to print all private, protected, public, and singleton methods of a class.&lt;br /&gt;
&lt;br /&gt;
*In Ruby, Aspect Oriented Programming(AOP) can be performed using ''aliasing'', ''reflection'', and ''metaprogramming''.  These language facilities make it much easier to perform AOP than in Java.&lt;br /&gt;
&lt;br /&gt;
=== Uses of Reflection ===&lt;br /&gt;
Reflection 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 and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
*Extensibility Features &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;
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;
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;
*Reflection API in java is also used in Database Access with SQL.&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;
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;
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;
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;
=== ''References'' ===&lt;br /&gt;
#Flanagan, David.  [http://books.google.com/books?id=kADTIsgeLh4C Java Examples in a Nutshell].  O'Reilly, 2004.&lt;br /&gt;
#Thomas, Dave.  [http://books.google.com/books?id=8LXOAAAACAAJ Programming Ruby].  Raleigh: Pragmatic Bookshelf, 2004.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
*http://phrogz.net/programmingruby/ospace.html&lt;br /&gt;
*http://java.sun.com/docs/books/tutorial/reflect/&lt;br /&gt;
*http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html&lt;br /&gt;
*http://www.javaworld.com/javaworld/jw-07-2006/jw-0717-ruby.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12426</id>
		<title>CSC/ECE 517 Summer 2008/wiki1 3 aobk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12426"/>
		<updated>2008-06-06T11:45:23Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* ''External Links'' */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection in Ruby and Java ==&lt;br /&gt;
;What is ''Reflection''?&lt;br /&gt;
Reflection, in the realm of Computer Science, is the process of introspection by a program.  Introspection allows program entities to discover things about themselves.  Through the technique of ''metaprogramming'', aspects of a program are examined from within the program itself, which enables the program to modify itself at runtime.  This provides for a very liberated and dynamic programming environment.&lt;br /&gt;
&lt;br /&gt;
By looking at and inside classes and objects, much can be discovered, including the following:&lt;br /&gt;
* what objects are contained in a program&lt;br /&gt;
* the class hierarchy&lt;br /&gt;
* the attributes and methods of the objects contained in the program&lt;br /&gt;
* various method information&lt;br /&gt;
&lt;br /&gt;
One of the design challenges when using Reflection is that you need to clarify what services you wish to provide at runtime, i.e. ones that you don't want to implement statically. The provision of reusable services that vary based upon the runtime environment is one of the reasons that frameworks such as Struts and JAAS are created.  However, even small applications can face design issues that make a dynamic solution preferable over a static one. A word of caution, though, over-reliance on Reflection can lead to performance issues due to the extra overhead when determining what class(es) need to be loaded.&lt;br /&gt;
&lt;br /&gt;
=== Java Reflection Sequences ===&lt;br /&gt;
;''Reflection in Java''&lt;br /&gt;
:Java uses a '''Reflection API''' to inspect and manipulate itself.  It includes the expanded ''class'' class in java.lang and the java.lang.reflect package, which represents the members of a class with ''Method'', ''Constructor'' and ''Field'' objects.Reflection can be used to obtain information about a class and its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
*This example uses the &amp;lt;tt&amp;gt;Class.forName()&amp;lt;/tt&amp;gt; method to dynamically load the named class, and then calls various methods of the ''class'' object to look up the Superclass, Interfaces, and Members of the class. The example uses ''Constructor'', ''Field'' and ''Method'' objects to obtain information about each member of the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
  /** The main method.  Print info about the named class */&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Display the modifiers, name, superclass and interfaces of a class&lt;br /&gt;
   *  or interface. Then go and list all constructors, fields, and methods. */&lt;br /&gt;
  public static void print_class(Class c)&lt;br /&gt;
  {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot;+c.getName());&lt;br /&gt;
    }&lt;br /&gt;
    else if (c.getSuperclass() != null)&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName() +&lt;br /&gt;
                       &amp;quot; extends &amp;quot; + c.getSuperclass().getName());&lt;br /&gt;
    else&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName());&lt;br /&gt;
        &lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface()) System.out.println(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for(int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(interfaces[i].getName());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;);            // Begin class member listing.&lt;br /&gt;
&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
    System.out.println(&amp;quot; // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for(int i = 0; i &amp;lt; constructors.length; i++)      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields();           // Look up fields.&lt;br /&gt;
    for(int i = 0; i &amp;lt; fields.length; i++)            // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods();        // Look up methods.&lt;br /&gt;
    for(int i = 0; i &amp;lt; methods.length; i++)           // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;);             // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while(t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    return t.getName() + brackets;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
  public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0) return &amp;quot;&amp;quot;;&lt;br /&gt;
    else return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, type, and name of a field */&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; +&lt;br /&gt;
                       modifiers(f.getModifiers()) +&lt;br /&gt;
                       typename(f.getType()) + &amp;quot; &amp;quot; + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, return type, name, parameter types and exception&lt;br /&gt;
   *  type of a method or constructor.  Note the use of the Member interface&lt;br /&gt;
   *  to allow this method to work with both Method and Constructor objects */&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype=null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) +&lt;br /&gt;
                     ((returntype!=null)? typename(returntype)+&amp;quot; &amp;quot; : &amp;quot;&amp;quot;) +&lt;br /&gt;
                     member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0) System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code is then compiled and run, with the following simple test class passed in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput {&lt;br /&gt;
    public static void main(String[] args) {}&lt;br /&gt;
    public int method1() {&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public String method2() {&lt;br /&gt;
        return &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    public void method3() {}&lt;br /&gt;
    public void method4(int num) {}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output generated is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput extends java.lang.Object {&lt;br /&gt;
 // Constructors&lt;br /&gt;
  public testInput();&lt;br /&gt;
 // Fields&lt;br /&gt;
 // Methods&lt;br /&gt;
  public int method1();&lt;br /&gt;
  public java.lang.String method2();&lt;br /&gt;
  public void method3();&lt;br /&gt;
  public void method4(int);&lt;br /&gt;
  public static void main(java.lang.String[]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection Sequences ===&lt;br /&gt;
The Java code above, which displays constructors, methods, and other information pertinent to the ''testInput'' class, is somewhat convoluted and difficult to trace.  In Ruby, the methods below can be called on a module/class to return similar information:&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.private_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.protected_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.public_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.singleton_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
For example, using the &amp;lt;tt&amp;gt;Demo&amp;lt;/tt&amp;gt; class below, all of the class' methods (private, protected, public, and singleton) can be easily displayed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; class Demo&lt;br /&gt;
irb(main):002:1&amp;gt;   public&lt;br /&gt;
irb(main):003:1&amp;gt;     def method1&lt;br /&gt;
irb(main):004:2&amp;gt;   end&lt;br /&gt;
irb(main):005:1&amp;gt;   private&lt;br /&gt;
irb(main):006:1&amp;gt;     def method2&lt;br /&gt;
irb(main):007:2&amp;gt;   end&lt;br /&gt;
irb(main):008:1&amp;gt;   public&lt;br /&gt;
irb(main):009:1&amp;gt;     def method3&lt;br /&gt;
irb(main):010:2&amp;gt;   end&lt;br /&gt;
irb(main):011:1&amp;gt;   protected&lt;br /&gt;
irb(main):012:1&amp;gt;     def method4&lt;br /&gt;
irb(main):013:2&amp;gt;   end&lt;br /&gt;
irb(main):014:1&amp;gt;   def Demo.method5&lt;br /&gt;
irb(main):015:2&amp;gt;   end&lt;br /&gt;
irb(main):016:1&amp;gt;   public&lt;br /&gt;
irb(main):017:1&amp;gt;     def method6&lt;br /&gt;
irb(main):018:2&amp;gt;   end&lt;br /&gt;
irb(main):019:1&amp;gt; end&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):020:0&amp;gt; Demo.private_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method2&amp;quot;]&lt;br /&gt;
irb(main):021:0&amp;gt; Demo.protected_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method4&amp;quot;]&lt;br /&gt;
irb(main):022:0&amp;gt; Demo.public_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method3&amp;quot;, &amp;quot;method6&amp;quot;, &amp;quot;method1&amp;quot;]&lt;br /&gt;
irb(main):023:0&amp;gt; Demo.singleton_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method5&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to looking inside classes, reflection in Ruby provides the ability to dynamically call methods.  An arbitrary method of some object can be called using &amp;lt;tt&amp;gt;Object#send&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;Method#call&amp;lt;/tt&amp;gt;, and also various versions of the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method (including &amp;lt;tt&amp;gt;class_eval&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;module_eval&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;instance_eval&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;send&amp;lt;/tt&amp;gt; works on any object, and it calls the passed in method on that object.  It will also find new methods added at runtime.  In the below example, the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; method is called on the object, which is a 14 character string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):105:0&amp;gt; &amp;quot;This is a test&amp;quot;.send(:length)&lt;br /&gt;
=&amp;gt; 14&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; objects represent a chunk of code and a context in which it executes.  The &amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; object is created, and it can be executed whenever desired by sending it the &amp;lt;tt&amp;gt;call&amp;lt;/tt&amp;gt; message:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):106:0&amp;gt; test_message = &amp;quot;Test message&amp;quot;.method(:length)&lt;br /&gt;
=&amp;gt; #&amp;lt;Method: String#length&amp;gt;&lt;br /&gt;
irb(main):107:0&amp;gt; test_message.call&lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A third way methods can be invoked dynamically using reflection is with the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method.  &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; provides the ability to parse and execute an arbitrary string of legal Ruby source code.  The string of code can be stored as an object, and then evaluated at a later time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):111:0&amp;gt; eval_test = %q{&amp;quot;A third test&amp;quot;.length}&lt;br /&gt;
=&amp;gt; &amp;quot;\&amp;quot;A third test\&amp;quot;.length&amp;quot;&lt;br /&gt;
irb(main):112:0&amp;gt; eval eval_test  &lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection vs. Java Reflection ===&lt;br /&gt;
&lt;br /&gt;
*By coding and comparing we find that Reflection in Java tends to be more verbose than Ruby code which accomplishes the same task.  Also, exceptions such as &amp;lt;tt&amp;gt;NoSuchMethodError&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;InvocationTargetException&amp;lt;/tt&amp;gt; tend to create further complications and confusion when using Java's reflection API.&lt;br /&gt;
**In order to print out all the methods contained in a class in Java, we approached 100 lines of code.  It took merely 4 lines of code in Ruby to print all private, protected, public, and singleton methods of a class.&lt;br /&gt;
&lt;br /&gt;
*In Ruby, Aspect Oriented Programming(AOP) can be performed using ''aliasing'', ''reflection'', and ''metaprogramming''.  These language facilities make it much easier to perform AOP than in Java.&lt;br /&gt;
&lt;br /&gt;
=== Uses of Reflection ===&lt;br /&gt;
Reflection 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 and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
*Extensibility Features &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;
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;
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;
*Reflection API in java is also used in Database Access with SQL.&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;
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;
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;
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;
=== ''Refrences'' ===&lt;br /&gt;
#Flanagan, David.  [http://books.google.com/books?id=kADTIsgeLh4C Java Examples in a Nutshell].  O'Reilly, 2004.&lt;br /&gt;
#Thomas, Dave.  [http://books.google.com/books?id=8LXOAAAACAAJ Programming Ruby].  Raleigh: Pragmatic Bookshelf, 2004.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
*http://phrogz.net/programmingruby/ospace.html&lt;br /&gt;
*http://java.sun.com/docs/books/tutorial/reflect/&lt;br /&gt;
*http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html&lt;br /&gt;
*http://www.javaworld.com/javaworld/jw-07-2006/jw-0717-ruby.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12395</id>
		<title>CSC/ECE 517 Summer 2008/wiki1 3 aobk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12395"/>
		<updated>2008-06-06T01:43:54Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Ruby Reflection vs. Java Reflection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection in Ruby and Java ==&lt;br /&gt;
;What is ''Reflection''?&lt;br /&gt;
Reflection, in the realm of Computer Science, is the process of introspection by a program.  Introspection allows program entities to discover things about themselves.  Through the technique of ''metaprogramming'', aspects of a program are examined from within the program itself, which enables the program to modify itself at runtime.  This provides for a very liberated and dynamic programming environment.&lt;br /&gt;
&lt;br /&gt;
By looking at and inside classes and objects, much can be discovered, including the following:&lt;br /&gt;
* what objects are contained in a program&lt;br /&gt;
* the class hierarchy&lt;br /&gt;
* the attributes and methods of the objects contained in the program&lt;br /&gt;
* various method information&lt;br /&gt;
&lt;br /&gt;
One of the design challenges when using Reflection is that you need to clarify what services you wish to provide at runtime, i.e. ones that you don't want to implement statically. The provision of reusable services that vary based upon the runtime environment is one of the reasons that frameworks such as Struts and JAAS are created.  However, even small applications can face design issues that make a dynamic solution preferable over a static one. A word of caution, though, over-reliance on Reflection can lead to performance issues due to the extra overhead when determining what class(es) need to be loaded.&lt;br /&gt;
&lt;br /&gt;
=== Java Reflection Sequences ===&lt;br /&gt;
;''Reflection in Java''&lt;br /&gt;
:Java uses a '''Reflection API''' to inspect and manipulate itself.  It includes the expanded ''class'' class in java.lang and the java.lang.reflect package, which represents the members of a class with ''Method'', ''Constructor'' and ''Field'' objects.Reflection can be used to obtain information about a class and its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
*This example uses the &amp;lt;tt&amp;gt;Class.forName()&amp;lt;/tt&amp;gt; method to dynamically load the named class, and then calls various methods of the ''class'' object to look up the Superclass, Interfaces, and Members of the class. The example uses ''Constructor'', ''Field'' and ''Method'' objects to obtain information about each member of the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
  /** The main method.  Print info about the named class */&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Display the modifiers, name, superclass and interfaces of a class&lt;br /&gt;
   *  or interface. Then go and list all constructors, fields, and methods. */&lt;br /&gt;
  public static void print_class(Class c)&lt;br /&gt;
  {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot;+c.getName());&lt;br /&gt;
    }&lt;br /&gt;
    else if (c.getSuperclass() != null)&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName() +&lt;br /&gt;
                       &amp;quot; extends &amp;quot; + c.getSuperclass().getName());&lt;br /&gt;
    else&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName());&lt;br /&gt;
        &lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface()) System.out.println(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for(int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(interfaces[i].getName());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;);            // Begin class member listing.&lt;br /&gt;
&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
    System.out.println(&amp;quot; // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for(int i = 0; i &amp;lt; constructors.length; i++)      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields();           // Look up fields.&lt;br /&gt;
    for(int i = 0; i &amp;lt; fields.length; i++)            // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods();        // Look up methods.&lt;br /&gt;
    for(int i = 0; i &amp;lt; methods.length; i++)           // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;);             // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while(t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    return t.getName() + brackets;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
  public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0) return &amp;quot;&amp;quot;;&lt;br /&gt;
    else return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, type, and name of a field */&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; +&lt;br /&gt;
                       modifiers(f.getModifiers()) +&lt;br /&gt;
                       typename(f.getType()) + &amp;quot; &amp;quot; + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, return type, name, parameter types and exception&lt;br /&gt;
   *  type of a method or constructor.  Note the use of the Member interface&lt;br /&gt;
   *  to allow this method to work with both Method and Constructor objects */&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype=null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) +&lt;br /&gt;
                     ((returntype!=null)? typename(returntype)+&amp;quot; &amp;quot; : &amp;quot;&amp;quot;) +&lt;br /&gt;
                     member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0) System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code is then compiled and run, with the following simple test class passed in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput {&lt;br /&gt;
    public static void main(String[] args) {}&lt;br /&gt;
    public int method1() {&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public String method2() {&lt;br /&gt;
        return &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    public void method3() {}&lt;br /&gt;
    public void method4(int num) {}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output generated is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput extends java.lang.Object {&lt;br /&gt;
 // Constructors&lt;br /&gt;
  public testInput();&lt;br /&gt;
 // Fields&lt;br /&gt;
 // Methods&lt;br /&gt;
  public int method1();&lt;br /&gt;
  public java.lang.String method2();&lt;br /&gt;
  public void method3();&lt;br /&gt;
  public void method4(int);&lt;br /&gt;
  public static void main(java.lang.String[]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection Sequences ===&lt;br /&gt;
The Java code above, which displays constructors, methods, and other information pertinent to the ''testInput'' class, is somewhat convoluted and difficult to trace.  In Ruby, the methods below can be called on a module/class to return similar information:&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.private_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.protected_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.public_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.singleton_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
For example, using the &amp;lt;tt&amp;gt;Demo&amp;lt;/tt&amp;gt; class below, all of the class' methods (private, protected, public, and singleton) can be easily displayed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; class Demo&lt;br /&gt;
irb(main):002:1&amp;gt;   public&lt;br /&gt;
irb(main):003:1&amp;gt;     def method1&lt;br /&gt;
irb(main):004:2&amp;gt;   end&lt;br /&gt;
irb(main):005:1&amp;gt;   private&lt;br /&gt;
irb(main):006:1&amp;gt;     def method2&lt;br /&gt;
irb(main):007:2&amp;gt;   end&lt;br /&gt;
irb(main):008:1&amp;gt;   public&lt;br /&gt;
irb(main):009:1&amp;gt;     def method3&lt;br /&gt;
irb(main):010:2&amp;gt;   end&lt;br /&gt;
irb(main):011:1&amp;gt;   protected&lt;br /&gt;
irb(main):012:1&amp;gt;     def method4&lt;br /&gt;
irb(main):013:2&amp;gt;   end&lt;br /&gt;
irb(main):014:1&amp;gt;   def Demo.method5&lt;br /&gt;
irb(main):015:2&amp;gt;   end&lt;br /&gt;
irb(main):016:1&amp;gt;   public&lt;br /&gt;
irb(main):017:1&amp;gt;     def method6&lt;br /&gt;
irb(main):018:2&amp;gt;   end&lt;br /&gt;
irb(main):019:1&amp;gt; end&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):020:0&amp;gt; Demo.private_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method2&amp;quot;]&lt;br /&gt;
irb(main):021:0&amp;gt; Demo.protected_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method4&amp;quot;]&lt;br /&gt;
irb(main):022:0&amp;gt; Demo.public_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method3&amp;quot;, &amp;quot;method6&amp;quot;, &amp;quot;method1&amp;quot;]&lt;br /&gt;
irb(main):023:0&amp;gt; Demo.singleton_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method5&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to looking inside classes, reflection in Ruby provides the ability to dynamically call methods.  An arbitrary method of some object can be called using &amp;lt;tt&amp;gt;Object#send&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;Method#call&amp;lt;/tt&amp;gt;, and also various versions of the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method (including &amp;lt;tt&amp;gt;class_eval&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;module_eval&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;instance_eval&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;send&amp;lt;/tt&amp;gt; works on any object, and it calls the passed in method on that object.  It will also find new methods added at runtime.  In the below example, the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; method is called on the object, which is a 14 character string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):105:0&amp;gt; &amp;quot;This is a test&amp;quot;.send(:length)&lt;br /&gt;
=&amp;gt; 14&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; objects represent a chunk of code and a context in which it executes.  The &amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; object is created, and it can be executed whenever desired by sending it the &amp;lt;tt&amp;gt;call&amp;lt;/tt&amp;gt; message:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):106:0&amp;gt; test_message = &amp;quot;Test message&amp;quot;.method(:length)&lt;br /&gt;
=&amp;gt; #&amp;lt;Method: String#length&amp;gt;&lt;br /&gt;
irb(main):107:0&amp;gt; test_message.call&lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A third way methods can be invoked dynamically using reflection is with the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method.  &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; provides the ability to parse and execute an arbitrary string of legal Ruby source code.  The string of code can be stored as an object, and then evaluated at a later time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):111:0&amp;gt; eval_test = %q{&amp;quot;A third test&amp;quot;.length}&lt;br /&gt;
=&amp;gt; &amp;quot;\&amp;quot;A third test\&amp;quot;.length&amp;quot;&lt;br /&gt;
irb(main):112:0&amp;gt; eval eval_test  &lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection vs. Java Reflection ===&lt;br /&gt;
&lt;br /&gt;
*By coding and comparing we find that Reflection in Java tends to be more verbose than Ruby code which accomplishes the same task.  Also, exceptions such as &amp;lt;tt&amp;gt;NoSuchMethodError&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;InvocationTargetException&amp;lt;/tt&amp;gt; tend to create further complications and confusion when using Java's reflection API.&lt;br /&gt;
**In order to print out all the methods contained in a class in Java, we approached 100 lines of code.  It took merely 4 lines of code in Ruby to print all private, protected, public, and singleton methods of a class.&lt;br /&gt;
&lt;br /&gt;
*In Ruby, Aspect Oriented Programming(AOP) can be performed using ''aliasing'', ''reflection'', and ''metaprogramming''.  These language facilities make it much easier to perform AOP than in Java.&lt;br /&gt;
&lt;br /&gt;
=== Uses of Reflection ===&lt;br /&gt;
Reflection 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 and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
*Extensibility Features &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;
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;
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;
*Reflection API in java is also used in Database Access with SQL.&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;
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;
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;
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;
=== ''Refrences'' ===&lt;br /&gt;
#Flanagan, David.  [http://books.google.com/books?id=kADTIsgeLh4C Java Examples in a Nutshell].  O'Reilly, 2004.&lt;br /&gt;
#Thomas, Dave.  [http://books.google.com/books?id=8LXOAAAACAAJ Programming Ruby].  Raleigh: Pragmatic Bookshelf, 2004.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
*http://phrogz.net/programmingruby/ospace.html&lt;br /&gt;
*http://java.sun.com/docs/books/tutorial/reflect/&lt;br /&gt;
*http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12394</id>
		<title>CSC/ECE 517 Summer 2008/wiki1 3 aobk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12394"/>
		<updated>2008-06-06T01:37:37Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Ruby Reflection Sequences */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection in Ruby and Java ==&lt;br /&gt;
;What is ''Reflection''?&lt;br /&gt;
Reflection, in the realm of Computer Science, is the process of introspection by a program.  Introspection allows program entities to discover things about themselves.  Through the technique of ''metaprogramming'', aspects of a program are examined from within the program itself, which enables the program to modify itself at runtime.  This provides for a very liberated and dynamic programming environment.&lt;br /&gt;
&lt;br /&gt;
By looking at and inside classes and objects, much can be discovered, including the following:&lt;br /&gt;
* what objects are contained in a program&lt;br /&gt;
* the class hierarchy&lt;br /&gt;
* the attributes and methods of the objects contained in the program&lt;br /&gt;
* various method information&lt;br /&gt;
&lt;br /&gt;
One of the design challenges when using Reflection is that you need to clarify what services you wish to provide at runtime, i.e. ones that you don't want to implement statically. The provision of reusable services that vary based upon the runtime environment is one of the reasons that frameworks such as Struts and JAAS are created.  However, even small applications can face design issues that make a dynamic solution preferable over a static one. A word of caution, though, over-reliance on Reflection can lead to performance issues due to the extra overhead when determining what class(es) need to be loaded.&lt;br /&gt;
&lt;br /&gt;
=== Java Reflection Sequences ===&lt;br /&gt;
;''Reflection in Java''&lt;br /&gt;
:Java uses a '''Reflection API''' to inspect and manipulate itself.  It includes the expanded ''class'' class in java.lang and the java.lang.reflect package, which represents the members of a class with ''Method'', ''Constructor'' and ''Field'' objects.Reflection can be used to obtain information about a class and its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
*This example uses the &amp;lt;tt&amp;gt;Class.forName()&amp;lt;/tt&amp;gt; method to dynamically load the named class, and then calls various methods of the ''class'' object to look up the Superclass, Interfaces, and Members of the class. The example uses ''Constructor'', ''Field'' and ''Method'' objects to obtain information about each member of the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
  /** The main method.  Print info about the named class */&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Display the modifiers, name, superclass and interfaces of a class&lt;br /&gt;
   *  or interface. Then go and list all constructors, fields, and methods. */&lt;br /&gt;
  public static void print_class(Class c)&lt;br /&gt;
  {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot;+c.getName());&lt;br /&gt;
    }&lt;br /&gt;
    else if (c.getSuperclass() != null)&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName() +&lt;br /&gt;
                       &amp;quot; extends &amp;quot; + c.getSuperclass().getName());&lt;br /&gt;
    else&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName());&lt;br /&gt;
        &lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface()) System.out.println(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for(int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(interfaces[i].getName());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;);            // Begin class member listing.&lt;br /&gt;
&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
    System.out.println(&amp;quot; // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for(int i = 0; i &amp;lt; constructors.length; i++)      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields();           // Look up fields.&lt;br /&gt;
    for(int i = 0; i &amp;lt; fields.length; i++)            // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods();        // Look up methods.&lt;br /&gt;
    for(int i = 0; i &amp;lt; methods.length; i++)           // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;);             // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while(t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    return t.getName() + brackets;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
  public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0) return &amp;quot;&amp;quot;;&lt;br /&gt;
    else return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, type, and name of a field */&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; +&lt;br /&gt;
                       modifiers(f.getModifiers()) +&lt;br /&gt;
                       typename(f.getType()) + &amp;quot; &amp;quot; + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, return type, name, parameter types and exception&lt;br /&gt;
   *  type of a method or constructor.  Note the use of the Member interface&lt;br /&gt;
   *  to allow this method to work with both Method and Constructor objects */&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype=null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) +&lt;br /&gt;
                     ((returntype!=null)? typename(returntype)+&amp;quot; &amp;quot; : &amp;quot;&amp;quot;) +&lt;br /&gt;
                     member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0) System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code is then compiled and run, with the following simple test class passed in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput {&lt;br /&gt;
    public static void main(String[] args) {}&lt;br /&gt;
    public int method1() {&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public String method2() {&lt;br /&gt;
        return &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    public void method3() {}&lt;br /&gt;
    public void method4(int num) {}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output generated is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput extends java.lang.Object {&lt;br /&gt;
 // Constructors&lt;br /&gt;
  public testInput();&lt;br /&gt;
 // Fields&lt;br /&gt;
 // Methods&lt;br /&gt;
  public int method1();&lt;br /&gt;
  public java.lang.String method2();&lt;br /&gt;
  public void method3();&lt;br /&gt;
  public void method4(int);&lt;br /&gt;
  public static void main(java.lang.String[]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection Sequences ===&lt;br /&gt;
The Java code above, which displays constructors, methods, and other information pertinent to the ''testInput'' class, is somewhat convoluted and difficult to trace.  In Ruby, the methods below can be called on a module/class to return similar information:&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.private_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.protected_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.public_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.singleton_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
For example, using the &amp;lt;tt&amp;gt;Demo&amp;lt;/tt&amp;gt; class below, all of the class' methods (private, protected, public, and singleton) can be easily displayed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; class Demo&lt;br /&gt;
irb(main):002:1&amp;gt;   public&lt;br /&gt;
irb(main):003:1&amp;gt;     def method1&lt;br /&gt;
irb(main):004:2&amp;gt;   end&lt;br /&gt;
irb(main):005:1&amp;gt;   private&lt;br /&gt;
irb(main):006:1&amp;gt;     def method2&lt;br /&gt;
irb(main):007:2&amp;gt;   end&lt;br /&gt;
irb(main):008:1&amp;gt;   public&lt;br /&gt;
irb(main):009:1&amp;gt;     def method3&lt;br /&gt;
irb(main):010:2&amp;gt;   end&lt;br /&gt;
irb(main):011:1&amp;gt;   protected&lt;br /&gt;
irb(main):012:1&amp;gt;     def method4&lt;br /&gt;
irb(main):013:2&amp;gt;   end&lt;br /&gt;
irb(main):014:1&amp;gt;   def Demo.method5&lt;br /&gt;
irb(main):015:2&amp;gt;   end&lt;br /&gt;
irb(main):016:1&amp;gt;   public&lt;br /&gt;
irb(main):017:1&amp;gt;     def method6&lt;br /&gt;
irb(main):018:2&amp;gt;   end&lt;br /&gt;
irb(main):019:1&amp;gt; end&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):020:0&amp;gt; Demo.private_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method2&amp;quot;]&lt;br /&gt;
irb(main):021:0&amp;gt; Demo.protected_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method4&amp;quot;]&lt;br /&gt;
irb(main):022:0&amp;gt; Demo.public_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method3&amp;quot;, &amp;quot;method6&amp;quot;, &amp;quot;method1&amp;quot;]&lt;br /&gt;
irb(main):023:0&amp;gt; Demo.singleton_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method5&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to looking inside classes, reflection in Ruby provides the ability to dynamically call methods.  An arbitrary method of some object can be called using &amp;lt;tt&amp;gt;Object#send&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;Method#call&amp;lt;/tt&amp;gt;, and also various versions of the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method (including &amp;lt;tt&amp;gt;class_eval&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;module_eval&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;instance_eval&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;send&amp;lt;/tt&amp;gt; works on any object, and it calls the passed in method on that object.  It will also find new methods added at runtime.  In the below example, the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; method is called on the object, which is a 14 character string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):105:0&amp;gt; &amp;quot;This is a test&amp;quot;.send(:length)&lt;br /&gt;
=&amp;gt; 14&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; objects represent a chunk of code and a context in which it executes.  The &amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; object is created, and it can be executed whenever desired by sending it the &amp;lt;tt&amp;gt;call&amp;lt;/tt&amp;gt; message:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):106:0&amp;gt; test_message = &amp;quot;Test message&amp;quot;.method(:length)&lt;br /&gt;
=&amp;gt; #&amp;lt;Method: String#length&amp;gt;&lt;br /&gt;
irb(main):107:0&amp;gt; test_message.call&lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
A third way methods can be invoked dynamically using reflection is with the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method.  &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; provides the ability to parse and execute an arbitrary string of legal Ruby source code.  The string of code can be stored as an object, and then evaluated at a later time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):111:0&amp;gt; eval_test = %q{&amp;quot;A third test&amp;quot;.length}&lt;br /&gt;
=&amp;gt; &amp;quot;\&amp;quot;A third test\&amp;quot;.length&amp;quot;&lt;br /&gt;
irb(main):112:0&amp;gt; eval eval_test  &lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection vs. Java Reflection ===&lt;br /&gt;
&lt;br /&gt;
*By coding and comparing we find that Reflection in Java tends to be more verbose than Ruby code which accomplishes the same task.  Also, exceptions such as &amp;lt;tt&amp;gt;NoSuchMethodError&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;InvocationTargetException&amp;lt;/tt&amp;gt; tend to create further complications and confusion when using Java's reflection API.&lt;br /&gt;
&lt;br /&gt;
*In Ruby, Aspect Oriented Programming(AOP) can be performed using ''aliasing, reflection and metaprogramming''.  These language facilities make it much easier to perform AOP than in Java.&lt;br /&gt;
&lt;br /&gt;
=== Uses of Reflection ===&lt;br /&gt;
Reflection 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 and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
*Extensibility Features &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;
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;
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;
*Reflection API in java is also used in Database Access with SQL.&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;
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;
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;
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;
=== ''Refrences'' ===&lt;br /&gt;
#Flanagan, David.  [http://books.google.com/books?id=kADTIsgeLh4C Java Examples in a Nutshell].  O'Reilly, 2004.&lt;br /&gt;
#Thomas, Dave.  [http://books.google.com/books?id=8LXOAAAACAAJ Programming Ruby].  Raleigh: Pragmatic Bookshelf, 2004.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
*http://phrogz.net/programmingruby/ospace.html&lt;br /&gt;
*http://java.sun.com/docs/books/tutorial/reflect/&lt;br /&gt;
*http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12389</id>
		<title>CSC/ECE 517 Summer 2008/wiki1 3 aobk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12389"/>
		<updated>2008-06-06T01:25:40Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Ruby Reflection Sequences */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection in Ruby and Java ==&lt;br /&gt;
;What is ''Reflection''?&lt;br /&gt;
Reflection, in the realm of Computer Science, is the process of introspection by a program.  Introspection allows program entities to discover things about themselves.  Through the technique of ''metaprogramming'', aspects of a program are examined from within the program itself, which enables the program to modify itself at runtime.  This provides for a very liberated and dynamic programming environment.&lt;br /&gt;
&lt;br /&gt;
By looking at and inside classes and objects, much can be discovered, including the following:&lt;br /&gt;
* what objects are contained in a program&lt;br /&gt;
* the class hierarchy&lt;br /&gt;
* the attributes and methods of the objects contained in the program&lt;br /&gt;
* various method information&lt;br /&gt;
&lt;br /&gt;
One of the design challenges when using Reflection is that you need to clarify what services you wish to provide at runtime, i.e. ones that you don't want to implement statically. The provision of reusable services that vary based upon the runtime environment is one of the reasons that frameworks such as Struts and JAAS are created.  However, even small applications can face design issues that make a dynamic solution preferable over a static one. A word of caution, though, over-reliance on Reflection can lead to performance issues due to the extra overhead when determining what class(es) need to be loaded.&lt;br /&gt;
&lt;br /&gt;
=== Java Reflection Sequences ===&lt;br /&gt;
;''Reflection in Java''&lt;br /&gt;
:Java uses a '''Reflection API''' to inspect and manipulate itself.  It includes the expanded ''class'' class in java.lang and the java.lang.reflect package, which represents the members of a class with ''Method'', ''Constructor'' and ''Field'' objects.Reflection can be used to obtain information about a class and its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
*This example uses the &amp;lt;tt&amp;gt;Class.forName()&amp;lt;/tt&amp;gt; method to dynamically load the named class, and then calls various methods of the ''class'' object to look up the Superclass, Interfaces, and Members of the class. The example uses ''Constructor'', ''Field'' and ''Method'' objects to obtain information about each member of the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
  /** The main method.  Print info about the named class */&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Display the modifiers, name, superclass and interfaces of a class&lt;br /&gt;
   *  or interface. Then go and list all constructors, fields, and methods. */&lt;br /&gt;
  public static void print_class(Class c)&lt;br /&gt;
  {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot;+c.getName());&lt;br /&gt;
    }&lt;br /&gt;
    else if (c.getSuperclass() != null)&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName() +&lt;br /&gt;
                       &amp;quot; extends &amp;quot; + c.getSuperclass().getName());&lt;br /&gt;
    else&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName());&lt;br /&gt;
        &lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface()) System.out.println(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for(int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(interfaces[i].getName());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;);            // Begin class member listing.&lt;br /&gt;
&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
    System.out.println(&amp;quot; // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for(int i = 0; i &amp;lt; constructors.length; i++)      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields();           // Look up fields.&lt;br /&gt;
    for(int i = 0; i &amp;lt; fields.length; i++)            // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods();        // Look up methods.&lt;br /&gt;
    for(int i = 0; i &amp;lt; methods.length; i++)           // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;);             // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while(t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    return t.getName() + brackets;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
  public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0) return &amp;quot;&amp;quot;;&lt;br /&gt;
    else return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, type, and name of a field */&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; +&lt;br /&gt;
                       modifiers(f.getModifiers()) +&lt;br /&gt;
                       typename(f.getType()) + &amp;quot; &amp;quot; + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, return type, name, parameter types and exception&lt;br /&gt;
   *  type of a method or constructor.  Note the use of the Member interface&lt;br /&gt;
   *  to allow this method to work with both Method and Constructor objects */&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype=null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) +&lt;br /&gt;
                     ((returntype!=null)? typename(returntype)+&amp;quot; &amp;quot; : &amp;quot;&amp;quot;) +&lt;br /&gt;
                     member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0) System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code is then compiled and run, with the following simple test class passed in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput {&lt;br /&gt;
    public static void main(String[] args) {}&lt;br /&gt;
    public int method1() {&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public String method2() {&lt;br /&gt;
        return &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    public void method3() {}&lt;br /&gt;
    public void method4(int num) {}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output generated is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput extends java.lang.Object {&lt;br /&gt;
 // Constructors&lt;br /&gt;
  public testInput();&lt;br /&gt;
 // Fields&lt;br /&gt;
 // Methods&lt;br /&gt;
  public int method1();&lt;br /&gt;
  public java.lang.String method2();&lt;br /&gt;
  public void method3();&lt;br /&gt;
  public void method4(int);&lt;br /&gt;
  public static void main(java.lang.String[]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection Sequences ===&lt;br /&gt;
The Java code above, which displays constructors, methods, and other information pertinent to the ''testInput'' class, is somewhat convoluted and difficult to trace.  In Ruby, the methods below can be called on a module/class to return similar information:&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.private_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.protected_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.public_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.singleton_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
For example, using the &amp;lt;tt&amp;gt;Demo&amp;lt;/tt&amp;gt; class below, all of the class' methods (private, protected, public, and singleton) can be easily displayed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; class Demo&lt;br /&gt;
irb(main):002:1&amp;gt;   public&lt;br /&gt;
irb(main):003:1&amp;gt;     def method1&lt;br /&gt;
irb(main):004:2&amp;gt;   end&lt;br /&gt;
irb(main):005:1&amp;gt;   private&lt;br /&gt;
irb(main):006:1&amp;gt;     def method2&lt;br /&gt;
irb(main):007:2&amp;gt;   end&lt;br /&gt;
irb(main):008:1&amp;gt;   public&lt;br /&gt;
irb(main):009:1&amp;gt;     def method3&lt;br /&gt;
irb(main):010:2&amp;gt;   end&lt;br /&gt;
irb(main):011:1&amp;gt;   protected&lt;br /&gt;
irb(main):012:1&amp;gt;     def method4&lt;br /&gt;
irb(main):013:2&amp;gt;   end&lt;br /&gt;
irb(main):014:1&amp;gt;   def Demo.method5&lt;br /&gt;
irb(main):015:2&amp;gt;   end&lt;br /&gt;
irb(main):016:1&amp;gt;   public&lt;br /&gt;
irb(main):017:1&amp;gt;     def method6&lt;br /&gt;
irb(main):018:2&amp;gt;   end&lt;br /&gt;
irb(main):019:1&amp;gt; end&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):020:0&amp;gt; Demo.private_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method2&amp;quot;]&lt;br /&gt;
irb(main):021:0&amp;gt; Demo.protected_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method4&amp;quot;]&lt;br /&gt;
irb(main):022:0&amp;gt; Demo.public_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method3&amp;quot;, &amp;quot;method6&amp;quot;, &amp;quot;method1&amp;quot;]&lt;br /&gt;
irb(main):023:0&amp;gt; Demo.singleton_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method5&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In addition to looking inside classes, reflection in Ruby provides the ability to dynamically call methods.  An arbitrary method of some object can be called using &amp;lt;tt&amp;gt;Object#send&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;Method#call&amp;lt;/tt&amp;gt;, and also various versions of the &amp;lt;tt&amp;gt;eval&amp;lt;/tt&amp;gt; method (including &amp;lt;tt&amp;gt;class_eval&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;module_eval&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;instance_eval&amp;lt;/tt&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;send&amp;lt;/tt&amp;gt; works on any object, and it calls the passed in method on that object.  It will also find new methods added at runtime.  In the below example, the &amp;lt;tt&amp;gt;length&amp;lt;/tt&amp;gt; method is called on the object, which is a 14 character string.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):105:0&amp;gt; &amp;quot;This is a test&amp;quot;.send(:length)&lt;br /&gt;
=&amp;gt; 14&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; objects represent a chunk of code and a context in which it executes.  The &amp;lt;tt&amp;gt;Method&amp;lt;/tt&amp;gt; object is created, and it can be executed whenever desired by sending it the &amp;lt;tt&amp;gt;call&amp;lt;/tt&amp;gt; message:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):106:0&amp;gt; test_message = &amp;quot;Test message&amp;quot;.method(:length)&lt;br /&gt;
=&amp;gt; #&amp;lt;Method: String#length&amp;gt;&lt;br /&gt;
irb(main):107:0&amp;gt; test_message.call&lt;br /&gt;
=&amp;gt; 12&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection vs. Java Reflection ===&lt;br /&gt;
&lt;br /&gt;
*By coding and comparing we find that Reflection in Java tends to be more verbose than Ruby code which accomplishes the same task.  Also, exceptions such as &amp;lt;tt&amp;gt;NoSuchMethodError&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;InvocationTargetException&amp;lt;/tt&amp;gt; tend to create further complications and confusion when using Java's reflection API.&lt;br /&gt;
&lt;br /&gt;
*In Ruby, Aspect Oriented Programming(AOP) can be performed using ''aliasing, reflection and metaprogramming''.  These language facilities make it much easier to perform AOP than in Java.&lt;br /&gt;
&lt;br /&gt;
=== Uses of Reflection ===&lt;br /&gt;
Reflection 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 and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
*Extensibility Features &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;
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;
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;
*Reflection API in java is also used in Database Access with SQL.&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;
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;
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;
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;
=== ''Refrences'' ===&lt;br /&gt;
#Flanagan, David.  [http://books.google.com/books?id=kADTIsgeLh4C Java Examples in a Nutshell].  O'Reilly, 2004.&lt;br /&gt;
#Thomas, Dave.  [http://books.google.com/books?id=8LXOAAAACAAJ Programming Ruby].  Raleigh: Pragmatic Bookshelf, 2004.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
*http://phrogz.net/programmingruby/ospace.html&lt;br /&gt;
*http://java.sun.com/docs/books/tutorial/reflect/&lt;br /&gt;
*http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12378</id>
		<title>CSC/ECE 517 Summer 2008/wiki1 3 aobk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12378"/>
		<updated>2008-06-06T00:56:22Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Reflection in Ruby and Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection in Ruby and Java ==&lt;br /&gt;
;What is ''Reflection''?&lt;br /&gt;
Reflection, in the realm of Computer Science, is the process of introspection by a program.  Introspection allows program entities to discover things about themselves.  Through the technique of ''metaprogramming'', aspects of a program are examined from within the program itself, which enables the program to modify itself at runtime.  This provides for a very liberated and dynamic programming environment.&lt;br /&gt;
&lt;br /&gt;
By looking at and inside classes and objects, much can be discovered, including the following:&lt;br /&gt;
* what objects are contained in a program&lt;br /&gt;
* the class hierarchy&lt;br /&gt;
* the attributes and methods of the objects contained in the program&lt;br /&gt;
* various method information&lt;br /&gt;
&lt;br /&gt;
One of the design challenges when using Reflection is that you need to clarify what services you wish to provide at runtime, i.e. ones that you don't want to implement statically. The provision of reusable services that vary based upon the runtime environment is one of the reasons that frameworks such as Struts and JAAS are created.  However, even small applications can face design issues that make a dynamic solution preferable over a static one. A word of caution, though, over-reliance on Reflection can lead to performance issues due to the extra overhead when determining what class(es) need to be loaded.&lt;br /&gt;
&lt;br /&gt;
=== Java Reflection Sequences ===&lt;br /&gt;
;''Reflection in Java''&lt;br /&gt;
:Java uses a '''Reflection API''' to inspect and manipulate itself.  It includes the expanded ''class'' class in java.lang and the java.lang.reflect package, which represents the members of a class with ''Method'', ''Constructor'' and ''Field'' objects.Reflection can be used to obtain information about a class and its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
*This example uses the &amp;lt;tt&amp;gt;Class.forName()&amp;lt;/tt&amp;gt; method to dynamically load the named class, and then calls various methods of the ''class'' object to look up the Superclass, Interfaces, and Members of the class. The example uses ''Constructor'', ''Field'' and ''Method'' objects to obtain information about each member of the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
  /** The main method.  Print info about the named class */&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Display the modifiers, name, superclass and interfaces of a class&lt;br /&gt;
   *  or interface. Then go and list all constructors, fields, and methods. */&lt;br /&gt;
  public static void print_class(Class c)&lt;br /&gt;
  {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot;+c.getName());&lt;br /&gt;
    }&lt;br /&gt;
    else if (c.getSuperclass() != null)&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName() +&lt;br /&gt;
                       &amp;quot; extends &amp;quot; + c.getSuperclass().getName());&lt;br /&gt;
    else&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName());&lt;br /&gt;
        &lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface()) System.out.println(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for(int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(interfaces[i].getName());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;);            // Begin class member listing.&lt;br /&gt;
&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
    System.out.println(&amp;quot; // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for(int i = 0; i &amp;lt; constructors.length; i++)      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields();           // Look up fields.&lt;br /&gt;
    for(int i = 0; i &amp;lt; fields.length; i++)            // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods();        // Look up methods.&lt;br /&gt;
    for(int i = 0; i &amp;lt; methods.length; i++)           // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;);             // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while(t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    return t.getName() + brackets;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
  public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0) return &amp;quot;&amp;quot;;&lt;br /&gt;
    else return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, type, and name of a field */&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; +&lt;br /&gt;
                       modifiers(f.getModifiers()) +&lt;br /&gt;
                       typename(f.getType()) + &amp;quot; &amp;quot; + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, return type, name, parameter types and exception&lt;br /&gt;
   *  type of a method or constructor.  Note the use of the Member interface&lt;br /&gt;
   *  to allow this method to work with both Method and Constructor objects */&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype=null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) +&lt;br /&gt;
                     ((returntype!=null)? typename(returntype)+&amp;quot; &amp;quot; : &amp;quot;&amp;quot;) +&lt;br /&gt;
                     member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0) System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code is then compiled and run, with the following simple test class passed in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput {&lt;br /&gt;
    public static void main(String[] args) {}&lt;br /&gt;
    public int method1() {&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public String method2() {&lt;br /&gt;
        return &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    public void method3() {}&lt;br /&gt;
    public void method4(int num) {}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output generated is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput extends java.lang.Object {&lt;br /&gt;
 // Constructors&lt;br /&gt;
  public testInput();&lt;br /&gt;
 // Fields&lt;br /&gt;
 // Methods&lt;br /&gt;
  public int method1();&lt;br /&gt;
  public java.lang.String method2();&lt;br /&gt;
  public void method3();&lt;br /&gt;
  public void method4(int);&lt;br /&gt;
  public static void main(java.lang.String[]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection Sequences ===&lt;br /&gt;
The Java code above, which displays constructors, methods, and other information pertinent to the ''testInput'' class, is somewhat convoluted and difficult to trace.  In Ruby, the methods below can be called on a module/class to return similar information:&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.private_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.protected_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.public_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.singleton_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
For example, using the &amp;lt;tt&amp;gt;Demo&amp;lt;/tt&amp;gt; class below, all of the class' methods (private, protected, public, and singleton) can be easily displayed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; class Demo&lt;br /&gt;
irb(main):002:1&amp;gt;   public&lt;br /&gt;
irb(main):003:1&amp;gt;     def method1&lt;br /&gt;
irb(main):004:2&amp;gt;   end&lt;br /&gt;
irb(main):005:1&amp;gt;   private&lt;br /&gt;
irb(main):006:1&amp;gt;     def method2&lt;br /&gt;
irb(main):007:2&amp;gt;   end&lt;br /&gt;
irb(main):008:1&amp;gt;   public&lt;br /&gt;
irb(main):009:1&amp;gt;     def method3&lt;br /&gt;
irb(main):010:2&amp;gt;   end&lt;br /&gt;
irb(main):011:1&amp;gt;   protected&lt;br /&gt;
irb(main):012:1&amp;gt;     def method4&lt;br /&gt;
irb(main):013:2&amp;gt;   end&lt;br /&gt;
irb(main):014:1&amp;gt;   def Demo.method5&lt;br /&gt;
irb(main):015:2&amp;gt;   end&lt;br /&gt;
irb(main):016:1&amp;gt;   public&lt;br /&gt;
irb(main):017:1&amp;gt;     def method6&lt;br /&gt;
irb(main):018:2&amp;gt;   end&lt;br /&gt;
irb(main):019:1&amp;gt; end&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):020:0&amp;gt; Demo.private_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method2&amp;quot;]&lt;br /&gt;
irb(main):021:0&amp;gt; Demo.protected_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method4&amp;quot;]&lt;br /&gt;
irb(main):022:0&amp;gt; Demo.public_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method3&amp;quot;, &amp;quot;method6&amp;quot;, &amp;quot;method1&amp;quot;]&lt;br /&gt;
irb(main):023:0&amp;gt; Demo.singleton_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method5&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection vs. Java Reflection ===&lt;br /&gt;
&lt;br /&gt;
*By coding and comparing we find that Reflection in Java tends to be more verbose than Ruby code which accomplishes the same task.  Also, exceptions such as &amp;lt;tt&amp;gt;NoSuchMethodError&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;InvocationTargetException&amp;lt;/tt&amp;gt; tend to create further complications and confusion when using Java's reflection API.&lt;br /&gt;
&lt;br /&gt;
*In Ruby, Aspect Oriented Programming(AOP) can be performed using ''aliasing, reflection and metaprogramming''.  These language facilities make it much easier to perform AOP than in Java.&lt;br /&gt;
&lt;br /&gt;
=== Uses of Reflection ===&lt;br /&gt;
Reflection 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 and can enable applications to perform operations which would otherwise be impossible.&lt;br /&gt;
&lt;br /&gt;
*Extensibility Features &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;
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;
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;
*Reflection API in java is also used in Database Access with SQL.&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;
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;
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;
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;
=== ''Refrences'' ===&lt;br /&gt;
#Flanagan, David.  [http://books.google.com/books?id=kADTIsgeLh4C Java Examples in a Nutshell].  O'Reilly, 2004.&lt;br /&gt;
#Thomas, Dave.  [http://books.google.com/books?id=8LXOAAAACAAJ Programming Ruby].  Raleigh: Pragmatic Bookshelf, 2004.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
*http://phrogz.net/programmingruby/ospace.html&lt;br /&gt;
*http://java.sun.com/docs/books/tutorial/reflect/&lt;br /&gt;
*http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12375</id>
		<title>CSC/ECE 517 Summer 2008/wiki1 3 aobk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12375"/>
		<updated>2008-06-06T00:43:34Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Reflection in Ruby and Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection in Ruby and Java ==&lt;br /&gt;
;What is ''Reflection''?&lt;br /&gt;
Reflection, in the realm of Computer Science, is the process of introspection by a program.  Introspection allows program entities to discover things about themselves.  Through the technique of ''metaprogramming'', aspects of a program are examined from within the program itself, which enables the program to modify itself at runtime.  This provides for a very liberated and dynamic programming environment.&lt;br /&gt;
&lt;br /&gt;
By looking at and inside classes and objects, much can be discovered, including the following:&lt;br /&gt;
* what objects are contained in a program&lt;br /&gt;
* the class hierarchy&lt;br /&gt;
* the attributes and methods of the objects contained in the program&lt;br /&gt;
* various method information&lt;br /&gt;
&lt;br /&gt;
One of the design challenges when using Reflection is that you need to clarify what services you wish to provide at runtime, i.e. ones that you don't want to implement statically. The provision of reusable services that vary based upon the runtime environment is one of the reasons that frameworks such as Struts and JAAS are created.  However, even small applications can face design issues that make a dynamic solution preferable over a static one. A word of caution, though, over-reliance on Reflection can lead to performance issues because of the extra overhead of determining what class(es) need to be loaded.&lt;br /&gt;
&lt;br /&gt;
=== Java Reflection Sequences ===&lt;br /&gt;
;''Reflection in Java''&lt;br /&gt;
:Java uses a '''Reflection API''' to inspect and manipulate itself.  It includes the expanded ''class'' class in java.lang and the java.lang.reflect package, which represents the members of a class with ''Method'', ''Constructor'' and ''Field'' objects.Reflection can be used to obtain information about a class and its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
*This example uses the &amp;lt;tt&amp;gt;Class.forName()&amp;lt;/tt&amp;gt; method to dynamically load the named class, and then calls various methods of the ''class'' object to look up the Superclass, Interfaces, and Members of the class. The example uses ''Constructor'', ''Field'' and ''Method'' objects to obtain information about each member of the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
  /** The main method.  Print info about the named class */&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Display the modifiers, name, superclass and interfaces of a class&lt;br /&gt;
   *  or interface. Then go and list all constructors, fields, and methods. */&lt;br /&gt;
  public static void print_class(Class c)&lt;br /&gt;
  {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot;+c.getName());&lt;br /&gt;
    }&lt;br /&gt;
    else if (c.getSuperclass() != null)&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName() +&lt;br /&gt;
                       &amp;quot; extends &amp;quot; + c.getSuperclass().getName());&lt;br /&gt;
    else&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName());&lt;br /&gt;
        &lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface()) System.out.println(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for(int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(interfaces[i].getName());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;);            // Begin class member listing.&lt;br /&gt;
&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
    System.out.println(&amp;quot; // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for(int i = 0; i &amp;lt; constructors.length; i++)      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields();           // Look up fields.&lt;br /&gt;
    for(int i = 0; i &amp;lt; fields.length; i++)            // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods();        // Look up methods.&lt;br /&gt;
    for(int i = 0; i &amp;lt; methods.length; i++)           // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;);             // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while(t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    return t.getName() + brackets;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
  public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0) return &amp;quot;&amp;quot;;&lt;br /&gt;
    else return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, type, and name of a field */&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; +&lt;br /&gt;
                       modifiers(f.getModifiers()) +&lt;br /&gt;
                       typename(f.getType()) + &amp;quot; &amp;quot; + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, return type, name, parameter types and exception&lt;br /&gt;
   *  type of a method or constructor.  Note the use of the Member interface&lt;br /&gt;
   *  to allow this method to work with both Method and Constructor objects */&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype=null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) +&lt;br /&gt;
                     ((returntype!=null)? typename(returntype)+&amp;quot; &amp;quot; : &amp;quot;&amp;quot;) +&lt;br /&gt;
                     member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0) System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code is then compiled and run, with the following simple test class passed in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput {&lt;br /&gt;
    public static void main(String[] args) {}&lt;br /&gt;
    public int method1() {&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public String method2() {&lt;br /&gt;
        return &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    public void method3() {}&lt;br /&gt;
    public void method4(int num) {}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output generated is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput extends java.lang.Object {&lt;br /&gt;
 // Constructors&lt;br /&gt;
  public testInput();&lt;br /&gt;
 // Fields&lt;br /&gt;
 // Methods&lt;br /&gt;
  public int method1();&lt;br /&gt;
  public java.lang.String method2();&lt;br /&gt;
  public void method3();&lt;br /&gt;
  public void method4(int);&lt;br /&gt;
  public static void main(java.lang.String[]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection Sequences ===&lt;br /&gt;
The Java code above, which displays constructors, methods, and other information pertinent to the ''testInput'' class, is somewhat convoluted and difficult to trace.  In Ruby, the methods below can be called on a module/class to return similar information:&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.private_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.protected_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.public_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.singleton_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
For example, using the &amp;lt;tt&amp;gt;Demo&amp;lt;/tt&amp;gt; class below, all of the class' methods (private, protected, public, and singleton) can be easily displayed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; class Demo&lt;br /&gt;
irb(main):002:1&amp;gt;   public&lt;br /&gt;
irb(main):003:1&amp;gt;     def method1&lt;br /&gt;
irb(main):004:2&amp;gt;   end&lt;br /&gt;
irb(main):005:1&amp;gt;   private&lt;br /&gt;
irb(main):006:1&amp;gt;     def method2&lt;br /&gt;
irb(main):007:2&amp;gt;   end&lt;br /&gt;
irb(main):008:1&amp;gt;   public&lt;br /&gt;
irb(main):009:1&amp;gt;     def method3&lt;br /&gt;
irb(main):010:2&amp;gt;   end&lt;br /&gt;
irb(main):011:1&amp;gt;   protected&lt;br /&gt;
irb(main):012:1&amp;gt;     def method4&lt;br /&gt;
irb(main):013:2&amp;gt;   end&lt;br /&gt;
irb(main):014:1&amp;gt;   def Demo.method5&lt;br /&gt;
irb(main):015:2&amp;gt;   end&lt;br /&gt;
irb(main):016:1&amp;gt;   public&lt;br /&gt;
irb(main):017:1&amp;gt;     def method6&lt;br /&gt;
irb(main):018:2&amp;gt;   end&lt;br /&gt;
irb(main):019:1&amp;gt; end&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):020:0&amp;gt; Demo.private_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method2&amp;quot;]&lt;br /&gt;
irb(main):021:0&amp;gt; Demo.protected_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method4&amp;quot;]&lt;br /&gt;
irb(main):022:0&amp;gt; Demo.public_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method3&amp;quot;, &amp;quot;method6&amp;quot;, &amp;quot;method1&amp;quot;]&lt;br /&gt;
irb(main):023:0&amp;gt; Demo.singleton_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method5&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection vs. Java Reflection ===&lt;br /&gt;
&lt;br /&gt;
*By coding and comparing we find that Reflection in Java tends to be more verbose than Ruby code which accomplishes the same task.  Also, exceptions such as &amp;lt;tt&amp;gt;NoSuchMethodError&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;InvocationTargetException&amp;lt;/tt&amp;gt; tend to create further complications and confusion when using Java's reflection API.&lt;br /&gt;
&lt;br /&gt;
*Reflection API in java is also used in Database Access with SQL.&lt;br /&gt;
&lt;br /&gt;
*In Ruby, Aspect Oriented Programming(AOP) can be performed using ''aliasing, reflection and metaprogramming''.  These language facilities make it much easier to perform AOP than in Java.&lt;br /&gt;
&lt;br /&gt;
=== Uses of Reflection ===&lt;br /&gt;
Reflection 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 and can enable applications to perform operations which would otherwise be impossible. &lt;br /&gt;
&lt;br /&gt;
*Extensibility Features &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;
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;
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;
=== 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;
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;
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;
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;
=== ''Refrences'' ===&lt;br /&gt;
#Flanagan, David.  [http://books.google.com/books?id=kADTIsgeLh4C Java Examples in a Nutshell].  O'Reilly, 2004.&lt;br /&gt;
#Thomas, Dave.  [http://books.google.com/books?id=8LXOAAAACAAJ Programming Ruby].  Raleigh: Pragmatic Bookshelf, 2004.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links'' ===&lt;br /&gt;
*http://phrogz.net/programmingruby/ospace.html&lt;br /&gt;
*http://java.sun.com/docs/books/tutorial/reflect/&lt;br /&gt;
*http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12372</id>
		<title>CSC/ECE 517 Summer 2008/wiki1 3 aobk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Summer_2008/wiki1_3_aobk&amp;diff=12372"/>
		<updated>2008-06-06T00:30:11Z</updated>

		<summary type="html">&lt;p&gt;Amosti: /* Reflection in Ruby and Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Reflection in Ruby and Java ==&lt;br /&gt;
;What is ''Reflection''?&lt;br /&gt;
Reflection, in the realm of Computer Science, is the process of introspection by a program.  Introspection allows program entities to discover things about themselves.  Through the technique of ''metaprogramming'', aspects of a program are examined from within the program itself, which enables the program to modify itself at runtime.&lt;br /&gt;
&lt;br /&gt;
By looking at and inside classes and objects, much can be discovered, including the following:&lt;br /&gt;
* what objects are contained in a program&lt;br /&gt;
* the class hierarchy&lt;br /&gt;
* the attributes and methods of the objects contained in the program&lt;br /&gt;
* various method information&lt;br /&gt;
&lt;br /&gt;
One of the design challenges of using Reflection is that you need to clarify what services you wish to provide at runtime that don't want to implement statically. The provision of reusable services that vary based upon the runtime environment is one of the reasons that frameworks such as Struts and JAAS are created. However, even small applications can face design issues that make a dynamic solution preferable over a static one. A word of caution though, over-reliance on Reflection can lead to performance issues because of the extra overhead of determining what class(es) need to be loaded.&lt;br /&gt;
&lt;br /&gt;
=== Java Reflection Sequences ===&lt;br /&gt;
;''Reflection in Java''&lt;br /&gt;
:Java uses a '''Reflection API''' to inspect and manipulate itself.  It includes the expanded ''class'' class in java.lang and the java.lang.reflect package, which represents the members of a class with ''Method'', ''Constructor'' and ''Field'' objects.Reflection can be used to obtain information about a class and its members.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
*This example uses the &amp;lt;tt&amp;gt;Class.forName()&amp;lt;/tt&amp;gt; method to dynamically load the named class, and then calls various methods of the ''class'' object to look up the Superclass, Interfaces, and Members of the class. The example uses ''Constructor'', ''Field'' and ''Method'' objects to obtain information about each member of the class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.lang.reflect.*;&lt;br /&gt;
&lt;br /&gt;
/** A program that displays a class synopsis for the named class */&lt;br /&gt;
&lt;br /&gt;
public class ShowClass {&lt;br /&gt;
  /** The main method.  Print info about the named class */&lt;br /&gt;
  public static void main(String[] args) throws ClassNotFoundException {&lt;br /&gt;
    Class c = Class.forName(args[0]);&lt;br /&gt;
    print_class(c);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Display the modifiers, name, superclass and interfaces of a class&lt;br /&gt;
   *  or interface. Then go and list all constructors, fields, and methods. */&lt;br /&gt;
  public static void print_class(Class c)&lt;br /&gt;
  {&lt;br /&gt;
    // Print modifiers, type (class or interface), name and superclass.&lt;br /&gt;
    if (c.isInterface()) {&lt;br /&gt;
      // The modifiers will include the &amp;quot;interface&amp;quot; keyword here...&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; &amp;quot;+c.getName());&lt;br /&gt;
    }&lt;br /&gt;
    else if (c.getSuperclass() != null)&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName() +&lt;br /&gt;
                       &amp;quot; extends &amp;quot; + c.getSuperclass().getName());&lt;br /&gt;
    else&lt;br /&gt;
      System.out.print(Modifier.toString(c.getModifiers()) + &amp;quot; class &amp;quot; +&lt;br /&gt;
                       c.getName());&lt;br /&gt;
        &lt;br /&gt;
    // Print interfaces or super-interfaces of the class or interface.&lt;br /&gt;
    Class[] interfaces = c.getInterfaces();&lt;br /&gt;
    if ((interfaces != null) &amp;amp;&amp;amp; (interfaces.length &amp;gt; 0)) {&lt;br /&gt;
      if (c.isInterface()) System.out.println(&amp;quot; extends &amp;quot;);&lt;br /&gt;
      else System.out.print(&amp;quot; implements &amp;quot;);&lt;br /&gt;
      for(int i = 0; i &amp;lt; interfaces.length; i++) {&lt;br /&gt;
        if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
        System.out.print(interfaces[i].getName());&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; {&amp;quot;);            // Begin class member listing.&lt;br /&gt;
&lt;br /&gt;
    // Now look up and display the members of the class.&lt;br /&gt;
    System.out.println(&amp;quot; // Constructors&amp;quot;);&lt;br /&gt;
    Constructor[] constructors = c.getDeclaredConstructors();&lt;br /&gt;
    for(int i = 0; i &amp;lt; constructors.length; i++)      // Display constructors.&lt;br /&gt;
      print_method_or_constructor(constructors[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Fields&amp;quot;);&lt;br /&gt;
    Field[] fields = c.getDeclaredFields();           // Look up fields.&lt;br /&gt;
    for(int i = 0; i &amp;lt; fields.length; i++)            // Display them.&lt;br /&gt;
      print_field(fields[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot; // Methods&amp;quot;);&lt;br /&gt;
    Method[] methods = c.getDeclaredMethods();        // Look up methods.&lt;br /&gt;
    for(int i = 0; i &amp;lt; methods.length; i++)           // Display them.&lt;br /&gt;
      print_method_or_constructor(methods[i]);&lt;br /&gt;
&lt;br /&gt;
    System.out.println(&amp;quot;}&amp;quot;);             // End class member listing.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return the name of an interface or primitive type, handling arrays. */&lt;br /&gt;
  public static String typename(Class t) {&lt;br /&gt;
    String brackets = &amp;quot;&amp;quot;;&lt;br /&gt;
    while(t.isArray()) {&lt;br /&gt;
      brackets += &amp;quot;[]&amp;quot;;&lt;br /&gt;
      t = t.getComponentType();&lt;br /&gt;
    }&lt;br /&gt;
    return t.getName() + brackets;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Return a string version of modifiers, handling spaces nicely. */&lt;br /&gt;
  public static String modifiers(int m) {&lt;br /&gt;
    if (m == 0) return &amp;quot;&amp;quot;;&lt;br /&gt;
    else return Modifier.toString(m) + &amp;quot; &amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, type, and name of a field */&lt;br /&gt;
  public static void print_field(Field f) {&lt;br /&gt;
    System.out.println(&amp;quot;  &amp;quot; +&lt;br /&gt;
                       modifiers(f.getModifiers()) +&lt;br /&gt;
                       typename(f.getType()) + &amp;quot; &amp;quot; + f.getName() + &amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  /** Print the modifiers, return type, name, parameter types and exception&lt;br /&gt;
   *  type of a method or constructor.  Note the use of the Member interface&lt;br /&gt;
   *  to allow this method to work with both Method and Constructor objects */&lt;br /&gt;
  public static void print_method_or_constructor(Member member) {&lt;br /&gt;
    Class returntype=null, parameters[], exceptions[];&lt;br /&gt;
    if (member instanceof Method) {&lt;br /&gt;
      Method m = (Method) member;&lt;br /&gt;
      returntype = m.getReturnType();&lt;br /&gt;
      parameters = m.getParameterTypes();&lt;br /&gt;
      exceptions = m.getExceptionTypes();&lt;br /&gt;
    } else {&lt;br /&gt;
      Constructor c = (Constructor) member;&lt;br /&gt;
      parameters = c.getParameterTypes();&lt;br /&gt;
      exceptions = c.getExceptionTypes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    System.out.print(&amp;quot;  &amp;quot; + modifiers(member.getModifiers()) +&lt;br /&gt;
                     ((returntype!=null)? typename(returntype)+&amp;quot; &amp;quot; : &amp;quot;&amp;quot;) +&lt;br /&gt;
                     member.getName() + &amp;quot;(&amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; parameters.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(parameters[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.print(&amp;quot;)&amp;quot;);&lt;br /&gt;
    if (exceptions.length &amp;gt; 0) System.out.print(&amp;quot; throws &amp;quot;);&lt;br /&gt;
    for(int i = 0; i &amp;lt; exceptions.length; i++) {&lt;br /&gt;
      if (i &amp;gt; 0) System.out.print(&amp;quot;, &amp;quot;);&lt;br /&gt;
      System.out.print(typename(exceptions[i]));&lt;br /&gt;
    }&lt;br /&gt;
    System.out.println(&amp;quot;;&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This code is then compiled and run, with the following simple test class passed in:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput {&lt;br /&gt;
    public static void main(String[] args) {}&lt;br /&gt;
    public int method1() {&lt;br /&gt;
        return 0;&lt;br /&gt;
    }&lt;br /&gt;
    public String method2() {&lt;br /&gt;
        return &amp;quot;&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    public void method3() {}&lt;br /&gt;
    public void method4(int num) {}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The output generated is as follows:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class testInput extends java.lang.Object {&lt;br /&gt;
 // Constructors&lt;br /&gt;
  public testInput();&lt;br /&gt;
 // Fields&lt;br /&gt;
 // Methods&lt;br /&gt;
  public int method1();&lt;br /&gt;
  public java.lang.String method2();&lt;br /&gt;
  public void method3();&lt;br /&gt;
  public void method4(int);&lt;br /&gt;
  public static void main(java.lang.String[]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection Sequences ===&lt;br /&gt;
The Java code above, which displays constructors, methods, and other information pertinent to the ''testInput'' class, is somewhat convoluted and difficult to trace.  In Ruby, the methods below can be called on a module/class to return similar information:&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.private_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.protected_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.public_instance_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
*&amp;lt;tt&amp;gt;''mod''.singleton_methods(''inc_super''=true)&amp;lt;/tt&amp;gt;&lt;br /&gt;
For example, using the &amp;lt;tt&amp;gt;Demo&amp;lt;/tt&amp;gt; class below, all of the class' methods (private, protected, public, and singleton) can be easily displayed.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
irb(main):001:0&amp;gt; class Demo&lt;br /&gt;
irb(main):002:1&amp;gt;   public&lt;br /&gt;
irb(main):003:1&amp;gt;     def method1&lt;br /&gt;
irb(main):004:2&amp;gt;   end&lt;br /&gt;
irb(main):005:1&amp;gt;   private&lt;br /&gt;
irb(main):006:1&amp;gt;     def method2&lt;br /&gt;
irb(main):007:2&amp;gt;   end&lt;br /&gt;
irb(main):008:1&amp;gt;   public&lt;br /&gt;
irb(main):009:1&amp;gt;     def method3&lt;br /&gt;
irb(main):010:2&amp;gt;   end&lt;br /&gt;
irb(main):011:1&amp;gt;   protected&lt;br /&gt;
irb(main):012:1&amp;gt;     def method4&lt;br /&gt;
irb(main):013:2&amp;gt;   end&lt;br /&gt;
irb(main):014:1&amp;gt;   def Demo.method5&lt;br /&gt;
irb(main):015:2&amp;gt;   end&lt;br /&gt;
irb(main):016:1&amp;gt;   public&lt;br /&gt;
irb(main):017:1&amp;gt;     def method6&lt;br /&gt;
irb(main):018:2&amp;gt;   end&lt;br /&gt;
irb(main):019:1&amp;gt; end&lt;br /&gt;
=&amp;gt; nil&lt;br /&gt;
irb(main):020:0&amp;gt; Demo.private_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method2&amp;quot;]&lt;br /&gt;
irb(main):021:0&amp;gt; Demo.protected_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method4&amp;quot;]&lt;br /&gt;
irb(main):022:0&amp;gt; Demo.public_instance_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method3&amp;quot;, &amp;quot;method6&amp;quot;, &amp;quot;method1&amp;quot;]&lt;br /&gt;
irb(main):023:0&amp;gt; Demo.singleton_methods(false)&lt;br /&gt;
=&amp;gt; [&amp;quot;method5&amp;quot;]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Ruby Reflection vs. Java Reflection ===&lt;br /&gt;
&lt;br /&gt;
*By coding and comparing we find that Reflection in Java tends to be more verbose than Ruby code which accomplishes the same task.  Also, exceptions such as &amp;lt;tt&amp;gt;NoSuchMethodError&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;InvocationTargetException&amp;lt;/tt&amp;gt; tend to create further complications and confusion when using Java's reflection API.&lt;br /&gt;
&lt;br /&gt;
*Reflection API in java is also used in Database Access with SQL.&lt;br /&gt;
&lt;br /&gt;
*In Ruby, Aspect Oriented Programming(AOP) can be performed using ''aliasing, reflection and metaprogramming''.  These language facilities make it much easier to perform AOP than in Java.&lt;br /&gt;
&lt;br /&gt;
=== Uses of Reflection ===&lt;br /&gt;
Reflection 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 and can enable applications to perform operations which would otherwise be impossible. &lt;br /&gt;
&lt;br /&gt;
*Extensibility Features &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;
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;
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;
=== 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;
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;
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;
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;
=== ''Refrences'' ===&lt;br /&gt;
#Flanagan, David.  [http://books.google.com/books?id=kADTIsgeLh4C Java Examples in a Nutshell].  O'Reilly, 2004.&lt;br /&gt;
#Thomas, Dave.  [http://books.google.com/books?id=8LXOAAAACAAJ Programming Ruby].  Raleigh: Pragmatic Bookshelf, 2004.&lt;br /&gt;
&lt;br /&gt;
=== ''External Links ===&lt;br /&gt;
*http://phrogz.net/programmingruby/ospace.html&lt;br /&gt;
*http://java.sun.com/docs/books/tutorial/reflect/&lt;br /&gt;
*http://java.sun.com/developer/technicalArticles/ALT/Reflection/index.html&lt;/div&gt;</summary>
		<author><name>Amosti</name></author>
	</entry>
</feed>