Search code examples
javajaxbpojo

Creating immutable objects using JAXB


I am using JAXB to create Java objects from XSD file. I am creating immutable wrappers to conceal objects generated by JAXB (earlier I was updating JAXB objects to implement immutable interface and return interface to client. But realised it is bad to change auto generated classes, hence using wrappers)

Currently I am returning these immutable wrappers to client app. Is there any option so that auto generated classes will be immutable and it will avoid extra work of creating immutable wrappers. Any other approach is encouraged.

  • Thanks

Solution

  • You can create a Proxy for your beans just before returning them to the client. You will need javassist to create Proxies from classes (create proxies from interfaces can be done with Java SE directly).

    Then, you can throw an exception if methods starting with "set" are invoked.

    Here is a reusable class with a method that can wrap "any" POJO:

    import java.lang.reflect.Method;
    
    import javassist.util.proxy.MethodFilter;
    import javassist.util.proxy.MethodHandler;
    import javassist.util.proxy.Proxy;
    import javassist.util.proxy.ProxyFactory;
    
    public class Utils {
    
     public static <C> C createInmutableBean(Class<C> clazz, final C instance)
            throws InstantiationException, IllegalAccessException {
        if (!clazz.isAssignableFrom(instance.getClass())) {
            throw new IllegalArgumentException("given instance of class "
                    + instance.getClass() + " is not a subclass of " + clazz);
        }
        ProxyFactory f = new ProxyFactory();
        f.setSuperclass(clazz);
        f.setFilter(new MethodFilter() {
            public boolean isHandled(Method m) {
                // ignore finalize()
                return !m.getName().equals("finalize");
            }
        });
        Class c = f.createClass();
        MethodHandler mi = new MethodHandler() {
            public Object invoke(Object self, Method m, Method proceed,
                    Object[] args) throws Throwable {
                if (m.getName().startsWith("set")) {
                    throw new RuntimeException("this bean is inmutable!");
                }
    
                return m.invoke(instance, args); // execute the original method
                                                    // over the instance
            }
        };
        C proxy = (C) c.newInstance();
    
        ((Proxy) proxy).setHandler(mi);
        return (C) proxy;
     }
    }
    

    And here is an example code. Let Employee be your bean:

    public class Employee{
      private String name="John";
      private String surname="Smith";
      public String getName() {
        return name;
      }
      public void setName(String name) {
        this.name = name;
      }
      public String getSurname() {
        return surname;
      }
      public void setSurname(String surname) {
        this.surname = surname;
      }
    };
    

    And here a test case showing that you can create a proxy for a POJO, use its getters, but you can't use its setters

    @Test
    public void testProxy() throws InstantiationException, IllegalAccessException{
        Employee aBean = new Employee();
    
        //I can modify the bean
        aBean.setName("Obi-Wan");
        aBean.setSurname("Kenobi");
    
        //create the protected java bean with the generic utility
        Employee protectedBean = Utils.createInmutableBean(Employee.class, aBean);
    
        //I can read
        System.out.println("Name: "+protectedBean.getName());
        System.out.println("Name: "+protectedBean.getSurname());
    
        //but I can't modify
        try{
            protectedBean.setName("Luke");
            protectedBean.setSurname("Skywalker");
            throw new RuntimeException("The test should not have reached this line!");
        }catch(Exception e){
            //I should be here
            System.out.println("The exception was expected! The bean should not be modified (exception message: "+e.getMessage()+")");
            assertEquals("Obi-Wan", protectedBean.getName());
            assertEquals("Kenobi", protectedBean.getSurname());
        }
    }