classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] [FYI/Java Beans] fix for Statement and Expression


From: Robert Schuster
Subject: [cp-patches] [FYI/Java Beans] fix for Statement and Expression
Date: Tue, 30 Aug 2005 03:34:10 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; de-AT; rv:1.7.11) Gecko/20050830

Hi,
nice to be back finally.

This fixes some incompatibilities and reduces costly + operator usage a bit.

2005-08-30  Robert Schuster  <address@hidden>

    * java/beans/Statement.java:
    (Statement): Use zero length array if argument array is null.
    (toString): Use StringBuffer for efficiency reasons, make use of internal
    name.
    (storeTargetName): New method, generates instance names like the JDK
    has.
    (doExecute): Removed debugging output that could lead to an exception
    because wrong loop variable usage.
    * java/beans/Expression.java: Static constant 'unset' renamed to 'UNSET´.

cu
Robert
Index: java/beans/Statement.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/beans/Statement.java,v
retrieving revision 1.2
diff -u -r1.2 Statement.java
--- java/beans/Statement.java   2 Jul 2005 20:32:37 -0000       1.2
+++ java/beans/Statement.java   30 Aug 2005 01:24:48 -0000
@@ -1,5 +1,5 @@
 /* java.beans.Statement
-   Copyright (C) 2004 Free Software Foundation, Inc.
+   Copyright (C) 2004, 2005 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -42,6 +42,9 @@
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
 
+import java.util.HashMap;
+import java.util.WeakHashMap;
+
 /**
  * class Statement
  *
@@ -54,6 +57,11 @@
  */
 public class Statement
 {
+  /** Nested map for the relation between a class, its instances and their
+    * names.
+    */
+  private static HashMap classMaps = new HashMap();
+
   private Object target;
   private String methodName;
   private Object[] arguments;
@@ -64,8 +72,11 @@
   private transient Constructor ctor;
 
   /**
-   * Constructs a statement representing the invocation of
-   * object.methodName(arg[0], arg[1], ...);
+   * <p>Constructs a statement representing the invocation of
+   * object.methodName(arg[0], arg[1], ...);</p>
+   *
+   * <p>If the argument array is null it is replaced with an
+   * array of zero length.</p>
    *
    * @param target The object to invoke the method on.
    * @param methodName The object method to invoke.
@@ -75,7 +86,41 @@
   {
     this.target = target;
     this.methodName = methodName;
-    this.arguments = arguments;
+    this.arguments = (arguments != null) ? arguments : new Object[0];
+    storeTargetName(target);
+  }
+
+  /** Creates a name for the target instance or does nothing if the object's
+   * name is already known. This makes sure that there *is* a name for every
+   * target instance.
+   */
+  private static synchronized void storeTargetName(Object obj)
+  {
+    Class klass = obj.getClass();
+    WeakHashMap names = (WeakHashMap) classMaps.get(klass);
+
+    if ( names == null )
+    {
+      names = new WeakHashMap();
+
+      names.put(obj,
+        ( klass == String.class ? ("\"" + obj + "\"") :
+        (klass.getName() + names.size()) ));
+
+      classMaps.put(klass, names);
+
+      return;
+    }
+
+    String targetName = (String) names.get(obj);
+    if ( targetName == null )
+    {
+      names.put(obj,
+        ( klass == String.class ? ("\"" + obj + "\"") :
+        (klass.getName() + names.size()) ));
+    }
+
+    // Nothing to do. The given object was already stored.
   }
 
   /**
@@ -234,15 +279,7 @@
          {
            // Skip methods with wrong number of args.
            Class ptypes[] = ctors[i].getParameterTypes();
-           System.out.println("ptypeslen = " + ptypes.length);
-           System.out.println("ptypes = " + ptypes);
-           System.out.println("ctor = " + ctors[i].getName());
-           for (int j=0; j < ptypes.length; j++) {
-             System.out.println("param = " + ptypes[i].getName());
-     
-           }
-             
-           
+
            if (ptypes.length != args.length)
              continue;
 
@@ -313,14 +350,24 @@
   /** Return a string representation. */
   public String toString()
   {
-    String result = target.getClass().getName() + "." + methodName + "(";
+    StringBuffer result = new StringBuffer(); 
+
+    Class klass = target.getClass();
+
+    result.append( ((WeakHashMap) classMaps.get(klass)).get(target));
+    result.append(".");
+    result.append(methodName);
+    result.append("(");
+
     String sep = "";
     for (int i = 0; i < arguments.length; i++)
       {
-       result = result + sep + arguments[i].getClass().getName();
-       sep = ", ";
+        result.append(sep);
+        result.append(arguments[i].getClass().getName());
+        sep = ", ";
       }
-    result = result + ")";
-    return result;
+    result.append(")");
+
+    return result.toString();
   }
 }
Index: java/beans/Expression.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/beans/Expression.java,v
retrieving revision 1.2
diff -u -r1.2 Expression.java
--- java/beans/Expression.java  2 Jul 2005 20:32:37 -0000       1.2
+++ java/beans/Expression.java  30 Aug 2005 01:24:48 -0000
@@ -1,5 +1,5 @@
 /* java.beans.Expression
-   Copyright (C) 2004 Free Software Foundation, Inc.
+   Copyright (C) 2004, 2005 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -51,7 +51,7 @@
 {
   // This is a placeholder to indicate that value hasn't been set
   // yet;
-  private static final Object unset = new Object();
+  private static final Object UNSET = new Object();
 
   // The value to return.  This is equal to unset until getValue is called.
   private Object value;
@@ -89,7 +89,7 @@
   public Expression(Object target, String methodName, Object[] arguments)
   {
     super(target, methodName, arguments);
-    this.value = unset;
+    this.value = UNSET;
   }
 
   /**
@@ -105,7 +105,7 @@
    */
   public Object getValue() throws Exception
   {
-    if (value == unset)
+    if (value == UNSET)
       value = doExecute();
     return value;
   }
@@ -126,7 +126,7 @@
   public String toString()
   {
     String result = super.toString();
-    if (value != unset)
+    if (value != UNSET)
       return value.getClass().getName() + " " + result;
     return result;
   }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]