[seam-commits] Seam SVN: r8468 - in trunk/src/wicket/org/jboss/seam/wicket: ioc and 1 other directory.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Tue Jul 15 17:28:37 EDT 2008


Author: pete.muir at jboss.org
Date: 2008-07-15 17:28:37 -0400 (Tue, 15 Jul 2008)
New Revision: 8468

Added:
   trunk/src/wicket/org/jboss/seam/wicket/ioc/InstrumentedComponent.java
Modified:
   trunk/src/wicket/org/jboss/seam/wicket/WicketComponent.java
   trunk/src/wicket/org/jboss/seam/wicket/ioc/BijectionInterceptor.java
   trunk/src/wicket/org/jboss/seam/wicket/ioc/InvocationContext.java
   trunk/src/wicket/org/jboss/seam/wicket/ioc/JavassistInstrumentor.java
   trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketHandler.java
Log:
Support for bijection in enclosing objects

Modified: trunk/src/wicket/org/jboss/seam/wicket/WicketComponent.java
===================================================================
--- trunk/src/wicket/org/jboss/seam/wicket/WicketComponent.java	2008-07-14 22:12:21 UTC (rev 8467)
+++ trunk/src/wicket/org/jboss/seam/wicket/WicketComponent.java	2008-07-15 21:28:37 UTC (rev 8468)
@@ -180,7 +180,7 @@
          
    }
    
-   public void inject(T instance) throws Exception
+   public void inject(T instance)
    {
       for ( BijectedAttribute<In> in : inAttributes )
       {
@@ -341,4 +341,10 @@
       }
    }
    
+   @Override
+   public String toString()
+   {
+      return "WicketComponent(" + type + ")";
+   }
+   
 }

Modified: trunk/src/wicket/org/jboss/seam/wicket/ioc/BijectionInterceptor.java
===================================================================
--- trunk/src/wicket/org/jboss/seam/wicket/ioc/BijectionInterceptor.java	2008-07-14 22:12:21 UTC (rev 8467)
+++ trunk/src/wicket/org/jboss/seam/wicket/ioc/BijectionInterceptor.java	2008-07-15 21:28:37 UTC (rev 8468)
@@ -1,6 +1,8 @@
 package org.jboss.seam.wicket.ioc;
 
+import org.jboss.seam.wicket.WicketComponent;
 
+
 public class BijectionInterceptor<T> extends RootInterceptor<T>
 {
 
@@ -9,6 +11,7 @@
    {
       invocationContext.getComponent().outject(invocationContext.getBean());
       invocationContext.getComponent().disinject(invocationContext.getBean());
+      disinjectEnclosingInstances(invocationContext);
    }
 
    @Override
@@ -17,11 +20,46 @@
       try
       {
          invocationContext.getComponent().inject(invocationContext.getBean());
+         injectEnclosingInstances(invocationContext);
       }
       catch (Exception e)
       {
          throw new RuntimeException(e);
       }
    }
+   
+   private static <T> void injectEnclosingInstances(InvocationContext<T> invocationContext)
+   {
+      InstrumentedComponent enclosingInstance = invocationContext.getInstrumentedComponent().getEnclosingInstance();
+      while (enclosingInstance != null)
+      {
+         if (!enclosingInstance.getHandler().isCallInProgress())
+         {
+            WicketComponent.getInstance(enclosingInstance.getClass()).inject(enclosingInstance);
+            enclosingInstance = enclosingInstance.getEnclosingInstance();
+         }
+         else
+         {
+            return;
+         }
+      }
+   }
+   
+   private static <T> void disinjectEnclosingInstances(InvocationContext<T> invocationContext)
+   {
+      InstrumentedComponent enclosingInstance = invocationContext.getInstrumentedComponent().getEnclosingInstance();
+      while (enclosingInstance != null)
+      {
+         if (!enclosingInstance.getHandler().isCallInProgress())
+         {
+            WicketComponent.getInstance(enclosingInstance.getClass()).disinject(enclosingInstance);
+            enclosingInstance = enclosingInstance.getEnclosingInstance();
+         }
+         else
+         {
+            return;
+         }
+      }
+   }
 
 }

