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

Sebastian Mancke s.mancke at tarent.de
Wed Apr 16 11:22:47 UTC 2008



Sebastian Mancke schrieb:
> 
> 
> Christian Thalinger schrieb:
>> On Wed, 2008-04-16 at 10:58 +0200, Sebastian Mancke wrote:
>>> I saw this patch and think it broke the behaviour, because of the wrong
>>> usage of fullName.lastIndexOf(".", pos): The pos argument, counted from
>>> left makes no sense in this method.
>>>
>>> Also, I think, that advancing 'pos', dependent on
>>> Character.isDigit(fullName.charAt(pos)) make no sense, because we return
>>> "" for anonymous classes already.
>> I see.  Can you try the CACAO testcase (I hope this is the right one):
>> tests/regression/TestAnnotations.java
> TestAnnotations hangs the current cacao. Dump is attached.
> 
> But maybe the testcase you mean is MinimalClassReflection, right?
> I have diff'ed cacaos output to the jdks output. In fact, I have missed
> one case: The Digits after the '$' exist for local classes and have to
> be skipped for the simple name. I will overwork my patch.

So, here is my patch again, now handling local classes as well.
I have tested it against the attached testcase, as well as against
cacaos MinimalClassReflection.java.

The following class name situations are covered:
Class in default package
Class in package
Inner Class (e.g. xyz.Abc$Inner -> "Inner")
Local Class (e.g. xyz.Abc$1Local -> "Local")
Anonymous (e.g. xyz.Abc$2 -> "")

--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	16 Apr 2008 11:12:18 -0000
@@ -304,19 +304,14 @@
       }
     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) { //inner class or local class
+        // skip digits of local classes
+        while (Character.isDigit(fullName.charAt(pos+1)))
+            pos++;
+    } else {
+        pos = fullName.lastIndexOf(".");
+    }
+    return fullName.substring(pos+1);
   }
 
   /**
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	16 Apr 2008 11:12:18 -0000
@@ -0,0 +1,54 @@
+package simplename;
+
+public class SimpleNameTest
+{
+    public static void main(final String[] args) {
+        new SimpleNameTest();
+    }
+
+    private SimpleNameTest() {
+
+        if ("Object".equals(Object.class.getSimpleName()))
+            passed("Object.class.getSimpleName() is \"Object\"");
+        else
+            failed("Object.class.getSimpleName() should be \"Object\", but is "+Object.class.getSimpleName());
+
+        
+        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());
+
+
+        class Local {
+        }
+        if ("Local".equals(Local.class.getSimpleName()))
+            passed("Local.class.getSimpleName() is \"Local\"");
+        else
+            failed("Local.class.getSimpleName() should be \"Local\",  but is "+Local.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