Skip to content

Commit

Permalink
refactor: use reflection for Vaadin 24 binary compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-godoy authored and paodb committed Jun 14, 2024
1 parent a2729a6 commit 277130c
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
import com.vaadin.flow.router.RouterLink;
import com.vaadin.flow.shared.Registration;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -198,6 +201,23 @@ public void add(Tab... tabs) {
}
}

private static final Method UI_navigate;
static {
try {
UI_navigate = UI.class.getMethod("navigate", Class.class);
} catch (NoSuchMethodException e) {
throw new NoSuchMethodError("UI.navigate(Class)");
}
}

private static void navigate(UI ui, Class<? extends Component> target) {
try {
UI_navigate.invoke(ui, target);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new UndeclaredThrowableException(e);
}
}

public RouterLink addRouterLink(String text, Class<? extends Component> target) {
RouterLink routerLink = new RouterLink(text, target);
routerLink.getElement().executeJs(
Expand All @@ -207,7 +227,7 @@ public RouterLink addRouterLink(String text, Class<? extends Component> target)
"});\n");

routerLink.getElement().addEventListener("client-side-click", event -> {
UI.getCurrent().navigate(target);
navigate(UI.getCurrent(), target);
});

add(new Tab(routerLink));
Expand Down

0 comments on commit 277130c

Please sign in to comment.