]> source.dussan.org Git - vaadin-framework.git/commitdiff
Handle generated method flags in a unified way (#15373)
authorLeif Åstrand <leif@vaadin.com>
Thu, 8 Jan 2015 20:21:42 +0000 (22:21 +0200)
committerHenri Sara <hesara@vaadin.com>
Mon, 12 Jan 2015 10:36:59 +0000 (10:36 +0000)
Refactor existing functionality without functional changes to make it
easier to add support for the @BackgroundMessage annotation added in a
separate commit.

Change-Id: I27454ba44af4b7b3b7beccf29b12e22bf9f75a86

client-compiler/src/com/vaadin/server/widgetsetutils/ConnectorBundleLoaderFactory.java
client-compiler/src/com/vaadin/server/widgetsetutils/metadata/ClientRpcVisitor.java
client-compiler/src/com/vaadin/server/widgetsetutils/metadata/ConnectorBundle.java
client-compiler/src/com/vaadin/server/widgetsetutils/metadata/ServerRpcVisitor.java
client/src/com/vaadin/client/metadata/TypeDataStore.java

index f946d876380c7af81a3019f006f32d35ffab8698..884852f7c89df46eb9ffc121c423de8cb79ca14d 100644 (file)
@@ -55,6 +55,7 @@ import com.vaadin.client.metadata.OnStateChangeMethod;
 import com.vaadin.client.metadata.ProxyHandler;
 import com.vaadin.client.metadata.TypeData;
 import com.vaadin.client.metadata.TypeDataStore;
+import com.vaadin.client.metadata.TypeDataStore.MethodAttribute;
 import com.vaadin.client.ui.UnknownComponentConnector;
 import com.vaadin.server.widgetsetutils.metadata.ClientRpcVisitor;
 import com.vaadin.server.widgetsetutils.metadata.ConnectorBundle;
@@ -67,7 +68,6 @@ import com.vaadin.server.widgetsetutils.metadata.ServerRpcVisitor;
 import com.vaadin.server.widgetsetutils.metadata.StateInitVisitor;
 import com.vaadin.server.widgetsetutils.metadata.TypeVisitor;
 import com.vaadin.server.widgetsetutils.metadata.WidgetInitVisitor;
-import com.vaadin.shared.annotations.Delayed;
 import com.vaadin.shared.annotations.DelegateToWidget;
 import com.vaadin.shared.annotations.NoLayout;
 import com.vaadin.shared.communication.ClientRpc;
@@ -502,8 +502,7 @@ public class ConnectorBundleLoaderFactory extends Generator {
         writeInvokers(logger, w, bundle);
         writeParamTypes(w, bundle);
         writeProxys(w, bundle);
-        writeDelayedInfo(w, bundle);
-        writeNoLayoutRpcMethods(w, bundle);
+        writeMethodAttributes(logger, w, bundle);
 
         w.println("%s(store);", loadNativeJsMethodName);
 
@@ -516,22 +515,6 @@ public class ConnectorBundleLoaderFactory extends Generator {
         writeOnStateChangeHandlers(logger, w, bundle);
     }
 
-    private void writeNoLayoutRpcMethods(SplittingSourceWriter w,
-            ConnectorBundle bundle) {
-        Map<JClassType, Set<JMethod>> needsNoLayout = bundle
-                .getNeedsNoLayoutRpcMethods();
-        for (Entry<JClassType, Set<JMethod>> entry : needsNoLayout.entrySet()) {
-            JClassType type = entry.getKey();
-
-            for (JMethod method : entry.getValue()) {
-                w.println("store.setNoLayoutRpcMethod(%s, \"%s\");",
-                        getClassLiteralString(type), method.getName());
-            }
-
-            w.splitIfNeeded();
-        }
-    }
-
     private void writeOnStateChangeHandlers(TreeLogger logger,
             SplittingSourceWriter w, ConnectorBundle bundle)
             throws UnableToCompleteException {
@@ -740,32 +723,20 @@ public class ConnectorBundleLoaderFactory extends Generator {
         }
     }
 
-    private void writeDelayedInfo(SplittingSourceWriter w,
-            ConnectorBundle bundle) {
-        Map<JClassType, Set<JMethod>> needsDelayedInfo = bundle
-                .getNeedsDelayedInfo();
-        Set<Entry<JClassType, Set<JMethod>>> entrySet = needsDelayedInfo
-                .entrySet();
-        for (Entry<JClassType, Set<JMethod>> entry : entrySet) {
-            JClassType type = entry.getKey();
-            Set<JMethod> methods = entry.getValue();
-            for (JMethod method : methods) {
-                Delayed annotation = method.getAnnotation(Delayed.class);
-                if (annotation != null) {
-                    w.print("store.setDelayed(");
-                    writeClassLiteral(w, type);
-                    w.print(", \"");
-                    w.print(escape(method.getName()));
-                    w.println("\");");
-
-                    if (annotation.lastOnly()) {
-                        w.print("store.setLastOnly(");
-                        writeClassLiteral(w, type);
-                        w.print(", \"");
-                        w.print(escape(method.getName()));
-                        w.println("\");");
-                    }
-
+    private void writeMethodAttributes(TreeLogger logger,
+            SplittingSourceWriter w, ConnectorBundle bundle) {
+        for (Entry<JClassType, Map<JMethod, Set<MethodAttribute>>> typeEntry : bundle
+                .getMethodAttributes().entrySet()) {
+            JClassType type = typeEntry.getKey();
+            for (Entry<JMethod, Set<MethodAttribute>> methodEntry : typeEntry
+                    .getValue().entrySet()) {
+                JMethod method = methodEntry.getKey();
+                Set<MethodAttribute> attributes = methodEntry.getValue();
+                for (MethodAttribute attribute : attributes) {
+                    w.println("store.setMethodAttribute(%s, \"%s\", %s.%s);",
+                            getClassLiteralString(type), method.getName(),
+                            MethodAttribute.class.getCanonicalName(),
+                            attribute.name());
                     w.splitIfNeeded();
                 }
             }
index 1df3d785889a28e553d22ef042836ca0a08d180b..992a012005bd5bc0dac311deaebd3b47ebc91a8e 100644 (file)
@@ -24,6 +24,7 @@ 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.JType;
+import com.vaadin.client.metadata.TypeDataStore.MethodAttribute;
 import com.vaadin.shared.annotations.NoLayout;
 
 public class ClientRpcVisitor extends TypeVisitor {
@@ -41,7 +42,8 @@ public class ClientRpcVisitor extends TypeVisitor {
                 bundle.setNeedsInvoker(type, method);
                 bundle.setNeedsParamTypes(type, method);
                 if (method.getAnnotation(NoLayout.class) != null) {
-                    bundle.setNeedsNoLayoutRpcMethod(type, method);
+                    bundle.setMethodAttribute(type, method,
+                            MethodAttribute.NO_LAYOUT);
                 }
 
                 JType[] parameterTypes = method.getParameterTypes();
index 31f59ddee41474a39822da54bf801b7e33f86478..405925a920c47023ccc299daecbf3eb6d11c2819 100644 (file)
@@ -43,6 +43,7 @@ import com.vaadin.client.ComponentConnector;
 import com.vaadin.client.ServerConnector;
 import com.vaadin.client.communication.JSONSerializer;
 import com.vaadin.client.connectors.AbstractRendererConnector;
+import com.vaadin.client.metadata.TypeDataStore.MethodAttribute;
 import com.vaadin.client.ui.UnknownComponentConnector;
 import com.vaadin.shared.communication.ClientRpc;
 import com.vaadin.shared.communication.ServerRpc;
@@ -71,9 +72,9 @@ public class ConnectorBundle {
     private final Map<JClassType, Set<JMethod>> needsReturnType = new HashMap<JClassType, Set<JMethod>>();
     private final Map<JClassType, Set<JMethod>> needsInvoker = new HashMap<JClassType, Set<JMethod>>();
     private final Map<JClassType, Set<JMethod>> needsParamTypes = new HashMap<JClassType, Set<JMethod>>();
-    private final Map<JClassType, Set<JMethod>> needsDelayedInfo = new HashMap<JClassType, Set<JMethod>>();
     private final Map<JClassType, Set<JMethod>> needsOnStateChange = new HashMap<JClassType, Set<JMethod>>();
-    private final Map<JClassType, Set<JMethod>> needsNoLayoutRpcMethods = new HashMap<JClassType, Set<JMethod>>();
+
+    private final Map<JClassType, Map<JMethod, Set<MethodAttribute>>> methodAttributes = new HashMap<JClassType, Map<JMethod, Set<MethodAttribute>>>();
 
     private final Set<Property> needsProperty = new HashSet<Property>();
     private final Map<JClassType, Set<Property>> needsDelegateToWidget = new HashMap<JClassType, Set<Property>>();
@@ -525,23 +526,35 @@ public class ConnectorBundle {
         return Collections.unmodifiableSet(needsProxySupport);
     }
 
-    public void setNeedsDelayedInfo(JClassType type, JMethod method) {
-        if (!isNeedsDelayedInfo(type, method)) {
-            addMapping(needsDelayedInfo, type, method);
+    public void setMethodAttribute(JClassType type, JMethod method,
+            MethodAttribute methodAttribute) {
+        if (!hasMethodAttribute(type, method, methodAttribute)) {
+            Map<JMethod, Set<MethodAttribute>> typeData = methodAttributes
+                    .get(type);
+            if (typeData == null) {
+                typeData = new HashMap<JMethod, Set<MethodAttribute>>();
+                methodAttributes.put(type, typeData);
+            }
+
+            addMapping(typeData, method, methodAttribute);
         }
     }
 
-    private boolean isNeedsDelayedInfo(JClassType type, JMethod method) {
-        if (hasMapping(needsDelayedInfo, type, method)) {
+    private boolean hasMethodAttribute(JClassType type, JMethod method,
+            MethodAttribute methodAttribute) {
+        Map<JMethod, Set<MethodAttribute>> typeData = methodAttributes
+                .get(type);
+        if (typeData != null && hasMapping(typeData, method, methodAttribute)) {
             return true;
         } else {
             return previousBundle != null
-                    && previousBundle.isNeedsDelayedInfo(type, method);
+                    && previousBundle.hasMethodAttribute(type, method,
+                            methodAttribute);
         }
     }
 
-    public Map<JClassType, Set<JMethod>> getNeedsDelayedInfo() {
-        return Collections.unmodifiableMap(needsDelayedInfo);
+    public Map<JClassType, Map<JMethod, Set<MethodAttribute>>> getMethodAttributes() {
+        return Collections.unmodifiableMap(methodAttributes);
     }
 
     public void setNeedsSerialize(JType type) {
@@ -635,25 +648,6 @@ public class ConnectorBundle {
         return Collections.unmodifiableMap(needsOnStateChange);
     }
 
-    public void setNeedsNoLayoutRpcMethod(JClassType type, JMethod method) {
-        if (!isNeedsNoLayoutRpcMethod(type, method)) {
-            addMapping(needsNoLayoutRpcMethods, type, method);
-        }
-    }
-
-    private boolean isNeedsNoLayoutRpcMethod(JClassType type, JMethod method) {
-        if (hasMapping(needsNoLayoutRpcMethods, type, method)) {
-            return true;
-        } else {
-            return previousBundle != null
-                    && previousBundle.isNeedsNoLayoutRpcMethod(type, method);
-        }
-    }
-
-    public Map<JClassType, Set<JMethod>> getNeedsNoLayoutRpcMethods() {
-        return Collections.unmodifiableMap(needsNoLayoutRpcMethods);
-    }
-
     public static JMethod findInheritedMethod(JClassType type,
             String methodName, JType... params) {
 
index 6ad0d2fd98f488791aa5ee78161157f9dd1f9614..a1852d5b32d312d0cc8c9eefe98429a970878365 100644 (file)
@@ -23,6 +23,8 @@ 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.JType;
+import com.vaadin.client.metadata.TypeDataStore.MethodAttribute;
+import com.vaadin.shared.annotations.Delayed;
 
 public class ServerRpcVisitor extends TypeVisitor {
     @Override
@@ -38,7 +40,17 @@ public class ServerRpcVisitor extends TypeVisitor {
                 JMethod[] methods = subType.getMethods();
                 for (JMethod method : methods) {
                     ClientRpcVisitor.checkReturnType(logger, method);
-                    bundle.setNeedsDelayedInfo(type, method);
+
+                    Delayed delayed = method.getAnnotation(Delayed.class);
+                    if (delayed != null) {
+                        bundle.setMethodAttribute(type, method,
+                                MethodAttribute.DELAYED);
+                        if (delayed.lastOnly()) {
+                            bundle.setMethodAttribute(type, method,
+                                    MethodAttribute.LAST_ONLY);
+                        }
+                    }
+
                     bundle.setNeedsParamTypes(type, method);
 
                     JType[] parameterTypes = method.getParameterTypes();
index 6d46ed1365687d2bd53f58e78cb410ee6cc7114e..e3db0ccded227fd7f8eb64362ad4efea4c817e95 100644 (file)
@@ -28,6 +28,10 @@ import com.vaadin.client.communication.JSONSerializer;
 import com.vaadin.shared.annotations.NoLayout;
 
 public class TypeDataStore {
+    public static enum MethodAttribute {
+        DELAYED, LAST_ONLY, NO_LAYOUT;
+    }
+
     private static final String CONSTRUCTOR_NAME = "!new";
 
     private final FastStringMap<Class<?>> identifiers = FastStringMap.create();
@@ -47,9 +51,8 @@ public class TypeDataStore {
     private final FastStringMap<FastStringMap<JsArrayObject<OnStateChangeMethod>>> onStateChangeMethods = FastStringMap
             .create();
 
-    private final FastStringSet delayedMethods = FastStringSet.create();
-    private final FastStringSet lastOnlyMethods = FastStringSet.create();
-    private final FastStringSet noLayoutRpcMethods = FastStringSet.create();
+    private final FastStringMap<FastStringSet> methodAttributes = FastStringMap
+            .create();
 
     private final FastStringMap<Type> returnTypes = FastStringMap.create();
     private final FastStringMap<Invoker> invokers = FastStringMap.create();
@@ -213,20 +216,29 @@ public class TypeDataStore {
     }
 
     public static boolean isDelayed(Method method) {
-        return get().delayedMethods.contains(method.getLookupKey());
+        return hasMethodAttribute(method, MethodAttribute.DELAYED);
     }
 
-    public void setDelayed(Class<?> type, String methodName) {
-        delayedMethods.add(getType(type).getMethod(methodName).getLookupKey());
+    private static boolean hasMethodAttribute(Method method,
+            MethodAttribute attribute) {
+        FastStringSet attributes = get().methodAttributes.get(method
+                .getLookupKey());
+        return attributes != null && attributes.contains(attribute.name());
     }
 
-    public static boolean isLastOnly(Method method) {
-        return get().lastOnlyMethods.contains(method.getLookupKey());
+    public void setMethodAttribute(Class<?> type, String methodName,
+            MethodAttribute attribute) {
+        String key = getType(type).getMethod(methodName).getLookupKey();
+        FastStringSet attributes = methodAttributes.get(key);
+        if (attributes == null) {
+            attributes = FastStringSet.create();
+            methodAttributes.put(key, attributes);
+        }
+        attributes.add(attribute.name());
     }
 
-    public void setLastOnly(Class<?> clazz, String methodName) {
-        lastOnlyMethods
-                .add(getType(clazz).getMethod(methodName).getLookupKey());
+    public static boolean isLastOnly(Method method) {
+        return hasMethodAttribute(method, MethodAttribute.LAST_ONLY);
     }
 
     /**
@@ -450,22 +462,7 @@ public class TypeDataStore {
      *         otherwise <code>false</code>
      */
     public static boolean isNoLayoutRpcMethod(Method method) {
-        return get().noLayoutRpcMethods.contains(method.getLookupKey());
-    }
-
-    /**
-     * Defines that a method is annotated with {@link NoLayout}.
-     * 
-     * @since 7.4
-     * 
-     * @param type
-     *            the where the method is defined
-     * @param methodName
-     *            the name of the method
-     */
-    public void setNoLayoutRpcMethod(Class<?> type, String methodName) {
-        noLayoutRpcMethods.add(new Method(getType(type), methodName)
-                .getLookupKey());
+        return hasMethodAttribute(method, MethodAttribute.NO_LAYOUT);
     }
 
     /**