Skip to main content

Posts

Showing posts with the label metaprogramming

Create mock objects with metaprogramming

We usually create mock objects in our test suites for those classes which communicate with some external entity that we cannot (or do not care to) setup in our test cases. However, mocking is possible only when we have used dependency injection properly. If we have classes that instantiate their own dependencies then we cannot mock the dependencies. Sometimes our mock solutions may also be time intensive at runtime. I will explain this later in the post. One solution (the correct one) is to refactor the code and use proper dependency injection to allow classes be be mocked. However, when we have a huge code base of non DI code, which cannot all be changed at a time, then we just have to make so with fewer unit tests. But all is not lost. If we use a dynamic language like Groovy to write the test cases then we can use metaprogramming to change a classes methods at runtime instead of creating mock classes. I am building a command line Twitter client in Groovy. I have a class called Frie...

Injecting methods in Groovy with ExpandoMetaClass

I am writing a Twitter client in Groovy. One of the things I want to do is create a thread which will poll Twitter for new updates at regular intervals, and go to sleep when it is not doing anything. The way we write such code is to invoke the sleep method with the number of milliseconds to sleep. To sleep for 2 minutes, I would write: Thread.sleep(1000 * 60 * 2) This code is fine, but not very readable. I would have preferred to write something like this: Thread.sleep(2.minutes()) This is not possible in Java, but it is in Groovy. In Groovy we can inject methods into a class at runtime. This is done using the class' ExpandoMetaClass, as the example below shows. Integer.metaClass.seconds = {return delegate * 1000} Integer.metaClass.minutes = {return delegate * 1000 * 60} Integer.metaClass.hours = {return delegate * 1000 * 60 * 60} //test assert 3.seconds() == 1000 * 3 assert 5.minutes() == 1000 * 60 * 5 assert 4.hours() == 1000 * 60 * 60 * 4 The above code gets Integer's metacl...

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 lan...