Java Code Examples for org.eclipse.xtext.xbase.interpreter.IEvaluationResult#getException()

The following examples show how to use org.eclipse.xtext.xbase.interpreter.IEvaluationResult#getException() . 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: ClosureInvocationHandler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@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 2
Source File: ScriptImpl.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@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 3
Source File: SarlScriptExecutor.java    From sarl with Apache License 2.0 5 votes vote down vote up
@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 4
Source File: ScriptImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@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 5
Source File: ReplAutoEdit.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
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;
}