CSC/ECE 517 Fall 2009/wiki2 1 SA: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 12: Line 12:


<br>'''Metaprogramming in Groovy'''
<br>'''Metaprogramming in Groovy'''
<br>
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.
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
  import org.apache.commons.lang.StringUtils
 
  class CommonsTest extends GroovyTestCase{
class CommonsTest extends GroovyTestCase{
   void testStringUtils(){
   void testStringUtils(){
     def word = "Introduction"
     def word = "Introduction"
Line 23: Line 23:
       delegate.toLowerCase()
       delegate.toLowerCase()
     }
     }
      use(StringUtils, StringHelper){
    use(StringUtils, StringHelper){
       //from org.apache.commons.lang.StringUtils
       //from org.apache.commons.lang.StringUtils
       assertEquals "Intro...", word.abbreviate(8)
       assertEquals "Intro...", word.abbreviate(8)
        //from the StringHelper Category
      //from the StringHelper Category
       assertEquals "INTRODUCTION", word.shout()
       assertEquals "INTRODUCTION", word.shout()
        //from the word.metaClass
      //from the word.metaClass
       assertEquals "introduction", word.whisper()
       assertEquals "introduction", word.whisper()
     }
     }
   }  
   }  
  }
  }
  class StringHelper{
  class StringHelper{
   static String shout(String self){
   static String shout(String self){

Revision as of 08:18, 9 October 2009

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.

Metaprogramming in various languages

There are many languages that do metaprogramming.


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()
  }
}

Examples