[cp-patches] Fix for Class.getSimpleName()

Sebastian Mancke s.mancke at tarent.de
Sat Apr 19 15:39:32 UTC 2008


Hi.

Now, the method getSimpleName() is merged with the one of glibgcj. (Not
changing behaviour, but avoiding recursion)

The Testcase has additional checks for array types and is converted to
mauve, now. I could not find regression due to my changes

--Sebastian

Sebastian Mancke schrieb:
> 
> 
> Mark Wielaard schrieb:
>> Hi,
> 
>> On Thu, 2008-04-17 at 00:50 +0100, Andrew John Hughes wrote:
>>> As to your contribution, the Classpath part (i.e. the java.lang.Class
>>> changes, Mauve has different contribution rules) looks minor enough to
>>> not require an assignment, but I'll let Mark (CCed) answer that for
>>> definite.  However, if you intend to do further Classpath
>>> contributions, I'd suggest sorting out the necessary paperwork with
>>> the FSF; you need to assign copyright to them.  Either Mark or I
>>> should be able to send you the form.
>> Yes, you are right. This is small enough to be "obvious", but it would
>> be nice to have paperwork on file with the FSF stating your willingness
>> to contribute (larger) patches in the future. I'll send you the request
>> form.
> I have sent the request to the FSF.
> 
>> BTW. libgcj has a slightly different implementation of
>> Class.getSimpleName() that might be worth merging with the classpath
>> version: http://gcc.gnu.org/ml/java-patches/2006-q3/msg00192.html
> Yes, looks good.
> 
> --Sebastian
> 

-- 
tarent Gesellschaft für Softwareentwicklung und IT-Beratung mbH

Heilsbachstr. 24, 53123 Bonn    | Poststr. 4-5, 10178 Berlin
fon: +49(228) / 52675-0         | fon: +49(30) / 27594853
fax: +49(228) / 52675-25        | fax: +49(30) / 78709617
durchwahl: +49(228) / 52675-17  | mobil: +49(171) / 7673249

Geschäftsführer:
Boris Esser, Elmar Geese, Thomas Müller-Ackermann
HRB AG Bonn 5168
Ust-ID: DE122264941
-------------- next part --------------
Index: vm/reference/java/lang/VMClass.java
===================================================================
RCS file: /sources/classpath/classpath/vm/reference/java/lang/VMClass.java,v
retrieving revision 1.20
diff -u -r1.20 VMClass.java
--- vm/reference/java/lang/VMClass.java	18 Sep 2007 21:52:38 -0000	1.20
+++ vm/reference/java/lang/VMClass.java	19 Apr 2008 15:19:00 -0000
@@ -296,27 +296,43 @@
    */
   static String getSimpleName(Class klass)
   {
+    int arrayCount = 0;
+    while (klass.isArray())
+      {
+        klass = klass.getComponentType();
+        ++arrayCount;
+      }
+    // now klass is the component type
+    
+    String simpleComponentName = null;
     if (isAnonymousClass(klass))
-      return "";
-    if (isArray(klass))
       {
-	return getComponentType(klass).getSimpleName() + "[]";
+        simpleComponentName = "";
       }
-    String fullName = getName(klass);
-    int pos = fullName.lastIndexOf("$");
-    if (pos == -1)
-      pos = 0;
     else
       {
-	++pos;
-	while (Character.isDigit(fullName.charAt(pos)))
-	  ++pos;
+        String fullName = getName(klass);
+        int pos = fullName.lastIndexOf("$");
+        if (pos != -1) 
+          { //inner class or local class
+            // skip digits of local classes
+            while (Character.isDigit(fullName.charAt(pos+1)))
+              pos++;
+          } 
+        else 
+          {
+            pos = fullName.lastIndexOf(".");
+          }
+        simpleComponentName = fullName.substring(pos+1);
       }
-    int packagePos = fullName.lastIndexOf(".", pos);
-    if (packagePos == -1)
-      return fullName.substring(pos);
-    else
-      return fullName.substring(packagePos + 1);
+    
+    if (arrayCount == 0)
+      return simpleComponentName;
+    
+    StringBuffer sb = new StringBuffer(simpleComponentName);
+    while (arrayCount-- > 0)
+      sb.append("[]");
+    return sb.toString();        
   }
 
   /**
-------------- next part --------------
Index: gnu/testlet/java/lang/Class/SimpleNameTest.java
===================================================================
RCS file: gnu/testlet/java/lang/Class/SimpleNameTest.java
diff -N gnu/testlet/java/lang/Class/SimpleNameTest.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/testlet/java/lang/Class/SimpleNameTest.java	19 Apr 2008 15:18:25 -0000
@@ -0,0 +1,82 @@
+// Copyright (C) 2008 Sebastian Mancke, Tarent GmbH <s.mancke at tarent.de>
+
+// This file is part of Mauve.
+
+// Mauve is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// Mauve 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 General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Mauve; see the file COPYING.  If not, write to
+// the Free Software Foundation, 59 Temple Place - Suite 330,
+// Boston, MA 02111-1307, USA.
+
+package gnu.testlet.java.lang.Class;
+
+import gnu.testlet.TestHarness;
+import gnu.testlet.Testlet;
+
+/**
+ * Tests the functionality of the Class#getSimpleName method.
+ */
+public class SimpleNameTest implements Testlet
+{
+
+  protected static TestHarness harness;
+  
+  public void test (TestHarness theHarness)
+  {
+    this.harness = theHarness;
+    testBasics();
+    testAnonymous();
+    testNested();
+    testArrayTypes();
+  }
+
+  private void testBasics() 
+  {    
+    harness.checkPoint("testBasics (Object)");
+    harness.check("Object".equals(Object.class.getSimpleName()));
+    
+    harness.checkPoint("testBasics (SimpleNameTest.class.getSimpleName())");
+    harness.check("SimpleNameTest".equals(SimpleNameTest.class.getSimpleName()));
+  }
+
+  private void testAnonymous() 
+  {
+    Object anonymous = new Object(){};
+    
+    harness.checkPoint("testAnonymous");
+    harness.check("".equals(anonymous.getClass().getSimpleName()));        
+  }
+  
+  private void testNested() 
+  {
+    harness.checkPoint("testNested (Inner)");
+    harness.check("Inner".equals(Inner.class.getSimpleName()));    
+    
+    harness.checkPoint("testNested (Local)");
+    class Local {
+    }
+    harness.check("Local".equals(Local.class.getSimpleName()));
+  }
+
+  private void testArrayTypes()
+  {
+    harness.checkPoint("testArrayTypes (1 dimension)");
+    harness.check("String[]".equals(String[].class.getSimpleName()));
+    
+    harness.checkPoint("testArrayTypes (3 dimension)");
+    harness.check("String[][][]".equals(String[][][].class.getSimpleName()));        
+  }
+  
+  public class Inner {
+    
+  }
+}


More information about the Classpath-patches mailing list