[jboss-cvs] JBossAS SVN: r83780 - in projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl: support/excluded/replacereferences and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Feb 2 09:53:16 EST 2009


Author: kabir.khan at jboss.com
Date: 2009-02-02 09:53:16 -0500 (Mon, 02 Feb 2009)
New Revision: 83780

Added:
   projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/
   projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/Invoked.java
   projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/child/
   projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/child/Child.java
   projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/child/ChildCaller.java
   projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/parent/
   projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/parent/Parent.java
   projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/parent/ParentCaller.java
   projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/test/ClassPoolWithReplaceReferencesTestCase.java
Log:
[JBAOP-666] Commit files missing in last commit

Added: projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/Invoked.java
===================================================================
--- projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/Invoked.java	                        (rev 0)
+++ projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/Invoked.java	2009-02-02 14:53:16 UTC (rev 83780)
@@ -0,0 +1,39 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors. 
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/ 
+package org.jboss.test.aop.classpool.jbosscl.support.excluded.replacereferences;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class Invoked
+{
+   public static boolean invoked;
+   
+   public static boolean getAndReset()
+   {
+      boolean inv = invoked;
+      invoked = false;
+      return inv;
+   }
+}

Added: projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/child/Child.java
===================================================================
--- projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/child/Child.java	                        (rev 0)
+++ projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/child/Child.java	2009-02-02 14:53:16 UTC (rev 83780)
@@ -0,0 +1,40 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors. 
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/ 
+package org.jboss.test.aop.classpool.jbosscl.support.excluded.replacereferences.child;
+
+import org.jboss.test.aop.classpool.jbosscl.support.excluded.replacereferences.parent.Parent;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class Child extends Parent
+{
+   int child;
+   
+   public static void child_write(Object o, int i)
+   {
+      org.jboss.test.aop.classpool.jbosscl.support.excluded.replacereferences.Invoked.invoked = true;
+      ((Child)o).child = i;
+   }
+}

Added: projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/child/ChildCaller.java
===================================================================
--- projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/child/ChildCaller.java	                        (rev 0)
+++ projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/child/ChildCaller.java	2009-02-02 14:53:16 UTC (rev 83780)
@@ -0,0 +1,78 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors. 
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/ 
+package org.jboss.test.aop.classpool.jbosscl.support.excluded.replacereferences.child;
+
+import org.jboss.test.aop.classpool.jbosscl.support.excluded.replacereferences.Invoked;
+import org.jboss.test.aop.classpool.jbosscl.support.excluded.replacereferences.parent.Parent;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class ChildCaller
+{
+   public static void test()
+   {
+      Invoked.invoked = false;
+      Child child = new Child();
+      assertCond(Invoked.invoked, "Invoked not called for Child constructor");
+      
+      Invoked.invoked = false;
+      child.child = 7;
+      assertCond(Invoked.invoked, "Invoked not called for Child.child write");
+      
+      int i = child.child;
+      assertCond(Invoked.invoked, "Invoked not called for Child.child read");
+      assertCond(i == 7, "Expected 5 for Child.child, was " + i);
+      
+      Invoked.invoked = false;
+      child.parent = 8;
+      assertCond(Invoked.invoked, "Invoked not called for Child.parent write");
+      
+      Invoked.invoked = false;
+      i = child.parent;
+      assertCond(Invoked.invoked, "Invoked not called for Child.parent read");
+      assertCond(i == 8, "Expected 5 for Child.child, was " + i);
+      
+      Invoked.invoked = false;
+      Parent parent = new Parent();
+      assertCond(Invoked.invoked, "Invoked not called for Parent constructor");
+      
+      Invoked.invoked = false;
+      parent.parent = 5;
+      assertCond(Invoked.invoked, "Invoked not called for Parent.parent write");
+      
+      Invoked.invoked = false;
+      i = parent.parent;
+      assertCond(Invoked.invoked, "Invoked not called for Parent.parent read");
+      assertCond(i == 5, "Expected 5 for Parent.parent, was " + i);
+   }
+   
+   private static void assertCond(boolean b, String msg)
+   {
+      if (!b)
+      {
+         throw new RuntimeException(msg);
+      }
+   }
+}

Added: projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/parent/Parent.java
===================================================================
--- projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/parent/Parent.java	                        (rev 0)
+++ projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/parent/Parent.java	2009-02-02 14:53:16 UTC (rev 83780)
@@ -0,0 +1,38 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors. 
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/ 
+package org.jboss.test.aop.classpool.jbosscl.support.excluded.replacereferences.parent;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class Parent
+{
+   public int parent;
+   
+   public static void parent_write(Object o, int i)
+   {
+      org.jboss.test.aop.classpool.jbosscl.support.excluded.replacereferences.Invoked.invoked = true;
+      ((Parent)o).parent = i;
+   }
+}

