From a2a679b2e3552870871679fc83b81e99f1b46138 Mon Sep 17 00:00:00 2001 From: Mike Poindexter Date: Mon, 20 Jul 2015 17:49:31 -0700 Subject: [PATCH 1/2] Implement Class.getSimpleName --- .../org/teavm/classlib/java/lang/TClass.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java index 662f9bcd3..a6d6861b1 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java @@ -17,6 +17,7 @@ package org.teavm.classlib.java.lang; import java.util.HashMap; import java.util.Map; + import org.teavm.classlib.impl.DeclaringClassMetadataGenerator; import org.teavm.classlib.java.lang.annotation.TAnnotation; import org.teavm.classlib.java.lang.reflect.TAnnotatedElement; @@ -32,6 +33,7 @@ import org.teavm.platform.metadata.ClassScopedMetadataProvider; */ public class TClass extends TObject implements TAnnotatedElement { TString name; + TString simpleName; private TClass componentType; private boolean componentTypeDirty = true; private PlatformClass platformClass; @@ -73,6 +75,26 @@ public class TClass extends TObject implements TAnnotatedElement { return name; } + public TString getSimpleName() { + if (simpleName == null) { + String name = platformClass.getMetadata().getName(); + int lastDollar = name.lastIndexOf('$'); + if (lastDollar != -1) { + name = name.substring(lastDollar + 1); + if (name.charAt(0) >= 0 && name.charAt(0) <= 9) { + name = ""; + } + } else { + int lastDot = name.lastIndexOf('.'); + if (lastDot != -1) { + name = name.substring(lastDot + 1); + } + } + simpleName = TString.wrap(name); + } + return simpleName; + } + public boolean isPrimitive() { return platformClass.getMetadata().isPrimitive(); } From d86c424aade8fa7bc897d0f104e4b829d55e4ac6 Mon Sep 17 00:00:00 2001 From: Mike Poindexter Date: Mon, 20 Jul 2015 23:00:32 -0700 Subject: [PATCH 2/2] Add tests and implement proper behavior for arrays --- .../java/org/teavm/classlib/java/lang/TClass.java | 6 +++++- .../org/teavm/classlib/java/lang/ClassTest.java | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java index a6d6861b1..4c6251fba 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java @@ -77,11 +77,15 @@ public class TClass extends TObject implements TAnnotatedElement { public TString getSimpleName() { if (simpleName == null) { + if (isArray()) { + simpleName = getComponentType().getSimpleName().concat(TString.wrap("[]")); + return simpleName; + } String name = platformClass.getMetadata().getName(); int lastDollar = name.lastIndexOf('$'); if (lastDollar != -1) { name = name.substring(lastDollar + 1); - if (name.charAt(0) >= 0 && name.charAt(0) <= 9) { + if (name.charAt(0) >= '0' && name.charAt(0) <= '9') { name = ""; } } else { diff --git a/teavm-tests/src/test/java/org/teavm/classlib/java/lang/ClassTest.java b/teavm-tests/src/test/java/org/teavm/classlib/java/lang/ClassTest.java index 8442bd37f..219dde718 100644 --- a/teavm-tests/src/test/java/org/teavm/classlib/java/lang/ClassTest.java +++ b/teavm-tests/src/test/java/org/teavm/classlib/java/lang/ClassTest.java @@ -38,6 +38,16 @@ public class ClassTest { assertEquals("[I", int[].class.getName()); } + @Test + public void classSimpleNameEvaluated() { + assertEquals("Object", Object.class.getSimpleName()); + assertEquals("Object[]", Object[].class.getSimpleName()); + assertEquals("int", int.class.getSimpleName()); + assertEquals("int[]", int[].class.getSimpleName()); + assertEquals("InnerClass", InnerClass.class.getSimpleName()); + assertEquals("", new Object(){}.getClass().getSimpleName()); + } + @Test public void objectClassNameEvaluated() { assertEquals("java.lang.Object", new Object().getClass().getName()); @@ -205,4 +215,7 @@ public class ClassTest { Class n(); } + + static class InnerClass { + } }