org.eclipse.xtext.xbase.interpreter.IEvaluationResult Java Examples
The following examples show how to use
org.eclipse.xtext.xbase.interpreter.IEvaluationResult.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PureXbaseInterpreterTest.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
public void assertEvaluatesTo(Object expectation, String model, boolean validate) throws Exception { XExpression expression = null; try { expression = expression(model, validate); IEvaluationResult result = interpreter.evaluate(expression); Assert.assertNull( "Expected no exception. Model was: " + model + ", Exception was: " + result.getException(), result.getException()); if (expectation != null && expectation.getClass().isArray()) { Assert.assertArrayEquals("Model was: " + model, (Object[]) expectation, (Object[]) result.getResult()); } else { Assert.assertEquals("Model was: " + model, expectation, result.getResult()); } } finally { if ((expression != null)) { cache.clear(expression.eResource()); } } }
Example #2
Source File: XbaseInterpreterTest.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
public void assertEvaluatesTo(Object expectation, String model, boolean validate) { XExpression expression = null; try { expression = expression(model, validate); IEvaluationResult result = interpreter.evaluate(expression); assertNull("Expected no exception. Model was: " + model + ", Exception was: " + result.getException(), result.getException()); if(expectation != null && expectation.getClass().isArray()) assertArrayEquals("Model was: " + model, (Object[]) expectation, (Object[]) result.getResult()); else assertEquals("Model was: " + model, expectation, result.getResult()); } catch (Exception e) { if (e instanceof RuntimeException) throw (RuntimeException) e; throw new RuntimeException(e); } finally { if (expression != null) { cache.clear(expression.eResource()); } } }
Example #3
Source File: XbaseInterpreterTest.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
@Override public void assertEvaluatesWithException(Class<? extends Throwable> expectatedException, String model) { XExpression expression = null; try { expression = expression(model,true); IEvaluationResult result = interpreter.evaluate(expression); assertTrue("Expected " + expectatedException.getSimpleName() + " but got: " + result.getException(), expectatedException.isInstance(result.getException())); } catch (Exception e) { if (e instanceof RuntimeException) throw (RuntimeException) e; throw new RuntimeException(e); } finally { if (expression != null) { cache.clear(expression.eResource()); } } }
Example #4
Source File: XbaseInterpreter.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
protected IEvaluationResult _doEvaluate(XDoWhileExpression doWhileLoop, IEvaluationContext context, CancelIndicator indicator) { Object condition = null; do { internalEvaluate(doWhileLoop.getBody(), context, indicator); condition = internalEvaluate(doWhileLoop.getPredicate(), context, indicator); } while (Boolean.TRUE.equals(condition)); return null; }
Example #5
Source File: ClosureInvocationHandler.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@Override protected Object doInvoke(Method method, Object[] args) throws Throwable { IEvaluationContext forkedContext = context.fork(); if (args != null) { initializeClosureParameters(forkedContext, args); } IEvaluationResult result = interpreter.evaluate(closure.getExpression(), forkedContext, indicator); if (indicator.isCanceled()) throw new InterpreterCanceledException(); if (result.getException() != null) throw result.getException(); return result.getResult(); }
Example #6
Source File: PureXbaseInterpreterTest.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@Override public void assertEvaluatesWithException(Class<? extends Throwable> expectatedException, String model) throws Exception { XExpression expression = null; try { expression = expression(model, true); IEvaluationResult result = interpreter.evaluate(expression); Assert.assertTrue("Expected " + expectatedException.getSimpleName() + " but got: " + result.getException(), expectatedException.isInstance(result.getException())); } finally { if (expression != null) { cache.clear(expression.eResource()); } } }
Example #7
Source File: ScriptImpl.java From openhab-core with Eclipse Public License 2.0 | 5 votes |
@Override public Object execute(final IEvaluationContext evaluationContext) throws ScriptExecutionException { if (xExpression != null) { Resource resource = xExpression.eResource(); IExpressionInterpreter interpreter = null; if (resource instanceof XtextResource) { IResourceServiceProvider provider = ((XtextResource) resource).getResourceServiceProvider(); interpreter = provider.get(IExpressionInterpreter.class); } if (interpreter == null) { throw new ScriptExecutionException("Script interpreter couldn't be obtain"); } try { IEvaluationResult result = interpreter.evaluate(xExpression, evaluationContext, CancelIndicator.NullImpl); if (result == null) { // this can only happen on an InterpreterCancelledException, // i.e. NEVER ;-) return null; } if (result.getException() != null) { throw new ScriptExecutionException(result.getException().getMessage(), result.getException()); } return result.getResult(); } catch (Throwable e) { if (e instanceof ScriptExecutionException) { throw (ScriptExecutionException) e; } else { throw new ScriptExecutionException( "An error occurred during the script execution: " + e.getMessage(), e); } } } else { throw new ScriptExecutionException("Script does not contain any expression"); } }
Example #8
Source File: SarlScriptExecutor.java From sarl with Apache License 2.0 | 5 votes |
@Override public Object execute(int lineno, String code) throws Exception { final List<String> issues = new ArrayList<>(); final Collection<Resource> resources = new ArrayList<>(); final CompiledFile outFile = compile(lineno, "package x.x.x;\n" //$NON-NLS-1$ + "class ____Fake_Class____ {\nstatic var __fake_attr__ : Object = {\n" //$NON-NLS-1$ + code + ";\n};\n}", //$NON-NLS-1$ issues, (it) -> { resources.add(it); }); try { assertNoIssue(lineno, issues); if (resources.isEmpty()) { throw new NoXtextResourceException(lineno); } for (Resource resource : resources) { SarlScript script = (SarlScript) resource.getContents().get(0); SarlClass clazz = (SarlClass) script.getXtendTypes().get(0); SarlField field = (SarlField) clazz.getMembers().get(0); XExpression xexpression = field.getInitialValue(); final IExpressionInterpreter interpreter = this.interpreterProvider.get(); if (interpreter instanceof SarlExpressionInterpreter && this.classLoaderBuilder != null) { final SarlExpressionInterpreter exprEvaluator = (SarlExpressionInterpreter) interpreter; exprEvaluator.expandClassLoader(this.classLoaderBuilder); } IEvaluationResult result = interpreter.evaluate(xexpression); if (result.getException() == null) { return result.getResult(); } throw new RuntimeException(result.getException()); } } finally { if (outFile != null && outFile.getRootFolder() != null) { FileSystem.delete(outFile.getRootFolder()); } } return null; }
Example #9
Source File: ScriptImpl.java From smarthome with Eclipse Public License 2.0 | 5 votes |
@Override public Object execute(final IEvaluationContext evaluationContext) throws ScriptExecutionException { if (xExpression != null) { Resource resource = xExpression.eResource(); IExpressionInterpreter interpreter = null; if (resource instanceof XtextResource) { IResourceServiceProvider provider = ((XtextResource) resource).getResourceServiceProvider(); interpreter = provider.get(IExpressionInterpreter.class); } if (interpreter == null) { throw new ScriptExecutionException("Script interpreter couldn't be obtain"); } try { IEvaluationResult result = interpreter.evaluate(xExpression, evaluationContext, CancelIndicator.NullImpl); if (result == null) { // this can only happen on an InterpreterCancelledException, // i.e. NEVER ;-) return null; } if (result.getException() != null) { throw new ScriptExecutionException(result.getException().getMessage(), result.getException()); } return result.getResult(); } catch (Throwable e) { if (e instanceof ScriptExecutionException) { throw (ScriptExecutionException) e; } else { throw new ScriptExecutionException( "An error occurred during the script execution: " + e.getMessage(), e); } } } else { throw new ScriptExecutionException("Script does not contain any expression"); } }
Example #10
Source File: XbaseInterpreter.java From xtext-extras with Eclipse Public License 2.0 | 4 votes |
@Override public IEvaluationResult evaluate(XExpression expression) { return evaluate(expression, createContext(), CancelIndicator.NullImpl); }
Example #11
Source File: ReplAutoEdit.java From xtext-eclipse with Eclipse Public License 2.0 | 4 votes |
protected String computeResultText(final IDocument document, final DocumentCommand command, XtextResource resource) throws BadLocationException { if (resource.getContents().isEmpty()) return null; if (!(resource.getContents().get(0) instanceof Model)) return null; Model model = (Model) resource.getContents().get(0); if (model.getBlock() == null) return null; EList<XExpression> expressions = model.getBlock().getExpressions(); if (expressions.isEmpty()) return null; XExpression expression = expressions.get(expressions.size() - 1); if (expression == null) { return null; } ICompositeNode node = NodeModelUtils.getNode(expression); if (node == null || document.getLineOfOffset(command.offset) + 1 != node.getEndLine()) return null; List<Issue> issues = validator.validate(resource, CheckMode.NORMAL_AND_FAST, CancelIndicator.NullImpl); if (issues.stream().anyMatch(i -> i.getSeverity() == Severity.ERROR)) { return null; } XbaseInterpreter xbaseInterpreter = getConfiguredInterpreter(resource); IEvaluationResult result = xbaseInterpreter.evaluate(model.getBlock(), new DefaultEvaluationContext(), new CancelIndicator() { private long stopAt = System.currentTimeMillis() + 2000; @Override public boolean isCanceled() { return System.currentTimeMillis() > stopAt; } }); if (result == null) return null; String value = "" + result.getResult(); if (result.getException() != null) value = "threw " + result.getException().getClass().getSimpleName(); LightweightTypeReference expressionType = typeResolver.resolveTypes(expression).getActualType(expression); if (expressionType != null) { String newText = command.text + "// " + value + " (" + expressionType.getSimpleName() + ")" + command.text; return newText; } return command.text + "// " + value; }