mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 07:27:52 +01:00
implements Class.getSimpleName()
This commit is contained in:
parent
4f4f9ddc29
commit
534cd19c0f
@ -171,6 +171,16 @@ class ReplacementForClass<T> {
|
|||||||
"return" )
|
"return" )
|
||||||
private static native int getIntFromMemory( int pos );
|
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()
|
* Replacement of the Java methods isArray()
|
||||||
* @return {@code true} if this object represents an array class;
|
* @return {@code true} if this object represents an array class;
|
||||||
@ -233,6 +243,35 @@ class ReplacementForClass<T> {
|
|||||||
return classIdx >= 0 ? classConstant( classIdx ) : null;
|
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()
|
* Replacement of the Java methods getDeclaredMethod()
|
||||||
*/
|
*/
|
||||||
|
@ -73,6 +73,11 @@ public class StructsNonGC extends AbstractBaseTest {
|
|||||||
addParam( list, script, "lambda1" );
|
addParam( list, script, "lambda1" );
|
||||||
addParam( list, script, "lambda2" );
|
addParam( list, script, "lambda2" );
|
||||||
addParam( list, script, "lambda3" );
|
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 );
|
rule.setTestParameters( list );
|
||||||
return list;
|
return list;
|
||||||
@ -227,6 +232,41 @@ public class StructsNonGC extends AbstractBaseTest {
|
|||||||
return JSObject.domString( clazz.getName() );
|
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
|
@Export
|
||||||
static boolean getComponentType() {
|
static boolean getComponentType() {
|
||||||
Class<?> clazz = byte.class;
|
Class<?> clazz = byte.class;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user