Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoratikhomirov2006-02-06 13:10:22 -0500
committeratikhomirov2006-02-06 13:10:22 -0500
commitab3cbec7ac30ebc2acac1565d3a11e5de969c32b (patch)
tree6975082a424975a565a3ea20368f2279f968c1c2 /plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal
parent6e7a0cab3de84e293361d07e3545872589e22f68 (diff)
downloadorg.eclipse.gmf-tooling-ab3cbec7ac30ebc2acac1565d3a11e5de969c32b.tar.gz
org.eclipse.gmf-tooling-ab3cbec7ac30ebc2acac1565d3a11e5de969c32b.tar.xz
org.eclipse.gmf-tooling-ab3cbec7ac30ebc2acac1565d3a11e5de969c32b.zip
major refactoring of gmfgraph codegen utilizing dispatcher infrastructure
Diffstat (limited to 'plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal')
-rw-r--r--plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/ArrayKeyChain.java38
-rw-r--r--plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/DispatcherImpl.java55
-rw-r--r--plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/HierarchyKeyMap.java46
-rw-r--r--plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/KeyChain.java59
-rw-r--r--plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/KeyMap.java29
-rw-r--r--plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/TemplateRegistry.java1
-rw-r--r--plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/YAEmitterFactory.java10
7 files changed, 228 insertions, 10 deletions
diff --git a/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/ArrayKeyChain.java b/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/ArrayKeyChain.java
new file mode 100644
index 000000000..a5a39b533
--- /dev/null
+++ b/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/ArrayKeyChain.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2006 Borland Software Corporation
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Artem Tikhomirov (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.internal.graphdef.codegen;
+
+/**
+ * Although it's more straightforward just to override hasNext/next then to perform checks in advance,
+ * did it that way for now to verify implementation in super. FIXME - refactor.
+ * @author artem
+ */
+public class ArrayKeyChain extends KeyChain {
+ private final Object[] myKeys;
+ private int myIndex = 0;
+
+ public ArrayKeyChain(Object origin, Object singleKey) {
+ this(origin, new Object[] {singleKey});
+ }
+
+ public ArrayKeyChain(Object origin, Object[] mappedKeys) {
+ super(origin);
+ myKeys = mappedKeys;
+ }
+
+ protected Object advance() {
+ if (myIndex < myKeys.length) {
+ return myKeys[myIndex++];
+ }
+ return null;
+ }
+}
diff --git a/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/DispatcherImpl.java b/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/DispatcherImpl.java
index 32b5bfeed..0e22d84f0 100644
--- a/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/DispatcherImpl.java
+++ b/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/DispatcherImpl.java
@@ -11,8 +11,13 @@
*/
package org.eclipse.gmf.internal.graphdef.codegen;
+import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.Status;
import org.eclipse.emf.codegen.jet.JETEmitter;
+import org.eclipse.emf.codegen.jet.JETException;
+import org.eclipse.gmf.common.UnexpectedBehaviourException;
import org.eclipse.gmf.graphdef.codegen.Dispatcher;
/**
@@ -22,20 +27,52 @@ public class DispatcherImpl extends Dispatcher {
private final YAEmitterFactory myFactory;
- public DispatcherImpl(YAEmitterFactory factory) {
+ private final KeyMap myKeyMap;
+
+ public DispatcherImpl(YAEmitterFactory factory, KeyMap keyMap) {
myFactory = factory;
+ myKeyMap = keyMap;
}
- public String dispatch(Object arg, Object[] orginalArgs) {
- return "asdasdasd";
+ public String dispatch(Object key, Object argument) {
+ final String pluginID = "org.eclipse.gmf.graphdef.codegen";
+ final ILog traceFacility = Platform.getLog(Platform.getBundle(pluginID));
+ try {
+ StringBuffer errors = new StringBuffer();
+ for (KeyChain it = myKeyMap.map(key); it.hasNext();) {
+ Object nextKey = it.next();
+ if (myFactory.checkEmitter(nextKey)) {
+ try {
+ JETEmitter em = myFactory.acquireEmitter(nextKey);
+ return em.generate(new NullProgressMonitor(), new Object[] {argument});
+ } catch (NoSuchTemplateException ex) {
+ traceFacility.log(new Status(Status.ERROR, pluginID, 0, "Template for key '" + nextKey + "' failed", ex));
+ errors.append(formatError(ex));
+ errors.append("\n");
+ } catch (UnexpectedBehaviourException ex) {
+ traceFacility.log(new Status(Status.ERROR, pluginID, 0, "Template for key '" + nextKey + "' failed", ex));
+ errors.append(formatError(ex));
+ errors.append("\n");
+ }
+ }
+ }
+ if (errors.length() == 0) {
+ // TODO option silent?
+ return "// no suitable template found for '" + key + "'";
+ } else {
+ return errors.toString();
+ }
+ } catch (JETException ex) {
+ traceFacility.log(new Status(Status.ERROR, pluginID, 0, ex.getMessage(), ex));
+ return "ERROR: " + formatError(ex);
+ }
}
public String dispatch(Object key, Args args) {
- try {
- JETEmitter em = myFactory.acquireEmitter(key);
- return em.generate(new NullProgressMonitor(), new Object[] {args});
- } catch (Exception ex) {
- return "// " + ex.getClass().getSimpleName() + ": " + ex.getMessage();
- }
+ return dispatch(key, (Object) args);
+ }
+
+ private static String formatError(Exception ex) {
+ return ex.getClass().getSimpleName() + ": " + ex.getMessage();
}
}
diff --git a/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/HierarchyKeyMap.java b/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/HierarchyKeyMap.java
new file mode 100644
index 000000000..2c5cd0bba
--- /dev/null
+++ b/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/HierarchyKeyMap.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2006 Borland Software Corporation
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Artem Tikhomirov (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.internal.graphdef.codegen;
+
+import java.util.Arrays;
+import java.util.LinkedHashSet;
+import java.util.LinkedList;
+
+/**
+ * XXX cache hierarchies?
+ * @author artem
+ */
+public class HierarchyKeyMap extends KeyMap {
+
+ public HierarchyKeyMap() {
+ }
+
+ /**
+ * Processes instances of {@link java.lang.Class} only, delegates to superclass otherwise
+ */
+ public KeyChain map(Object key) {
+ if (false == key instanceof Class) {
+ return super.map(key);
+ }
+ Class keyClass = (Class) key;
+ Class[] immediateInterfaces = keyClass.getInterfaces();
+ // make sure immediate interfaces are considered first in the key chain
+ LinkedHashSet result = new LinkedHashSet(Arrays.asList(immediateInterfaces));
+ LinkedList l = new LinkedList(result);
+ while (!l.isEmpty()) {
+ Class iface = (Class) l.removeFirst();
+ result.add(iface);
+ l.addAll(0, Arrays.asList(iface.getInterfaces()));
+ }
+ return new ArrayKeyChain(keyClass, result.toArray());
+ }
+}
diff --git a/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/KeyChain.java b/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/KeyChain.java
new file mode 100644
index 000000000..d189cf566
--- /dev/null
+++ b/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/KeyChain.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2006 Borland Software Corporation
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Artem Tikhomirov (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.internal.graphdef.codegen;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/**
+ * Key extraction and prioritization. <code>null</code> keys are not supported (useing null as eol indicator)
+ * TODO add absract #reset()?
+ * @author artem
+ */
+public abstract class KeyChain/*<T>*/ implements Iterator/*<E>*/ {
+ private final Object/*<T>*/ myOrigin;
+ private Object/*<E>*/ myNextValue;
+ private boolean myIsInitialized = false;
+
+ public KeyChain(Object origin) {
+ myOrigin = origin;
+ }
+
+ public final Object getOrigin() {
+ return myOrigin;
+ }
+
+ public boolean hasNext() {
+ if (!myIsInitialized) {
+ myNextValue = advance();
+ myIsInitialized = true;
+ }
+ return myNextValue != null;
+ }
+
+ /**
+ * @return <code>null</code> to indicate EOL
+ */
+ protected abstract Object/*<E>*/ advance();
+
+ public Object next() {
+ if (hasNext()) {
+ myIsInitialized = false;
+ return myNextValue;
+ }
+ throw new NoSuchElementException();
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+}
diff --git a/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/KeyMap.java b/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/KeyMap.java
new file mode 100644
index 000000000..e87a50f44
--- /dev/null
+++ b/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/KeyMap.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2006 Borland Software Corporation
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Artem Tikhomirov (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.internal.graphdef.codegen;
+
+/**
+ * Logic of key extraction and prioritization.
+ * @author artem
+ * @see KeyChain
+ */
+public class KeyMap/*<T,E>*/ {
+
+ /**
+ * Default implementation just returns the key itself as sole item in the chain
+ * @param key
+ * @return key chain with sole item that is key itself.
+ */
+ public KeyChain/*<T,E>*/ map(Object/*<T>*/ key) {
+ return new ArrayKeyChain(key, key);
+ }
+}
diff --git a/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/TemplateRegistry.java b/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/TemplateRegistry.java
index 3904813f1..1362fb8fc 100644
--- a/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/TemplateRegistry.java
+++ b/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/TemplateRegistry.java
@@ -12,6 +12,7 @@
package org.eclipse.gmf.internal.graphdef.codegen;
/**
+ * XXX hm, it's possible to have only one JETEmitter per key. What if we'd like to reference same emitter with different keys?
* TODO CompositeTemplateRegistry?
* TODO move to gmf.common
* @author artem
diff --git a/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/YAEmitterFactory.java b/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/YAEmitterFactory.java
index acc8a1911..9d270df99 100644
--- a/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/YAEmitterFactory.java
+++ b/plugins/org.eclipse.gmf.graphdef.codegen/src/org/eclipse/gmf/internal/graphdef/codegen/YAEmitterFactory.java
@@ -68,11 +68,19 @@ public class YAEmitterFactory {
* from constructor, object may not be fully initialized, don't use this
* method for anything but cache instantiation.
*/
- protected Map/* <Object, JETEmitter> */createCache() {
+ protected Map/*<Object, JETEmitter>*/ createCache() {
return new HashMap();
}
/**
+ * @param key
+ * @return <code>true</code> if template for the key passes is known to this factory
+ */
+ public boolean checkEmitter(Object key) {
+ return checkCache(key) != null || myTemplates.getTemplatePath(key) != null;
+ }
+
+ /**
* This is primary way to get emitters from this factory.
* Checks cache (if there's one) first. Produces new emitter (with {@link #newEmitter(Object)}), caches and returns its outcome.
*/

Back to the top