]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add logging and tweak visitor API (#9371)
authorLeif Åstrand <leif@vaadin.com>
Tue, 21 Aug 2012 06:06:21 +0000 (09:06 +0300)
committerLeif Åstrand <leif@vaadin.com>
Wed, 22 Aug 2012 16:25:31 +0000 (19:25 +0300)
client-compiler/src/com/vaadin/terminal/gwt/widgetsetutils/ConnectorBundleLoaderFactory.java
client-compiler/src/com/vaadin/terminal/gwt/widgetsetutils/metadata/ConnectorBundle.java
client-compiler/src/com/vaadin/terminal/gwt/widgetsetutils/metadata/ConnectorInitVisitor.java
client-compiler/src/com/vaadin/terminal/gwt/widgetsetutils/metadata/StateInitVisitor.java
client-compiler/src/com/vaadin/terminal/gwt/widgetsetutils/metadata/TypeVisitor.java
client-compiler/src/com/vaadin/terminal/gwt/widgetsetutils/metadata/WidgetInitVisitor.java

index 893bebfd0da6faf09cf93431c33623cbab19363d..e0c88ced66a817be81edf9948fd9e040553cf063 100644 (file)
@@ -8,7 +8,6 @@ import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -259,22 +258,26 @@ public class ConnectorBundleLoaderFactory extends Generator {
         Collection<TypeVisitor> visitors = getVisitors(typeOracle);
 
         ConnectorBundle eagerBundle = new ConnectorBundle(
-                ConnectorBundleLoader.EAGER_BUNDLE_NAME, null);
+                ConnectorBundleLoader.EAGER_BUNDLE_NAME, visitors);
+        TreeLogger eagerLogger = logger.branch(Type.TRACE,
+                "Populating eager bundle");
 
         // Eager connectors and all RPC interfaces are loaded by default
-        eagerBundle.visitTypes(logger,
-                connectorsByLoadStyle.get(LoadStyle.EAGER), visitors);
-        eagerBundle.visitSubTypes(logger,
-                typeOracle.getType(ClientRpc.class.getName()), visitors);
-        eagerBundle.visitSubTypes(logger,
-                typeOracle.getType(ServerRpc.class.getName()), visitors);
+        eagerBundle.processTypes(eagerLogger,
+                connectorsByLoadStyle.get(LoadStyle.EAGER));
+        eagerBundle.processSubTypes(eagerLogger,
+                typeOracle.getType(ClientRpc.class.getName()));
+        eagerBundle.processSubTypes(eagerLogger,
+                typeOracle.getType(ServerRpc.class.getName()));
 
         bundles.add(eagerBundle);
 
         ConnectorBundle deferredBundle = new ConnectorBundle(
                 ConnectorBundleLoader.DEFERRED_BUNDLE_NAME, eagerBundle);
-        deferredBundle.visitTypes(logger,
-                connectorsByLoadStyle.get(LoadStyle.DEFERRED), visitors);
+        TreeLogger deferredLogger = logger.branch(Type.TRACE,
+                "Populating deferred bundle");
+        deferredBundle.processTypes(deferredLogger,
+                connectorsByLoadStyle.get(LoadStyle.DEFERRED));
 
         bundles.add(deferredBundle);
 
@@ -282,7 +285,9 @@ public class ConnectorBundleLoaderFactory extends Generator {
         for (JClassType type : lazy) {
             ConnectorBundle bundle = new ConnectorBundle(type.getName(),
                     deferredBundle);
-            bundle.visitTypes(logger, Collections.singleton(type), visitors);
+            TreeLogger subLogger = logger.branch(Type.TRACE, "Populating "
+                    + type.getName() + " bundle");
+            bundle.processType(subLogger, type);
 
             bundles.add(bundle);
         }
index f734b77101ea7342656ac0ff74accfa1866b7da5..26e293351c713fdc3ddcb6d78c0d4327980c8024 100644 (file)
@@ -9,6 +9,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
 
@@ -18,6 +19,11 @@ import com.google.gwt.core.ext.UnableToCompleteException;
 import com.google.gwt.core.ext.typeinfo.JClassType;
 import com.google.gwt.core.ext.typeinfo.JMethod;
 import com.google.gwt.core.ext.typeinfo.NotFoundException;
+import com.vaadin.shared.communication.ClientRpc;
+import com.vaadin.shared.communication.ServerRpc;
+import com.vaadin.shared.ui.Connect;
+import com.vaadin.terminal.gwt.client.ComponentConnector;
+import com.vaadin.terminal.gwt.client.ServerConnector;
 
 public class ConnectorBundle {
     private final String name;
@@ -29,18 +35,26 @@ public class ConnectorBundle {
     private final Set<JClassType> visitQueue = new HashSet<JClassType>();
     private final Map<JClassType, Set<JMethod>> needsReturnType = new HashMap<JClassType, Set<JMethod>>();
 
-    private boolean visiting = false;
+    private final Collection<TypeVisitor> visitors;
 
-    public ConnectorBundle(String name, ConnectorBundle previousBundle) {
+    private ConnectorBundle(String name, ConnectorBundle previousBundle,
+            Collection<TypeVisitor> visitors) {
         this.name = name;
         this.previousBundle = previousBundle;
+        this.visitors = visitors;
+    }
+
+    public ConnectorBundle(String name, ConnectorBundle previousBundle) {
+        this(name, previousBundle, previousBundle.visitors);
+    }
+
+    public ConnectorBundle(String name, Collection<TypeVisitor> visitors) {
+        this(name, null, visitors);
     }
 
     public void setNeedsGwtConstructor(JClassType type) {
         if (!needsGwtConstructor(type)) {
-            if (!isTypeVisited(type)) {
-                visitQueue.add(type);
-            }
+            ensureVisited(type);
             needsGwtConstructor.add(type);
         }
     }
@@ -56,9 +70,7 @@ public class ConnectorBundle {
 
     public void setIdentifier(JClassType type, String identifier) {
         if (!hasIdentifier(type, identifier)) {
-            if (!isTypeVisited(type)) {
-                visitQueue.add(type);
-            }
+            ensureVisited(type);
             Set<String> set = identifiers.get(type);
             if (set == null) {
                 set = new HashSet<String>();
@@ -94,14 +106,17 @@ public class ConnectorBundle {
         return Collections.unmodifiableSet(needsGwtConstructor);
     }
 
-    public void visitTypes(TreeLogger logger, Collection<JClassType> types,
-            Collection<TypeVisitor> visitors) throws UnableToCompleteException {
+    public void processTypes(TreeLogger logger, Collection<JClassType> types)
+            throws UnableToCompleteException {
         for (JClassType type : types) {
-            if (!isTypeVisited(type)) {
-                visitQueue.add(type);
-            }
+            processType(logger, type);
         }
-        visitQueue(logger, visitors);
+    }
+
+    public void processType(TreeLogger logger, JClassType type)
+            throws UnableToCompleteException {
+        ensureVisited(type);
+        purgeQueue(logger);
     }
 
     private boolean isTypeVisited(JClassType type) {
@@ -112,33 +127,50 @@ public class ConnectorBundle {
         }
     }
 
-    private void visitQueue(TreeLogger logger, Collection<TypeVisitor> visitors)
-            throws UnableToCompleteException {
+    private void ensureVisited(JClassType type) {
+        if (!isTypeVisited(type)) {
+            visitQueue.add(type);
+        }
+    }
+
+    private void purgeQueue(TreeLogger logger) throws UnableToCompleteException {
         while (!visitQueue.isEmpty()) {
-            JClassType type = visitQueue.iterator().next();
+            Iterator<JClassType> iterator = visitQueue.iterator();
+            JClassType type = iterator.next();
+            iterator.remove();
+
+            if (isTypeVisited(type)) {
+                continue;
+            }
             for (TypeVisitor typeVisitor : visitors) {
-                try {
-                    typeVisitor.visit(type, this);
-                } catch (NotFoundException e) {
-                    logger.log(Type.ERROR, e.getMessage(), e);
-                    throw new UnableToCompleteException();
-                }
+                invokeVisitor(logger, type, typeVisitor);
             }
-            visitQueue.remove(type);
             visitedTypes.add(type);
         }
     }
 
-    public void visitSubTypes(TreeLogger logger, JClassType type,
-            Collection<TypeVisitor> visitors) throws UnableToCompleteException {
-        visitTypes(logger, Arrays.asList(type.getSubtypes()), visitors);
+    private void invokeVisitor(TreeLogger logger, JClassType type,
+            TypeVisitor typeVisitor) throws UnableToCompleteException {
+        TreeLogger subLogger = logger.branch(Type.TRACE,
+                "Visiting " + type.getName() + " with "
+                        + typeVisitor.getClass().getSimpleName());
+        if (isConnectedConnector(type)) {
+            typeVisitor.visitConnector(subLogger, type, this);
+        } else if (isClientRpc(type)) {
+            typeVisitor.visitClientRpc(subLogger, type, this);
+        } else if (isServerRpc(type)) {
+            typeVisitor.visitServerRpc(subLogger, type, this);
+        }
+    }
+
+    public void processSubTypes(TreeLogger logger, JClassType type)
+            throws UnableToCompleteException {
+        processTypes(logger, Arrays.asList(type.getSubtypes()));
     }
 
     public void setNeedsReturnType(JClassType type, JMethod method) {
         if (!isNeedsReturnType(type, method)) {
-            if (!isTypeVisited(type)) {
-                visitQueue.add(type);
-            }
+            ensureVisited(type);
             Set<JMethod> set = needsReturnType.get(type);
             if (set == null) {
                 set = new HashSet<JMethod>();
@@ -161,4 +193,34 @@ public class ConnectorBundle {
     public Map<JClassType, Set<JMethod>> getMethodReturnTypes() {
         return Collections.unmodifiableMap(needsReturnType);
     }
+
+    private static boolean isClientRpc(JClassType type) {
+        return isType(type, ClientRpc.class);
+    }
+
+    private static boolean isServerRpc(JClassType type) {
+        return isType(type, ServerRpc.class);
+    }
+
+    public static boolean isConnectedConnector(JClassType type) {
+        return isConnected(type) && isType(type, ServerConnector.class);
+    }
+
+    private static boolean isConnected(JClassType type) {
+        return type.isAnnotationPresent(Connect.class);
+    }
+
+    public static boolean isConnectedComponentConnector(JClassType type) {
+        return isConnected(type) && isType(type, ComponentConnector.class);
+    }
+
+    private static boolean isType(JClassType type, Class<?> class1) {
+        try {
+            return type.getOracle().getType(class1.getName())
+                    .isAssignableFrom(type);
+        } catch (NotFoundException e) {
+            throw new RuntimeException("Could not find " + class1.getName(), e);
+        }
+    }
+
 }
\ No newline at end of file
index 6dd840d612aeb0ed9a07f803290fb382c309f11e..09cdaddc31c928ece84f9986e0b23548c8c42054 100644 (file)
@@ -4,19 +4,21 @@
 
 package com.vaadin.terminal.gwt.widgetsetutils.metadata;
 
+import com.google.gwt.core.ext.TreeLogger;
+import com.google.gwt.core.ext.TreeLogger.Type;
 import com.google.gwt.core.ext.typeinfo.JClassType;
 import com.vaadin.shared.ui.Connect;
 
 public class ConnectorInitVisitor extends TypeVisitor {
 
     @Override
-    public void visit(JClassType type, ConnectorBundle bundle) {
-        if (isConnectedConnector(type)) {
-            Connect connectAnnotation = type.getAnnotation(Connect.class);
-            bundle.setIdentifier(type, connectAnnotation.value()
-                    .getCanonicalName());
-            bundle.setNeedsGwtConstructor(type);
-        }
+    public void visitConnector(TreeLogger logger, JClassType type,
+            ConnectorBundle bundle) {
+        logger.log(Type.INFO, type.getName() + " will be in the "
+                + bundle.getName().replaceAll("^_*", "") + " bundle");
+        Connect connectAnnotation = type.getAnnotation(Connect.class);
+        bundle.setIdentifier(type, connectAnnotation.value().getCanonicalName());
+        bundle.setNeedsGwtConstructor(type);
     }
 
 }
index b88afdf37af89c7c91216481e2c9b7329c2e19b5..d22f03635b1736b7c342dfba9b6dee2082ae984d 100644 (file)
@@ -4,22 +4,20 @@
 
 package com.vaadin.terminal.gwt.widgetsetutils.metadata;
 
+import com.google.gwt.core.ext.TreeLogger;
 import com.google.gwt.core.ext.typeinfo.JClassType;
 import com.google.gwt.core.ext.typeinfo.JMethod;
 import com.google.gwt.core.ext.typeinfo.JType;
-import com.google.gwt.core.ext.typeinfo.NotFoundException;
 
 public class StateInitVisitor extends TypeVisitor {
     @Override
-    public void visit(JClassType type, ConnectorBundle bundle)
-            throws NotFoundException {
-        if (isConnectedConnector(type)) {
-            JMethod getState = getInheritedMethod(type, "getState");
-            bundle.setNeedsReturnType(type, getState);
+    public void visitConnector(TreeLogger logger, JClassType type,
+            ConnectorBundle bundle) {
+        JMethod getState = findInheritedMethod(type, "getState");
+        bundle.setNeedsReturnType(type, getState);
 
-            JType stateType = getState.getReturnType();
-            bundle.setNeedsGwtConstructor(stateType.isClass());
-        }
+        JType stateType = getState.getReturnType();
+        bundle.setNeedsGwtConstructor(stateType.isClass());
     }
 
 }
index 163eda0675bf48dd3d358f519367bb73908656a2..976eb6417a76d28dc0fb2224bd2a1b97a542eec3 100644 (file)
@@ -4,39 +4,35 @@
 
 package com.vaadin.terminal.gwt.widgetsetutils.metadata;
 
+import com.google.gwt.core.ext.TreeLogger;
 import com.google.gwt.core.ext.typeinfo.JClassType;
 import com.google.gwt.core.ext.typeinfo.JMethod;
 import com.google.gwt.core.ext.typeinfo.JType;
 import com.google.gwt.core.ext.typeinfo.NotFoundException;
 import com.google.gwt.core.ext.typeinfo.TypeOracle;
-import com.vaadin.shared.ui.Connect;
-import com.vaadin.terminal.gwt.client.ComponentConnector;
-import com.vaadin.terminal.gwt.client.ServerConnector;
 
 public abstract class TypeVisitor {
-    private JClassType serverConnector;
-    private JClassType componentConnector;
-
     public void init(TypeOracle oracle) throws NotFoundException {
-        serverConnector = oracle.getType(ServerConnector.class.getName());
-        componentConnector = oracle.getType(ComponentConnector.class.getName());
+        // Default does nothing
     }
 
-    public abstract void visit(JClassType type, ConnectorBundle bundle)
-            throws NotFoundException;
+    public void visitConnector(TreeLogger logger, JClassType type,
+            ConnectorBundle bundle) {
+        // Default does nothing
+    }
 
-    protected boolean isConnectedConnector(JClassType type) {
-        return serverConnector.isAssignableFrom(type)
-                && type.isAnnotationPresent(Connect.class);
+    public void visitClientRpc(TreeLogger logger, JClassType type,
+            ConnectorBundle bundle) {
+        // Default does nothing
     }
 
-    protected boolean isConnectedComponentConnector(JClassType type) {
-        return componentConnector.isAssignableFrom(type)
-                && type.isAnnotationPresent(Connect.class);
+    public void visitServerRpc(TreeLogger logger, JClassType type,
+            ConnectorBundle bundle) {
+        // Default does nothing
     }
 
-    protected JMethod getInheritedMethod(JClassType type, String methodName,
-            JType... params) throws NotFoundException {
+    protected JMethod findInheritedMethod(JClassType type, String methodName,
+            JType... params) {
 
         JClassType currentType = type;
         while (currentType != null) {
@@ -55,6 +51,7 @@ public abstract class TypeVisitor {
             }
         }
 
-        throw new NotFoundException(methodName + " not found in " + type);
+        return null;
     }
+
 }
index be6f303c5bc8052ded965ebc32e6dc26da445051..50ea60a3c6d01c5786a94d24c6932a083de26d06 100644 (file)
@@ -4,18 +4,18 @@
 
 package com.vaadin.terminal.gwt.widgetsetutils.metadata;
 
+import com.google.gwt.core.ext.TreeLogger;
 import com.google.gwt.core.ext.typeinfo.JClassType;
 import com.google.gwt.core.ext.typeinfo.JMethod;
 import com.google.gwt.core.ext.typeinfo.JType;
-import com.google.gwt.core.ext.typeinfo.NotFoundException;
 
 public class WidgetInitVisitor extends TypeVisitor {
 
     @Override
-    public void visit(JClassType type, ConnectorBundle bundle)
-            throws NotFoundException {
-        if (isConnectedComponentConnector(type)) {
-            JMethod getWidget = getInheritedMethod(type, "getWidget");
+    public void visitConnector(TreeLogger logger, JClassType type,
+            ConnectorBundle bundle) {
+        if (ConnectorBundle.isConnectedComponentConnector(type)) {
+            JMethod getWidget = findInheritedMethod(type, "getWidget");
             bundle.setNeedsReturnType(type, getWidget);
 
             JType widgetType = getWidget.getReturnType();