Skip to content

Commit e7ff8be

Browse files
javier-godoypaodb
authored andcommitted
feat: add support for Vaadin 25
Close #104 Close #105
1 parent 6d6e84e commit e7ff8be

4 files changed

Lines changed: 50 additions & 4 deletions

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@
8686
<optional>true</optional>
8787
</dependency>
8888

89+
<dependency>
90+
<groupId>com.flowingcode.vaadin</groupId>
91+
<artifactId>json-migration-helper</artifactId>
92+
<version>0.9.2</version>
93+
</dependency>
94+
8995
<dependency>
9096
<groupId>org.slf4j</groupId>
9197
<artifactId>slf4j-simple</artifactId>

src/main/java/com/flowingcode/vaadin/addons/xterm/ClientTerminalAddon.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* XTerm Console Addon
44
* %%
5-
* Copyright (C) 2020 - 2025 Flowing Code
5+
* Copyright (C) 2020 - 2026 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -19,11 +19,17 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.xterm;
2121

22+
import com.flowingcode.vaadin.jsonmigration.JsonMigration;
2223
import com.vaadin.flow.dom.Element;
23-
import com.vaadin.flow.internal.JsonCodec;
24+
import com.vaadin.flow.server.Version;
2425
import elemental.json.Json;
2526
import elemental.json.JsonArray;
27+
import elemental.json.JsonValue;
2628
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;
2733

2834
/**
2935
* 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) {
96102

97103
JsonArray args = Json.createArray();
98104
for (int i = 0; i < parameters.length; i++) {
99-
args.set(i, JsonCodec.encodeWithTypeInfo(parameters[i]));
105+
args.set(i, encodeWithTypeInfo(parameters[i]));
100106
}
101107

102108
expression = expression.replaceAll("\\$(\\d+)", "\\$1[$1]");
103109
xterm.executeJs("(function(){" + expression + "}).apply(this.addons[$0],$1);", name, args);
104110
}
105111

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+
106140
}

src/main/java/com/flowingcode/vaadin/addons/xterm/ITerminalConsole.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* #%L
33
* XTerm Console Addon
44
* %%
5-
* Copyright (C) 2020 - 2023 Flowing Code
5+
* Copyright (C) 2020 - 2026 Flowing Code
66
* %%
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.xterm;
2121

22+
import com.flowingcode.vaadin.jsonmigration.JsonMigration;
2223
import com.vaadin.flow.component.Component;
2324
import com.vaadin.flow.component.ComponentEvent;
2425
import com.vaadin.flow.component.ComponentEventListener;
@@ -30,12 +31,14 @@
3031
import elemental.json.JsonValue;
3132
import java.util.concurrent.CompletableFuture;
3233
import lombok.Getter;
34+
import lombok.experimental.ExtensionMethod;
3335

3436
/**
3537
* Add console support to XTerm. This provides handling of cursor, home/end, insert, delete and
3638
* backspace keys, as well as a {@link #addLineListener(ComponentEventListener) line event}.
3739
*/
3840
@SuppressWarnings("serial")
41+
@ExtensionMethod(value = JsonMigration.class, suppressBaseMethods = true)
3942
public interface ITerminalConsole extends HasElement {
4043

4144
// TODO set cursor properties (blink, style, width) separately for insert and overwrite modes

src/main/java/com/flowingcode/vaadin/addons/xterm/XTermBase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
package com.flowingcode.vaadin.addons.xterm;
2121

22+
import com.flowingcode.vaadin.jsonmigration.JsonMigration;
2223
import com.vaadin.flow.component.Component;
2324
import com.vaadin.flow.component.HasEnabled;
2425
import com.vaadin.flow.component.HasSize;
@@ -55,12 +56,14 @@
5556
import java.util.stream.Collectors;
5657
import java.util.stream.IntStream;
5758
import lombok.experimental.Delegate;
59+
import lombok.experimental.ExtensionMethod;
5860

5961
/** Server-side component for the XTerm component. */
6062
@SuppressWarnings("serial")
6163
@NpmPackage(value = "xterm", version = "5.1.0")
6264
@JsModule("./fc-xterm/xterm-element.ts")
6365
@CssImport("xterm/css/xterm.css")
66+
@ExtensionMethod(value = JsonMigration.class, suppressBaseMethods = true)
6467
public abstract class XTermBase extends Component
6568
implements ITerminal, ITerminalOptions, HasSize, HasEnabled {
6669

0 commit comments

Comments
 (0)