Added: projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/parent/ParentCaller.java
===================================================================
--- projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/parent/ParentCaller.java	                        (rev 0)
+++ projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/support/excluded/replacereferences/parent/ParentCaller.java	2009-02-02 14:53:16 UTC (rev 83780)
@@ -0,0 +1,56 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors. 
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/ 
+package org.jboss.test.aop.classpool.jbosscl.support.excluded.replacereferences.parent;
+
+import org.jboss.test.aop.classpool.jbosscl.support.excluded.replacereferences.Invoked;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class ParentCaller
+{
+   public static void test()
+   {
+      Invoked.invoked = false;
+      Parent parent = new Parent();
+      assertCond(Invoked.invoked, "Invoked not called for Parent constructor");
+      
+      Invoked.invoked = false;
+      parent.parent = 5;
+      assertCond(Invoked.invoked, "Invoked not called for Parent.parent write");
+      
+      Invoked.invoked = false;
+      int i = parent.parent;
+      assertCond(Invoked.invoked, "Invoked not called for Parent.parent read");
+      assertCond(i == 5, "Expected 5 for Parent.parent, was " + i);
+   }
+   
+   private static void assertCond(boolean b, String msg)
+   {
+      if (!b)
+      {
+         throw new RuntimeException(msg);
+      }
+   }
+}

