harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject svn commit: r647723 - in /harmony/enhanced/classlib/trunk/modules/beans/src: main/java/java/beans/XMLEncoder.java test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java
Date Mon, 14 Apr 2008 10:20:36 GMT
Author: ayza
Date: Mon Apr 14 03:20:26 2008
New Revision: 647723

URL: http://svn.apache.org/viewvc?rev=647723&view=rev
Log:
Committing a workaround for HARMONY-5707 ([classlib][beans] dead loop in Encoder)

Modified:
    harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLEncoder.java
    harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLEncoder.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLEncoder.java?rev=647723&r1=647722&r2=647723&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLEncoder.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/XMLEncoder.java
Mon Apr 14 03:20:26 2008
@@ -42,7 +42,9 @@
  */
 public class XMLEncoder extends Encoder {
 
-	/*
+    private static int DEADLOCK_THRESHOLD = 7;
+
+    /*
 	 * Every object written by the encoder has a record.
 	 */
 	private static class Record {
@@ -813,7 +815,41 @@
 		rec.stats.add(stat);
 	}
 
-	/**
+    /**
+     * Imperfect attempt to detect a dead loop. This works with specific
+     * patterns that can be found in our AWT implementaiton.
+     * See HARMONY-5707 for details.
+     *
+     * @param value the object to check dupes for
+     * @return true if a dead loop detected; false otherwise
+     * FIXME
+     */
+    private boolean checkDeadLoop(Object value) {
+        int n = 0;
+        Object obj = value;
+
+        while (obj != null) {
+            Record rec = (Record) records.get(obj);
+
+            if (rec != null && rec.exp != null) {
+                obj = rec.exp.getTarget();
+            } else {
+                break;
+            }
+            
+            if (obj != null && value.equals(obj)) {
+                n++;
+
+                if (n >= DEADLOCK_THRESHOLD) {
+                    //System.out.println("Dead loop hit!");
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    /**
 	 * Sets the owner of this encoder.
 	 * 
 	 * @param owner
@@ -833,7 +869,8 @@
 	    writingObject = true;
 		// get expression value
 		Object oldValue = null;
-		try {
+
+        try {
 			oldValue = oldExp.getValue();
 		} catch (Exception e) {
 			getExceptionListener()
@@ -853,7 +890,12 @@
 			recordExpression(oldValue, oldExp);
 		}
 
-		super.writeExpression(oldExp);
+        // try to detect if we run into a dead loop
+        if (checkDeadLoop(oldValue)) {
+            return;
+        }
+
+        super.writeExpression(oldExp);
 		writingObject = oldWritingObject;
 	}
 

Modified: harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java?rev=647723&r1=647722&r2=647723&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/XMLEncoderTest.java
Mon Apr 14 03:20:26 2008
@@ -704,4 +704,43 @@
             e.close();
         }
     }
+
+    /**
+     * This is a regression test for HARMONY-5707.
+     */
+    public void test5707() {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        XMLEncoder encoder = new XMLEncoder(baos);
+        TstBean5707 bean1 = new TstBean5707();
+
+        encoder.writeObject(bean1);
+        encoder.close();        
+    }
+
+    public class TstBean5707 {
+        int val;
+
+        public TstBean5707() {
+            val = 0;
+        }
+
+        public TstBean5707(int n) {
+            val = n;
+        }
+
+        public TstBean5707 getProp1() {
+            return new TstBean5707(val);
+        }
+
+        public void setProp1(TstBean5707 val) {}
+
+        public boolean equals(Object obj) {
+            if (obj instanceof TstBean5707) {
+                return ((TstBean5707) obj).val == val;
+            }
+
+            return false;
+        }
+
+    }
 }



Mime
View raw message