From 0dfb7c4a34b1f930e3a162c4276f78cdf1f2fbb9 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sat, 16 Apr 2022 17:27:02 +0200 Subject: [PATCH] add forName(String,ClassLoader) and cast(Object) --- .../nativecode/ReplacementForClass.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/de/inetsoftware/jwebassembly/module/nativecode/ReplacementForClass.java b/src/de/inetsoftware/jwebassembly/module/nativecode/ReplacementForClass.java index 7a40a57..2f6291e 100644 --- a/src/de/inetsoftware/jwebassembly/module/nativecode/ReplacementForClass.java +++ b/src/de/inetsoftware/jwebassembly/module/nativecode/ReplacementForClass.java @@ -191,6 +191,17 @@ class ReplacementForClass { throw new ClassNotFoundException( className ); // TODO } + /** + * Replacement of the Java method forName(String) + * + * @param name + * the fully qualified name of the desired class. + * @return the {@code Class} object for the class with the specified name. + */ + public static Class forName(String name, boolean initialize, ClassLoader loader) throws ClassNotFoundException { + return forName( name ); + } + /** * Replacement of the Java method newInstance() * @@ -470,4 +481,17 @@ class ReplacementForClass { */ @WasmTextCode( "unreachable" ) // TODO native Map enumConstantDirectory(); + + /** + * Replacement of the Java method cast(Object) + * + * @param obj the object to be cast + * @return the object after casting, or null if obj is null + */ + @SuppressWarnings("unchecked") + public T cast(Object obj) { + if (obj != null && !isInstance(obj)) + throw new ClassCastException("Cannot cast " + obj.getClass().getName() + " to " + getName()); + return (T) obj; + } }