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

Andrew John Hughes gnu_andrew at member.fsf.org
Wed Apr 16 23:50:07 UTC 2008


On 16/04/2008, Sebastian Mancke <s.mancke at tarent.de> wrote:
>
>
>  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
>
> 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);
>  +    }
>  +}
>
>

Firstly, we don't do tests in the Classpath codebase any more.  These
are held in Mauve:

http://sourceware.org/mauve/

It's a long standing bug that the ones still in Classpath need moving
over.  Sorry for us having led you to fall into this trap, but making
the test fit the Mauve framework should be trivial.  I'd also check
there for any other tests for java.lang.Class and check that the
output doesn't regress due to the patch.

twisti, we should also maybe port the CACAO test if it's not VM specific?

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.

Thanks for your contribution! :)
-- 
Andrew :-)

Support Free Java!
Contribute to GNU Classpath and the OpenJDK
http://www.gnu.org/software/classpath
http://openjdk.java.net

PGP Key: 94EFD9D8 (http://subkeys.pgp.net)
Fingerprint: F8EF F1EA 401E 2E60 15FA  7927 142C 2591 94EF D9D8


More information about the Classpath-patches mailing list