<?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=Jrwills2</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=Jrwills2"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Jrwills2"/>
	<updated>2026-07-18T07:12:09Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517/M601_Design_Document&amp;diff=74568</id>
		<title>CSC/ECE 517/M601 Design Document</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517/M601_Design_Document&amp;diff=74568"/>
		<updated>2013-04-02T03:17:48Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Resources */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Design Document for M601 - Add ability to update css through&lt;br /&gt;
webapp&lt;br /&gt;
Team members: Yuri Kolesnikov, Jonathan Wills, Jeffrey Plemmons, Roshna Agarwal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Purpose==&lt;br /&gt;
	The purpose is to add the functionality of allowing the admin to customize the OpenMRS css file to their own specifications. By having the ability to customize the OpenMRS css file, the admin will be free to change the look and feel in their installation of OpenMRS.&lt;br /&gt;
&lt;br /&gt;
==Problem Definition==&lt;br /&gt;
	Add admin page for customization of the openmrs css file. Admins can 'override' any openmrs css with their own file by specifying that file in the openmrs runtime properties file. (See https://wiki.openmrs.org/display/docs/Overriding+OpenMRS+Default+Runtime+Properties)&lt;br /&gt;
This would be best as a new module in openmrs (See https://wiki.openmrs.org/display/docs/Modules)&lt;br /&gt;
Or perhaps just adding a new feature in the Custom Logo module? The admin should see a page with a large text box. The text box value should be persisted in the database as a new object/table/column somehow. When the module starts up, the Activator is called. Modify the activator to update the filesystem css file with whatever is defined by the admin. https://wiki.openmrs.org/display/docs/Module+Activator&lt;br /&gt;
&lt;br /&gt;
==Requirements==&lt;br /&gt;
&lt;br /&gt;
The admin should be able to customize the openmrs css file through an admin page.&lt;br /&gt;
The admin should see a page with a large text box.&lt;br /&gt;
The text box content should be stored in the database.&lt;br /&gt;
The activator needs to be modified so that it updates the filesystem css file with whatever is defined by the admin.&lt;br /&gt;
Need to use Eclipse, Maven, and Ant as development tools for the module.&lt;br /&gt;
==Tasks==&lt;br /&gt;
'''Week 1'''&lt;br /&gt;
&lt;br /&gt;
*Get OpenMRS Id&lt;br /&gt;
*Get ticket assigned and in progress&lt;br /&gt;
*Get openmrs module name assigned&lt;br /&gt;
*Create openmrs module&lt;br /&gt;
*Commit module to github&lt;br /&gt;
*Check out module on partner machines&lt;br /&gt;
*Get new admin page link&lt;br /&gt;
*Create new spring controller and jsp page&lt;br /&gt;
'''Week 2'''&lt;br /&gt;
&lt;br /&gt;
*Create object for storing css in db&lt;br /&gt;
*Create service layer and dao layer methods to save object&lt;br /&gt;
*Connect controller to db object for saving + retrieving&lt;br /&gt;
*Add unit tests for saving/retrieving&lt;br /&gt;
'''Week 3'''&lt;br /&gt;
&lt;br /&gt;
*Create settings page to allow admin to choose where tomcat is running&lt;br /&gt;
*Add some magic so that module tries to guess where tomcat is running&lt;br /&gt;
*Add ability to put the saved css into the right folder in tomcat so that the css is actually overriding the openmrs style.css file.&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517/M601_Design_Document&amp;diff=74567</id>
		<title>CSC/ECE 517/M601 Design Document</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517/M601_Design_Document&amp;diff=74567"/>
		<updated>2013-04-02T03:06:12Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Problem Definition */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Design Document for M601 - Add ability to update css through&lt;br /&gt;
webapp&lt;br /&gt;
Team members: Yuri Kolesnikov, Jonathan Wills, Jeffrey Plemmons, Roshna Agarwal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Purpose==&lt;br /&gt;
	The purpose is to add the functionality of allowing the admin to customize the OpenMRS css file to their own specifications. By having the ability to customize the OpenMRS css file, the admin will be free to change the look and feel in their installation of OpenMRS.&lt;br /&gt;
&lt;br /&gt;
==Problem Definition==&lt;br /&gt;
	Add admin page for customization of the openmrs css file. Admins can 'override' any openmrs css with their own file by specifying that file in the openmrs runtime properties file. (See https://wiki.openmrs.org/display/docs/Overriding+OpenMRS+Default+Runtime+Properties)&lt;br /&gt;
This would be best as a new module in openmrs (See https://wiki.openmrs.org/display/docs/Modules)&lt;br /&gt;
Or perhaps just adding a new feature in the Custom Logo module? The admin should see a page with a large text box. The text box value should be persisted in the database as a new object/table/column somehow. When the module starts up, the Activator is called. Modify the activator to update the filesystem css file with whatever is defined by the admin. https://wiki.openmrs.org/display/docs/Module+Activator&lt;br /&gt;
&lt;br /&gt;
==Requirements==&lt;br /&gt;
&lt;br /&gt;
The admin should be able to customize the openmrs css file through an admin page.&lt;br /&gt;
The admin should see a page with a large text box.&lt;br /&gt;
The text box content should be stored in the database.&lt;br /&gt;
The activator needs to be modified so that it updates the filesystem css file with whatever is defined by the admin.&lt;br /&gt;
Need to use Eclipse, Maven, and Ant as development tools for the module.&lt;br /&gt;
==Tasks==&lt;br /&gt;
'''Week 1'''&lt;br /&gt;
&lt;br /&gt;
*Get OpenMRS Id&lt;br /&gt;
*Get ticket assigned and in progress&lt;br /&gt;
*Get openmrs module name assigned&lt;br /&gt;
*Create openmrs module&lt;br /&gt;
*Commit module to github&lt;br /&gt;
*Check out module on partner machines&lt;br /&gt;
*Get new admin page link&lt;br /&gt;
*Create new spring controller and jsp page&lt;br /&gt;
'''Week 2'''&lt;br /&gt;
&lt;br /&gt;
*Create object for storing css in db&lt;br /&gt;
*Create service layer and dao layer methods to save object&lt;br /&gt;
*Connect controller to db object for saving + retrieving&lt;br /&gt;
*Add unit tests for saving/retrieving&lt;br /&gt;
'''Week 3'''&lt;br /&gt;
&lt;br /&gt;
*Create settings page to allow admin to choose where tomcat is running&lt;br /&gt;
*Add some magic so that module tries to guess where tomcat is running&lt;br /&gt;
*Add ability to put the saved css into the right folder in tomcat so that the css is actually overriding the openmrs style.css file.&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517/M601_Design_Document&amp;diff=74566</id>
		<title>CSC/ECE 517/M601 Design Document</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517/M601_Design_Document&amp;diff=74566"/>
		<updated>2013-04-02T03:01:48Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Tasks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Design Document for M601 - Add ability to update css through&lt;br /&gt;
webapp&lt;br /&gt;
Team members: Yuri Kolesnikov, Jonathan Wills, Jeffrey Plemmons, Roshna Agarwal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Purpose==&lt;br /&gt;
	The purpose is to add the functionality of allowing the admin to customize the OpenMRS css file to their own specifications. By having the ability to customize the OpenMRS css file, the admin will be free to change the look and feel in their installation of OpenMRS.&lt;br /&gt;
&lt;br /&gt;
==Problem Definition==&lt;br /&gt;
	Add admin page for customization of the openmrs css file. Admins can 'override' any openmrs css with their own file by specifying that file in the openmrs runtime properties file. See https://wiki.openmrs.org/display/docs/Overriding+OpenMRS+Default+Runtime+Properties&lt;br /&gt;
This would be best as a new module in openmrs. https://wiki.openmrs.org/display/docs/Modules&lt;br /&gt;
Or perhaps just adding a new feature in the Custom Logo module? The admin should see a page with a large text box. The text box value should be persisted in the database as a new object/table/column somehow. When the module starts up, the Activator is called. Modify the activator to update the filesystem css file with whatever is defined by the admin. https://wiki.openmrs.org/display/docs/Module+Activator&lt;br /&gt;
	&lt;br /&gt;
==Requirements==&lt;br /&gt;
&lt;br /&gt;
The admin should be able to customize the openmrs css file through an admin page.&lt;br /&gt;
The admin should see a page with a large text box.&lt;br /&gt;
The text box content should be stored in the database.&lt;br /&gt;
The activator needs to be modified so that it updates the filesystem css file with whatever is defined by the admin.&lt;br /&gt;
Need to use Eclipse, Maven, and Ant as development tools for the module.&lt;br /&gt;
==Tasks==&lt;br /&gt;
'''Week 1'''&lt;br /&gt;
&lt;br /&gt;
*Get OpenMRS Id&lt;br /&gt;
*Get ticket assigned and in progress&lt;br /&gt;
*Get openmrs module name assigned&lt;br /&gt;
*Create openmrs module&lt;br /&gt;
*Commit module to github&lt;br /&gt;
*Check out module on partner machines&lt;br /&gt;
*Get new admin page link&lt;br /&gt;
*Create new spring controller and jsp page&lt;br /&gt;
'''Week 2'''&lt;br /&gt;
&lt;br /&gt;
*Create object for storing css in db&lt;br /&gt;
*Create service layer and dao layer methods to save object&lt;br /&gt;
*Connect controller to db object for saving + retrieving&lt;br /&gt;
*Add unit tests for saving/retrieving&lt;br /&gt;
'''Week 3'''&lt;br /&gt;
&lt;br /&gt;
*Create settings page to allow admin to choose where tomcat is running&lt;br /&gt;
*Add some magic so that module tries to guess where tomcat is running&lt;br /&gt;
*Add ability to put the saved css into the right folder in tomcat so that the css is actually overriding the openmrs style.css file.&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517/M601_Design_Document&amp;diff=74565</id>
		<title>CSC/ECE 517/M601 Design Document</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517/M601_Design_Document&amp;diff=74565"/>
		<updated>2013-04-02T03:01:22Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Tasks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Design Document for M601 - Add ability to update css through&lt;br /&gt;
webapp&lt;br /&gt;
Team members: Yuri Kolesnikov, Jonathan Wills, Jeffrey Plemmons, Roshna Agarwal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Purpose==&lt;br /&gt;
	The purpose is to add the functionality of allowing the admin to customize the OpenMRS css file to their own specifications. By having the ability to customize the OpenMRS css file, the admin will be free to change the look and feel in their installation of OpenMRS.&lt;br /&gt;
&lt;br /&gt;
==Problem Definition==&lt;br /&gt;
	Add admin page for customization of the openmrs css file. Admins can 'override' any openmrs css with their own file by specifying that file in the openmrs runtime properties file. See https://wiki.openmrs.org/display/docs/Overriding+OpenMRS+Default+Runtime+Properties&lt;br /&gt;
This would be best as a new module in openmrs. https://wiki.openmrs.org/display/docs/Modules&lt;br /&gt;
Or perhaps just adding a new feature in the Custom Logo module? The admin should see a page with a large text box. The text box value should be persisted in the database as a new object/table/column somehow. When the module starts up, the Activator is called. Modify the activator to update the filesystem css file with whatever is defined by the admin. https://wiki.openmrs.org/display/docs/Module+Activator&lt;br /&gt;
	&lt;br /&gt;
==Requirements==&lt;br /&gt;
&lt;br /&gt;
The admin should be able to customize the openmrs css file through an admin page.&lt;br /&gt;
The admin should see a page with a large text box.&lt;br /&gt;
The text box content should be stored in the database.&lt;br /&gt;
The activator needs to be modified so that it updates the filesystem css file with whatever is defined by the admin.&lt;br /&gt;
Need to use Eclipse, Maven, and Ant as development tools for the module.&lt;br /&gt;
==Tasks==&lt;br /&gt;
'''Week 1:'''&lt;br /&gt;
&lt;br /&gt;
*Get OpenMRS Id&lt;br /&gt;
*Get ticket assigned and in progress&lt;br /&gt;
*Get openmrs module name assigned&lt;br /&gt;
*Create openmrs module&lt;br /&gt;
*Commit module to github&lt;br /&gt;
*Check out module on partner machines&lt;br /&gt;
*Get new admin page link&lt;br /&gt;
*Create new spring controller and jsp page&lt;br /&gt;
Week 2:&lt;br /&gt;
&lt;br /&gt;
Create object for storing css in db&lt;br /&gt;
Create service layer and dao layer methods to save object&lt;br /&gt;
Connect controller to db object for saving + retrieving&lt;br /&gt;
Add unit tests for saving/retrieving&lt;br /&gt;
Week 3:&lt;br /&gt;
&lt;br /&gt;
Create settings page to allow admin to choose where tomcat is running&lt;br /&gt;
Add some magic so that module tries to guess where tomcat is running&lt;br /&gt;
Add ability to put the saved css into the right folder in tomcat so that the css is actually overriding the openmrs style.css file.&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517/M601_Design_Document&amp;diff=74564</id>
		<title>CSC/ECE 517/M601 Design Document</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517/M601_Design_Document&amp;diff=74564"/>
		<updated>2013-04-02T03:01:11Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Tasks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Design Document for M601 - Add ability to update css through&lt;br /&gt;
webapp&lt;br /&gt;
Team members: Yuri Kolesnikov, Jonathan Wills, Jeffrey Plemmons, Roshna Agarwal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Purpose==&lt;br /&gt;
	The purpose is to add the functionality of allowing the admin to customize the OpenMRS css file to their own specifications. By having the ability to customize the OpenMRS css file, the admin will be free to change the look and feel in their installation of OpenMRS.&lt;br /&gt;
&lt;br /&gt;
==Problem Definition==&lt;br /&gt;
	Add admin page for customization of the openmrs css file. Admins can 'override' any openmrs css with their own file by specifying that file in the openmrs runtime properties file. See https://wiki.openmrs.org/display/docs/Overriding+OpenMRS+Default+Runtime+Properties&lt;br /&gt;
This would be best as a new module in openmrs. https://wiki.openmrs.org/display/docs/Modules&lt;br /&gt;
Or perhaps just adding a new feature in the Custom Logo module? The admin should see a page with a large text box. The text box value should be persisted in the database as a new object/table/column somehow. When the module starts up, the Activator is called. Modify the activator to update the filesystem css file with whatever is defined by the admin. https://wiki.openmrs.org/display/docs/Module+Activator&lt;br /&gt;
	&lt;br /&gt;
==Requirements==&lt;br /&gt;
&lt;br /&gt;
The admin should be able to customize the openmrs css file through an admin page.&lt;br /&gt;
The admin should see a page with a large text box.&lt;br /&gt;
The text box content should be stored in the database.&lt;br /&gt;
The activator needs to be modified so that it updates the filesystem css file with whatever is defined by the admin.&lt;br /&gt;
Need to use Eclipse, Maven, and Ant as development tools for the module.&lt;br /&gt;
==Tasks==&lt;br /&gt;
Week 1:&lt;br /&gt;
&lt;br /&gt;
*Get OpenMRS Id&lt;br /&gt;
*Get ticket assigned and in progress&lt;br /&gt;
*Get openmrs module name assigned&lt;br /&gt;
*Create openmrs module&lt;br /&gt;
*Commit module to github&lt;br /&gt;
*Check out module on partner machines&lt;br /&gt;
*Get new admin page link&lt;br /&gt;
*Create new spring controller and jsp page&lt;br /&gt;
Week 2:&lt;br /&gt;
&lt;br /&gt;
Create object for storing css in db&lt;br /&gt;
Create service layer and dao layer methods to save object&lt;br /&gt;
Connect controller to db object for saving + retrieving&lt;br /&gt;
Add unit tests for saving/retrieving&lt;br /&gt;
Week 3:&lt;br /&gt;
&lt;br /&gt;
Create settings page to allow admin to choose where tomcat is running&lt;br /&gt;
Add some magic so that module tries to guess where tomcat is running&lt;br /&gt;
Add ability to put the saved css into the right folder in tomcat so that the css is actually overriding the openmrs style.css file.&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517/M601_Design_Document&amp;diff=74563</id>
		<title>CSC/ECE 517/M601 Design Document</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517/M601_Design_Document&amp;diff=74563"/>
		<updated>2013-04-02T02:59:52Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: Created page with &amp;quot;Design Document for M601 - Add ability to update css through webapp Team members: Yuri Kolesnikov, Jonathan Wills, Jeffrey Plemmons, Roshna Agarwal   ==Purpose== 	The purpose is ...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Design Document for M601 - Add ability to update css through&lt;br /&gt;
webapp&lt;br /&gt;
Team members: Yuri Kolesnikov, Jonathan Wills, Jeffrey Plemmons, Roshna Agarwal&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Purpose==&lt;br /&gt;
	The purpose is to add the functionality of allowing the admin to customize the OpenMRS css file to their own specifications. By having the ability to customize the OpenMRS css file, the admin will be free to change the look and feel in their installation of OpenMRS.&lt;br /&gt;
&lt;br /&gt;
==Problem Definition==&lt;br /&gt;
	Add admin page for customization of the openmrs css file. Admins can 'override' any openmrs css with their own file by specifying that file in the openmrs runtime properties file. See https://wiki.openmrs.org/display/docs/Overriding+OpenMRS+Default+Runtime+Properties&lt;br /&gt;
This would be best as a new module in openmrs. https://wiki.openmrs.org/display/docs/Modules&lt;br /&gt;
Or perhaps just adding a new feature in the Custom Logo module? The admin should see a page with a large text box. The text box value should be persisted in the database as a new object/table/column somehow. When the module starts up, the Activator is called. Modify the activator to update the filesystem css file with whatever is defined by the admin. https://wiki.openmrs.org/display/docs/Module+Activator&lt;br /&gt;
	&lt;br /&gt;
==Requirements==&lt;br /&gt;
&lt;br /&gt;
The admin should be able to customize the openmrs css file through an admin page.&lt;br /&gt;
The admin should see a page with a large text box.&lt;br /&gt;
The text box content should be stored in the database.&lt;br /&gt;
The activator needs to be modified so that it updates the filesystem css file with whatever is defined by the admin.&lt;br /&gt;
Need to use Eclipse, Maven, and Ant as development tools for the module.&lt;br /&gt;
==Tasks==&lt;br /&gt;
Week 1:&lt;br /&gt;
&lt;br /&gt;
Get OpenMRS Id&lt;br /&gt;
Get ticket assigned and in progress&lt;br /&gt;
Get openmrs module name assigned&lt;br /&gt;
Create openmrs module&lt;br /&gt;
Commit module to github&lt;br /&gt;
Check out module on partner machines&lt;br /&gt;
Get new admin page link&lt;br /&gt;
Create new spring controller and jsp page&lt;br /&gt;
Week 2:&lt;br /&gt;
&lt;br /&gt;
Create object for storing css in db&lt;br /&gt;
Create service layer and dao layer methods to save object&lt;br /&gt;
Connect controller to db object for saving + retrieving&lt;br /&gt;
Add unit tests for saving/retrieving&lt;br /&gt;
Week 3:&lt;br /&gt;
&lt;br /&gt;
Create settings page to allow admin to choose where tomcat is running&lt;br /&gt;
Add some magic so that module tries to guess where tomcat is running&lt;br /&gt;
Add ability to put the saved css into the right folder in tomcat so that the css is actually overriding the openmrs style.css file.&lt;br /&gt;
&lt;br /&gt;
==Resources==&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74433</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74433"/>
		<updated>2013-03-29T00:57:31Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Questionnaire Subclass Unit Tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/chunsingtsui/expertiza Project Github Repository]&lt;br /&gt;
&lt;br /&gt;
[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki]&lt;br /&gt;
&lt;br /&gt;
== QuestionnaireController Functional Tests ==&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing QuestionnaireController Test ===&lt;br /&gt;
&lt;br /&gt;
There were a few tests already written for the Questionnaire controller, but they were not passing initially. We had to modify several things in the test and questionnaire fixture in order to get things up and running. The setup method contained login code that was outdated, and thus each test would result in a redirect due to the invalid login credential. So we looked through other more up to date functional test files and found the correct way to login as a particular user. We also put this code in a helper method so we can call it more easily in different tests if we so choose (more on this in the &amp;quot;Helper Methods&amp;quot; section).&lt;br /&gt;
&lt;br /&gt;
In addition, we added new fixtures to help with our testing. Through debugging the test error messages, we saw that an additional required column called &amp;quot;section&amp;quot; has been added to the &amp;quot;Questionnaire&amp;quot; model, which was contributing to the failures. We added the additional column accordingly in our new fixures.&lt;br /&gt;
&lt;br /&gt;
=== Helper Methods ===&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
We tried to cover as many types of useful cases as possible according to the project requirements. We used assertions that checked counts after create and delete, as well as asserted redirects. We also tested against invalid inputs that could cause errors. All of the tests are self-explanatory based on their method names, and there really is no need to further provide explanations here.&lt;br /&gt;
&lt;br /&gt;
We also attempted to test the private methods of the controller as well at the beginning of the project. However, we couldn't find a way to test many of them due to the fact that they reference the params[] hash in the methods, and we can't isolate the testing of those methods without calling some controller action. Since these private methods are only used by the public controller action methods, we did the next best thing by creating tests for the action methods that would use these private methods to test them indirectly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Controller Test ===&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test. In Rubymine, this can be done by right clicking the test file and selecting &amp;quot;Run &amp;lt;filename&amp;gt;&amp;quot;. Individual tests inside each test file can also be run by right clicking on the test method name and selecting &amp;quot;Run &amp;lt;method_name&amp;gt;&amp;quot;. If the command line is preferred, the following command can be used:&lt;br /&gt;
&lt;br /&gt;
 ruby -Itest test/functional/questionnaire_controller_test.rb&lt;br /&gt;
&lt;br /&gt;
== Questionnaire Subclass Unit Tests ==&lt;br /&gt;
&lt;br /&gt;
The original assignment was to write unit tests for the Questionnaire subclasses:&lt;br /&gt;
* author_feedback_questionnaire.rb&lt;br /&gt;
* metareview_questionnaire.rb&lt;br /&gt;
* review_questionnaire.rb&lt;br /&gt;
* teammate_review_questionnaire.rb&lt;br /&gt;
&lt;br /&gt;
As we reviewed the tests, it was found that most of the methods were trivial (ex: the method &amp;quot;symbol&amp;quot; only returns a hardcoded symbol).  The only method that was found to be worth testing was the get_weighted_score method.  Upon further review, however, we found that testing this method in each of the four subclasses violated the DRY principle.  Therefore, we moved the test to the superclass (questionnaire.rb) to test the &amp;quot;get_weighted_score&amp;quot; method.&lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing Questionnaire Subclass Tests ===&lt;br /&gt;
&lt;br /&gt;
There was only one test in the existing unit test for AuthorFeedbackQuestionnaire for testing the &amp;quot;get_weighted_score&amp;quot; method. Even though the test could be run, we found that it was not making assertions as it should. The files for the other 3 questionnaire subclasses were created to complete the testing on them.  As stated earlier, we then found that testing the &amp;quot;get_weighted_score&amp;quot; method was redundant, violating the DRY principle, so it was moved to the superclass (Questionnaire).  The tests in the subclasses were deleted (since they were redundant) but the test files were kept for future testing of the subclass if/when needed.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
The method &amp;quot;compute_weighted_score&amp;quot; from the the base class was the only thing that needed to be tested (due to the above reason). Based on an average, it was asserted that the value was not nil and that the value was computed correctly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;compute_weighted_score&amp;quot; test can be found in the Questionnaire Unit Test file. Same as directions for the functional test, this can be run in either Rubymine or the command line. In Rubymine, this can be done by right clicking the test file and selecting &amp;quot;Run &amp;lt;filename&amp;gt;&amp;quot;. Individual tests inside each test file can also be run by right clicking on the test method name and selecting &amp;quot;Run &amp;lt;method_name&amp;gt;&amp;quot;. If the command line is preferred, the following command can be used:&lt;br /&gt;
&lt;br /&gt;
 ruby -Itest test/unit/questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
=== VNC Server Information ===&lt;br /&gt;
&lt;br /&gt;
IP: 152.46.16.122:2&lt;br /&gt;
Password: project517&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
=== Members ===&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
=== Highlights of Project ===&lt;br /&gt;
* Added unit test for questionnaire class&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* The test that uses the &amp;quot;delete&amp;quot; action in the controller was failing due to a potential bug in the questionnaire.rb model class (refer to ''Potential Bug'' section)&lt;br /&gt;
&lt;br /&gt;
=== List of files changed ===&lt;br /&gt;
*test/fixtures/assignment_questionnaires.yml&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/questionnaire_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
= Potential Bug =&lt;br /&gt;
The Questionnaire model class contains a ''has_many :assignment_questionnaire'' that should be ''has_many :assignment_questionnaire'''s''' '' according to convention. Because of this, our &amp;quot;delete&amp;quot; action test fails since it doesn't match the convention. If it is indeed incorrect, then in the &amp;quot;compute_weighted_score&amp;quot; method, the ''self.assignemnt_questionnaire...'' call also needs to be changed to ''self.assignment_questionnaires...''.  This most likely needs to be refactored, but we did not want to do a refactor without testing its effects on the system thoroughly.&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74432</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74432"/>
		<updated>2013-03-29T00:56:20Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Testing - questionnaire */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/chunsingtsui/expertiza Project Github Repository]&lt;br /&gt;
&lt;br /&gt;
[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza Wiki]&lt;br /&gt;
&lt;br /&gt;
== QuestionnaireController Functional Tests ==&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing QuestionnaireController Test ===&lt;br /&gt;
&lt;br /&gt;
There were a few tests already written for the Questionnaire controller, but they were not passing initially. We had to modify several things in the test and questionnaire fixture in order to get things up and running. The setup method contained login code that was outdated, and thus each test would result in a redirect due to the invalid login credential. So we looked through other more up to date functional test files and found the correct way to login as a particular user. We also put this code in a helper method so we can call it more easily in different tests if we so choose (more on this in the &amp;quot;Helper Methods&amp;quot; section).&lt;br /&gt;
&lt;br /&gt;
In addition, we added new fixtures to help with our testing. Through debugging the test error messages, we saw that an additional required column called &amp;quot;section&amp;quot; has been added to the &amp;quot;Questionnaire&amp;quot; model, which was contributing to the failures. We added the additional column accordingly in our new fixures.&lt;br /&gt;
&lt;br /&gt;
=== Helper Methods ===&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
We tried to cover as many types of useful cases as possible according to the project requirements. We used assertions that checked counts after create and delete, as well as asserted redirects. We also tested against invalid inputs that could cause errors. All of the tests are self-explanatory based on their method names, and there really is no need to further provide explanations here.&lt;br /&gt;
&lt;br /&gt;
We also attempted to test the private methods of the controller as well at the beginning of the project. However, we couldn't find a way to test many of them due to the fact that they reference the params[] hash in the methods, and we can't isolate the testing of those methods without calling some controller action. Since these private methods are only used by the public controller action methods, we did the next best thing by creating tests for the action methods that would use these private methods to test them indirectly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Controller Test ===&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test. In Rubymine, this can be done by right clicking the test file and selecting &amp;quot;Run &amp;lt;filename&amp;gt;&amp;quot;. Individual tests inside each test file can also be run by right clicking on the test method name and selecting &amp;quot;Run &amp;lt;method_name&amp;gt;&amp;quot;. If the command line is preferred, the following command can be used:&lt;br /&gt;
&lt;br /&gt;
 ruby -Itest test/functional/questionnaire_controller_test.rb&lt;br /&gt;
&lt;br /&gt;
== Questionnaire Subclass Unit Tests ==&lt;br /&gt;
&lt;br /&gt;
The original assignment was to write unit tests for the Questionnaire subclasses:&lt;br /&gt;
* author_feedback_questionnaire.rb&lt;br /&gt;
* metareview_questionnaire.rb&lt;br /&gt;
* review_questionnaire.rb&lt;br /&gt;
* teammate_review_questionnaire.rb&lt;br /&gt;
&lt;br /&gt;
As we reviewed the tests, it was found that most of the methods were trivial (ex: the method &amp;quot;symbol&amp;quot; only only returns a hardcoded symbol).  The only method that was found to be worth testing was the get_weighted_score method.  Upon further review, however, we found that testing this method in each of the four subclasses violated the DRY principle.  Therefore, we moved the test to the superclass (questionnaire.rb) to test the &amp;quot;get_weighted_score&amp;quot; method.&lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing Questionnaire Subclass Tests ===&lt;br /&gt;
&lt;br /&gt;
There was only one test in the existing unit test for AuthorFeedbackQuestionnaire for testing the &amp;quot;get_weighted_score&amp;quot; method. Even though the test could be run, we found that it was not making assertions as it should. The files for the other 3 questionnaire subclasses were created to complete the testing on them.  As stated earlier, we then found that testing the &amp;quot;get_weighted_score&amp;quot; method was redundant, violating the DRY principle, so it was moved to the superclass (Questionnaire).  The tests in the subclasses were deleted (since they were redundant) but the test files were kept for future testing of the subclass if/when needed.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
The method &amp;quot;compute_weighted_score&amp;quot; from the the base class was the only thing that needed to be tested (due to the above reason). Based on an average, it was asserted that the value was not nil and that the value was computed correctly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;compute_weighted_score&amp;quot; test can be found in the Questionnaire Unit Test file. Same as directions for the functional test, this can be run in either Rubymine or the command line. In Rubymine, this can be done by right clicking the test file and selecting &amp;quot;Run &amp;lt;filename&amp;gt;&amp;quot;. Individual tests inside each test file can also be run by right clicking on the test method name and selecting &amp;quot;Run &amp;lt;method_name&amp;gt;&amp;quot;. If the command line is preferred, the following command can be used:&lt;br /&gt;
&lt;br /&gt;
 ruby -Itest test/unit/questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
=== VNC Server Information ===&lt;br /&gt;
&lt;br /&gt;
IP: 152.46.16.122:2&lt;br /&gt;
Password: project517&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
=== Members ===&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
=== Highlights of Project ===&lt;br /&gt;
* Added unit test for questionnaire class&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* The test that uses the &amp;quot;delete&amp;quot; action in the controller was failing due to a potential bug in the questionnaire.rb model class (refer to ''Potential Bug'' section)&lt;br /&gt;
&lt;br /&gt;
=== List of files changed ===&lt;br /&gt;
*test/fixtures/assignment_questionnaires.yml&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/questionnaire_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
= Potential Bug =&lt;br /&gt;
The Questionnaire model class contains a ''has_many :assignment_questionnaire'' that should be ''has_many :assignment_questionnaire'''s''' '' according to convention. Because of this, our &amp;quot;delete&amp;quot; action test fails since it doesn't match the convention. If it is indeed incorrect, then in the &amp;quot;compute_weighted_score&amp;quot; method, the ''self.assignemnt_questionnaire...'' call also needs to be changed to ''self.assignment_questionnaires...''.  This most likely needs to be refactored, but we did not want to do a refactor without testing its effects on the system thoroughly.&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74431</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74431"/>
		<updated>2013-03-29T00:54:19Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Testing - questionnaire */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/chunsingtsui/expertiza Project Github Repository]&lt;br /&gt;
&lt;br /&gt;
== QuestionnaireController Functional Tests ==&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing QuestionnaireController Test ===&lt;br /&gt;
&lt;br /&gt;
There were a few tests already written for the Questionnaire controller, but they were not passing initially. We had to modify several things in the test and questionnaire fixture in order to get things up and running. The setup method contained login code that was outdated, and thus each test would result in a redirect due to the invalid login credential. So we looked through other more up to date functional test files and found the correct way to login as a particular user. We also put this code in a helper method so we can call it more easily in different tests if we so choose (more on this in the &amp;quot;Helper Methods&amp;quot; section).&lt;br /&gt;
&lt;br /&gt;
In addition, we added new fixtures to help with our testing. Through debugging the test error messages, we saw that an additional required column called &amp;quot;section&amp;quot; has been added to the &amp;quot;Questionnaire&amp;quot; model, which was contributing to the failures. We added the additional column accordingly in our new fixures.&lt;br /&gt;
&lt;br /&gt;
=== Helper Methods ===&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
We tried to cover as many types of useful cases as possible according to the project requirements. We used assertions that checked counts after create and delete, as well as asserted redirects. We also tested against invalid inputs that could cause errors. All of the tests are self-explanatory based on their method names, and there really is no need to further provide explanations here.&lt;br /&gt;
&lt;br /&gt;
We also attempted to test the private methods of the controller as well at the beginning of the project. However, we couldn't find a way to test many of them due to the fact that they reference the params[] hash in the methods, and we can't isolate the testing of those methods without calling some controller action. Since these private methods are only used by the public controller action methods, we did the next best thing by creating tests for the action methods that would use these private methods to test them indirectly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Controller Test ===&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test. In Rubymine, this can be done by right clicking the test file and selecting &amp;quot;Run &amp;lt;filename&amp;gt;&amp;quot;. Individual tests inside each test file can also be run by right clicking on the test method name and selecting &amp;quot;Run &amp;lt;method_name&amp;gt;&amp;quot;. If the command line is preferred, the following command can be used:&lt;br /&gt;
&lt;br /&gt;
 ruby -Itest test/functional/questionnaire_controller_test.rb&lt;br /&gt;
&lt;br /&gt;
== Questionnaire Subclass Unit Tests ==&lt;br /&gt;
&lt;br /&gt;
The original assignment was to write unit tests for the Questionnaire subclasses:&lt;br /&gt;
* author_feedback_questionnaire.rb&lt;br /&gt;
* metareview_questionnaire.rb&lt;br /&gt;
* review_questionnaire.rb&lt;br /&gt;
* teammate_review_questionnaire.rb&lt;br /&gt;
&lt;br /&gt;
As we reviewed the tests, it was found that most of the methods were trivial (ex: the method &amp;quot;symbol&amp;quot; only only returns a hardcoded symbol).  The only method that was found to be worth testing was the get_weighted_score method.  Upon further review, however, we found that testing this method in each of the four subclasses violated the DRY principle.  Therefore, we moved the test to the superclass (questionnaire.rb) to test the &amp;quot;get_weighted_score&amp;quot; method.&lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing Questionnaire Subclass Tests ===&lt;br /&gt;
&lt;br /&gt;
There was only one test in the existing unit test for AuthorFeedbackQuestionnaire for testing the &amp;quot;get_weighted_score&amp;quot; method. Even though the test could be run, we found that it was not making assertions as it should. The files for the other 3 questionnaire subclasses were created to complete the testing on them.  As stated earlier, we then found that testing the &amp;quot;get_weighted_score&amp;quot; method was redundant, violating the DRY principle, so it was moved to the superclass (Questionnaire).  The tests in the subclasses were deleted (since they were redundant) but the test files were kept for future testing of the subclass if/when needed.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
The method &amp;quot;compute_weighted_score&amp;quot; from the the base class was the only thing that needed to be tested (due to the above reason). Based on an average, it was asserted that the value was not nil and that the value was computed correctly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;compute_weighted_score&amp;quot; test can be found in the Questionnaire Unit Test file. Same as directions for the functional test, this can be run in either Rubymine or the command line. In Rubymine, this can be done by right clicking the test file and selecting &amp;quot;Run &amp;lt;filename&amp;gt;&amp;quot;. Individual tests inside each test file can also be run by right clicking on the test method name and selecting &amp;quot;Run &amp;lt;method_name&amp;gt;&amp;quot;. If the command line is preferred, the following command can be used:&lt;br /&gt;
&lt;br /&gt;
 ruby -Itest test/unit/questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
=== VNC Server Information ===&lt;br /&gt;
&lt;br /&gt;
IP: 152.46.16.122:2&lt;br /&gt;
Password: project517&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
=== Members ===&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
=== Highlights of Project ===&lt;br /&gt;
* Added unit test for questionnaire class&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* The test that uses the &amp;quot;delete&amp;quot; action in the controller was failing due to a potential bug in the questionnaire.rb model class (refer to ''Potential Bug'' section)&lt;br /&gt;
&lt;br /&gt;
=== List of files changed ===&lt;br /&gt;
*test/fixtures/assignment_questionnaires.yml&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/questionnaire_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
= Potential Bug =&lt;br /&gt;
The Questionnaire model class contains a ''has_many :assignment_questionnaire'' that should be ''has_many :assignment_questionnaire'''s''' '' according to convention. Because of this, our &amp;quot;delete&amp;quot; action test fails since it doesn't match the convention. If it is indeed incorrect, then in the &amp;quot;compute_weighted_score&amp;quot; method, the ''self.assignemnt_questionnaire...'' call also needs to be changed to ''self.assignment_questionnaires...''.  This most likely needs to be refactored, but we did not want to do a refactor without testing its effects on the system thoroughly.&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74430</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74430"/>
		<updated>2013-03-29T00:54:01Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Testing - questionnaire */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/chunsingtsui/expertiza Project Repository https://github.com/chunsingtsui/expertiza]&lt;br /&gt;
&lt;br /&gt;
== QuestionnaireController Functional Tests ==&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing QuestionnaireController Test ===&lt;br /&gt;
&lt;br /&gt;
There were a few tests already written for the Questionnaire controller, but they were not passing initially. We had to modify several things in the test and questionnaire fixture in order to get things up and running. The setup method contained login code that was outdated, and thus each test would result in a redirect due to the invalid login credential. So we looked through other more up to date functional test files and found the correct way to login as a particular user. We also put this code in a helper method so we can call it more easily in different tests if we so choose (more on this in the &amp;quot;Helper Methods&amp;quot; section).&lt;br /&gt;
&lt;br /&gt;
In addition, we added new fixtures to help with our testing. Through debugging the test error messages, we saw that an additional required column called &amp;quot;section&amp;quot; has been added to the &amp;quot;Questionnaire&amp;quot; model, which was contributing to the failures. We added the additional column accordingly in our new fixures.&lt;br /&gt;
&lt;br /&gt;
=== Helper Methods ===&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
We tried to cover as many types of useful cases as possible according to the project requirements. We used assertions that checked counts after create and delete, as well as asserted redirects. We also tested against invalid inputs that could cause errors. All of the tests are self-explanatory based on their method names, and there really is no need to further provide explanations here.&lt;br /&gt;
&lt;br /&gt;
We also attempted to test the private methods of the controller as well at the beginning of the project. However, we couldn't find a way to test many of them due to the fact that they reference the params[] hash in the methods, and we can't isolate the testing of those methods without calling some controller action. Since these private methods are only used by the public controller action methods, we did the next best thing by creating tests for the action methods that would use these private methods to test them indirectly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Controller Test ===&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test. In Rubymine, this can be done by right clicking the test file and selecting &amp;quot;Run &amp;lt;filename&amp;gt;&amp;quot;. Individual tests inside each test file can also be run by right clicking on the test method name and selecting &amp;quot;Run &amp;lt;method_name&amp;gt;&amp;quot;. If the command line is preferred, the following command can be used:&lt;br /&gt;
&lt;br /&gt;
 ruby -Itest test/functional/questionnaire_controller_test.rb&lt;br /&gt;
&lt;br /&gt;
== Questionnaire Subclass Unit Tests ==&lt;br /&gt;
&lt;br /&gt;
The original assignment was to write unit tests for the Questionnaire subclasses:&lt;br /&gt;
* author_feedback_questionnaire.rb&lt;br /&gt;
* metareview_questionnaire.rb&lt;br /&gt;
* review_questionnaire.rb&lt;br /&gt;
* teammate_review_questionnaire.rb&lt;br /&gt;
&lt;br /&gt;
As we reviewed the tests, it was found that most of the methods were trivial (ex: the method &amp;quot;symbol&amp;quot; only only returns a hardcoded symbol).  The only method that was found to be worth testing was the get_weighted_score method.  Upon further review, however, we found that testing this method in each of the four subclasses violated the DRY principle.  Therefore, we moved the test to the superclass (questionnaire.rb) to test the &amp;quot;get_weighted_score&amp;quot; method.&lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing Questionnaire Subclass Tests ===&lt;br /&gt;
&lt;br /&gt;
There was only one test in the existing unit test for AuthorFeedbackQuestionnaire for testing the &amp;quot;get_weighted_score&amp;quot; method. Even though the test could be run, we found that it was not making assertions as it should. The files for the other 3 questionnaire subclasses were created to complete the testing on them.  As stated earlier, we then found that testing the &amp;quot;get_weighted_score&amp;quot; method was redundant, violating the DRY principle, so it was moved to the superclass (Questionnaire).  The tests in the subclasses were deleted (since they were redundant) but the test files were kept for future testing of the subclass if/when needed.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
The method &amp;quot;compute_weighted_score&amp;quot; from the the base class was the only thing that needed to be tested (due to the above reason). Based on an average, it was asserted that the value was not nil and that the value was computed correctly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;compute_weighted_score&amp;quot; test can be found in the Questionnaire Unit Test file. Same as directions for the functional test, this can be run in either Rubymine or the command line. In Rubymine, this can be done by right clicking the test file and selecting &amp;quot;Run &amp;lt;filename&amp;gt;&amp;quot;. Individual tests inside each test file can also be run by right clicking on the test method name and selecting &amp;quot;Run &amp;lt;method_name&amp;gt;&amp;quot;. If the command line is preferred, the following command can be used:&lt;br /&gt;
&lt;br /&gt;
 ruby -Itest test/unit/questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
=== VNC Server Information ===&lt;br /&gt;
&lt;br /&gt;
IP: 152.46.16.122:2&lt;br /&gt;
Password: project517&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
=== Members ===&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
=== Highlights of Project ===&lt;br /&gt;
* Added unit test for questionnaire class&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* The test that uses the &amp;quot;delete&amp;quot; action in the controller was failing due to a potential bug in the questionnaire.rb model class (refer to ''Potential Bug'' section)&lt;br /&gt;
&lt;br /&gt;
=== List of files changed ===&lt;br /&gt;
*test/fixtures/assignment_questionnaires.yml&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/questionnaire_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
= Potential Bug =&lt;br /&gt;
The Questionnaire model class contains a ''has_many :assignment_questionnaire'' that should be ''has_many :assignment_questionnaire'''s''' '' according to convention. Because of this, our &amp;quot;delete&amp;quot; action test fails since it doesn't match the convention. If it is indeed incorrect, then in the &amp;quot;compute_weighted_score&amp;quot; method, the ''self.assignemnt_questionnaire...'' call also needs to be changed to ''self.assignment_questionnaires...''.  This most likely needs to be refactored, but we did not want to do a refactor without testing its effects on the system thoroughly.&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74429</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74429"/>
		<updated>2013-03-29T00:53:40Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Testing - questionnaire */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
[https://github.com/chunsingtsui/expertiza Project Repository]&lt;br /&gt;
&lt;br /&gt;
== QuestionnaireController Functional Tests ==&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing QuestionnaireController Test ===&lt;br /&gt;
&lt;br /&gt;
There were a few tests already written for the Questionnaire controller, but they were not passing initially. We had to modify several things in the test and questionnaire fixture in order to get things up and running. The setup method contained login code that was outdated, and thus each test would result in a redirect due to the invalid login credential. So we looked through other more up to date functional test files and found the correct way to login as a particular user. We also put this code in a helper method so we can call it more easily in different tests if we so choose (more on this in the &amp;quot;Helper Methods&amp;quot; section).&lt;br /&gt;
&lt;br /&gt;
In addition, we added new fixtures to help with our testing. Through debugging the test error messages, we saw that an additional required column called &amp;quot;section&amp;quot; has been added to the &amp;quot;Questionnaire&amp;quot; model, which was contributing to the failures. We added the additional column accordingly in our new fixures.&lt;br /&gt;
&lt;br /&gt;
=== Helper Methods ===&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
We tried to cover as many types of useful cases as possible according to the project requirements. We used assertions that checked counts after create and delete, as well as asserted redirects. We also tested against invalid inputs that could cause errors. All of the tests are self-explanatory based on their method names, and there really is no need to further provide explanations here.&lt;br /&gt;
&lt;br /&gt;
We also attempted to test the private methods of the controller as well at the beginning of the project. However, we couldn't find a way to test many of them due to the fact that they reference the params[] hash in the methods, and we can't isolate the testing of those methods without calling some controller action. Since these private methods are only used by the public controller action methods, we did the next best thing by creating tests for the action methods that would use these private methods to test them indirectly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Controller Test ===&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test. In Rubymine, this can be done by right clicking the test file and selecting &amp;quot;Run &amp;lt;filename&amp;gt;&amp;quot;. Individual tests inside each test file can also be run by right clicking on the test method name and selecting &amp;quot;Run &amp;lt;method_name&amp;gt;&amp;quot;. If the command line is preferred, the following command can be used:&lt;br /&gt;
&lt;br /&gt;
 ruby -Itest test/functional/questionnaire_controller_test.rb&lt;br /&gt;
&lt;br /&gt;
== Questionnaire Subclass Unit Tests ==&lt;br /&gt;
&lt;br /&gt;
The original assignment was to write unit tests for the Questionnaire subclasses:&lt;br /&gt;
* author_feedback_questionnaire.rb&lt;br /&gt;
* metareview_questionnaire.rb&lt;br /&gt;
* review_questionnaire.rb&lt;br /&gt;
* teammate_review_questionnaire.rb&lt;br /&gt;
&lt;br /&gt;
As we reviewed the tests, it was found that most of the methods were trivial (ex: the method &amp;quot;symbol&amp;quot; only only returns a hardcoded symbol).  The only method that was found to be worth testing was the get_weighted_score method.  Upon further review, however, we found that testing this method in each of the four subclasses violated the DRY principle.  Therefore, we moved the test to the superclass (questionnaire.rb) to test the &amp;quot;get_weighted_score&amp;quot; method.&lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing Questionnaire Subclass Tests ===&lt;br /&gt;
&lt;br /&gt;
There was only one test in the existing unit test for AuthorFeedbackQuestionnaire for testing the &amp;quot;get_weighted_score&amp;quot; method. Even though the test could be run, we found that it was not making assertions as it should. The files for the other 3 questionnaire subclasses were created to complete the testing on them.  As stated earlier, we then found that testing the &amp;quot;get_weighted_score&amp;quot; method was redundant, violating the DRY principle, so it was moved to the superclass (Questionnaire).  The tests in the subclasses were deleted (since they were redundant) but the test files were kept for future testing of the subclass if/when needed.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
The method &amp;quot;compute_weighted_score&amp;quot; from the the base class was the only thing that needed to be tested (due to the above reason). Based on an average, it was asserted that the value was not nil and that the value was computed correctly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;compute_weighted_score&amp;quot; test can be found in the Questionnaire Unit Test file. Same as directions for the functional test, this can be run in either Rubymine or the command line. In Rubymine, this can be done by right clicking the test file and selecting &amp;quot;Run &amp;lt;filename&amp;gt;&amp;quot;. Individual tests inside each test file can also be run by right clicking on the test method name and selecting &amp;quot;Run &amp;lt;method_name&amp;gt;&amp;quot;. If the command line is preferred, the following command can be used:&lt;br /&gt;
&lt;br /&gt;
 ruby -Itest test/unit/questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
=== VNC Server Information ===&lt;br /&gt;
&lt;br /&gt;
IP: 152.46.16.122:2&lt;br /&gt;
Password: project517&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
=== Members ===&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
=== Highlights of Project ===&lt;br /&gt;
* Added unit test for questionnaire class&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* The test that uses the &amp;quot;delete&amp;quot; action in the controller was failing due to a potential bug in the questionnaire.rb model class (refer to ''Potential Bug'' section)&lt;br /&gt;
&lt;br /&gt;
=== List of files changed ===&lt;br /&gt;
*test/fixtures/assignment_questionnaires.yml&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/questionnaire_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
= Potential Bug =&lt;br /&gt;
The Questionnaire model class contains a ''has_many :assignment_questionnaire'' that should be ''has_many :assignment_questionnaire'''s''' '' according to convention. Because of this, our &amp;quot;delete&amp;quot; action test fails since it doesn't match the convention. If it is indeed incorrect, then in the &amp;quot;compute_weighted_score&amp;quot; method, the ''self.assignemnt_questionnaire...'' call also needs to be changed to ''self.assignment_questionnaires...''.  This most likely needs to be refactored, but we did not want to do a refactor without testing its effects on the system thoroughly.&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74428</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74428"/>
		<updated>2013-03-29T00:53:29Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Testing - questionnaire */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
[https://github.com/chunsingtsui/expertiza Project Repository]&lt;br /&gt;
&lt;br /&gt;
== QuestionnaireController Functional Tests ==&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing QuestionnaireController Test ===&lt;br /&gt;
&lt;br /&gt;
There were a few tests already written for the Questionnaire controller, but they were not passing initially. We had to modify several things in the test and questionnaire fixture in order to get things up and running. The setup method contained login code that was outdated, and thus each test would result in a redirect due to the invalid login credential. So we looked through other more up to date functional test files and found the correct way to login as a particular user. We also put this code in a helper method so we can call it more easily in different tests if we so choose (more on this in the &amp;quot;Helper Methods&amp;quot; section).&lt;br /&gt;
&lt;br /&gt;
In addition, we added new fixtures to help with our testing. Through debugging the test error messages, we saw that an additional required column called &amp;quot;section&amp;quot; has been added to the &amp;quot;Questionnaire&amp;quot; model, which was contributing to the failures. We added the additional column accordingly in our new fixures.&lt;br /&gt;
&lt;br /&gt;
=== Helper Methods ===&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
We tried to cover as many types of useful cases as possible according to the project requirements. We used assertions that checked counts after create and delete, as well as asserted redirects. We also tested against invalid inputs that could cause errors. All of the tests are self-explanatory based on their method names, and there really is no need to further provide explanations here.&lt;br /&gt;
&lt;br /&gt;
We also attempted to test the private methods of the controller as well at the beginning of the project. However, we couldn't find a way to test many of them due to the fact that they reference the params[] hash in the methods, and we can't isolate the testing of those methods without calling some controller action. Since these private methods are only used by the public controller action methods, we did the next best thing by creating tests for the action methods that would use these private methods to test them indirectly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Controller Test ===&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test. In Rubymine, this can be done by right clicking the test file and selecting &amp;quot;Run &amp;lt;filename&amp;gt;&amp;quot;. Individual tests inside each test file can also be run by right clicking on the test method name and selecting &amp;quot;Run &amp;lt;method_name&amp;gt;&amp;quot;. If the command line is preferred, the following command can be used:&lt;br /&gt;
&lt;br /&gt;
 ruby -Itest test/functional/questionnaire_controller_test.rb&lt;br /&gt;
&lt;br /&gt;
== Questionnaire Subclass Unit Tests ==&lt;br /&gt;
&lt;br /&gt;
The original assignment was to write unit tests for the Questionnaire subclasses:&lt;br /&gt;
* author_feedback_questionnaire.rb&lt;br /&gt;
* metareview_questionnaire.rb&lt;br /&gt;
* review_questionnaire.rb&lt;br /&gt;
* teammate_review_questionnaire.rb&lt;br /&gt;
&lt;br /&gt;
As we reviewed the tests, it was found that most of the methods were trivial (ex: the method &amp;quot;symbol&amp;quot; only only returns a hardcoded symbol).  The only method that was found to be worth testing was the get_weighted_score method.  Upon further review, however, we found that testing this method in each of the four subclasses violated the DRY principle.  Therefore, we moved the test to the superclass (questionnaire.rb) to test the &amp;quot;get_weighted_score&amp;quot; method.&lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing Questionnaire Subclass Tests ===&lt;br /&gt;
&lt;br /&gt;
There was only one test in the existing unit test for AuthorFeedbackQuestionnaire for testing the &amp;quot;get_weighted_score&amp;quot; method. Even though the test could be run, we found that it was not making assertions as it should. The files for the other 3 questionnaire subclasses were created to complete the testing on them.  As stated earlier, we then found that testing the &amp;quot;get_weighted_score&amp;quot; method was redundant, violating the DRY principle, so it was moved to the superclass (Questionnaire).  The tests in the subclasses were deleted (since they were redundant) but the test files were kept for future testing of the subclass if/when needed.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
The method &amp;quot;compute_weighted_score&amp;quot; from the the base class was the only thing that needed to be tested (due to the above reason). Based on an average, it was asserted that the value was not nil and that the value was computed correctly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;compute_weighted_score&amp;quot; test can be found in the Questionnaire Unit Test file. Same as directions for the functional test, this can be run in either Rubymine or the command line. In Rubymine, this can be done by right clicking the test file and selecting &amp;quot;Run &amp;lt;filename&amp;gt;&amp;quot;. Individual tests inside each test file can also be run by right clicking on the test method name and selecting &amp;quot;Run &amp;lt;method_name&amp;gt;&amp;quot;. If the command line is preferred, the following command can be used:&lt;br /&gt;
&lt;br /&gt;
 ruby -Itest test/unit/questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
=== VNC Server Information ===&lt;br /&gt;
&lt;br /&gt;
IP: 152.46.16.122:2&lt;br /&gt;
Password: project517&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
=== Members ===&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
=== Highlights of Project ===&lt;br /&gt;
* Added unit test for questionnaire class&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* The test that uses the &amp;quot;delete&amp;quot; action in the controller was failing due to a potential bug in the questionnaire.rb model class (refer to ''Potential Bug'' section)&lt;br /&gt;
&lt;br /&gt;
=== List of files changed ===&lt;br /&gt;
*test/fixtures/assignment_questionnaires.yml&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/questionnaire_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
= Potential Bug =&lt;br /&gt;
The Questionnaire model class contains a ''has_many :assignment_questionnaire'' that should be ''has_many :assignment_questionnaire'''s''' '' according to convention. Because of this, our &amp;quot;delete&amp;quot; action test fails since it doesn't match the convention. If it is indeed incorrect, then in the &amp;quot;compute_weighted_score&amp;quot; method, the ''self.assignemnt_questionnaire...'' call also needs to be changed to ''self.assignment_questionnaires...''.  This most likely needs to be refactored, but we did not want to do a refactor without testing its effects on the system thoroughly.&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74335</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=74335"/>
		<updated>2013-03-21T14:58:43Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Testing - questionnaire */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/document/d/1SVNx7Eh6dUdrz2a9rGSgBxLkxAmuBUm8eu08nHEdjIg/edit# Project Description]&lt;br /&gt;
&lt;br /&gt;
== QuestionnaireController Functional Tests ==&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing QuestionnaireController Test ===&lt;br /&gt;
&lt;br /&gt;
There were a few tests already written for the Questionnaire controller, but they were not passing initially. We had to modify several things in the test and questionnaire fixture in order to get things up and running. The setup method contained login code that was outdated, and thus each test would result in a redirect due to the invalid login credential. So we looked through other more up to date functional test files and found the correct way to login as a particular user. We also put this code in a helper method so we can call it more easily in different tests if we so choose (more on this in the &amp;quot;Helper Methods&amp;quot; section).&lt;br /&gt;
&lt;br /&gt;
In addition, we added new fixtures to help with our testing. Through debugging the test error messages, we saw that an additional required column called &amp;quot;section&amp;quot; has been added to the &amp;quot;Questionnaire&amp;quot; model, which was contributing to the failures. We added the additional column accordingly in our new fixures.&lt;br /&gt;
&lt;br /&gt;
=== Helper Methods ===&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
We tried to cover as many types of useful cases as possible according to the project requirements. We used assertions that checked counts after create and delete, as well as asserted redirects. We also tested against invalid inputs that could cause errors. All of the tests are self-explanatory based on their method names, and there really is no need to further provide explanations here.&lt;br /&gt;
&lt;br /&gt;
We also attempted to test the private methods of the controller as well at the beginning of the project. However, we couldn't find a way to test many of them due to the fact that they reference the params[] hash in the methods, and we can't isolate the testing of those methods without calling some controller action. Since these private methods are only used by the public controller action methods, we did the next best thing by creating tests for the action methods that would use these private methods to test them indirectly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Controller Test ===&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test. In Rubymine, this can be done by right clicking the test file and selecting &amp;quot;Run &amp;lt;filename&amp;gt;&amp;quot;. Individual tests inside each test file can also be run by right clicking on the test method name and selecting &amp;quot;Run &amp;lt;method_name&amp;gt;&amp;quot;. If the command line is preferred, the following command can be used:&lt;br /&gt;
&lt;br /&gt;
 ruby -Itest test/functional/questionnaire_controller_test.rb&lt;br /&gt;
&lt;br /&gt;
== Questionnaire Subclass Unit Tests ==&lt;br /&gt;
&lt;br /&gt;
The original assignment was to write unit tests for the Questionnaire subclasses:&lt;br /&gt;
* author_feedback_questionnaire.rb&lt;br /&gt;
* metareview_questionnaire.rb&lt;br /&gt;
* review_questionnaire.rb&lt;br /&gt;
* teammate_review_questionnaire.rb&lt;br /&gt;
&lt;br /&gt;
As we reviewed the tests, it was found that most of the methods were trivial (ex: the method &amp;quot;symbol&amp;quot; only only returns a hardcoded symbol).  The only method that was found to be worth testing was the get_weighted_score method.  Upon further review, however, we found that testing this method in each of the four subclasses violated the DRY principle.  Therefore, we moved the test to the superclass (questionnaire.rb) to test the &amp;quot;get_weighted_score&amp;quot; method.&lt;br /&gt;
&lt;br /&gt;
=== Modifications to Existing Questionnaire Subclass Tests ===&lt;br /&gt;
&lt;br /&gt;
There was only one test in the existing unit test for AuthorFeedbackQuestionnaire for testing the &amp;quot;get_weighted_score&amp;quot; method. Even though the test could be run, we found that it was not making assertions as it should. The files for the other 3 questionnaire subclasses were created to complete the testing on them.  As stated earlier, we then found that testing the &amp;quot;get_weighted_score&amp;quot; method was redundant, violating the DRY principle, so it was moved to the superclass (Questionnaire).  The tests in the subclasses were deleted (since they were redundant) but the test files were kept for future testing of the subclass if/when needed.&lt;br /&gt;
&lt;br /&gt;
=== Test Coverage ===&lt;br /&gt;
&lt;br /&gt;
The method &amp;quot;compute_weighted_score&amp;quot; from the the base class was the only thing that needed to be tested (due to the above reason). Based on an average, it was asserted that the value was not nil and that the value was computed correctly.&lt;br /&gt;
&lt;br /&gt;
=== How to Run Unit Tests ===&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;compute_weighted_score&amp;quot; test can be found in the Questionnaire Unit Test file. Same as directions for the functional test, this can be run in either Rubymine or the command line. In Rubymine, this can be done by right clicking the test file and selecting &amp;quot;Run &amp;lt;filename&amp;gt;&amp;quot;. Individual tests inside each test file can also be run by right clicking on the test method name and selecting &amp;quot;Run &amp;lt;method_name&amp;gt;&amp;quot;. If the command line is preferred, the following command can be used:&lt;br /&gt;
&lt;br /&gt;
 ruby -Itest test/unit/questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
=== VNC Server Information ===&lt;br /&gt;
&lt;br /&gt;
IP: 152.46.16.122:2&lt;br /&gt;
Password: project517&lt;br /&gt;
&lt;br /&gt;
== General Information ==&lt;br /&gt;
&lt;br /&gt;
=== Members ===&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
=== Highlights of Project ===&lt;br /&gt;
* Added unit test for questionnaire class&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* The test that uses the &amp;quot;delete&amp;quot; action in the controller was failing due to a potential bug in the questionnaire.rb model class (refer to ''Potential Bug'' section)&lt;br /&gt;
&lt;br /&gt;
=== List of files changed ===&lt;br /&gt;
*test/fixtures/assignment_questionnaires.yml&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/questionnaire_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;br /&gt;
&lt;br /&gt;
= Potential Bug =&lt;br /&gt;
The Questionnaire model class contains a ''has_many :assignment_questionnaire'' that should be ''has_many :assignment_questionnaire'''s''' '' according to convention. Because of this, our &amp;quot;delete&amp;quot; action test fails since it doesn't match the convention. If it is indeed incorrect, then in the &amp;quot;compute_weighted_score&amp;quot; method, the ''self.assignemnt_questionnaire...'' call also needs to be changed to ''self.assignment_questionnaires...''.  This most likely needs to be refactored, but we did not want to do a refactor without testing its effects on the system thoroughly.&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73997</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73997"/>
		<updated>2013-03-20T04:27:04Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Summary of Project */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
== Summary of Project ==&lt;br /&gt;
&lt;br /&gt;
=== QuestionnaireController Functional Tests ===&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
===== Helper Methods =====&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
===== Test Coverage =====&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
===== How to Run Controller Test =====&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test.&lt;br /&gt;
&lt;br /&gt;
== List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73996</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73996"/>
		<updated>2013-03-20T04:26:44Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Summary of Project */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
== Summary of Project ==&lt;br /&gt;
&lt;br /&gt;
=== QuestionnaireController Functional Tests ===&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
===== Helper Methods =====&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
==== Test Coverage ====&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
==== How to Run Controller Test ====&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test.&lt;br /&gt;
&lt;br /&gt;
== List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73995</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73995"/>
		<updated>2013-03-20T04:26:04Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Summary of Project */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
== Summary of Project ==&lt;br /&gt;
&lt;br /&gt;
=== QuestionnaireController Functional Tests ===&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
==== Helper Methods ====&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
==== Test Coverage ====&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
==== How to Run Controller Test ====&lt;br /&gt;
&lt;br /&gt;
All of the QuestionnaireController tests are in the questionnaire_controller_test.rb file and can be run as a standard functional test.&lt;br /&gt;
&lt;br /&gt;
== List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73994</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73994"/>
		<updated>2013-03-20T04:24:14Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Test Coverage */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
== Summary of Project ==&lt;br /&gt;
&lt;br /&gt;
=== QuestionnaireController Functional Tests ===&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
==== Helper Methods ====&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
==== Test Coverage ====&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
* Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
* Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
* Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
== List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73993</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73993"/>
		<updated>2013-03-20T04:24:02Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Test Coverage */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
== Summary of Project ==&lt;br /&gt;
&lt;br /&gt;
=== QuestionnaireController Functional Tests ===&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
==== Helper Methods ====&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
==== Test Coverage ====&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
*1. Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
*2. Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
*3. Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
== List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73992</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73992"/>
		<updated>2013-03-20T04:23:36Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Helper Methods */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
== Summary of Project ==&lt;br /&gt;
&lt;br /&gt;
=== QuestionnaireController Functional Tests ===&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
==== Helper Methods ====&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. There seem to be sufficient fixtures for all of the different user types currently, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
==== Test Coverage ====&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
1. Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
2. Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
3. Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
== List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73991</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73991"/>
		<updated>2013-03-20T04:22:32Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Helper Methods */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
== Summary of Project ==&lt;br /&gt;
&lt;br /&gt;
=== QuestionnaireController Functional Tests ===&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
==== Helper Methods ====&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup method assigns three special instance variables that are associated with the ActionController::TestCase (@controller, @request, and @response). In addition to setting these, the @Questionnaire variable is assigned to id of the :questionnaire5 fixture. This is the default questionnaire fixture that is used in a lot of the test cases. You will also notice that the fixture :admin is being logged in as the default user. This is done so that you don't have explicitly log into a user account for each test case. The only time you would need to call the method to log in for a particular test case is if you needed to test something specifically in another user type's account (for example a student account as is done in test_create_questionnaire_with_student.&lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The login_user method was created to &amp;quot;DRY out&amp;quot; the code associated with logging a user in. The method signature is:&lt;br /&gt;
&lt;br /&gt;
   def login_user(userType)&lt;br /&gt;
&lt;br /&gt;
If you pass a symbol of the user type you would like to use for a test, it will log in as that user type for the particular test. The parameter name is userType and assumes that there is a fixture with the name of the user type. This is true of the current user types, but additional fixtures may need to be created in the future if new user types are added to the system.&lt;br /&gt;
&lt;br /&gt;
==== Test Coverage ====&lt;br /&gt;
&lt;br /&gt;
There are three main types of tests that we created. &lt;br /&gt;
&lt;br /&gt;
1. Happy path tests of the controller's public methods. There is a test for every method. (ex: test_new)&lt;br /&gt;
2. Sad path tests that passed invalid parameters. (ex: test_edit_questionnaire_with_lower_max_score_than_min)&lt;br /&gt;
3. Sad path tests that tried to do things with insufficient access level. (ex: test_create_questionnaire_with_student)&lt;br /&gt;
&lt;br /&gt;
== List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73989</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73989"/>
		<updated>2013-03-20T04:01:58Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Helper Methods */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
== Summary of Project ==&lt;br /&gt;
&lt;br /&gt;
=== QuestionnaireController Functional Tests ===&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
==== Helper Methods ====&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The setup method &lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73988</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73988"/>
		<updated>2013-03-20T04:01:46Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Helper Methods */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
== Summary of Project ==&lt;br /&gt;
&lt;br /&gt;
=== QuestionnaireController Functional Tests ===&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
==== Helper Methods ====&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The setup method &lt;br /&gt;
&lt;br /&gt;
  def setup&lt;br /&gt;
    @controller = QuestionnaireController.new&lt;br /&gt;
    @request = ActionController::TestRequest.new&lt;br /&gt;
    @response = ActionController::TestResponse.new&lt;br /&gt;
    @Questionnaire = questionnaires(:questionnaire5).id&lt;br /&gt;
&lt;br /&gt;
    login_user(:admin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
== List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73987</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73987"/>
		<updated>2013-03-20T04:00:43Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* questionnaire_controller_test */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
== Summary of Project ==&lt;br /&gt;
&lt;br /&gt;
=== QuestionnaireController Functional Tests ===&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
==== Helper Methods ====&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup&lt;br /&gt;
&lt;br /&gt;
== List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73985</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73985"/>
		<updated>2013-03-20T04:00:21Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* questionnaire_controller_test */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
== Summary of Project ==&lt;br /&gt;
&lt;br /&gt;
=== questionnaire_controller_test ===&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. &lt;br /&gt;
&lt;br /&gt;
==== Helper Methods ====&lt;br /&gt;
&lt;br /&gt;
There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
The setup&lt;br /&gt;
&lt;br /&gt;
== List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73984</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73984"/>
		<updated>2013-03-20T03:59:34Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* questionnaire_controller_test */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
== Summary of Project ==&lt;br /&gt;
&lt;br /&gt;
=== questionnaire_controller_test ===&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided by Rails. There are two methods that assist in setting up and running the tests:&lt;br /&gt;
&lt;br /&gt;
*The setup method&lt;br /&gt;
*The login_user method&lt;br /&gt;
&lt;br /&gt;
==== setup ====&lt;br /&gt;
&lt;br /&gt;
== List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73983</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73983"/>
		<updated>2013-03-20T03:57:47Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* questionnaire_controller_test */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
== Summary of Project ==&lt;br /&gt;
&lt;br /&gt;
=== questionnaire_controller_test ===&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the [http://api.rubyonrails.org/classes/ActionController/TestCase.html ActionController::TestCase] framework provided in Rails.&lt;br /&gt;
&lt;br /&gt;
== List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73982</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73982"/>
		<updated>2013-03-20T03:56:39Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Testing - questionnaire */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
== Summary of Project ==&lt;br /&gt;
&lt;br /&gt;
=== questionnaire_controller_test ===&lt;br /&gt;
&lt;br /&gt;
The majority of the tests implemented in this project are functional tests of the questionnaire controller. The test cases were written using the &amp;lt;a href=&amp;quot;http://api.rubyonrails.org/classes/ActionController/TestCase.html&amp;quot;&amp;gt;ActionController::TestCas&amp;lt;/a&amp;gt; framework provided in Rails.&lt;br /&gt;
&lt;br /&gt;
== List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73981</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73981"/>
		<updated>2013-03-20T03:47:28Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Summary of Project */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Highlights of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
== List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73980</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73980"/>
		<updated>2013-03-20T03:47:09Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* =List of files changed */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Summary of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
== List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73979</id>
		<title>CSC/ECE 517 Spring 2013/OSS E605B</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/OSS_E605B&amp;diff=73979"/>
		<updated>2013-03-20T03:44:57Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Highlights */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Testing - questionnaire=&lt;br /&gt;
==Members==&lt;br /&gt;
*Jonathan Wills - jrwills2&lt;br /&gt;
*Chun Sing Tsui - ctsui&lt;br /&gt;
*Travis Folsom - twfolsom&lt;br /&gt;
&lt;br /&gt;
== Summary of Project ==&lt;br /&gt;
* Added unit tests for questionnaire subclasses&lt;br /&gt;
* Added functional tests for questionnaire controller&lt;br /&gt;
* 24 total tests&lt;br /&gt;
* Delete test was failing due to a potential bug in the questionnaire controller&lt;br /&gt;
&lt;br /&gt;
===List of files changed ==&lt;br /&gt;
*test/fixtures/question_types.yml&lt;br /&gt;
*test/fixtures/questionnaires.yml&lt;br /&gt;
*test/fixtures/questions.yml&lt;br /&gt;
*test/functional/questionnaire_controller_test.rb&lt;br /&gt;
*test/unit/author_feedback_questionnaire_test.rb&lt;br /&gt;
*test/unit/metareview_questionnaire_test.rb&lt;br /&gt;
*test/unit/review_questionnaire_test.rb&lt;br /&gt;
*test/unit/teammate_review_questionnaire_test.rb&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73766</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73766"/>
		<updated>2013-02-26T04:31:26Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Programming Language API */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language. &amp;lt;ref name=&amp;quot;mp1&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Metaprogramming&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself. &amp;lt;ref name=&amp;quot;ref1&amp;quot;&amp;gt;https://blogs.oracle.com/java/entry/metaprogramming_manipulating_data_about_data&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String[] args) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework. &amp;lt;ref name=&amp;quot;pm1&amp;quot;&amp;gt;http://www.program-transformation.org/Transform/ProgramMigration&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion.&amp;lt;ref name=&amp;quot;boiler&amp;quot;&amp;gt;K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&amp;lt;/ref&amp;gt; This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&amp;lt;ref name=&amp;quot;meta_java&amp;quot;&amp;gt;Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73757</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73757"/>
		<updated>2013-02-26T04:18:54Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* What is metaprogramming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language. &amp;lt;ref name=&amp;quot;mp1&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Metaprogramming&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String[] args) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework. &amp;lt;ref name=&amp;quot;pm1&amp;quot;&amp;gt;http://www.program-transformation.org/Transform/ProgramMigration&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion.&amp;lt;ref name=&amp;quot;boiler&amp;quot;&amp;gt;K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&amp;lt;/ref&amp;gt; This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&amp;lt;ref name=&amp;quot;meta_java&amp;quot;&amp;gt;Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73756</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73756"/>
		<updated>2013-02-26T04:18:30Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* What is metaprogramming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language. &amp;lt;ref name=&amp;quot;mp1&amp;quot;&amp;gt;http://en.wikipedia.org/wiki/Metaprogramming&amp;lt;/ref1&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String[] args) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework. &amp;lt;ref name=&amp;quot;pm1&amp;quot;&amp;gt;http://www.program-transformation.org/Transform/ProgramMigration&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion.&amp;lt;ref name=&amp;quot;boiler&amp;quot;&amp;gt;K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&amp;lt;/ref&amp;gt; This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&amp;lt;ref name=&amp;quot;meta_java&amp;quot;&amp;gt;Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73747</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73747"/>
		<updated>2013-02-26T03:46:53Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Programming Language API */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String[] args) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework. &amp;lt;ref name=&amp;quot;pm1&amp;quot;&amp;gt;http://www.program-transformation.org/Transform/ProgramMigration&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion.&amp;lt;ref name=&amp;quot;boiler&amp;quot;&amp;gt;K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&amp;lt;/ref&amp;gt; This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&amp;lt;ref name=&amp;quot;meta_java&amp;quot;&amp;gt;Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73741</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73741"/>
		<updated>2013-02-26T03:28:13Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Program transformation system */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Metaprogramming Metaprogramming] refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of [http://en.wikipedia.org/wiki/Reflection_(computer_programming) reflection] if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed languages] have some advantages over [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing statically typed languages] when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an [http://en.wikipedia.org/wiki/API API] that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework. &amp;lt;ref name=&amp;quot;pm1&amp;quot;&amp;gt;http://www.program-transformation.org/Transform/ProgramMigration&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&amp;lt;ref name=&amp;quot;use1&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion.&amp;lt;ref name=&amp;quot;boiler&amp;quot;&amp;gt;K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&amp;lt;/ref&amp;gt; This mini-language will then be converted into your regular source code language before compiling.&amp;lt;ref name=&amp;quot;use2&amp;quot;&amp;gt;Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
[http://en.wikipedia.org/wiki/Reflection_(computer_programming) Reflection] is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&amp;lt;ref name=&amp;quot;meta_java&amp;quot;&amp;gt;Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Generics_in_Java Generics] are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated.&amp;lt;ref name=&amp;quot;reflection&amp;quot;&amp;gt;Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73206</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73206"/>
		<updated>2013-02-20T00:02:55Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Metaprogramming in statically typed languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of ''reflection'' if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages, however dynamically typed languages have some advantages over statically typed languages when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an API that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
Reflection is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&lt;br /&gt;
&lt;br /&gt;
[2] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&lt;br /&gt;
&lt;br /&gt;
[3] Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&lt;br /&gt;
&lt;br /&gt;
[4] Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&lt;br /&gt;
&lt;br /&gt;
[5] K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;br /&gt;
&lt;br /&gt;
[6] http://www.program-transformation.org&lt;br /&gt;
&lt;br /&gt;
[7] http://www.codeproject.com/Articles/15429/Ruby-Add-class-methods-at-runtime&lt;br /&gt;
&lt;br /&gt;
[8] http://docs.oracle.com/javase/tutorial/reflect/index.html&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73198</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73198"/>
		<updated>2013-02-19T03:34:09Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of ''reflection'' if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages. Dynamically type languages have some advantages over statically typed languages when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an API that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
Reflection is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&lt;br /&gt;
&lt;br /&gt;
[2] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&lt;br /&gt;
&lt;br /&gt;
[3] Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&lt;br /&gt;
&lt;br /&gt;
[4] Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&lt;br /&gt;
&lt;br /&gt;
[5] K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;br /&gt;
&lt;br /&gt;
[6] http://www.program-transformation.org&lt;br /&gt;
&lt;br /&gt;
[7] http://www.codeproject.com/Articles/15429/Ruby-Add-class-methods-at-runtime&lt;br /&gt;
&lt;br /&gt;
[8] http://docs.oracle.com/javase/tutorial/reflect/index.html&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73197</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73197"/>
		<updated>2013-02-19T03:33:54Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of ''reflection'' if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages. Dynamically type languages have some advantages over statically typed languages when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an API that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
Reflection is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&lt;br /&gt;
&lt;br /&gt;
[2] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&lt;br /&gt;
&lt;br /&gt;
[3] Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&lt;br /&gt;
&lt;br /&gt;
[4] Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&lt;br /&gt;
&lt;br /&gt;
[5] K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;br /&gt;
&lt;br /&gt;
[6] www.program-transformation.org&lt;br /&gt;
&lt;br /&gt;
[7] http://www.codeproject.com/Articles/15429/Ruby-Add-class-methods-at-runtime&lt;br /&gt;
&lt;br /&gt;
[8] http://docs.oracle.com/javase/tutorial/reflect/index.html&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73196</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73196"/>
		<updated>2013-02-19T03:31:22Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of ''reflection'' if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages. Dynamically type languages have some advantages over statically typed languages when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an API that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
Reflection is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&lt;br /&gt;
&lt;br /&gt;
[2] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&lt;br /&gt;
&lt;br /&gt;
[3] Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&lt;br /&gt;
&lt;br /&gt;
[4] Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&lt;br /&gt;
&lt;br /&gt;
[5] K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73195</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73195"/>
		<updated>2013-02-19T03:30:38Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of ''reflection'' if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages. Dynamically type languages have some advantages over statically typed languages when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an API that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming using Scheme===&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
Reflection is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&lt;br /&gt;
&lt;br /&gt;
[2] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&lt;br /&gt;
&lt;br /&gt;
[3] Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&lt;br /&gt;
&lt;br /&gt;
[4] Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&lt;br /&gt;
&lt;br /&gt;
[5] K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73194</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73194"/>
		<updated>2013-02-19T03:30:23Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Metaprogramming in statically typed languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of ''reflection'' if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages. Dynamically type languages have some advantages over statically typed languages when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
Accomplishing something like this in a statically typed language would be much more difficult or impossible depending on the language. It is for this reason that dynamically typed languages are usually viewed as being better equipped to handle metaprogramming overall.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an API that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming using Scheme===&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
Reflection is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=Limitations=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&lt;br /&gt;
&lt;br /&gt;
[2] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&lt;br /&gt;
&lt;br /&gt;
[3] Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&lt;br /&gt;
&lt;br /&gt;
[4] Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&lt;br /&gt;
&lt;br /&gt;
[5] K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73193</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73193"/>
		<updated>2013-02-19T03:20:52Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Metaprogramming in statically typed languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of ''reflection'' if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages. Dynamically type languages have some advantages over statically typed languages when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Sample code in Ruby to perform this task is outlined below. First is a definition of a Ruby class called TestAddMethodAtRuntime:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
The code below created a new instance of the class. After that, it defines a new method called newlyAddedMethod. Then the original method is called, follow by the newly added method at runtime:&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
Below is the output of the code above:&lt;br /&gt;
&lt;br /&gt;
  Original Method&lt;br /&gt;
  Newly Added Method&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an API that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming using Scheme===&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
Reflection is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=Limitations=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&lt;br /&gt;
&lt;br /&gt;
[2] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&lt;br /&gt;
&lt;br /&gt;
[3] Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&lt;br /&gt;
&lt;br /&gt;
[4] Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&lt;br /&gt;
&lt;br /&gt;
[5] K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73192</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73192"/>
		<updated>2013-02-19T03:12:59Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Metaprogramming in statically typed languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of ''reflection'' if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages. Dynamically type languages have some advantages over statically typed languages when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Below is the sample code in Ruby to perform this task:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an API that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming using Scheme===&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
Reflection is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=Limitations=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&lt;br /&gt;
&lt;br /&gt;
[2] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&lt;br /&gt;
&lt;br /&gt;
[3] Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&lt;br /&gt;
&lt;br /&gt;
[4] Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&lt;br /&gt;
&lt;br /&gt;
[5] K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73191</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73191"/>
		<updated>2013-02-19T03:12:34Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Metaprogramming in statically typed languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of ''reflection'' if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages. Dynamically type languages have some advantages over statically typed languages when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Below is the sample code in Ruby to perform this task:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
t = TestAddMethodAtRuntime.new&lt;br /&gt;
&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an API that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming using Scheme===&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
Reflection is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=Limitations=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&lt;br /&gt;
&lt;br /&gt;
[2] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&lt;br /&gt;
&lt;br /&gt;
[3] Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&lt;br /&gt;
&lt;br /&gt;
[4] Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&lt;br /&gt;
&lt;br /&gt;
[5] K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73190</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73190"/>
		<updated>2013-02-19T03:12:07Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Metaprogramming in statically typed languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of ''reflection'' if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages. Dynamically type languages have some advantages over statically typed languages when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime. Below is the sample code in Ruby to perform this task:&lt;br /&gt;
&lt;br /&gt;
  class TestAddMethodAtRuntime&lt;br /&gt;
    def originalMethod&lt;br /&gt;
        puts &amp;quot;Original Method&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  t = TestAddMethodAtRuntime.new&lt;br /&gt;
&lt;br /&gt;
  def t.newlyAddedMethod&lt;br /&gt;
    puts &amp;quot;Newly Added Method&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  t.originalMethod&lt;br /&gt;
  puts &amp;quot;\n&amp;quot;&lt;br /&gt;
  t.newlyAddedMethod&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an API that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming using Scheme===&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
Reflection is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=Limitations=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
[1] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 1: Introduction to metaprogramming&amp;quot;, 20 Oct 2005   http://www.ibm.com/developerworks/library/l-metaprog1/index.html#N10052&lt;br /&gt;
&lt;br /&gt;
[2] Jonathan Bartlett, &amp;quot;The art of metaprogramming, Part 2: Metaprogramming using Scheme&amp;quot;, 02 May 2006  http://www.ibm.com/developerworks/linux/library/l-metaprog2/index.html&lt;br /&gt;
&lt;br /&gt;
[3] Abdelmonaim Remani, &amp;quot;The Art of Metaprogramming in Java&amp;quot;, Jul 19, 2012  http://www.slideshare.net/PolymathicCoder/the-art-of-metaprogramming-in-java&lt;br /&gt;
&lt;br /&gt;
[4] Mika Haapakorpi, &amp;quot;Meta Programming In Java&amp;quot;&lt;br /&gt;
&lt;br /&gt;
[5] K. Czarnecki, &amp;quot;Generative Programming&amp;quot; chapter 8, Static Metaprogramming in C++&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73188</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73188"/>
		<updated>2013-02-19T02:14:38Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Metaprogramming in statically typed languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of ''reflection'' if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming can be accomplished in both statically and dynamically typed languages. Dynamically type languages have some advantages over statically typed languages when it comes to metaprogramming.&lt;br /&gt;
&lt;br /&gt;
One example of a metaprogramming task is to add a method to a particular class at runtime.&lt;br /&gt;
&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an API that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming using Scheme===&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
Reflection is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=Limitations=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73151</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73151"/>
		<updated>2013-02-18T04:46:12Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Program transformation system */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of ''reflection'' if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an API that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
In the case of the Java compiler, the program input is being output into another program in a different &amp;quot;language&amp;quot;. There are other types of transformation systems that take a program as an input and output a different program in the same language. One example of this would be ''program migration'' which migrates source code to a newer or older version of the same language. This can be useful if upgrading a software system to run on a newer version of a framework.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming using Scheme===&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
Reflection is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=Limitations=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73148</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73148"/>
		<updated>2013-02-18T03:18:12Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Program transformation system */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of ''reflection'' if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an API that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
&lt;br /&gt;
A program transformation system is something that takes a program as the input, and outputs a different program. One example of a program transformation system is the Java compiler. The Java compiler takes a Java program as its input, and outputs a .class file with platform independent bytecode in it. Another example of a program transformation system is a Java decompiler, which takes bytecode as an input and outputs a Java program.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming using Scheme===&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
Reflection is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=Limitations=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73146</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73146"/>
		<updated>2013-02-18T02:44:03Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Programming Language API */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of ''reflection'' if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an API that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
===Metaprogramming using Scheme===&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
Reflection is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=Limitations=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73145</id>
		<title>CSC/ECE 517 Spring 2013/ch1 1h jc</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2013/ch1_1h_jc&amp;diff=73145"/>
		<updated>2013-02-18T02:39:52Z</updated>

		<summary type="html">&lt;p&gt;Jrwills2: /* Programming Language API */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Metaprogramming in statically typed languages&lt;br /&gt;
=Metaprogramming in statically typed languages=&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is metaprogramming===&lt;br /&gt;
&lt;br /&gt;
Metaprogramming refers to writing programs that can manipulate other programs or themselves. The program that is doing the manipulating is called the ''metaprogram'', and the program that is being manipulated is called the ''object program''. The language of the ''metaprogram'' is referred to as the ''metalanguage'' and the language of the object program is called the ''object language''. A language has the ability of ''reflection'' if it can be used to write a metaprogram that can manipulate a program written in the same language.&lt;br /&gt;
&lt;br /&gt;
===Metaprogramming in statically typed languages===&lt;br /&gt;
==Implementation==&lt;br /&gt;
===Programming Language API===&lt;br /&gt;
&lt;br /&gt;
One way that metaprogramming can be possible in a language is through an API that helps programmers achieve a metaprogramming task. An example of this is a language that provides an API for reflection. One type of reflection is ''introspection'' in which the program can access the source code of the program itself.&lt;br /&gt;
&lt;br /&gt;
Below is an example from the Java API to illustrate how an API can be used to achieve some metaprogramming tasks:&lt;br /&gt;
&lt;br /&gt;
   import java.lang.reflect.*; &lt;br /&gt;
   public class DumpMethods {&lt;br /&gt;
   public static void main(String args[]) {&lt;br /&gt;
     	try {&lt;br /&gt;
           Class c = Class.forName(args[0]);&lt;br /&gt;
           Method m[] = c.getDeclaredMethods();&lt;br /&gt;
           for (int i = 0; i &amp;lt; m.length; i++) {&lt;br /&gt;
              System.out.println(m[i].toString());&lt;br /&gt;
           }&lt;br /&gt;
     	}&lt;br /&gt;
     	catch (Throwable e) {&lt;br /&gt;
        	System.err.println(e);&lt;br /&gt;
     	}&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The above example prints out all of the methods that are declared for a particular class. This is an example of metaprogramming since the program is is accessing the source code of itself.&lt;br /&gt;
&lt;br /&gt;
===Program transformation system===&lt;br /&gt;
===Metaprogramming using Scheme===&lt;br /&gt;
==Common Uses==&lt;br /&gt;
===Pre-generate static data at compile time===&lt;br /&gt;
One common use of metaprogramming in statically typed languages is to write programs that will pre-generate tables of data for use at runtime.&lt;br /&gt;
&lt;br /&gt;
One simple but useful code generator is to build static lookup tables. Often, in order to build fast functions in C programming, we simply create a lookup table of all of the answers. This means that we either need to pre-compute them by hand (which is wasteful of your time) or build them at runtime (which is wasteful of the user's time).&lt;br /&gt;
&lt;br /&gt;
In following example we will build a generator that will take a function or set of functions on an integer and build lookup tables for the answer.&lt;br /&gt;
&lt;br /&gt;
To think of how to make such a program, we can start from the end and work backward. Firstly we need a lookup table that will return square roots of numbers between 5 and 20. A simple program can be written to generate such a table like this:&lt;br /&gt;
&lt;br /&gt;
Generate and use a lookup table of square roots&lt;br /&gt;
&lt;br /&gt;
        /* our lookup table */&lt;br /&gt;
        double square_roots[21];&lt;br /&gt;
        /* function to load the table at runtime */&lt;br /&gt;
        void init_square_roots()&lt;br /&gt;
        {&lt;br /&gt;
           int i;&lt;br /&gt;
           for(i = 5; i &amp;lt; 21; i++)&lt;br /&gt;
             {&lt;br /&gt;
               square_roots[i] = sqrt((double)i);&lt;br /&gt;
             }&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
With that single macro, we can take away a lot of work for any program that has to generate mathematical tables indexed by integer. A little extra work would also allow tables containing full struct definitions; a little more would ensure that space isn't wasted at the front of the array with useless empty entries.&lt;br /&gt;
        /* program that uses the table */&lt;br /&gt;
        int main ()&lt;br /&gt;
        {&lt;br /&gt;
           init_square_roots();&lt;br /&gt;
           printf(&amp;quot;The square root of 5 is %f\n&amp;quot;, square_roots[5]);&lt;br /&gt;
           return 0;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
Now, to convert this to a statically initialized array, you would remove the first part of the program and replace it with something like this, calculated by hand:&lt;br /&gt;
&lt;br /&gt;
Square root program with a static lookup table&lt;br /&gt;
&lt;br /&gt;
        double square_roots[] = {&lt;br /&gt;
           /* these are the ones we skipped */ 0.0, 0.0, 0.0, 0.0, 0.0&lt;br /&gt;
           2.236068, /* Square root of 5 */&lt;br /&gt;
           2.449490, /* Square root of 6 */&lt;br /&gt;
           2.645751, /* Square root of 7 */&lt;br /&gt;
           2.828427, /* Square root of 8 */&lt;br /&gt;
           3.0, /* Square root of 9 */&lt;br /&gt;
           ...&lt;br /&gt;
           4.472136 /* Square root of 20 */&lt;br /&gt;
           };&lt;br /&gt;
&lt;br /&gt;
What is needed is a program that will produce these values and print them out in a table like the previous one so they are loaded in at compile-time.&lt;br /&gt;
&lt;br /&gt;
Code generator for the table macro&lt;br /&gt;
&lt;br /&gt;
           #!/usr/bin/perl  &lt;br /&gt;
           #&lt;br /&gt;
           #tablegen.pl &lt;br /&gt;
           #&lt;br /&gt;
           ##Puts each program line into $line&lt;br /&gt;
           while(my $line = &amp;lt;&amp;gt;)&lt;br /&gt;
           {&lt;br /&gt;
             #Is this a macro invocation?&lt;br /&gt;
             if($line =~ m/TABLE:/)&lt;br /&gt;
             {&lt;br /&gt;
                #If so, split it apart into its component pieces&lt;br /&gt;
                my ($dummy, $table_name, $type, $start_idx, $end_idx, $default, $procedure) = split(m/:/, $line, 7);&lt;br /&gt;
                #The main difference between C and Perl for mathematical expressions is that&lt;br /&gt;
                #Perl prefixes its variables with a dollar sign, so we will add that here&lt;br /&gt;
                $procedure =~ s/VAL/\$VAL/g;&lt;br /&gt;
                #Print out the array declaration&lt;br /&gt;
                print &amp;quot;${type} ${table_name} [] = {\n&amp;quot;;&lt;br /&gt;
                #Go through each array element&lt;br /&gt;
                foreach my $VAL (0 .. $end_idx)&lt;br /&gt;
                {&lt;br /&gt;
                   #Only process an answer if we have reached our starting index&lt;br /&gt;
                   if($VAL &amp;gt;= $start_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      #evaluate the procedure specified (this sets $@ if there are any errors)&lt;br /&gt;
                      $result = eval $procedure;&lt;br /&gt;
                      die(&amp;quot;Error processing: $@&amp;quot;) if $@;&lt;br /&gt;
                   }&lt;br /&gt;
                   else&lt;br /&gt;
                   {&lt;br /&gt;
                      #if we haven't reached the starting index, just use the default&lt;br /&gt;
                      $result = $default;&lt;br /&gt;
                   }&lt;br /&gt;
                   #Print out the value&lt;br /&gt;
                   print &amp;quot;\t${result}&amp;quot;;&lt;br /&gt;
                   #If there are more to be processed, add a comma after the value&lt;br /&gt;
                   if($VAL != $end_idx)&lt;br /&gt;
                   {&lt;br /&gt;
                      print &amp;quot;,&amp;quot;;&lt;br /&gt;
                   }&lt;br /&gt;
                   print &amp;quot;\n&amp;quot;&lt;br /&gt;
                }&lt;br /&gt;
                #Finish the declaration&lt;br /&gt;
                print &amp;quot;};\n&amp;quot;;&lt;br /&gt;
             }&lt;br /&gt;
             else&lt;br /&gt;
             {&lt;br /&gt;
                #If this is not a macro invocation, just copy the line directly to the output&lt;br /&gt;
                print $line;&lt;br /&gt;
             }&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
To run this program, do this:&lt;br /&gt;
&lt;br /&gt;
Running the code generator&lt;br /&gt;
&lt;br /&gt;
           ./tablegen.pl &amp;lt; sqrt.in &amp;gt; sqrt.c&lt;br /&gt;
           gcc sqrt.c -o sqrt&lt;br /&gt;
           ./a.out&lt;br /&gt;
&lt;br /&gt;
===Mini-language for boiler-plate===&lt;br /&gt;
If you have a large application where many of the functions include a lot of boilerplate code, it is often a good idea to create a mini-language that allows you to work with your boilerplate code in an easier fashion. This mini-language will then be converted into your regular source code language before compiling.&lt;br /&gt;
&lt;br /&gt;
The following is an example:&lt;br /&gt;
&lt;br /&gt;
Let's say that we are building a CGI application consisting of many independent CGI scripts. In most CGI applications, much of the state is stored in a database, but only a session ID is passed to each script via a cookie.&lt;br /&gt;
&lt;br /&gt;
However, in nearly every page we need to know the other standard information (such as the username, group number, the current job being worked on, whatever else information is pertinent). In addition, we need to redirect the user if they do not have an appropriate cookie.&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (let (&lt;br /&gt;
                     (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                    (webserver:redirect-to-login-page)&lt;br /&gt;
                    (let (&lt;br /&gt;
                          (username (webserver:username-for-session session-id))&lt;br /&gt;
                          (group (webserver:group-for-user username))&lt;br /&gt;
                          (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                       ;;Code for processing goes here&lt;br /&gt;
                       ))))&lt;br /&gt;
&lt;br /&gt;
While some of that can be handled by a procedure, the bindings certainly cannot. However, we can turn most of it into a macro. The macro can be implemented like this:&lt;br /&gt;
&lt;br /&gt;
            (define-syntax cgi-boilerplate&lt;br /&gt;
              (lambda (x)&lt;br /&gt;
                (syntax-case x ()&lt;br /&gt;
                 (&lt;br /&gt;
                   (cgi-boilerplate expr)&lt;br /&gt;
                   (datum-&amp;gt;syntax-object&lt;br /&gt;
                     (syntax k)&lt;br /&gt;
                      (let (&lt;br /&gt;
                            (session-id (webserver:cookie req &amp;quot;sessionid&amp;quot;)))&lt;br /&gt;
                           (if (not (webserver:valid-session-id session-id))&lt;br /&gt;
                               (webserver:redirect-to-login-page)&lt;br /&gt;
                               (let (&lt;br /&gt;
                                      (username (webserver:username-for-session session-id))&lt;br /&gt;
                                      (group (webserver:group-for-user username))&lt;br /&gt;
                                      (current-job (webserver:current-job-for-user username)))&lt;br /&gt;
                                      (syntax-object-&amp;gt;datum (syntax expr))))))&lt;br /&gt;
                  )&lt;br /&gt;
             )))&lt;br /&gt;
&lt;br /&gt;
We can now create new forms based on our boilerplate code by doing the following:&lt;br /&gt;
&lt;br /&gt;
            (define (handle-cgi-request req)&lt;br /&gt;
              (cgi-boilerplate&lt;br /&gt;
               (begin&lt;br /&gt;
                 ;;Do whatever I want here&lt;br /&gt;
                 )))&lt;br /&gt;
&lt;br /&gt;
In addition, since we are not defining our variables explicitly, adding new variable definitions to our boilerplate won't affect its calling conventions, so new features can be added without having to create a whole new function.&lt;br /&gt;
&lt;br /&gt;
In any large project, there are inevitably templates to follow which cannot be reduced to functions, usually because of the bindings being created. Using boilerplate macros can make maintenance of such templated code much easier.&lt;br /&gt;
&lt;br /&gt;
Likewise, other standard macros can be created which make use of variables defined in the boilerplate. Using macros like this significantly reduces typing because you do not have to constantly be writing and rewriting variable bindings, derivations, and parameter passing. This also reduces the potential for errors in such code.&lt;br /&gt;
&lt;br /&gt;
Realize though that boilerplate macros are not a panacea. There are many significant problems that can occur, including:&lt;br /&gt;
*  Accidentally overwriting bindings by introducing a variable name that was previously defined in a macro.&lt;br /&gt;
*  Difficulty tracing problems because the inputs and the outputs of the macros are implicit, not explicit.&lt;br /&gt;
&lt;br /&gt;
These can be largely avoided by doing a few things in conjunction with your boilerplate macros:&lt;br /&gt;
*  Have a naming convention which clearly labels macros as such, as well as indicate that a variable came from boilerplate code. This could be done by affixing -m to macros and -b to variables defined within a boilerplate.&lt;br /&gt;
*  Carefully document all boilerplate macros, especially the introduced variable bindings and all changes between versions.&lt;br /&gt;
*  Only use boilerplate macros when the savings in repetitiveness clearly outweigh the negatives of implicit functionality.&lt;br /&gt;
&lt;br /&gt;
===Abbreviate statements and prevent mistakes===&lt;br /&gt;
A lot of programming languages make you write really verbose statements to do really simple things. Code-generating programs allow you to abbreviate such statements and save a lot of typing, which also prevents a lot of mistakes because there is less chance of mistyping.&lt;br /&gt;
&lt;br /&gt;
=Metaprogramming Framework in Java=&lt;br /&gt;
==Reflection==&lt;br /&gt;
===Introduction to reflection===&lt;br /&gt;
Reflection is the ability to introspect metalevel information about the program structure itself at runtime. Usually this metalevel information is modeled using the general abstraction mechanisms available in the language. In Java, reflection enables to discover information about the loaded classes:&lt;br /&gt;
*  Fields,&lt;br /&gt;
*  Methods, and&lt;br /&gt;
*  Constructors&lt;br /&gt;
*  Generics information&lt;br /&gt;
*  Metadata annotations&lt;br /&gt;
It also enables to use these metaobjects to their instances in runtime environment.&lt;br /&gt;
&lt;br /&gt;
The following is the Java metalevel architecture&lt;br /&gt;
&lt;br /&gt;
[[File:Metalevel architecture.PNG]]&lt;br /&gt;
&lt;br /&gt;
===Dynamic proxy classes===&lt;br /&gt;
A dynamic proxy class implements a list of interfaces specified at runtime when the class is created.&lt;br /&gt;
*  A proxy interface is an interface that is implemented by a proxy class.&lt;br /&gt;
*  A proxy instance is an instance of a proxy class which has an associated invocation handler object.&lt;br /&gt;
The following is an example of dynamic proxy classes&lt;br /&gt;
&lt;br /&gt;
[[File:Dynamic_proxy_class.PNG]]&lt;br /&gt;
&lt;br /&gt;
==Generics==&lt;br /&gt;
Generics are a facility of generic programming that was added to the Java programming language in 2004 as part of J2SE 5.0. They allow &amp;quot;a type or method to operate on objects of various types while providing compile-time type safety.&amp;quot;  The generics can be used in classes, interfaces, methods and constructors.&lt;br /&gt;
===Two new types in generics===&lt;br /&gt;
Two new types in generics:&lt;br /&gt;
*  Parametrized types&lt;br /&gt;
*  Type variables&lt;br /&gt;
&lt;br /&gt;
A type variable is an unqualified identifier. Class and interface declarations can have type arguments (type variables). Method and constructors definitions can have type arguments (type variables). &lt;br /&gt;
&lt;br /&gt;
The following is an example for type variables&lt;br /&gt;
&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample = new ArrayList&amp;lt;String&amp;gt;()&lt;br /&gt;
&lt;br /&gt;
*  List : interface&lt;br /&gt;
*  ArrayList : class&lt;br /&gt;
*  String : class (the actual type argument)&lt;br /&gt;
*  List&amp;lt;String&amp;gt; and ArrayList&amp;lt;String&amp;gt; : parametrized type&lt;br /&gt;
&lt;br /&gt;
===Class/Interface Declarations===&lt;br /&gt;
The following is an example of class declaration:&lt;br /&gt;
&lt;br /&gt;
       public interface List&amp;lt;E&amp;gt; {&lt;br /&gt;
         void add(Ex);&lt;br /&gt;
         Iterator&amp;lt;E&amp;gt; iterator();&lt;br /&gt;
       }&lt;br /&gt;
       public interface Iterator&amp;lt;E&amp;gt; {&lt;br /&gt;
         E next();&lt;br /&gt;
         booleanhasNext();&lt;br /&gt;
       }&lt;br /&gt;
       List&amp;lt;String&amp;gt; anExample;&lt;br /&gt;
       anExample.add(”sdfdfss”);&lt;br /&gt;
       anExample.add(new Object()); // compile time error&lt;br /&gt;
       String aTest = anExample.iterator().next();&lt;br /&gt;
&lt;br /&gt;
==Metadata annotation==&lt;br /&gt;
An annotation, in the Java computer programming language, is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. &lt;br /&gt;
&lt;br /&gt;
The following is an example of annotation:&lt;br /&gt;
&lt;br /&gt;
        /**&lt;br /&gt;
        * Designates a formatter to pretty-print the annotated class.&lt;br /&gt;
        */&lt;br /&gt;
        public @interface PrettyPrinter{&lt;br /&gt;
        Class&amp;lt;? extends Formatter&amp;gt; value();&lt;br /&gt;
        }&lt;br /&gt;
        // Single-member annotation with Class&lt;br /&gt;
        // member restricted by bounded wildcard&lt;br /&gt;
        // The annotation presumes the existence of this class.&lt;br /&gt;
        class GorgeousFormatterimplements Formatter { ... }&lt;br /&gt;
        @PrettyPrinter(GorgeousFormatter.class)&lt;br /&gt;
        public class Petunia { ... }&lt;br /&gt;
&lt;br /&gt;
‘@’ indicates the start of an annotation definition.&lt;br /&gt;
&lt;br /&gt;
=Limitations=&lt;br /&gt;
&lt;br /&gt;
=References=&lt;/div&gt;</summary>
		<author><name>Jrwills2</name></author>
	</entry>
</feed>