implement Class.isArray

This commit is contained in:
Volker Berlin 2021-05-23 12:42:37 +02:00
parent 466714bd88
commit 4f4f9ddc29
2 changed files with 38 additions and 12 deletions

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2020 Volker Berlin (i-net software) Copyright 2020 - 2021 Volker Berlin (i-net software)
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -39,6 +39,7 @@ import de.inetsoftware.jwebassembly.module.TypeManager;
/** /**
* Replacement for java.lang.Class * Replacement for java.lang.Class
* *
* @param <T> the type of the class modeled by this {@code Class}
* @author Volker Berlin * @author Volker Berlin
*/ */
@Replace( "java/lang/Class" ) @Replace( "java/lang/Class" )
@ -59,15 +60,6 @@ class ReplacementForClass<T> {
this.vtable = vtable; this.vtable = vtable;
} }
/**
* Replacement for {@link Class#getName()}
*
* @return the name
*/
public String getName() {
return StringTable.stringConstant( getIntFromMemory( vtable + TYPE_DESCRIPTION_TYPE_NAME ) );
}
/** /**
* Replacement for {@link Object#getClass()}. The documentation of the memory of the type description is done in method: * Replacement for {@link Object#getClass()}. The documentation of the memory of the type description is done in method:
* {@link TypeManager.StructType#writeToStream(java.io.ByteArrayOutputStream, java.util.function.ToIntFunction)} * {@link TypeManager.StructType#writeToStream(java.io.ByteArrayOutputStream, java.util.function.ToIntFunction)}
@ -179,6 +171,24 @@ class ReplacementForClass<T> {
"return" ) "return" )
private static native int getIntFromMemory( int pos ); private static native int getIntFromMemory( int pos );
/**
* Replacement of the Java methods isArray()
* @return {@code true} if this object represents an array class;
*/
public boolean isArray() {
int classIdx = getIntFromMemory( vtable + TYPE_DESCRIPTION_ARRAY_TYPE );
return classIdx >= 0;
}
/**
* Replacement for {@link Class#getName()}
*
* @return the name
*/
public String getName() {
return StringTable.stringConstant( getIntFromMemory( vtable + TYPE_DESCRIPTION_TYPE_NAME ) );
}
/** /**
* Replacement of the Java methods * Replacement of the Java methods
*/ */
@ -187,7 +197,7 @@ class ReplacementForClass<T> {
} }
/** /**
* Replacement of the Java methods * Replacement of the Java methods getClassLoader0()
*/ */
ClassLoader getClassLoader0() { ClassLoader getClassLoader0() {
return null; return null;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2018 - 2019 Volker Berlin (i-net software) * Copyright 2018 - 2021 Volker Berlin (i-net software)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -85,6 +85,8 @@ public class ArrayOperations extends AbstractBaseTest {
addParam( list, script, "booleanArrayComponentTypeClassName" ); addParam( list, script, "booleanArrayComponentTypeClassName" );
addParam( list, script, "objectArrayComponentTypeClassName" ); addParam( list, script, "objectArrayComponentTypeClassName" );
addParam( list, script, "arrayNewInstance_getLength" ); addParam( list, script, "arrayNewInstance_getLength" );
addParam( list, script, "isArrayOfArray" );
addParam( list, script, "isArrayOfObject" );
} }
rule.setTestParameters( list ); rule.setTestParameters( list );
return list; return list;
@ -328,5 +330,19 @@ public class ArrayOperations extends AbstractBaseTest {
Object obj = Array.newInstance( byte.class, 42 ); Object obj = Array.newInstance( byte.class, 42 );
return Array.getLength( obj ); return Array.getLength( obj );
} }
@Export
static boolean isArrayOfArray() {
Object obj = new Object[42];
Class<?> clazz = obj.getClass();
return clazz.isArray();
}
@Export
static boolean isArrayOfObject() {
Object obj = new Object();
Class<?> clazz = obj.getClass();
return clazz.isArray();
}
} }
} }