Liz Douglass

Archive for August 2008

Small things amuse small…. hmph, well anyway….

with one comment

My first four months in the software industry has been a really exciting challenge. There is a lot to get up to speed with and it’s changing all the time. One thing that really impresses me about the people that I get to work with is that they are all eager to learn, as am I. What is a little daunting is that I often find myself in conversations where I know absolutely nothing about the technology that the other person is talking about(!), simply because I haven’t been exposed to it before. For the most part though I feel like a kid in a candy store wanting to get my sticky little fingers into all the amazing colourful jars.

Two things that I’ve recently found out about are dynamic proxies and (drum roll) pixel rulers! I did probably get a little too excited about the latter, but hey, that’s me. My colleague suggested the dynamic proxy implementation and it turned out really neat….

In our application we are reading some data from an MS Excel file. The Excel file is very simple: it has a column listing the names of parameters and an adjacent column listing the values for each of those parameters. We’re using JExcelAPI to read the parameter values. Each of the parameter names matches up to a method name on our interface, which looks something like this:

public interface Parameters {
    String parameterA();
    String parameterB();

Our first attempt at providing access to the value for each parameter was:

public class ExcelParameters implements Parameters
    private ExcelReader reader = new ExcelReader();

    public String parameterA() {
         return reader.readParameter("parameterA");

    public String parameterB() {
         return reader.readParameter("parameterB");

Obviously this solution was neither elegant nor easy to maintain. All we were really after was a way to return the value of the parameter corresponding to the method name (which is also the parameter name) from Excel. Enter the dynamic proxy…

There is a lot of material available describing the implementation dynamic proxies in Java, including this article. Our dynamic proxy use is unconventional because, unlike in the normal use case, we never invoke the called method on an actual implementation of the Parameters interface. Instead we simply get the name of the called method and look up the corresponding parameter value in Excel:

The big advantage of this approach is that we never need an implementation of our Parameters interface – the proxy does this for us automatically. Our proxy is an implementation of the java.lang.reflect.Proxy class and looks like this:

public static Parameters getExcelRepository () {
    ClassLoader cl = Parameters.class.getClassLoader();
    Parameters proxy =
         (Parameters) Proxy.newProxyInstance(cl,
         new Class [] {Parameters.class},
         new ExcelParameterHandler());
    return proxy;

And the invocation handler that actually looks up the Excel sheet looks like this:

public class ExcelParameterHandler
   implements InvocationHandler
    private ExcelReader reader = new ExcelReader();

    public Object invoke(Object proxy,
                 Method method, Object[] args)
                 throws Exception
       return reader.readParameter(method.getName());

And here is how we can use it:

public void shouldBeAbleToReadParametersFromExcel()
  throws Exception
    Parameters excelParameters =
    assertEquals("value for ParameterA",


Written by lizdouglass

August 31, 2008 at 5:09 am

Posted in dynamic proxy, Excel, Java

Tagged with , ,