Added: trunk/src/wicket/org/jboss/seam/wicket/ioc/InstrumentedComponent.java
===================================================================
--- trunk/src/wicket/org/jboss/seam/wicket/ioc/InstrumentedComponent.java	                        (rev 0)
+++ trunk/src/wicket/org/jboss/seam/wicket/ioc/InstrumentedComponent.java	2008-07-15 21:28:37 UTC (rev 8468)
@@ -0,0 +1,10 @@
+package org.jboss.seam.wicket.ioc;
+
+public interface InstrumentedComponent
+{
+   
+   public WicketHandler getHandler();
+   
+   public InstrumentedComponent getEnclosingInstance();
+
+}
\ No newline at end of file


Property changes on: trunk/src/wicket/org/jboss/seam/wicket/ioc/InstrumentedComponent.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: trunk/src/wicket/org/jboss/seam/wicket/ioc/InvocationContext.java
===================================================================
--- trunk/src/wicket/org/jboss/seam/wicket/ioc/InvocationContext.java	2008-07-14 22:12:21 UTC (rev 8467)
+++ trunk/src/wicket/org/jboss/seam/wicket/ioc/InvocationContext.java	2008-07-15 21:28:37 UTC (rev 8468)
@@ -20,8 +20,9 @@
       this.component = component;
    }
    
-   public InvocationContext(Constructor<T> constructor, T bean, WicketComponent<T> component)
+   public InvocationContext(T bean, WicketComponent<T> component)
    {
+      // TODO Write the constructor discovery code
       this.constructor = constructor;
       this.bean = bean;
       this.component = component;
@@ -44,4 +45,9 @@
       return component;
    }
    
+   public InstrumentedComponent getInstrumentedComponent()
+   {
+      return (InstrumentedComponent) bean;
+   }
+   
 }
\ No newline at end of file

Modified: trunk/src/wicket/org/jboss/seam/wicket/ioc/JavassistInstrumentor.java
===================================================================
--- trunk/src/wicket/org/jboss/seam/wicket/ioc/JavassistInstrumentor.java	2008-07-14 22:12:21 UTC (rev 8467)
+++ trunk/src/wicket/org/jboss/seam/wicket/ioc/JavassistInstrumentor.java	2008-07-15 21:28:37 UTC (rev 8468)
@@ -10,6 +10,7 @@
 import javassist.CtConstructor;
 import javassist.CtField;
 import javassist.CtMethod;
+import javassist.CtNewMethod;
 import javassist.LoaderClassPath;
 import javassist.Modifier;
 import javassist.NotFoundException;
@@ -123,46 +124,62 @@
       log.debug("Instrumenting " + className);
       CtClass implementation = classPool.get(className);
       CtClass handlerClass = classPool.get(WicketHandler.class.getName());
+      
       CtField handlerField = new CtField(handlerClass, "handler", implementation);
       Initializer handlerInitializer = Initializer.byCall(handlerClass, "create");
       implementation.addField(handlerField, handlerInitializer);
