]> source.dussan.org Git - vaadin-framework.git/commitdiff
Verify the connector hierarchy if assertions are enabled (#12271)
authorLeif Åstrand <leif@vaadin.com>
Tue, 23 Jul 2013 13:12:49 +0000 (16:12 +0300)
committerLeif Åstrand <leif@vaadin.com>
Tue, 23 Jul 2013 13:12:49 +0000 (16:12 +0300)
Change-Id: I87632abe9d86927b7ba4254724fedfdb04513205

client/src/com/vaadin/client/ApplicationConnection.java
server/src/com/vaadin/ui/ConnectorTracker.java

index 236417f23b783f72abaae58c3f94095c23954b53..0b64f56c3e517028f64338c665cc3bde9943bcf8 100644 (file)
@@ -2000,12 +2000,15 @@ public class ApplicationConnection {
                                     .getConnector(childConnectorId);
                             if (childConnector == null) {
                                 VConsole.error("Hierarchy claims that "
-                                        + childConnectorId + " is a child for "
-                                        + connectorId + " ("
+                                        + childConnectorId
+                                        + " is a child for "
+                                        + connectorId
+                                        + " ("
                                         + parentConnector.getClass().getName()
                                         + ") but no connector with id "
                                         + childConnectorId
-                                        + " has been registered");
+                                        + " has been registered. "
+                                        + "More information might be available in the server-side log if assertions are enabled");
                                 continue;
                             }
                             newChildren.add(childConnector);
index c2aeebcd44b84bc851dfcb80f26211c7e7e0be3c..0f8ec601041505304b40584d22287919600742e1 100644 (file)
@@ -22,6 +22,7 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
@@ -271,6 +272,12 @@ public class ConnectorTracker implements Serializable {
         }
         unregisteredConnectors.clear();
 
+        // Do this expensive check only with assertions enabled
+        assert isHierarchyComplete() : "The connector hierarchy is corrupted. "
+                + "Check for missing calls to super.setParent(), super.attach() and super.detach() "
+                + "and that all custom component containers call child.setParent(this) when a child is added and child.setParent(null) when the child is no longer used. "
+                + "See previous log messages for details.";
+
         // remove detached components from paintableIdMap so they
         // can be GC'ed
         Iterator<String> iterator = connectorIdToConnector.keySet().iterator();
@@ -313,6 +320,49 @@ public class ConnectorTracker implements Serializable {
         cleanStreamVariables();
     }
 
+    private boolean isHierarchyComplete() {
+        boolean noErrors = true;
+
+        Set<ClientConnector> danglingConnectors = new HashSet<ClientConnector>(
+                connectorIdToConnector.values());
+
+        LinkedList<ClientConnector> stack = new LinkedList<ClientConnector>();
+        stack.add(uI);
+        while (!stack.isEmpty()) {
+            ClientConnector connector = stack.pop();
+            danglingConnectors.remove(connector);
+
+            Iterable<ClientConnector> children = AbstractClientConnector
+                    .getAllChildrenIterable(connector);
+            for (ClientConnector child : children) {
+                stack.add(child);
+
+                if (child.getParent() != connector) {
+                    noErrors = false;
+                    getLogger()
+                            .log(Level.WARNING,
+                                    "{0} claims that {1} is its child, but the child claims {2} is its parent.",
+                                    new Object[] {
+                                            getConnectorString(connector),
+                                            getConnectorString(child),
+                                            getConnectorString(child
+                                                    .getParent()) });
+                }
+            }
+        }
+
+        for (ClientConnector dangling : danglingConnectors) {
+            noErrors = false;
+            getLogger()
+                    .log(Level.WARNING,
+                            "{0} claims that {1} is its parent, but the parent does not acknowledge the parenthood.",
+                            new Object[] { getConnectorString(dangling),
+                                    getConnectorString(dangling.getParent()) });
+        }
+
+        return noErrors;
+    }
+
     /**
      * Finds the uI that the connector is attached to.
      *