Dynamic Proxies

Search for: Dynamic Proxies

Java theory and practice: Decorating with dynamic proxies - Brian Goetz

Java Dynamic Proxies: One Step from Aspect-oriented Programming - Lara D'bero

Java 7 dynamic proxy api


//Consider
I1: Interface1
I2: Interface2
I3: Interface3

Object MainProxy = SomeProxyFor(I1, I2, I3) and Call(Handler);
I1 i1Proxy = (I1)MainProxy;
I2 i2Proxy = (I2)MainProxy;

Handler
{
   invoke(method) { figure out who to call and call; }
}
etc.

//I am thinking this is possible

Can you figure out which interface a method is invoked on in a java dynamic proxy?

Search for: Can you figure out which interface a method is invoked on in a java dynamic proxy?

Understand JavaAssist

JavaAssist and Android

Search for: JavaAssist and Android

An introductory article to a number of things around code injection and aspects


/**
 * 
 * Allow a name space for clients to discover various services
 * Usage:
 *   Services.persistenceServices.bookps.addBook(); etc.
 * Dynamic proxy will take care of transactions.
 * Dynamic proxy will take care of mock data.
 * Dynamic Proxy will allow more than one interface 
 *   to apply the above aspects. 
 */
public class Services 
{
   public static PersistenceServices persistenceServices = new PersistenceServices();
   public static class PersistenceServices   {
      ////se this pointer during initialization
      public static IBookPS bookps = null; 
   }
   
   private static Object mainProxy;
   static 
   {
      //set up bookps
      ClassLoader cl = IBookPS.class.getClassLoader();
      //Add more interfaces as available
      Class[] variousServiceInterfaces = new Class[] { IBookPS.class };
      
      //Create a big object that can proxy all the related interfaces
      //for which similar common aspects are applied
      //In this cases it is android sqllite transactions
      mainProxy = 
         Proxy.newProxyInstance(cl, 
               variousServiceInterfaces, 
               new TestInterceptor());
      
      //Preset the namespace for easy discovery
      PersistenceServices.bookps = (IBookPS)mainProxy;
   }
}

/**
 * A test interceptor for multiple interfaces
 * Tests to see how to route a method call
 * to a suitable implementation
 * 
 *  Eventually will be used for Android SQLite 
 *  transaction support.
 *  
 */
public class TestInterceptor implements InvocationHandler
{
   private BookPSSQLite bookServiceImpl;
   TestInterceptor()
   {
      this.bookServiceImpl = new BookPSSQLite();
   }
   public Object invoke(Object proxy, Method method, Object[] args)
         throws Throwable {
      logMethodSignature(method);
      return null;
   }
   
   private void logMethodSignature(Method method)
   {
      String interfaceName = method.getDeclaringClass().getName();
      String mname = method.getName();
      System.out.println(String.format("%s : %s", interfaceName, mname));
   }
}

/**
 * 
 * Allow a name space for clients to discover various services
 * Usage:
 *   Services.PersistenceServices.bookps.addBook(); etc.
 * Dynamic proxy will take care of transactions.
 * Dynamic proxy will take care of mock data.
 * Dynamic Proxy will allow more than one interface 
 *   to apply the above aspects. 
 */
public class Services 
{
   private static Object mainProxy;

   //A psuedo trick to kick off initializing this static class
   //All of the code in static blocks will run
   static void init(){}

   static  
   {
      //set up bookps
      ClassLoader cl = IBookPS.class.getClassLoader();
      //Add more interfaces as available
      Class[] variousServiceInterfaces = new Class[] { IBookPS.class };
      
      //Create a big object that can proxy all the related interfaces
      //for which similar common aspects are applied
      //In this cases it is android sqllite transactions
      mainProxy = 
         Proxy.newProxyInstance(cl, 
               variousServiceInterfaces, 
               new TestInterceptor());
      
      //Preset the namespace for easy discovery
      PersistenceServices.bookps = (IBookPS)mainProxy;
   }
   
   public static class PersistenceServices   {
      ////se this pointer during initialization
      public static IBookPS bookps = null;
      
      //Make sure things are initializd;
      static {Services.init();}

   }
}

In this approach the proxy will get the db and put it on the current running thread without explicitly passing that variable to the functions downstream. Code is cleaner and less error prone.

Here is some research on thread locals