Java Code Examples for org.eclipse.xtext.xbase.XTryCatchFinallyExpression#getFinallyExpression()

The following examples show how to use org.eclipse.xtext.xbase.XTryCatchFinallyExpression#getFinallyExpression() . 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: PyExpressionGenerator.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Generate the given object.
 *
 * @param tryStatement the try-catch-finally statement.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the statement.
 */
protected XExpression _generate(XTryCatchFinallyExpression tryStatement, IAppendable it, IExtraLanguageGeneratorContext context) {
	it.append("try:"); //$NON-NLS-1$
	it.increaseIndentation().newLine();
	generate(tryStatement.getExpression(), context.getExpectedExpressionType(), it, context);
	it.decreaseIndentation().newLine();
	for (final XCatchClause clause : tryStatement.getCatchClauses()) {
		it.append("except "); //$NON-NLS-1$
		it.append(clause.getDeclaredParam().getParameterType().getType());
		it.append(", "); //$NON-NLS-1$
		it.append(it.declareUniqueNameVariable(clause.getDeclaredParam(), clause.getDeclaredParam().getSimpleName()));
		it.append(":"); //$NON-NLS-1$
		it.increaseIndentation().newLine();
		generate(clause.getExpression(), context.getExpectedExpressionType(), it, context);
		it.decreaseIndentation().newLine();
	}
	if (tryStatement.getFinallyExpression() != null) {
		it.append("finally:"); //$NON-NLS-1$
		it.increaseIndentation().newLine();
		generate(tryStatement.getFinallyExpression(), it, context);
		it.decreaseIndentation();
	}
	return tryStatement;
}
 
Example 2
Source File: LinkingErrorTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testResourceNotVisibleInFinally() throws Exception {
	XtendFunction function = function("def tryCatch() {\n" + 
			"	    try(val a = []) {} finally a" + 
			"	}");
	XTryCatchFinallyExpression tryCatch = (XTryCatchFinallyExpression) ((XBlockExpression) function.getExpression()).getExpressions().get(0);
	XFeatureCall call = (XFeatureCall) tryCatch.getFinallyExpression();
	JvmIdentifiableElement feature = call.getFeature();
	assertTrue(feature.eIsProxy());
}
 
Example 3
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void appendCatchAndFinally(XTryCatchFinallyExpression expr, ITreeAppendable b, boolean isReferenced) {
	final EList<XCatchClause> catchClauses = expr.getCatchClauses();
	final XExpression finallyExp = expr.getFinallyExpression();
	boolean isTryWithResources = !expr.getResources().isEmpty();
	// If Java 7 or better: use Java's try-with-resources
	boolean nativeTryWithResources = isAtLeast(b, JAVA7);
	final String throwablesStore = b.getName(Tuples.pair(expr, "_caughtThrowables"));

	// Catch
	if (!catchClauses.isEmpty()) {
		String variable = b.declareSyntheticVariable(Tuples.pair(expr, "_caughtThrowable"), "_t");
		b.append(" catch (final Throwable ").append(variable).append(") ");
		b.append("{").increaseIndentation().newLine();
		Iterator<XCatchClause> iterator = catchClauses.iterator();
		while (iterator.hasNext()) {
			XCatchClause catchClause = iterator.next();
			ITreeAppendable catchClauseAppendable = b.trace(catchClause);
			appendCatchClause(catchClause, isReferenced, variable, catchClauseAppendable);
			if (iterator.hasNext()) {
				b.append(" else ");
			}
		}
		b.append(" else {");
		b.increaseIndentation().newLine();
		if (isTryWithResources && !nativeTryWithResources) {
			b.append(throwablesStore + ".add(" + variable + ");").newLine();
		}
		appendSneakyThrow(expr, b, variable);
		closeBlock(b);
		closeBlock(b);
	}

	// Finally
	if (finallyExp != null || (!nativeTryWithResources && isTryWithResources)) {
		b.append(" finally {").increaseIndentation();
		if (finallyExp != null)
			internalToJavaStatement(finallyExp, b, false);
		if (!nativeTryWithResources && isTryWithResources)
			appendFinallyWithResources(expr, b);
		b.decreaseIndentation().newLine().append("}");
	}
}
 
Example 4
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected Object _doEvaluate(XTryCatchFinallyExpression tryCatchFinally, 
		IEvaluationContext context,
		CancelIndicator indicator) {
	Object result = null;
	ReturnValue returnValue = null;
	Map<String, Boolean> resIsInit = new HashMap<String, Boolean>();
	List<XVariableDeclaration> resources = tryCatchFinally.getResources();
	List<EvaluationException> caughtExceptions = newArrayList();
	// Resources
	try {
		for (XVariableDeclaration res : resources) {
			resIsInit.put(res.getName(), false);
			result = internalEvaluate(res, context, indicator);
			// Remember for automatic close which resources are initialized
			resIsInit.put(res.getName(), true);
		}
		// Expression Body
		result = internalEvaluate(tryCatchFinally.getExpression(), context, indicator);

	} catch (ReturnValue value) {
		// Keep thrown return value in mind until resources are closed
		returnValue = value;
	} catch (EvaluationException evaluationException) {
		Throwable cause = evaluationException.getCause();
		boolean caught = false;
		// Catch Clauses
		for (XCatchClause catchClause : tryCatchFinally.getCatchClauses()) {
			JvmFormalParameter exception = catchClause.getDeclaredParam();
			JvmTypeReference catchParameterType = exception.getParameterType();
			if (!isInstanceoOf(cause, catchParameterType)) {
				continue;
			}
			IEvaluationContext forked = context.fork();
			forked.newValue(QualifiedName.create(exception.getName()), cause);
			result = internalEvaluate(catchClause.getExpression(), forked, indicator);
			caught = true;
			break;
		}
		// Save uncaught exception
		if(!caught) caughtExceptions.add(evaluationException);
	}

	// finally expressions ...
	// ... given
	if (tryCatchFinally.getFinallyExpression() != null) {
		try {
			internalEvaluate(tryCatchFinally.getFinallyExpression(), context, indicator);
		} catch (EvaluationException e) {
			throw new EvaluationException(new FinallyDidNotCompleteException(e));
		}
	}
	// ... prompted by try with resources (automatic close)
	if (!resources.isEmpty()) {
		for (int i = resources.size() - 1; i >= 0; i--) {
			XVariableDeclaration resource = resources.get(i);
			// Only close resources that are instantiated (= avoid
			// NullPointerException)
			if (resIsInit.get(resource.getName())) {
				// Find close method for resource
				JvmOperation close = findCloseMethod(resource);
				// Invoke close on resource
				if (close != null) {
					// Invoking the close method might throw
					// a EvaluationException. Hence, we collect those thrown
					// EvaluationExceptions and propagate them later on.
					try {
						invokeOperation(close,
								context.getValue(QualifiedName.create(resource.getSimpleName())),
								Collections.emptyList());
					} catch (EvaluationException t) {
						caughtExceptions.add(t);
					}
				}
			}
		}
	}
	
	// Throw caught exceptions if there are any
	if (!caughtExceptions.isEmpty()) throw caughtExceptions.get(0);
				
	// throw return value from expression block after resources are closed
	if (returnValue != null)
		throw returnValue;

	return result;
}