[jboss-cvs] JBossAS SVN: r59219 - in projects/aop/branches/arrays/aop/src: main/org/jboss/aop/array resources/test/array test/org/jboss/test/aop/array

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sat Dec 23 19:11:42 EST 2006


Author: kabir.khan at jboss.com
Date: 2006-12-23 19:11:36 -0500 (Sat, 23 Dec 2006)
New Revision: 59219

Added:
   projects/aop/branches/arrays/aop/src/test/org/jboss/test/aop/array/ClassWithSeveralReferencesToSameArray.java
Modified:
   projects/aop/branches/arrays/aop/src/main/org/jboss/aop/array/ArrayRegistry.java
   projects/aop/branches/arrays/aop/src/main/org/jboss/aop/array/ArrayRegistryEntry.java
   projects/aop/branches/arrays/aop/src/main/org/jboss/aop/array/ElementArrayRegistryEntry.java
   projects/aop/branches/arrays/aop/src/main/org/jboss/aop/array/FieldArrayRegistryEntry.java
   projects/aop/branches/arrays/aop/src/resources/test/array/jboss-aop.xml
   projects/aop/branches/arrays/aop/src/test/org/jboss/test/aop/array/AOPArrayTestCase.java
Log:
Better reference counting in the case of an array referenced by several "owners"

Modified: projects/aop/branches/arrays/aop/src/main/org/jboss/aop/array/ArrayRegistry.java
===================================================================
--- projects/aop/branches/arrays/aop/src/main/org/jboss/aop/array/ArrayRegistry.java	2006-12-23 23:27:35 UTC (rev 59218)
+++ projects/aop/branches/arrays/aop/src/main/org/jboss/aop/array/ArrayRegistry.java	2006-12-24 00:11:36 UTC (rev 59219)
@@ -66,7 +66,15 @@
             arrayReferences = new WeakHashMap<Object, ArrayRegistryEntry>();
             cache.put(array, arrayReferences);
          }
-         arrayReferences.put(owner, new FieldArrayRegistryEntry(owner, fieldName, array));
+         ArrayRegistryEntry regEntry = arrayReferences.get(owner);
+         if (regEntry != null)
+         {
+            ((FieldArrayRegistryEntry)regEntry).addFieldName(fieldName);
+         }
+         else
+         {
+            arrayReferences.put(owner, new FieldArrayRegistryEntry(owner, fieldName, array));
+         }
          addNestedArrays(array);
       }
       finally
@@ -89,7 +97,15 @@
          WeakHashMap<Object, ArrayRegistryEntry> arrayReferences = cache.get(array);
          if (arrayReferences != null)
          {
-            arrayReferences.remove(owner);
+            ArrayRegistryEntry regEntry = arrayReferences.get(owner);
+            if (owner != null)
+            {
+               ((FieldArrayRegistryEntry)regEntry).removeFieldName(field);
+               if (!regEntry.ownerHasReferences())
+               {
+                  arrayReferences.remove(owner);
+               }
+            }
          }
          removeNestedArrays(owner, array);
       }
@@ -116,7 +132,15 @@
             arrayReferences = new WeakHashMap<Object, ArrayRegistryEntry>();
             cache.put(array, arrayReferences);
          }
-         arrayReferences.put(owner, new ElementArrayRegistryEntry(owner, index, array));
+         ArrayRegistryEntry regEntry = arrayReferences.get(owner);
+         if (regEntry != null)
+         {
+            ((ElementArrayRegistryEntry)regEntry).addIndex(index);
+         }
+         else
+         {
+            arrayReferences.put(owner, new ElementArrayRegistryEntry(owner, index, array));
+         }
          addNestedArrays(array);
       }
       finally
@@ -140,7 +164,15 @@
          WeakHashMap<Object, ArrayRegistryEntry> arrayReferences = cache.get(array);
          if (arrayReferences != null)
          {
-            arrayReferences.remove(owner);
+            ArrayRegistryEntry regEntry = arrayReferences.get(owner);
+            if (owner != null)
+            {
+               ((ElementArrayRegistryEntry)regEntry).removeIndex(index);
+               if (!regEntry.ownerHasReferences())
+               {
+                  arrayReferences.remove(owner);
+               }
+            }
          }
          removeNestedArrays(owner, array);
       }