Added: projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/test/ClassPoolWithReplaceReferencesTestCase.java
===================================================================
--- projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/test/ClassPoolWithReplaceReferencesTestCase.java	                        (rev 0)
+++ projects/aop/trunk/asintegration-mc/src/test/java/org/jboss/test/aop/classpool/jbosscl/test/ClassPoolWithReplaceReferencesTestCase.java	2009-02-02 14:53:16 UTC (rev 83780)
@@ -0,0 +1,342 @@
+/*
+* JBoss, Home of Professional Open Source.
+* Copyright 2006, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file in the
+* distribution for a full listing of individual contributors. 
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.aop.classpool.jbosscl.test;
+
+import java.lang.instrument.IllegalClassFormatException;
+import java.lang.reflect.Method;
+import java.net.URL;
+import java.security.ProtectionDomain;
+import java.util.Collection;
+
+import org.jboss.aop.AspectManager;
+import org.jboss.aop.classpool.AOPClassPool;
+import org.jboss.aop.classpool.AOPClassPoolRepository;
+import org.jboss.classloading.spi.DomainClassLoader;
+import org.jboss.util.loading.Translator;
+
+import javassist.ClassPool;
+import javassist.CodeConverter;
+import javassist.CtClass;
+import javassist.CtConstructor;
+import javassist.CtField;
+import javassist.CtMethod;
+import javassist.CtNewMethod;
+import javassist.Modifier;
+import junit.framework.Test;
+
+/**
+ * Test that field/constructor wrapper replacement works since there were some problems in AS5
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class ClassPoolWithReplaceReferencesTestCase extends JBossClClassPoolTest
+{
+   final static String STRING = String.class.getName();
+
+   public final static URL JAR_PARENT = getURLRelativeToProjectRoot("target/jboss-aop-asintegration-mc-test-classpool-replacereferences-parent.jar");
+
+   public final static URL JAR_CHILD = getURLRelativeToProjectRoot("target/jboss-aop-asintegration-mc-test-classpool-replacereferences-child.jar");
+
+   public final static String PACKAGE_REPLACEMENT = PACKAGE_ROOT + ".replacereferences";
+
+   public final static String PACKAGE_REPLACEMENT_CHILD = PACKAGE_REPLACEMENT + ".child";
+
+   public final static String PACKAGE_REPLACEMENT_PARENT = PACKAGE_REPLACEMENT + ".parent";
+
+   public final static String CLASS_INVOKED = PACKAGE_REPLACEMENT + ".Invoked";
+
+   public final static String CLASS_PARENT_CALLER = PACKAGE_REPLACEMENT_PARENT + ".ParentCaller";
+
+   public final static String CLASS_CHILD_CALLER = PACKAGE_REPLACEMENT_CHILD + ".ChildCaller";
+
+   public final String x = "org.jboss.test.aop.classpool.jbosscl.support.excluded";
+
+   private final static Translator TRANSLATOR = new MyTranslator();
+
+   public ClassPoolWithReplaceReferencesTestCase(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      return suite(ClassPoolWithReplaceReferencesTestCase.class, new Class[0]);
+   }
+
+   public void testParentLoadedParentDelegation() throws Exception
+   {
+      ClassPool globalPool = null;
+      ClassPool scopedPool = null;
+      try
+      {
+         getSystem().setTranslator(TRANSLATOR);
+         globalPool = createClassPool("GLOBAL", true, JAR_PARENT);
+         scopedPool = createChildDomainParentFirstClassPool("SCOPED", "SCOPED", true, JAR_CHILD);
+
+         loadClassAndRunTest(CLASS_PARENT_CALLER, globalPool.getClassLoader());
+         loadClassAndRunTest(CLASS_CHILD_CALLER, scopedPool.getClassLoader());
+      }
+      finally
+      {
+         unregisterClassPool(globalPool);
+         unregisterClassPool(scopedPool);
+         unregisterDomain(scopedPool);
+         getSystem().setTranslator(null);
+      }
+   }
+
+   public void testParentLoadedNoParentDelegation() throws Exception
+   {
+      ClassPool globalPool = null;
+      ClassPool scopedPool = null;
+      try
+      {
+         getSystem().setTranslator(TRANSLATOR);
+         globalPool = createClassPool("GLOBAL", true, JAR_PARENT);
+         scopedPool = createChildDomainParentLastClassPool("SCOPED", "SCOPED", true, JAR_CHILD);
+
+         loadClassAndRunTest(CLASS_PARENT_CALLER, globalPool.getClassLoader());
+         loadClassAndRunTest(CLASS_CHILD_CALLER, scopedPool.getClassLoader());
+      }
+      finally
+      {
+         unregisterClassPool(globalPool);
+         unregisterClassPool(scopedPool);
+         unregisterDomain(scopedPool);
+         getSystem().setTranslator(null);
+      }
+   }
+
+   public void testParentNotLoadedParentDelegation() throws Exception
+   {
+      ClassPool globalPool = null;
+      ClassPool scopedPool = null;
+      try
+      {
+         getSystem().setTranslator(TRANSLATOR);
+         globalPool = createClassPool("GLOBAL", true, JAR_PARENT);
+         scopedPool = createChildDomainParentFirstClassPool("SCOPED", "SCOPED", true, JAR_CHILD);
+
+         loadClassAndRunTest(CLASS_CHILD_CALLER, scopedPool.getClassLoader());
+      }
+      finally
+      {
+         unregisterClassPool(globalPool);
+         unregisterClassPool(scopedPool);
+         unregisterDomain(scopedPool);
+         getSystem().setTranslator(null);
+      }
+   }
+
+   public void testParentNotLoadedNoParentDelegation() throws Exception
+   {
+      ClassPool globalPool = null;
+      ClassPool scopedPool = null;
+      try
+      {
+         getSystem().setTranslator(TRANSLATOR);
+         globalPool = createClassPool("GLOBAL", true, JAR_PARENT);
+         scopedPool = createChildDomainParentLastClassPool("SCOPED", "SCOPED", true, JAR_CHILD);
+
+         loadClassAndRunTest(CLASS_CHILD_CALLER, scopedPool.getClassLoader());
+      }
+      finally
+      {
+         unregisterClassPool(globalPool);
+         unregisterClassPool(scopedPool);
+         unregisterDomain(scopedPool);
+         getSystem().setTranslator(null);
+      }
+   }
+
+   private void loadClassAndRunTest(String classname, ClassLoader loader) throws Exception
+   {
+      Class<?> caller = loader.loadClass(classname);
+      Method m = caller.getMethod("test");
+      m.invoke(null, new Object[]
+      {});
+   }
+
+   final static class MyTranslator implements Translator
+   {
+      public void unregisterClassLoader(DomainClassLoader loader)
+      {
+      }
+
+      public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
+            ProtectionDomain protectionDomain, byte[] classfileBuffer) throws Exception
+      {
+         if (!className.startsWith(PACKAGE_REPLACEMENT))
+         {
+            return null;
+         }
+         if (className.endsWith("Invoked"))
+         {
+            return null;
+         }
+
+         ClassPool pool = AspectManager.instance().registerClassLoader(loader);
+         CtClass clazz = pool.get(className);
+         if (className.endsWith("Caller"))
+         {
+            instrumentReplaceReferences(clazz);
+         }
+         else
+         {
+            instrumentCreateWrappers(clazz);
+         }
+
+         if (clazz.isModified())
+         {
+            return clazz.toBytecode();
+         }
+         return null;
+      }
+
+      private CtClass instrumentCreateWrappers(CtClass clazz)
+      {
+         try
+         {
+            if (!clazz.getName().startsWith(PACKAGE_REPLACEMENT))
+            {
+               return clazz;
+            }
+            addWrappersToClass(clazz);
+         }
+         catch (Exception e)
+         {
+            System.err.println("Exception creating wrappers for " + clazz);
+            e.printStackTrace();
+         }
+         return clazz;
+      }
+
+      private CtClass instrumentReplaceReferences(CtClass clazz) throws Exception
+      {
+         if (!clazz.getName().startsWith(PACKAGE_REPLACEMENT))
+         {
+            return clazz;
+         }
+         replaceReferences(clazz);
+         return clazz;
+      }
+
+      public void unregisterClassLoader(ClassLoader loader)
+      {
+      }
+
+      private void addWrappersToClass(CtClass clazz) throws Exception
+      {
+         addFieldWrappers(clazz);
+         addConstructorWrappers(clazz);
+      }
+
+      private void addFieldWrappers(CtClass clazz) throws Exception
+      {
+         CtClass objectCt = clazz.getClassPool().getCtClass(Object.class.getName());
+
+         for (CtField field : clazz.getFields())
+         {
+            if (field.getDeclaringClass() == objectCt)
+            {
+               continue;
+            }
+            CtMethod rmethod = 
+               CtNewMethod.make(
+                     CtClass.intType, 
+                     getFieldReadWrapperName(field.getName()),
+                     new CtClass[]{objectCt}, 
+                     null, 
+                     "{" + CLASS_INVOKED + ".invoked = true; return ((" + clazz.getName() + ")$0)." + field.getName() + ";}", 
+                     clazz);
+            rmethod.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
+            clazz.addMethod(rmethod);
+
+// TODO This gives VerifyErrors, mailed Chiba in the meantime have added these methods directly to Parent and Child            
+//            CtMethod wmethod = 
+//               CtNewMethod.make(
+//                     CtClass.voidType, 
+//                     getFieldWriteWrapperName(field.getName()),
+//                     new CtClass[]{objectCt, field.getType()}, 
+//                     null,
+//                     "{" + CLASS_INVOKED + ".invoked = true; ((" + clazz.getName() + ")$0)." + field.getName() + "=(int)$1;}", 
+//                     clazz);
+//            wmethod.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
+//            clazz.addMethod(wmethod);
+         }
+      }
+
+      private void addConstructorWrappers(CtClass clazz) throws Exception
+      {
+         for (CtConstructor ctor : clazz.getConstructors())
+         {
+            CtMethod wrapper = CtNewMethod.make(clazz, getConstructorName(clazz), ctor.getParameterTypes(), null,
+                  "{" + CLASS_INVOKED + ".invoked = true; return new " + clazz.getName() + "($$);}", clazz);
+            wrapper.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
+            clazz.addMethod(wrapper);
+         }
+      }
+
+      private void replaceReferences(CtClass clazz) throws Exception
+      {
+         CodeConverter conv = new CodeConverter();
+         AOPClassPool pool = AOPClassPool
+               .createAOPClassPool(clazz.getClassPool(), AOPClassPoolRepository.getInstance());
+         Collection<String> refs = clazz.getRefClasses();
+         for (String ref : refs)
+         {
+            if (ref.startsWith(PACKAGE_REPLACEMENT_CHILD) || ref.startsWith(PACKAGE_REPLACEMENT_PARENT))
+            {
+               if (ref.endsWith("Caller"))
+               {
+                  continue;
+               }
+               CtClass ctRef = pool.get(ref);
+               CtField[] fields = ctRef.getDeclaredFields();
+               for (CtField fld : fields)
+               {
+                  conv.replaceFieldRead(fld, ctRef, getFieldReadWrapperName(fld.getName()));
+                  conv.replaceFieldWrite(fld, ctRef, getFieldWriteWrapperName(fld.getName()));
+               }
+               conv.replaceNew(ctRef, ctRef, getConstructorName(ctRef));
+            }
+         }
+         clazz.instrument(conv);
+      }
+
+      private String getFieldReadWrapperName(String fieldName)
+      {
+         return fieldName + "_read";
+      }
+
+      private String getFieldWriteWrapperName(String fieldName)
+      {
+         return fieldName + "_write";
+      }
+
+      private String getConstructorName(CtClass clazz)
+      {
+         return clazz.getSimpleName() + "_new";
+      }
+   }
+}




More information about the jboss-cvs-commits mailing list