Skip to content

Commit

Permalink
feat: Pass Java class info to browser in dev mode (#18892)
Browse files Browse the repository at this point in the history
If the mode cannot be determined (missing session or configuration), do not send anything
  • Loading branch information
Artur- authored Mar 15, 2024
1 parent e36214b commit 7f6e320
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.vaadin.client.flow.binding.Binder;
import com.vaadin.client.flow.dom.DomApi;
import com.vaadin.client.flow.util.NativeFunction;
import com.vaadin.flow.internal.nodefeature.NodeFeatures;
import com.vaadin.flow.internal.nodefeature.NodeProperties;

import elemental.client.Browser;
import elemental.dom.Element;
Expand Down Expand Up @@ -166,6 +168,12 @@ private native void publishJavascriptMethods(String applicationId,
client.getByNodeId = $entry(function(nodeId) {
return ap.@ApplicationConnection::getDomElementByNodeId(*)(nodeId);
});
client.getNodeInfo = $entry(function(nodeId) {
return {
element: ap.@ApplicationConnection::getDomElementByNodeId(*)(nodeId),
javaClass: ap.@ApplicationConnection::getJavaClass(*)(nodeId)
};
});
client.getNodeId = $entry(function(element) {
return ap.@ApplicationConnection::getNodeId(*)(element);
});
Expand Down Expand Up @@ -222,6 +230,15 @@ private native void publishJavascriptMethods(String applicationId,
private Node getDomElementByNodeId(int id) {
StateNode node = registry.getStateTree().getNode(id);
return node == null ? null : node.getDomNode();

}

private String getJavaClass(int id) {
StateNode node = registry.getStateTree().getNode(id);
return node == null ? null
: node.getMap(NodeFeatures.ELEMENT_DATA)
.getProperty(NodeProperties.JAVA_CLASS)
.getValueOrDefault(null);
}

private int getNodeId(Element element) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
import com.vaadin.flow.internal.ReflectionCache;
import com.vaadin.flow.internal.StateNode;
import com.vaadin.flow.internal.StateTree;
import com.vaadin.flow.internal.nodefeature.ElementData;
import com.vaadin.flow.internal.nodefeature.VirtualChildrenList;
import com.vaadin.flow.router.Router;
import com.vaadin.flow.server.Attributes;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.shared.Registration;

/**
Expand Down Expand Up @@ -245,6 +247,26 @@ && isAttachedToParent(component.getElement(), parent)) {
component.getElement().isEnabled());
}
}

ComponentUtil.setJavaClassNameInDevelopment(component);
}

private static void setJavaClassNameInDevelopment(Component component) {
Optional<UI> ui = component.getUI();
if (ui.isEmpty()) {
return;
}
VaadinSession session = ui.get().getSession();
if (session == null || session.getConfiguration() == null
|| session.getConfiguration().isProductionMode()) {
return;
}

StateNode n = component.getElement().getNode();
if (n.hasFeature(ElementData.class)) {
n.getFeature(ElementData.class).setJavaClass(component.getClass());

}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.Serializable;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.internal.StateNode;

import elemental.json.JsonValue;
Expand Down Expand Up @@ -105,4 +106,12 @@ public JsonValue getPayload() {
public boolean allowsChanges() {
return isVisible();
}

public void setJavaClass(Class<? extends Component> componentClass) {
put(NodeProperties.JAVA_CLASS, componentClass.getName());
}

public String getJavaClass() {
return getOrDefault(NodeProperties.JAVA_CLASS, (String) null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public final class NodeProperties {
*/
public static final String PAYLOAD = "payload";

/**
* Key for {@link ElementData#getJavaClass()}.
*/
public static final String JAVA_CLASS = "jc";

/**
* Key for {@link TextNodeMap#getText()}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.shared.Registration;
import com.vaadin.flow.shared.ui.Dependency;
import com.vaadin.tests.util.AlwaysLockedVaadinSession;
import com.vaadin.tests.util.MockDeploymentConfiguration;
import com.vaadin.tests.util.MockUI;
import com.vaadin.tests.util.TestUtil;
Expand Down Expand Up @@ -836,8 +837,9 @@ public void testUIInitialAttach() {
});

MockDeploymentConfiguration config = new MockDeploymentConfiguration();
ui.getInternals().setSession(
new VaadinSession(new MockVaadinServletService(config)));
VaadinSession session = new AlwaysLockedVaadinSession(
new MockVaadinServletService(config));
ui.getInternals().setSession(session);
Assert.assertTrue(initialAttach.get());
// UI is never detached and reattached
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,12 @@ Logger getLogger() {

private static MockUI createAccessableTestUI() {
// Needs a service to be able to do service.accessSession
return new MockUI(
new MockVaadinSession(new MockVaadinServletService()));
MockVaadinSession session = new MockVaadinSession(
new MockVaadinServletService());
session.lock();
MockUI ui = new MockUI(session);
session.unlock();
return ui;
}

private static void initUI(UI ui, String initialLocation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.vaadin.flow.server.MockVaadinSession;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.tests.util.AlwaysLockedVaadinSession;
import com.vaadin.tests.util.MockUI;

public class RoutingTestBase {
Expand Down Expand Up @@ -79,7 +80,7 @@ public RouterTestMockUI(Router router) {
private static VaadinSession createMockSession(Router router) {
MockVaadinServletService service = new MockVaadinServletService();
service.setRouter(router);
return new MockVaadinSession(service);
return new AlwaysLockedVaadinSession(service);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public <T extends HasElement> T createRouteTarget(
return (T) new Text("foo");
}
});
MockUI ui = new MockUI(new MockVaadinSession(service));
MockUI ui = new MockUI(new AlwaysLockedVaadinSession(service));

NavigationEvent event = new NavigationEvent(
new Router(new TestRouteRegistry()), new Location(""), ui,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private static class AnotherCustomRouteNotFound extends RouteNotFoundError {
@Before
public void setUp() throws ServiceException {
MockVaadinSession session = new MockVaadinSession();
session.lock();
ui = new UI();
ui.getInternals().setSession(session);
}
Expand Down

0 comments on commit 7f6e320

Please sign in to comment.