Modified: projects/aop/branches/arrays/aop/src/main/org/jboss/aop/array/ArrayRegistryEntry.java
===================================================================
--- projects/aop/branches/arrays/aop/src/main/org/jboss/aop/array/ArrayRegistryEntry.java	2006-12-23 23:27:35 UTC (rev 59218)
+++ projects/aop/branches/arrays/aop/src/main/org/jboss/aop/array/ArrayRegistryEntry.java	2006-12-24 00:11:36 UTC (rev 59219)
@@ -66,4 +66,6 @@
    {
       return ownerIsRoot;
    }
+   
+   public abstract boolean ownerHasReferences();
 }

Modified: projects/aop/branches/arrays/aop/src/main/org/jboss/aop/array/ElementArrayRegistryEntry.java
===================================================================
--- projects/aop/branches/arrays/aop/src/main/org/jboss/aop/array/ElementArrayRegistryEntry.java	2006-12-23 23:27:35 UTC (rev 59218)
+++ projects/aop/branches/arrays/aop/src/main/org/jboss/aop/array/ElementArrayRegistryEntry.java	2006-12-24 00:11:36 UTC (rev 59219)
@@ -21,6 +21,8 @@
 */ 
 package org.jboss.aop.array;
 
+import java.util.HashSet;
+
 /**
  * 
  * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
@@ -28,11 +30,27 @@
  */
 public class ElementArrayRegistryEntry extends ArrayRegistryEntry
 {
-   int index;
+   HashSet<Integer> indices = new HashSet<Integer>();
    
    ElementArrayRegistryEntry(Object owner, int index, Object array)
    {
       super(owner, false, array);
-      this.index = index; 
+      this.indices.add(index); 
    }
+   
+   public void addIndex(int index)
+   {
+      indices.add(index);
+   }
+   
+   public void removeIndex(int index)
+   {
+      indices.remove(index);
+   }
+
+   @Override
+   public boolean ownerHasReferences()
+   {
+      return indices.size() > 0;
+   }
 }

Modified: projects/aop/branches/arrays/aop/src/main/org/jboss/aop/array/FieldArrayRegistryEntry.java
===================================================================
--- projects/aop/branches/arrays/aop/src/main/org/jboss/aop/array/FieldArrayRegistryEntry.java	2006-12-23 23:27:35 UTC (rev 59218)
+++ projects/aop/branches/arrays/aop/src/main/org/jboss/aop/array/FieldArrayRegistryEntry.java	2006-12-24 00:11:36 UTC (rev 59219)
@@ -21,6 +21,8 @@
 */ 
 package org.jboss.aop.array;
 
+import java.util.HashSet;
+
 /**
  * 
  * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
@@ -28,17 +30,28 @@
  */
 public class FieldArrayRegistryEntry extends ArrayRegistryEntry
 {
-   String fieldName;
+   HashSet fields = new HashSet<String>();
    
    FieldArrayRegistryEntry(Object owner, String fieldName, Object array)
    {
       // FIXME FieldArrayRegistryEntry constructor
       super(owner, true, array);
-      this.fieldName = fieldName;
+      this.fields.add(fieldName);
    }
    
-   public String getFieldName()
+   public void addFieldName(String fieldName)
    {
-      return fieldName;
+      fields.add(fieldName);
    }
-}
+   
+   public void removeFieldName(String fieldName)
+   {
+      fields.remove(fieldName);
+   }
+   
+   @Override
+   public boolean ownerHasReferences()
+   {
+      return fields.size() > 0;
+   }
+}
\ No newline at end of file

Modified: projects/aop/branches/arrays/aop/src/resources/test/array/jboss-aop.xml
===================================================================
--- projects/aop/branches/arrays/aop/src/resources/test/array/jboss-aop.xml	2006-12-23 23:27:35 UTC (rev 59218)
+++ projects/aop/branches/arrays/aop/src/resources/test/array/jboss-aop.xml	2006-12-24 00:11:36 UTC (rev 59219)
@@ -1,6 +1,7 @@
 <aop>
    <arrayreplacement class="org.jboss.test.aop.array.AOPArrayTestCase"/>
