Class JSBridge
java.lang.Object
org.daiitech.naftah.playground.utils.JSBridge
Utility bridge between Java and JavaScript (JSObject).
Provides helper methods to convert JavaScript arrays/objects into
Java Map, List, and arrays.
- Author:
- Chakib Daii
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static ObjectcallJSMethod(String methodName, Object... objs) Calls a JavaScript method on the given object.static ObjectgetAtIndex(JSObject obj, int index) Retrieves an element at a given index from a JavaScript array.static ObjectgetAtIndex(JSObject obj, String methodName, int index) Internal helper to access a JavaScript array-like structure using a JS method.static ObjectgetEntryAtIndex(JSObject obj, int index) Retrieves a JS object entry at a given index.static intRetrieves the length of a JavaScript array-like object.private static JSObjectRetrieves the JavaScript global window object.static <R> R[]Converts a JavaScript array into a Java array.static <R> List<R>Converts a JavaScript array into a JavaList.Converts a JavaScript array of key-value entries into a JavaMap.
-
Constructor Details
-
JSBridge
private JSBridge()Private constructor to prevent instantiation. ThrowsNaftahBugErrorif called.
-
-
Method Details
-
toMap
Converts a JavaScript array of key-value entries into a JavaMap.- Type Parameters:
R- value type of resulting map- Parameters:
jsEntries- JavaScript object representing an array of entriesentryValueMapper- function used to convert JS value into type R- Returns:
- a
LinkedHashMappreserving JS iteration order
-
toList
Converts a JavaScript array into a JavaList.- Type Parameters:
R- element type- Parameters:
jsArray- JavaScript array objectarrayValueMapper- function to convert JS values into type R- Returns:
- a
ArrayListcontaining converted values
-
toArray
Converts a JavaScript array into a Java array.- Type Parameters:
R- element type- Parameters:
jsArray- JavaScript array objecttype- component type of resulting arrayarrayValueMapper- function to convert JS values into type R- Returns:
- a Java array of type R
-
getLength
Retrieves the length of a JavaScript array-like object.- Parameters:
obj- JavaScript object- Returns:
- length as integer, or 0 if unavailable
-
getAtIndex
Retrieves an element at a given index from a JavaScript array.- Parameters:
obj- JavaScript arrayindex- index to access- Returns:
- value at index, or null if unavailable
-
getEntryAtIndex
Retrieves a JS object entry at a given index.- Parameters:
obj- JavaScript array of entriesindex- index to access- Returns:
- entry object, or null if unavailable
-
getAtIndex
Internal helper to access a JavaScript array-like structure using a JS method.- Parameters:
obj- JavaScript objectmethodName- JavaScript method name to invokeindex- index parameter- Returns:
- result of JS call, or null if unavailable
-
callJSMethod
Calls a JavaScript method on the given object.- Parameters:
methodName- JS method nameobjs- arguments (first argument must be JSObject target)- Returns:
- result of JS call
-
getWindow
Retrieves the JavaScript global window object.- Parameters:
anyJsObject- any JSObject instance- Returns:
- globalThis JS object
-