|
2 | 2 | * #%L |
3 | 3 | * XTerm Console Addon |
4 | 4 | * %% |
5 | | - * Copyright (C) 2020 - 2025 Flowing Code |
| 5 | + * Copyright (C) 2020 - 2026 Flowing Code |
6 | 6 | * %% |
7 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | 8 | * you may not use this file except in compliance with the License. |
|
19 | 19 | */ |
20 | 20 | package com.flowingcode.vaadin.addons.xterm; |
21 | 21 |
|
| 22 | +import com.flowingcode.vaadin.jsonmigration.JsonMigration; |
22 | 23 | import com.vaadin.flow.dom.Element; |
23 | | -import com.vaadin.flow.internal.JsonCodec; |
| 24 | +import com.vaadin.flow.server.Version; |
24 | 25 | import elemental.json.Json; |
25 | 26 | import elemental.json.JsonArray; |
| 27 | +import elemental.json.JsonValue; |
26 | 28 | import java.io.Serializable; |
| 29 | +import java.lang.invoke.MethodHandle; |
| 30 | +import java.lang.invoke.MethodHandles; |
| 31 | +import java.lang.invoke.MethodType; |
| 32 | +import lombok.SneakyThrows; |
27 | 33 |
|
28 | 34 | /** |
29 | 35 | * Represents an abstract base class for server-side terminal add-ons that have a corresponding |
@@ -96,11 +102,39 @@ protected final void executeJs(String expression, Serializable... parameters) { |
96 | 102 |
|
97 | 103 | JsonArray args = Json.createArray(); |
98 | 104 | for (int i = 0; i < parameters.length; i++) { |
99 | | - args.set(i, JsonCodec.encodeWithTypeInfo(parameters[i])); |
| 105 | + args.set(i, encodeWithTypeInfo(parameters[i])); |
100 | 106 | } |
101 | 107 |
|
102 | 108 | expression = expression.replaceAll("\\$(\\d+)", "\\$1[$1]"); |
103 | 109 | xterm.executeJs("(function(){" + expression + "}).apply(this.addons[$0],$1);", name, args); |
104 | 110 | } |
105 | 111 |
|
| 112 | + private static final MethodHandle encodeWithTypeInfo = lookup_encodeWithTypeInfo(); |
| 113 | + |
| 114 | + @SneakyThrows |
| 115 | + private static MethodHandle lookup_encodeWithTypeInfo() { |
| 116 | + MethodHandle handle; |
| 117 | + if (Version.getMajorVersion() > 24) { |
| 118 | + Class<?> result = Class.forName("tools.jackson.databind.JsonNode"); |
| 119 | + Class<?> codec = Class.forName("com.vaadin.flow.internal.JacksonCodec"); |
| 120 | + MethodType type = MethodType.methodType(result, Object.class); |
| 121 | + handle = MethodHandles.lookup().findStatic(codec, "encodeWithTypeInfo", type); |
| 122 | + } else { |
| 123 | + Class<?> codec = Class.forName("com.vaadin.flow.internal.JsonCodec"); |
| 124 | + MethodType type = MethodType.methodType(JsonValue.class, Object.class); |
| 125 | + handle = MethodHandles.lookup().findStatic(codec, "encodeWithTypeInfo", type); |
| 126 | + } |
| 127 | + return handle.asType(MethodType.methodType(Object.class, Object.class)); |
| 128 | + } |
| 129 | + |
| 130 | + private static JsonValue encodeWithTypeInfo(Object obj) { |
| 131 | + try { |
| 132 | + return JsonMigration.convertToJsonValue(encodeWithTypeInfo.invokeExact(obj)); |
| 133 | + } catch (RuntimeException | Error e) { |
| 134 | + throw e; |
| 135 | + } catch (Throwable e) { |
| 136 | + throw new RuntimeException(e); |
| 137 | + } |
| 138 | + } |
| 139 | + |
106 | 140 | } |
0 commit comments