+      
+      CtClass instrumentedComponent = classPool.get(InstrumentedComponent.class.getName());
+      implementation.addInterface(instrumentedComponent);
+      CtMethod getHandlerMethod = CtNewMethod.getter("getHandler", handlerField);
+      CtMethod getEnclosingInstance = CtNewMethod.make("public " + InstrumentedComponent.class.getName() +" getEnclosingInstance() { return " + WicketHandler.class.getName() + ".getEnclosingInstance(this); }", implementation);
+      implementation.addMethod(getEnclosingInstance);
+      implementation.addMethod(getHandlerMethod);
+      
       for (CtMethod method : implementation.getDeclaredMethods())
       {
          if (!Modifier.isStatic(method.getModifiers()))
          {
             String methodName = method.getName();
-            String methodSignature = "";
-            for (int i = 0; i < method.getParameterTypes().length; i++)
+            if (!("getHandler".equals(method.getName()) || "getEnclosingInstance".equals(method.getName()) ))
             {
-               if (i > 0)
+               String methodSignature = "";
+               for (int i = 0; i < method.getParameterTypes().length; i++)
                {
-                  methodSignature += ",";
+                  if (i > 0)
+                  {
+                     methodSignature += ",";
+                  }
+                  methodSignature += method.getParameterTypes()[i].getName() + ".class";
                }
-               methodSignature += method.getParameterTypes()[i].getName() + ".class";
+               String methodCall = "this.getClass().getDeclaredMethod(\""+ methodName + "\", methodParameters)";
+               String methodParameters;
+               if (methodSignature.length() > 0)
+               {
+                  methodParameters = "Class[] methodParameters = {" + methodSignature + "};";
+               }
+               else
+               {
+                  methodParameters = "Class[] methodParameters = new Class[0];";
+               }
+               log.trace("Method call: " + methodCall);
+               
+               method.insertBefore(methodParameters + "handler.beforeInvoke(this, " + methodCall + ");");
+               method.insertBefore("handler.setCallInProgress(true);");
+               method.insertAfter(methodParameters + "handler.afterInvoke(this, " + methodCall + ");");
+               method.insertAfter("handler.setCallInProgress(false);", true);
+               log.trace("instrumented method " + method.getName());
             }
-            String methodCall = "this.getClass().getDeclaredMethod(\""+ methodName + "\", methodParameters)";
-            String methodParameters;
-            if (methodSignature.length() > 0)
-            {
-               methodParameters = "Class[] methodParameters = {" + methodSignature + "};";
-            }
-            else
-            {
-               methodParameters = "Class[] methodParameters = new Class[0];";
-            }
-            log.trace("Method call: " + methodCall);
-            
-            method.insertBefore(methodParameters + "handler.beforeInvoke(this, " + methodCall + ");");
-            method.insertAfter(methodParameters + "handler.afterInvoke(this, " + methodCall + ");");
-            log.trace("instrumented method " + method.getName());
          }
       }
       for (CtConstructor constructor : implementation.getConstructors())
       {
          if (constructor.isConstructor())
          {
-            constructor.insertBeforeBody("handler.beforeInvoke(this, null);");
-            constructor.insertAfter("handler.afterInvoke(this, null);");
+            constructor.insertBeforeBody("handler.beforeInvoke(this);");
+            constructor.insertBeforeBody("handler.setCallInProgress(true);");
+            constructor.insertAfter("handler.afterInvoke(this);");
+            constructor.insertAfter("handler.setCallInProgress(false);");
             log.trace("instrumented constructor " + constructor.getName());
          }
       }

Modified: trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketHandler.java
===================================================================
--- trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketHandler.java	2008-07-14 22:12:21 UTC (rev 8467)
+++ trunk/src/wicket/org/jboss/seam/wicket/ioc/WicketHandler.java	2008-07-15 21:28:37 UTC (rev 8468)
@@ -26,13 +26,8 @@
    private List<RootInterceptor> interceptors;
    private Class<?> type;
    private transient WicketComponent component;
+   private boolean callInProgress;
    
-   public void init()
-   {
-      
-
-   }
-   
    private WicketComponent getComponent()
    {
       if (component == null)
@@ -62,14 +57,15 @@
       afterInvoke(new InvocationContext(calledMethod, target, getComponent()));
    }
    
-   public void beforeInvoke(Object target, Constructor constructor)
+   public void beforeInvoke(Object target)
    {
-      beforeInvoke(new InvocationContext(constructor, target, getComponent()));
+      getComponent().initialize(target);
+      beforeInvoke(new InvocationContext(target, getComponent()));
    }
    
-   public void afterInvoke(Object target, Constructor constructor)
+   public void afterInvoke(Object target)
    {
-      afterInvoke(new InvocationContext(constructor, target, getComponent()));
+      afterInvoke(new InvocationContext(target, getComponent()));
    }
    
    private void beforeInvoke(InvocationContext invocationContext)
@@ -82,11 +78,40 @@
    
    private void afterInvoke(InvocationContext invocationContext)
    {
-      invocationContext.getComponent().initialize(invocationContext.getBean());
       for (RootInterceptor interceptor : getInterceptors())
       {
          interceptor.afterInvoke(invocationContext);
       }
    }
+ 
+   public boolean isCallInProgress()
+   {
+      return callInProgress;
+   }
    
+   public void setCallInProgress(boolean callInProgress)
+   {
+      this.callInProgress = callInProgress;
+   }
+   
+   public static InstrumentedComponent getEnclosingInstance(Object bean)
+   {
+      Class enclosingType = bean.getClass().getEnclosingClass();
+      if (enclosingType != null)
+      {
+         try 
+         {
+            java.lang.reflect.Field enclosingField = bean.getClass().getDeclaredField("this$0");
+            enclosingField.setAccessible(true);
+            Object enclosingInstance = enclosingField.get(bean);
+            if (enclosingInstance instanceof InstrumentedComponent)
+            {
+               return (InstrumentedComponent) enclosingInstance;
+            }
+         }
+         catch (Exception e) {}
+      }
+      return null;
+   }
+
 }




More information about the seam-commits mailing list