implements Class.getSimpleName()

This commit is contained in:
Volker Berlin 2021-05-23 14:36:18 +02:00
parent 4f4f9ddc29
commit 534cd19c0f
2 changed files with 79 additions and 0 deletions

View File

@ -171,6 +171,16 @@ class ReplacementForClass<T> {
"return" )
private static native int getIntFromMemory( int pos );
/**
* Replacement of the Java methods isInstance()
*
* @param obj
* the object to check
* @return true if {@code obj} is an instance of this class
*/
@WasmTextCode( "unreachable" ) // TODO
public native boolean isInstance( Object obj );
/**
* Replacement of the Java methods isArray()
* @return {@code true} if this object represents an array class;
@ -233,6 +243,35 @@ class ReplacementForClass<T> {
return classIdx >= 0 ? classConstant( classIdx ) : null;
}
/**
* Replacement of the Java methods getSimpleName()
*
* @return the simple name of the underlying class
*/
public String getSimpleName() {
if( isArray() )
return getComponentType().getSimpleName() + "[]";
String simpleName = getName();
int index = simpleName.lastIndexOf( "$" ) + 1;
if( index == 0 ) { // top level class
return simpleName.substring( simpleName.lastIndexOf( "." ) + 1 ); // strip the package name
}
// Remove leading "\$[0-9]*" from the name
int length = simpleName.length();
while( index < length ) {
char ch = simpleName.charAt( index );
if( '0' <= ch && ch <= '9' ) {
index++;
} else {
break;
}
}
// Eventually, this is the empty string iff this is an anonymous class
return simpleName.substring( index );
}
/**
* Replacement of the Java methods getDeclaredMethod()
*/

View File

@ -73,6 +73,11 @@ public class StructsNonGC extends AbstractBaseTest {
addParam( list, script, "lambda1" );
addParam( list, script, "lambda2" );
addParam( list, script, "lambda3" );
addParam( list, script, "simpleName_Object" );
addParam( list, script, "simpleName_Anonymous" );
addParam( list, script, "simpleName_Array" );
addParam( list, script, "simpleName_InnerClass" );
addParam( list, script, "simpleName_LocalClass" );
}
rule.setTestParameters( list );
return list;
@ -227,6 +232,41 @@ public class StructsNonGC extends AbstractBaseTest {
return JSObject.domString( clazz.getName() );
}
@Export
static DOMString simpleName_Object() {
Object obj = new Object();
Class clazz = obj.getClass();
return JSObject.domString( clazz.getSimpleName() );
}
@Export
static DOMString simpleName_Anonymous() {
Object obj = new Object() {};
Class clazz = obj.getClass();
return JSObject.domString( clazz.getSimpleName() );
}
@Export
static DOMString simpleName_Array() {
Object obj = new Object[0];
Class clazz = obj.getClass();
return JSObject.domString( clazz.getSimpleName() );
}
@Export
static DOMString simpleName_InnerClass() {
Class clazz = TestClass.class;
return JSObject.domString( clazz.getSimpleName() );
}
@Export
static DOMString simpleName_LocalClass() {
class Foobar {}
Object obj = new Foobar();
Class clazz = obj.getClass();
return JSObject.domString( clazz.getSimpleName() );
}
@Export
static boolean getComponentType() {
Class<?> clazz = byte.class;