diff options
| author | Pierre-Charles David | 2015-12-15 15:55:05 +0000 |
|---|---|---|
| committer | Pierre-Charles David | 2016-01-08 09:30:09 +0000 |
| commit | 7ed9d867900b8d42bacd2ee68be65788ea5e6056 (patch) | |
| tree | 5ee9510ea37b9061af93b08e2f573f60851e0604 | |
| parent | a6fe87cc952ed74af12bd659ea5d675027f7f85b (diff) | |
| download | org.eclipse.sirius-7ed9d867900b8d42bacd2ee68be65788ea5e6056.tar.gz org.eclipse.sirius-7ed9d867900b8d42bacd2ee68be65788ea5e6056.tar.xz org.eclipse.sirius-7ed9d867900b8d42bacd2ee68be65788ea5e6056.zip | |
[482993] Reorganize the code in SiriusInterpreter
Bug: 482993
Change-Id: I5cf0a89e439894088785708214d84213e4a70a0a
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
| -rw-r--r-- | incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/SiriusInterpreter.java | 101 |
1 files changed, 53 insertions, 48 deletions
diff --git a/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/SiriusInterpreter.java b/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/SiriusInterpreter.java index 733ef69558..75f9c921ba 100644 --- a/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/SiriusInterpreter.java +++ b/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/SiriusInterpreter.java @@ -14,78 +14,83 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import org.eclipse.emf.common.util.Diagnostic; +import org.eclipse.emf.common.util.BasicDiagnostic; import org.eclipse.emf.ecore.EObject; +import org.eclipse.sirius.business.api.session.Session; +import org.eclipse.sirius.common.interpreter.api.EvaluationResult; import org.eclipse.sirius.common.interpreter.api.IEvaluationResult; import org.eclipse.sirius.common.interpreter.api.IInterpreter; import org.eclipse.sirius.common.tools.api.interpreter.EvaluationException; import org.eclipse.sirius.common.tools.api.interpreter.IInterpreterWithDiagnostic; +import com.google.common.base.Preconditions; import com.google.common.collect.Lists; +/** + * Provides an implementation of {@link IInterpreter} backed by an old-style + * {@link IInterpreterWithDiagnostic}. + */ public class SiriusInterpreter implements IInterpreter { private IInterpreterWithDiagnostic interpreter; + public SiriusInterpreter(Session session) { + this((IInterpreterWithDiagnostic) session.getInterpreter()); + } + public SiriusInterpreter(IInterpreterWithDiagnostic interpreterWithDiagnostic) { - this.interpreter = interpreterWithDiagnostic; + this.interpreter = Preconditions.checkNotNull(interpreterWithDiagnostic); } @Override - public IEvaluationResult evaluateExpression(Map<String, Object> variables, String expressionBody) { + public IEvaluationResult evaluateExpression(Map<String, Object> variables, String expr) { + IEvaluationResult result = EvaluationResult.noEvaluation(); + Object self = variables.get("self"); + if (self instanceof EObject) { + try { + setupInterpreter(variables); + org.eclipse.sirius.common.tools.api.interpreter.IInterpreterWithDiagnostic.IEvaluationResult evaluationResult = this.interpreter.evaluateExpression((EObject) self, expr); + result = EvaluationResult.of(evaluationResult.getValue(), evaluationResult.getDiagnostic()); + } catch (EvaluationException e) { + result = EvaluationResult.withError(BasicDiagnostic.toDiagnostic(e)); + } finally { + tearDownInterpreter(variables); + } + } + return result; + } + + private void setupInterpreter(Map<String, Object> variables) { if (this.interpreter instanceof org.eclipse.sirius.common.tools.api.interpreter.IInterpreter) { org.eclipse.sirius.common.tools.api.interpreter.IInterpreter i = (org.eclipse.sirius.common.tools.api.interpreter.IInterpreter) this.interpreter; + // FIXME This breaks the rest of Sirius by wiping the session + // interpreter's "classpath" for services i.setProperty(org.eclipse.sirius.common.tools.api.interpreter.IInterpreter.FILES, Lists.newArrayList("org.eclipse.sirius.ui.properties")); i.addImport(org.eclipse.sirius.ui.properties.internal.SiriusToolServices.class.getName()); - Set<Entry<String, Object>> entries = variables.entrySet(); - for (Entry<String, Object> entry : entries) { - i.setVariable(entry.getKey(), entry.getValue()); - } + declareLocals(variables, i); } + } - IEvaluationResult result = new IEvaluationResult() { - @Override - public Object getValue() { - return null; - } - - @Override - public Diagnostic getDiagnostic() { - return Diagnostic.CANCEL_INSTANCE; - } - }; - - Object object = variables.get("self"); - if (object instanceof EObject) { - try { - final org.eclipse.sirius.common.tools.api.interpreter.IInterpreterWithDiagnostic.IEvaluationResult evaluationResult = this.interpreter.evaluateExpression((EObject) object, - expressionBody); - result = new IEvaluationResult() { - - @Override - public Object getValue() { - return evaluationResult.getValue(); - } + private void declareLocals(Map<String, Object> variables, org.eclipse.sirius.common.tools.api.interpreter.IInterpreter i) { + Set<Entry<String, Object>> entries = variables.entrySet(); + for (Entry<String, Object> entry : entries) { + i.setVariable(entry.getKey(), entry.getValue()); + } + } - @Override - public Diagnostic getDiagnostic() { - return evaluationResult.getDiagnostic(); - } - }; - } catch (EvaluationException e) { - e.printStackTrace(); - } finally { - if (this.interpreter instanceof org.eclipse.sirius.common.tools.api.interpreter.IInterpreter) { - org.eclipse.sirius.common.tools.api.interpreter.IInterpreter i = (org.eclipse.sirius.common.tools.api.interpreter.IInterpreter) this.interpreter; - i.removeImport(org.eclipse.sirius.ui.properties.internal.SiriusToolServices.class.getName()); - Set<Entry<String, Object>> entries = variables.entrySet(); - for (Entry<String, Object> entry : entries) { - i.unSetVariable(entry.getKey()); - } - } - } + private void tearDownInterpreter(Map<String, Object> variables) { + if (this.interpreter instanceof org.eclipse.sirius.common.tools.api.interpreter.IInterpreter) { + org.eclipse.sirius.common.tools.api.interpreter.IInterpreter i = (org.eclipse.sirius.common.tools.api.interpreter.IInterpreter) this.interpreter; + i.removeImport(org.eclipse.sirius.ui.properties.internal.SiriusToolServices.class.getName()); + unsetLocals(variables, i); } + } - return result; + private void unsetLocals(Map<String, Object> variables, org.eclipse.sirius.common.tools.api.interpreter.IInterpreter i) { + Set<Entry<String, Object>> entries = variables.entrySet(); + for (Entry<String, Object> entry : entries) { + i.unSetVariable(entry.getKey()); + } } + } |
