Replacement for asSubclass()

This commit is contained in:
Volker Berlin 2022-07-16 21:59:51 +02:00
parent 0ff60a2809
commit 56ac30b3b1
No known key found for this signature in database
GPG Key ID: 988423EF815BE4CB

View File

@ -229,7 +229,7 @@ class ReplacementForClass<T> {
* @return true, if type {@code cls} can be assigned to objects of this class
*/
@WasmTextCode( "unreachable" ) // TODO
public native boolean isAssignableFrom( Class<?> cls );
public native boolean isAssignableFrom( ReplacementForClass<?> cls );
/**
* Replacement of the Java method isInterface()
@ -501,4 +501,19 @@ class ReplacementForClass<T> {
throw new ClassCastException("Cannot cast " + obj.getClass().getName() + " to " + getName());
return (T) obj;
}
/**
* Replacement of the Java method asSubclass(Class)
*
* @param clazz
* the class of the type to cast this class object to
* @return this
*/
@SuppressWarnings( "unchecked" )
public <U> ReplacementForClass<? extends U> asSubclass( ReplacementForClass<U> clazz ) {
if( clazz.isAssignableFrom( this ) )
return (ReplacementForClass<? extends U>)this;
else
throw new ClassCastException( this.toString() );
}
}