Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ public static void setPropertyJson(Element element, String name, JsonValue json)
invoke(Element_setPropertyJson, element, name, json);
}

private static Method Element_executeJs = lookup_executeJs();

@SneakyThrows
private static Method lookup_executeJs() {
if (Version.getMajorVersion() > 24) {
return Element.class.getMethod("executeJs", String.class, Object[].class);
} else {
return Element.class.getMethod("executeJs", String.class, Serializable[].class);
}
}

/**
* Asynchronously runs the given JavaScript expression in the browser in the context of this
* element.
Expand All @@ -128,7 +139,8 @@ public static void setPropertyJson(Element element, String name, JsonValue json)
*/
public static ElementalPendingJavaScriptResult executeJs(Element element, String expression,
Serializable... parameters) {
PendingJavaScriptResult result = element.executeJs(expression, parameters);
PendingJavaScriptResult result =
(PendingJavaScriptResult) invoke(Element_executeJs, element, expression, parameters);
return helper.convertPendingJavaScriptResult(result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import elemental.json.JsonArray;
import elemental.json.JsonObject;
import elemental.json.JsonValue;
import java.lang.reflect.Array;
import java.util.concurrent.CompletableFuture;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -58,22 +59,63 @@ public JsonValue convertToJsonValue(Object object) {
@SneakyThrows
public Object invoke(Method method, Object instance, Object... args) {
Object[] convertedArgs = null;

int j = args.length - 1;
Class<?> parameterTypes[] = method.getParameterTypes();
for (int i = 0; i < parameterTypes.length; i++) {
if (args[i] instanceof JsonValue && parameterTypes[i] == BaseJsonNode.class) {

if (convertedArgs == null) {
if (args.length == parameterTypes.length) {
if (method.isVarArgs() && args[j] instanceof Object[]) {
Object[] convertedArray =
convertArray((Object[]) args[j], parameterTypes[j].getComponentType());
if (convertedArray != null) {
convertedArgs = Arrays.copyOf(args, args.length);
convertedArgs[j] = convertedArray;
}
}

for (int i = 0; i < parameterTypes.length; i++) {
if (args[i] instanceof JsonValue && parameterTypes[i] == BaseJsonNode.class) {

if (convertedArgs == null) {
convertedArgs = Arrays.copyOf(args, args.length);
}
convertedArgs[i] = convertToJsonNode((JsonValue) args[i]);
}
convertedArgs[i] = convertToJsonNode((JsonValue) args[i]);
}
}

if (convertedArgs == null) {
convertedArgs = args;
}
return method.invoke(instance, convertedArgs);
}


private static <T> T[] convertArray(Object[] array, Class<? extends T> newType) {
T[] convertedArray = null;
if (newType.isAssignableFrom(BaseJsonNode.class)) {
for (int i = 0; i < array.length; i++) {
if (array[i] instanceof JsonValue) {
if (convertedArray == null) {
@SuppressWarnings("unchecked")
T[] copy = (newType == Object.class)
? (T[]) new Object[array.length]
: (T[]) Array.newInstance(newType, array.length);
if (i>0) {
System.arraycopy(array, 0, copy, 0, i);
}
convertedArray = copy;
}
@SuppressWarnings("unchecked")
T t = (T) convertToJsonNode((JsonValue) array[i]);
convertedArray[i] = t;
} else if (convertedArray != null) {
convertedArray[i] = newType.cast(array[i]);
}
}
}
return convertedArray;
}

private static JsonValue convertToJsonValue(JsonNode jsonNode) {
switch (jsonNode.getNodeType()) {
case OBJECT:
Expand Down