-   <arrayreplacement expr="class(org.jboss.test.aop.array.ClassWithArrayFields)"/>
+   <arrayreplacement expr="class(org.jboss.test.aop.array.ClassWithArrayFields) OR class(org.jboss.test.aop.array.ClassWithSeveralReferencesToSameArray)"/>
    <prepare expr="field(* org.jboss.test.aop.array.ClassWithArrayFields->*)"/>
+   <prepare expr="field(* org.jboss.test.aop.array.ClassWithSeveralReferencesToSameArray->*)"/> 
 </aop>
 

Modified: projects/aop/branches/arrays/aop/src/test/org/jboss/test/aop/array/AOPArrayTestCase.java
===================================================================
--- projects/aop/branches/arrays/aop/src/test/org/jboss/test/aop/array/AOPArrayTestCase.java	2006-12-23 23:27:35 UTC (rev 59218)
+++ projects/aop/branches/arrays/aop/src/test/org/jboss/test/aop/array/AOPArrayTestCase.java	2006-12-24 00:11:36 UTC (rev 59219)
@@ -27,6 +27,7 @@
 import org.jboss.aop.array.TestArrayElementInterceptor;
 import org.jboss.test.aop.AOPTestWithSetup;
 
+
 /**
  * 
  * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
@@ -64,18 +65,6 @@
       assertNull(TestArrayElementInterceptor.value);
    }
    
-   public void testCrap()
-   {
-      ClassWithArrayFields obj = new ClassWithArrayFields();
-      System.out.println("created");
-      int[] orig = obj.iifield[0];
-      System.out.println("backed up");
-      int[] x = new int[] {45};
-      
-      obj.iifield[0] = x;
-      orig[1] = 1;
-   }
-   
    public void testMultiDimensionalTopLevelArray()
    {
       ClassWithArrayFields obj = new ClassWithArrayFields();
@@ -164,4 +153,157 @@
       assertNull(TestArrayElementInterceptor.value);
    }
    
+   public static void testMultipleFieldReferences()
+   {
+      ClassWithSeveralReferencesToSameArray obj = new ClassWithSeveralReferencesToSameArray();
+      assertEquals(obj.one, obj.two);
+      
+      int[] original = obj.one;
+      
+      TestArrayElementInterceptor.clear();
+      obj.one[1] = 100;
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(100, TestArrayElementInterceptor.value);
+      TestArrayElementInterceptor.clear();
+      int i = obj.one[1];
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(100, i);
+      TestArrayElementInterceptor.clear();
+      i = obj.two[1];
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(100, i);
+      
+      assertEquals(obj.one, obj.two);
+      
+      TestArrayElementInterceptor.clear();
+      int replacement1[] = new int[2];
+      replacement1[0] = 11;
+      replacement1[1] = 22;
+      
+      obj.one = replacement1;
+      TestArrayElementInterceptor.clear();
+      obj.one[1] = 99;
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(99, TestArrayElementInterceptor.value);
+
+      //obj.two still references original, so make sure that we have interception 
+      TestArrayElementInterceptor.clear();
+      i = original[1];
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(100, i);
+
+      obj.two = replacement1;
+      TestArrayElementInterceptor.clear();
+      obj.one[1] = 101;
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(101, TestArrayElementInterceptor.value);
+
+      //original is no longer referenced by and advised field, we should not have interception 
+      TestArrayElementInterceptor.clear();
+      i = original[1];
+      assertEquals(-1, TestArrayElementInterceptor.index);
+   }
+   
+   public static void testMultipleNestedReferences()
+   {
+      ClassWithSeveralReferencesToSameArray obj = new ClassWithSeveralReferencesToSameArray();
+      assertEquals(obj.multi[0], obj.multi[1]);
+      
+      int[] original = obj.multi[0];
+      
+      TestArrayElementInterceptor.clear();
+      obj.multi[0][1] = 100;
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(100, TestArrayElementInterceptor.value);
+      TestArrayElementInterceptor.clear();
+      int i = obj.multi[0][1];
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(100, i);
+      TestArrayElementInterceptor.clear();
+      i = obj.multi[1][1];
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(100, i);
+      
+      assertEquals(obj.multi[0], obj.multi[1]);
+      
+      TestArrayElementInterceptor.clear();
+      int replacement1[] = new int[2];
+      replacement1[0] = 11;
+      replacement1[1] = 22;
+      
+      obj.multi[0] = replacement1;
+      TestArrayElementInterceptor.clear();
+      obj.multi[0][1] = 99;
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(99, TestArrayElementInterceptor.value);
+
+      //obj.multi[1] still references original, so make sure that we have interception 
+      TestArrayElementInterceptor.clear();
+      i = original[1];
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(100, i);
+
+      obj.multi[1] = replacement1;
+      TestArrayElementInterceptor.clear();
+      obj.multi[0][1] = 101;
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(101, TestArrayElementInterceptor.value);
+
+      //original is no longer referenced by and advised field, we should not have interception 
+      TestArrayElementInterceptor.clear();
+      i = original[1];
+      assertEquals(-1, TestArrayElementInterceptor.index);
+   }
+   
+   public static void testMultipleMixedReferences()
+   {
+      ClassWithSeveralReferencesToSameArray obj = new ClassWithSeveralReferencesToSameArray();
+      obj.multi[0] = obj.one = new int[] {8,6,4};
+      
+      int[] original = obj.multi[0];
+      
+      TestArrayElementInterceptor.clear();
+      obj.multi[0][1] = 100;
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(100, TestArrayElementInterceptor.value);
+      TestArrayElementInterceptor.clear();
+      int i = obj.multi[0][1];
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(100, i);
+      TestArrayElementInterceptor.clear();
+      i = obj.one[1];
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(100, i);
+      
+      assertEquals(obj.multi[0], obj.one);
+      
+      TestArrayElementInterceptor.clear();
+      int replacement1[] = new int[2];
+      replacement1[0] = 11;
+      replacement1[1] = 22;
+      
+      obj.multi[0] = replacement1;
+      TestArrayElementInterceptor.clear();
+      obj.multi[0][1] = 99;
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(99, TestArrayElementInterceptor.value);
+
+      //obj.one still references original, so make sure that we have interception 
+      TestArrayElementInterceptor.clear();
+      i = original[1];
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(100, i);
+
+      obj.one = replacement1;
+      TestArrayElementInterceptor.clear();
+      obj.multi[0][1] = 101;
+      assertEquals(1, TestArrayElementInterceptor.index);
+      assertEquals(101, TestArrayElementInterceptor.value);
+
+      //original is no longer referenced by and advised field, we should not have interception 
+      TestArrayElementInterceptor.clear();
+      i = original[1];
+      assertEquals(-1, TestArrayElementInterceptor.index);
+   }
+   
 }

Added: projects/aop/branches/arrays/aop/src/test/org/jboss/test/aop/array/ClassWithSeveralReferencesToSameArray.java
===================================================================
--- projects/aop/branches/arrays/aop/src/test/org/jboss/test/aop/array/ClassWithSeveralReferencesToSameArray.java	2006-12-23 23:27:35 UTC (rev 59218)
+++ projects/aop/branches/arrays/aop/src/test/org/jboss/test/aop/array/ClassWithSeveralReferencesToSameArray.java	2006-12-24 00:11:36 UTC (rev 59219)
@@ -0,0 +1,46 @@
+/*
+* 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.array;
+
+/**
+ * 
+ * @author <a href="kabir.khan at jboss.com">Kabir Khan</a>
+ * @version $Revision: 1.1 $
+ */
+public class ClassWithSeveralReferencesToSameArray
+{
+   int[] one;
+   int[] two;
+   
+   int[][] multi;
+   
+   public ClassWithSeveralReferencesToSameArray()
+   {
+      one = new int[] {1,2,3};
+      two = one;
+      
+      int[] nested = new int[] {4,5,6};
+      multi = new int[2][];
+      multi[0] = nested;
+      multi[1] = nested;
+   }
+}




More information about the jboss-cvs-commits mailing list