summaryrefslogtreecommitdiffstats
path: root/client-compiler
diff options
context:
space:
mode:
authorLeif Åstrand <leif@vaadin.com>2012-10-11 10:31:11 +0300
committerLeif Åstrand <leif@vaadin.com>2012-10-11 10:31:11 +0300
commit04413150ac7ef28e50d9e207080f2a00db5a59be (patch)
tree1b03928961161a5b059c8770a8beab1ea98c5148 /client-compiler
parentdec9f91a708d5b7b4ec1d6a2faaddf2b64827d3e (diff)
downloadvaadin-framework-04413150ac7ef28e50d9e207080f2a00db5a59be.tar.gz
vaadin-framework-04413150ac7ef28e50d9e207080f2a00db5a59be.zip
Use class hierachy for resolving @Connect conflicts (#9826)
Change-Id: Ic268bfb5704a5c9113dee80017196c72b5236b93
Diffstat (limited to 'client-compiler')
-rw-r--r--client-compiler/src/com/vaadin/server/widgetsetutils/ConnectorBundleLoaderFactory.java83
-rw-r--r--client-compiler/src/com/vaadin/server/widgetsetutils/metadata/ConnectorInitVisitor.java17
2 files changed, 79 insertions, 21 deletions
diff --git a/client-compiler/src/com/vaadin/server/widgetsetutils/ConnectorBundleLoaderFactory.java b/client-compiler/src/com/vaadin/server/widgetsetutils/ConnectorBundleLoaderFactory.java
index aa220225c9..0e29c07405 100644
--- a/client-compiler/src/com/vaadin/server/widgetsetutils/ConnectorBundleLoaderFactory.java
+++ b/client-compiler/src/com/vaadin/server/widgetsetutils/ConnectorBundleLoaderFactory.java
@@ -583,10 +583,12 @@ public class ConnectorBundleLoaderFactory extends Generator {
JClassType connectorType = typeOracle.getType(ServerConnector.class
.getName());
JClassType[] subtypes = connectorType.getSubtypes();
- for (JClassType connectorSubtype : subtypes) {
- if (!connectorSubtype.isAnnotationPresent(Connect.class)) {
- continue;
- }
+
+ // Find all types with a valid mapping
+ Collection<JClassType> selectedTypes = getMappedTypes(logger, subtypes);
+
+ // Group by load style
+ for (JClassType connectorSubtype : selectedTypes) {
LoadStyle loadStyle = getLoadStyle(connectorSubtype);
if (loadStyle != null) {
connectorsByLoadStyle.get(loadStyle).add(connectorSubtype);
@@ -637,6 +639,79 @@ public class ConnectorBundleLoaderFactory extends Generator {
return bundles;
}
+ private Collection<JClassType> getMappedTypes(TreeLogger logger,
+ JClassType[] types) throws UnableToCompleteException {
+ Map<String, JClassType> mappings = new HashMap<String, JClassType>();
+
+ // Keep track of what has happened to avoid logging intermediate state
+ Map<JClassType, List<JClassType>> replaced = new HashMap<JClassType, List<JClassType>>();
+
+ for (JClassType type : types) {
+ Connect connectAnnotation = type.getAnnotation(Connect.class);
+ if (connectAnnotation == null) {
+ continue;
+ }
+
+ String identifier = connectAnnotation.value().getCanonicalName();
+
+ JClassType previousMapping = mappings.put(identifier, type);
+ if (previousMapping != null) {
+ // There are multiple mappings, pick the subclass
+ JClassType subclass;
+ JClassType superclass;
+ if (previousMapping.isAssignableFrom(type)) {
+ subclass = type;
+ superclass = previousMapping;
+ } else if (type.isAssignableFrom(previousMapping)) {
+ subclass = previousMapping;
+ superclass = type;
+ } else {
+ // Neither inherits from the other - this is a conflict
+ logger.log(
+ Type.ERROR,
+ "Conflicting @Connect mappings detected for "
+ + identifier
+ + ": "
+ + type.getQualifiedSourceName()
+ + " and "
+ + previousMapping.getQualifiedSourceName()
+ + ". There can only be multiple @Connect mappings for the same server-side type if one is the subclass of the other.");
+ throw new UnableToCompleteException();
+ }
+
+ mappings.put(identifier, subclass);
+
+ // Inherit any previous replacements
+ List<JClassType> previousReplacements = replaced
+ .remove(superclass);
+ if (previousReplacements == null) {
+ previousReplacements = new ArrayList<JClassType>();
+ }
+
+ previousReplacements.add(superclass);
+ replaced.put(subclass, previousReplacements);
+ }
+ }
+
+ // Log the final set of replacements
+ for (Entry<JClassType, List<JClassType>> entry : replaced.entrySet()) {
+ String msg = entry.getKey().getQualifiedSourceName() + " replaces ";
+
+ List<JClassType> list = entry.getValue();
+ for (int i = 0; i < list.size(); i++) {
+ if (i != 0) {
+ msg += ", ";
+ }
+ msg += list.get(i).getQualifiedSourceName();
+ }
+
+ logger.log(Type.INFO, msg);
+ }
+
+ // Return the types of the final mapping
+ return mappings.values();
+ }
+
private Collection<TypeVisitor> getVisitors(TypeOracle oracle)
throws NotFoundException {
List<TypeVisitor> visitors = Arrays.<TypeVisitor> asList(
diff --git a/client-compiler/src/com/vaadin/server/widgetsetutils/metadata/ConnectorInitVisitor.java b/client-compiler/src/com/vaadin/server/widgetsetutils/metadata/ConnectorInitVisitor.java
index 7500d65443..253d657af0 100644
--- a/client-compiler/src/com/vaadin/server/widgetsetutils/metadata/ConnectorInitVisitor.java
+++ b/client-compiler/src/com/vaadin/server/widgetsetutils/metadata/ConnectorInitVisitor.java
@@ -4,19 +4,14 @@
package com.vaadin.server.widgetsetutils.metadata;
-import java.util.Map;
-
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.TreeLogger.Type;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JClassType;
-import com.google.gwt.dev.util.collect.HashMap;
import com.vaadin.shared.ui.Connect;
public class ConnectorInitVisitor extends TypeVisitor {
- private Map<String, JClassType> processedConnections = new HashMap<String, JClassType>();
-
@Override
public void visitConnector(TreeLogger logger, JClassType type,
ConnectorBundle bundle) throws UnableToCompleteException {
@@ -26,18 +21,6 @@ public class ConnectorInitVisitor extends TypeVisitor {
+ bundle.getName().replaceAll("^_*", "") + " bundle");
String identifier = connectAnnotation.value().getCanonicalName();
- JClassType previousMapping = processedConnections.put(identifier,
- type);
- if (previousMapping != null) {
- logger.log(
- Type.ERROR,
- "Multiple @Connect mappings detected for " + identifier
- + ": " + type.getQualifiedSourceName()
- + " and "
- + previousMapping.getQualifiedSourceName());
- throw new UnableToCompleteException();
- }
-
bundle.setIdentifier(type, identifier);
bundle.setNeedsGwtConstructor(type);
}