CSC/ECE 517 Fall 2009/wiki2 1 SA

From Expertiza_Wiki
Jump to navigation Jump to search

Metaprogramming

Introduction

Metaprogramming,is the creation of procedures and programs that automatically construct the definitions of other procedures and programs. Metaprogramming automates some of the tedious and error-prone parts of the programmer's job.It refers to a variety of ways a program has knowledge of itself or can manipulate itself.It's basically "writing code that writes code".

To understand metaprogramming,compiler a very good example. It takes as input, code in some language, and creates code for another language. The most liberal definition of metaprogramming is:
1) Compile code generation or Runtime code generation (or both)
2) Aspect-Oriented Thinking or Aspect Oriented Programming
3) DRY Thinking

Metaprogramming in various languages

There are many languages that do metaprogramming.In languages like C#, reflection is a form of metaprogramming since the program can examine information about itself. For example returning a list of all the properties of an object.In languages like ActionScript, we can evaluate functions at runtime to create new programs such as eval("x" + i).DoSomething() would affect an object called x1 when i is 1 and x2 when i is 2.

Another common form of metaprogramming is when the program can change itself in non-trivial fashions. LISP is well known for this. The program would change another part of the program based on its state. This allows a level of flexibility to make decisions at runtime that is very difficult in most popular languages today.It is also worth noting that back in the good old days of programming in straight assembly, programs that altered themselves at runtime were necessary and very commonplace.

It can be implemented by using any of these and in combination:
1) Reflection
2) DSLs (Domain Specific Languages)
3) Attributes (.NET) or Annotations (Java)
4) Generics (.NET/Java)
5) Templates (C++)
6) method_missing (Ruby)
7) closures / first class functions / delegates
8) AOP - Aspect Oriented Programming


Metaprogramming in Ruby

We have a C program that needs to include a PNG image, but for some reason, the deployment platform can accept one file only, the executable file. Thus, the data that conforms the PNG file data has to be integrated within the program code itself. To achieve this, we can read the PNG file beforehand and generate the C source text for an array declaration, initialized with the corresponding data as literal values. This Ruby script does exactly that:

INPUT_FILE_NAME = 'ljlogo.png'
OUTPUT_FILE_NAME = 'ljlogo.h'
DATA_VARIABLE_NAME = 'ljlogo'
File.open(INPUT_FILE_NAME, 'r') do |input|
  File.open(OUTPUT_FILE_NAME, 'w') do |output|
    output.print "unsigned char #{DATA_VARIABLE_NAME}[] = {"
    data = input.read.unpack('C*')
    data.length.times do |i|
      if i % 8 == 0
        output.print "\n    "
      end
      output.print '0x%02X' % data[i]
      output.print ', ' if i < data.length - 1
    end
    output.puts "\n};"
  end
end



Metaprogramming in Groovy

Below example demonstrates using classes from the Jakarta Commons Lang package for metaprogramming. All of the methods in org.apache.commons.lang.StringUtils coincidentally follow the Category pattern — static methods that accept a String as the first parameter. This means that you can use the StringUtils class right out of the box as a Category.

import org.apache.commons.lang.StringUtils
 class CommonsTest extends GroovyTestCase{
  void testStringUtils(){
    def word = "Introduction"
    word.metaClass.whisper = {->
      delegate.toLowerCase()
    }
     use(StringUtils, StringHelper){
      //from org.apache.commons.lang.StringUtils
      assertEquals "Intro...", word.abbreviate(8)
       //from the StringHelper Category
      assertEquals "INTRODUCTION", word.shout()
       //from the word.metaClass
      assertEquals "introduction", word.whisper()
    }
  } 
}
class StringHelper{
  static String shout(String self){
    return self.toUpperCase()
  }
}

References

1.http://www.ibm.com/developerworks/java/library/j-pg06239.html
2.http://stackoverflow.com/questions/514644/what-exactly-is-metaprogramming