Skip to main content

What is metaprogramming?

The word metaprogramming keeps coming up everywhere in stuff I read nowadays. Even though I sort of knew what it meant I wasn't really sure. So I decided to ask the question 'what exactly is metaprogramming' on SO.

I got some really nice answers and links to articles. I have written this blog post as an effort to summarize what I have learned and to be able to share it with others who might be interested.

According to Wikipedia

''
Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at compile time that would otherwise be done at runtime. In many cases, this allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation."

The compiler is the simplest example of a metaprogram. It takes as input, code in some language, and creates code for another language. An example of metaprogramming in Java, is the use of generics. Here is a blog post I wrote (although for a different reason) some time back to show a simple example of how the compiler transforms Java code which has generics.

In languages like C and C++, metaprogramming can be done by the preprocessor.

In the Groovy programming language, there are two types of metaprogramming techniques: compile time metaprogramming, and runtime metaprogramming.

In compile time metaprogramming, Groovy allows us to hook into the compilation process at various stages, and modify the Abstract Syntax Tree which the compiler works on. (If you are interested, here is an Eclipse plugin to visualize the AST of a Java program)

An example of compile time metaprogramming is the use of the @Singleton annotation in Groovy. Whenever, we implement a Singleton, we always create a private constructor and a static method which gives us an instance of the Singleton. If we have several Singleton classes in our software, we have to implement this pattern for all of them. Using the @Singleton annotation gives us pattern reuse through AST transformations.

Let's say I want to create a Singleton class called MySingleton. In Groovy I can create it as follows without having to write a single line of plumbing code.


@Singleton class MySingleton {
//implementation of the class
}


During the compilation phase, some custom code will be invoked which will add the private constructor and a static instance() method to the class. Isn't this great? I think AST transformations can take reuse to a totally new level

Runtime metaprogramming can be done by using the Groovy MetaClass or more specifically the MetaObjectProtocol. Using the MetaClass, we can:
  • Add methods to objects at runtime
  • Determine if an object responds to a message or contains a property
  • Respond to calls made on non-existent methods
  • Respond to queries made on non-existent properties
Various builders in Groovy such as the MarkupBuilder and the SwingBuilder use the MetaObjectProtocol to achieve their magic. Here is some very simple code which uses SwingBuilder to create a simple GUI.


def swing = new SwingBuilder()
def gui = swing.frame(title:'GroovyTwitterClient',
size:[600,400],
defaultCloseOperation:WindowConstants.EXIT_ON_CLOSE) {
scrollPane {
tabbedPane = widget(new JTabbedPane())
}
}


What you see in the code above is a very simple DSL to create Swing GUI's. The code creates a JFrame, set's it's title, size, and defaultCloseOperation. Then it creates a JScrollPane and puts it in the JFrame. Finally it creates a JTabbedPane and puts it in the JScrollPane. If you look at the code carefully, you will realize that we call the method 'frame' on an instance of SwingBuilder and give it certain parameters and a closure. In the closure we call a method 'scrollPane' and give it another closure, and so on. Here each method call actually results in the creation of a Swing component. The parameters of the call are the properties which will be set on that component and the closure represents the component(s) to be added to that component. However, the most interesting thins is yet to come. The SwingBuilder class does not even have methods called 'frame', 'scollPane', and 'tabbedPane'. So then how does this work? The SwingBuilder class overrides invokeMethod(...) which is invoked for every single method call. So, when the 'frame' method is invoked on SwingBuilder, it goes through the MetaObjectProtocol to invokeMethod(...) . Here the call is intercepted and a swing component is created based on the name of the method.

This was just an overview. There is much more to metaprogramming and I will write more about it in future posts.

Comments

Popular posts from this blog

My HSQLDB schema inspection story

This is a simple story of my need to inspect the schema of an HSQLDB database for a participar FOREIGN KEY, and the interesting things I had to do to actually inspect it. I am using an HSQLDB 1.8 database in one of my web applications. The application has been developed using the Play framework , which by default uses JPA and Hibernate . A few days back, I wanted to inspect the schema which Hibernate had created for one of my model objects. I started the HSQLDB database on my local machine, and then started the database manager with the following command java -cp ./hsqldb-1.8.0.7.jar org.hsqldb.util.DatabaseManagerSwing When I tried the view the schema of my table, it showed me the columns and column types on that table, but it did not show me columns were FOREIGN KEYs. Image 1: Table schema as shown by HSQLDB's database manager I decided to search on StackOverflow and find out how I could view the full schema of the table in question. I got a few hints, and they all pointed to

Fuctional Programming Principles in Scala - Getting Started

Sometime back I registered for the Functional Programming Principles in Scala , on Coursera. I have been meaning to learn Scala from a while, but have been putting it on the back burner because of other commitments. But  when I saw this course being offered by Martin Odersky, on Coursera , I just had to enroll in it. This course is a 7 week course. I will blog my learning experience and notes here for the next seven weeks (well actually six, since the course started on Sept 18th). The first step was to install the required tools: JDK - Since this is my work machine, I already have a couple of JDK's installed SBT - SBT is the Scala Build Tool. Even though I have not looked into it in detail, it seems like a replacement for Maven. I am sure we will use it for several things, however upto now I only know about two uses for it - to submit assignments (which must be a feature added by the course team), and to start the Scala console. Installed sbt from here , and added the path

Five Reasons Why Your Product Needs an Awesome User Guide

Photo Credit: Peter Merholz ( Creative Commons 2.0 SA License ) A user guide is essentially a book-length document containing instructions for installing, using or troubleshooting a hardware or software product. A user guide can be very brief - for example, only 10 or 20 pages or it can be a full-length book of 200 pages or more. -- prismnet.com As engineers, we give a lot of importance to product design, architecture, code quality, and UX. However, when it comes to the user manual, we often only manage to pay lip service. This is not good. A usable manual is as important as usable software because it is the first line of help for the user and the first line of customer service for the organization. Any organization that prides itself on great customer service must have an awesome user manual for the product. In the spirit of listicles - here are at least five reasons why you should have an awesome user manual! Enhance User Satisfaction In my fourteen years as a