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

Sebastian Mancke s.mancke at tarent.de
Tue Apr 15 21:28:21 UTC 2008


Hi,

attached is a fix for the method Class.getSimpleName(), together with a
small testcase. The old implementation failed on usual class names, as
well as on inner classes.

Please comment/commit.

This is my first direct classpath contribution. Do I need to sign any
copyright assignment?

Regards,
  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	15 Apr 2008 21:09:51 -0000
@@ -304,19 +304,11 @@
       }
     String fullName = getName(klass);
     int pos = fullName.lastIndexOf("$");
-    if (pos == -1)
-      pos = 0;
-    else
-      {
-	++pos;
-	while (Character.isDigit(fullName.charAt(pos)))
-	  ++pos;
-      }
-    int packagePos = fullName.lastIndexOf(".", pos);
-    if (packagePos == -1)
-      return fullName.substring(pos);
-    else
-      return fullName.substring(packagePos + 1);
+    if (pos == -1) {
+        pos = fullName.lastIndexOf(".");
+    }
+    pos++;
+    return fullName.substring(pos);
   }
 
   /**
Index: testsuite/java.lang/SimpleNameTest.java
===================================================================
RCS file: testsuite/java.lang/SimpleNameTest.java
diff -N testsuite/java.lang/SimpleNameTest.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ testsuite/java.lang/SimpleNameTest.java	15 Apr 2008 21:09:51 -0000
@@ -0,0 +1,40 @@
+package simplename;
+
+public class SimpleNameTest
+{
+    public static void main(final String[] args) {
+        new SimpleNameTest();
+    }
+
+    private SimpleNameTest() {
+        if ("SimpleNameTest".equals(SimpleNameTest.class.getSimpleName()))
+            passed("SimpleNameTest.class.getSimpleName() is \"SimpleNameTest\"");
+        else
+            failed("SimpleNameTest.class.getSimpleName() should be \"SimpleNameTest\", but is "+SimpleNameTest.class.getSimpleName());
+        
+        Object anonymous = new Object(){};
+        if ("".equals(anonymous.getClass().getSimpleName()))
+            passed("anonymous.getClass().getSimpleName() is \"\"");
+        else
+            failed("anonymous.getClass().getSimpleName() should be \"\",  but is "+anonymous.getClass().getSimpleName());
+        
+        
+        if ("Inner".equals(Inner.class.getSimpleName()))
+            passed("Inner.class.getSimpleName() is \"Inner\"");
+        else
+            failed("Inner.class.getSimpleName() should be \"Inner\",  but is "+Inner.class.getSimpleName());
+        
+    }
+
+    public class Inner {
+
+    }
+    
+    static void passed(String msg) {
+        System.out.println("PASSED: "+msg);
+    }
+
+    static void failed(String msg) {
+        System.out.println("FAILED: "+msg);
+    }
+}


More information about the Classpath-patches mailing list