Java Code Examples for org.eclipse.xtext.xbase.XTryCatchFinallyExpression#getCatchClauses()
The following examples show how to use
org.eclipse.xtext.xbase.XTryCatchFinallyExpression#getCatchClauses() .
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: XbaseValidator.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
@Check public void checkCatchClausesOrder(XTryCatchFinallyExpression expression) { ITypeReferenceOwner owner = new StandardTypeReferenceOwner(getServices(), expression); List<LightweightTypeReference> previousTypeReferences = new ArrayList<LightweightTypeReference>(); for (XCatchClause catchClause : expression.getCatchClauses()) { LightweightTypeReference actualTypeReference = owner.toLightweightTypeReference(catchClause.getDeclaredParam().getParameterType()); if (actualTypeReference == null) { continue; } if (isHandled(actualTypeReference, previousTypeReferences)) { error("Unreachable code: The catch block can never match. It is already handled by a previous condition.", catchClause.getDeclaredParam().getParameterType(), null, IssueCodes.UNREACHABLE_CATCH_BLOCK); continue; } previousTypeReferences.add(actualTypeReference); } }
Example 2
Source File: DefaultEarlyExitComputer.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
protected Collection<IEarlyExitComputer.ExitPoint> _exitPoints(final XTryCatchFinallyExpression expression) { Collection<IEarlyExitComputer.ExitPoint> tryExitPoints = this.getExitPoints(expression.getExpression()); boolean _isNotEmpty = this.isNotEmpty(tryExitPoints); if (_isNotEmpty) { Collection<IEarlyExitComputer.ExitPoint> result = Lists.<IEarlyExitComputer.ExitPoint>newArrayList(tryExitPoints); EList<XCatchClause> _catchClauses = expression.getCatchClauses(); for (final XCatchClause catchClause : _catchClauses) { { Collection<IEarlyExitComputer.ExitPoint> catchExitPoints = this.getExitPoints(catchClause.getExpression()); boolean _isNotEmpty_1 = this.isNotEmpty(catchExitPoints); if (_isNotEmpty_1) { result.addAll(catchExitPoints); } else { return this.getExitPoints(expression.getFinallyExpression()); } } } return result; } return this.getExitPoints(expression.getFinallyExpression()); }
Example 3
Source File: SARLOperationHelper.java From sarl with Apache License 2.0 | 6 votes |
/** Test if the given expression has side effects. * * @param expression the expression. * @param context the list of context expressions. * @return {@code true} if the expression has side effects. */ protected Boolean _hasSideEffects(XTryCatchFinallyExpression expression, ISideEffectContext context) { final List<Map<String, List<XExpression>>> buffers = new ArrayList<>(); Map<String, List<XExpression>> buffer = context.createVariableAssignmentBufferForBranch(); if (hasSideEffects(expression.getExpression(), context.branch(buffer))) { return true; } buffers.add(buffer); for (final XCatchClause clause : expression.getCatchClauses()) { context.open(); try { buffer = context.createVariableAssignmentBufferForBranch(); if (hasSideEffects(clause.getExpression(), context.branch(buffer))) { return true; } buffers.add(buffer); } finally { context.close(); } } context.mergeBranchVariableAssignments(buffers); if (hasSideEffects(expression.getFinallyExpression(), context)) { return true; } return false; }
Example 4
Source File: PyExpressionGenerator.java From sarl with Apache License 2.0 | 6 votes |
/** 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 5
Source File: XbaseExpectedTypeProviderTest.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@Test public void testTryCatchExpression() throws Exception { XTryCatchFinallyExpression exp = (XTryCatchFinallyExpression) expressionWithExpectedType("try null catch (java.lang.Throwable t) null finally null", "String"); assertExpected("java.lang.String", exp.getExpression()); for (XCatchClause cc : exp.getCatchClauses()) { assertExpected("java.lang.String", cc.getExpression()); } assertExpected(null, exp.getFinallyExpression()); }
Example 6
Source File: ThrownExceptionSwitch.java From xtext-extras with Eclipse Public License 2.0 | 4 votes |
@Override public Boolean caseXTryCatchFinallyExpression(XTryCatchFinallyExpression object) { List<XVariableDeclaration> resources = object.getResources(); List<XCatchClause> clauses = object.getCatchClauses(); if (clauses.isEmpty()) { // collect exceptions thrown by automatic close method // of given resources processExceptionsFromAutoclosable(resources, delegate); // let procedure traverse child elements, to check if they throw // exceptions return Boolean.TRUE; } // traverse child elements explicitly, to filter for caught exceptions final List<LightweightTypeReference> caughtExceptions = Lists.newArrayList(); boolean wasThrowable = false; for (XCatchClause clause : clauses) { JvmTypeReference caught = clause.getDeclaredParam().getParameterType(); if (caught != null) { LightweightTypeReference caughtException = delegate.toLightweightReference(caught) .getRawTypeReference(); if (caughtException.isType(Throwable.class)) { wasThrowable = true; } if (caughtException.isSynonym()) { caughtExceptions.addAll(caughtException.getMultiTypeComponents()); } else { caughtExceptions.add(caughtException); } } delegate.collectThrownExceptions(clause.getExpression()); } delegate.collectThrownExceptions(object.getFinallyExpression()); if (wasThrowable) { // Stop child traversing of procedure return Boolean.FALSE; } // Filter caught exceptions from all thrown excpetions // (thrown by resource constructor/automatic close, try expressions) IThrownExceptionDelegate filteredDelegate = delegate.catchExceptions(caughtExceptions); processExceptionsFromAutoclosable(resources, filteredDelegate); filteredDelegate.collectThrownExceptions(object.getExpression()); // Stop child traversing of procedure return Boolean.FALSE; }
Example 7
Source File: ExtendedEarlyExitComputer.java From xtext-extras with Eclipse Public License 2.0 | 4 votes |
/** * Returns <code>true</code> for expressions that seem to be early exit expressions, e.g. * <pre> * while(condition) { * if (anotherCondition) * return value * changeResultOfFirstCondition * } * </pre> */ public boolean isIntentionalEarlyExit(/* @Nullable */ XExpression expression) { if (expression == null) { return true; } if (expression instanceof XBlockExpression) { XBlockExpression block = (XBlockExpression) expression; List<XExpression> children = block.getExpressions(); for(XExpression child: children) { if (isIntentionalEarlyExit(child)) { return true; } } } else if (expression instanceof XIfExpression) { return isIntentionalEarlyExit(((XIfExpression) expression).getThen()) || isIntentionalEarlyExit(((XIfExpression) expression).getElse()); } else if (expression instanceof XSwitchExpression) { XSwitchExpression switchExpression = (XSwitchExpression) expression; for(XCasePart caseExpression: switchExpression.getCases()) { if (isIntentionalEarlyExit(caseExpression.getThen())) { return true; } } if (isIntentionalEarlyExit(switchExpression.getDefault())) { return true; } } else if (expression instanceof XTryCatchFinallyExpression) { XTryCatchFinallyExpression tryCatchFinally = (XTryCatchFinallyExpression) expression; if (isIntentionalEarlyExit(tryCatchFinally.getExpression())) { for(XCatchClause catchClause: tryCatchFinally.getCatchClauses()) { if (!isIntentionalEarlyExit(catchClause.getExpression())) return false; } return true; } return false; } else if (expression instanceof XAbstractWhileExpression) { return isIntentionalEarlyExit(((XAbstractWhileExpression) expression).getBody()); } else if (expression instanceof XForLoopExpression) { return isIntentionalEarlyExit(((XForLoopExpression) expression).getEachExpression()); } else if (expression instanceof XBasicForLoopExpression) { return isIntentionalEarlyExit(((XBasicForLoopExpression) expression).getEachExpression()); } else if (expression instanceof XSynchronizedExpression) { return isIntentionalEarlyExit(((XSynchronizedExpression) expression).getExpression()); } return expression instanceof XReturnExpression || expression instanceof XThrowExpression; }
Example 8
Source File: ExtendedEarlyExitComputer.java From xtext-extras with Eclipse Public License 2.0 | 4 votes |
public boolean isDefiniteEarlyExit(XExpression expression) { // TODO further improvements if (expression instanceof XIfExpression) { XIfExpression ifExpression = (XIfExpression) expression; return isDefiniteEarlyExit(ifExpression.getThen()) && isDefiniteEarlyExit(ifExpression.getElse()); } else if (expression instanceof XSwitchExpression) { XSwitchExpression switchExpression = (XSwitchExpression) expression; if (isDefiniteEarlyExit(switchExpression.getDefault())) { for(XCasePart caseExpression: switchExpression.getCases()) { if (!isDefiniteEarlyExit(caseExpression.getThen())) { return false; } } return true; } return false; } else if (expression instanceof XTryCatchFinallyExpression) { XTryCatchFinallyExpression tryExpression = (XTryCatchFinallyExpression) expression; if (isDefiniteEarlyExit(tryExpression.getFinallyExpression())) { return true; } if (isDefiniteEarlyExit(tryExpression.getExpression())) { for(XCatchClause catchClause: tryExpression.getCatchClauses()) { if (!isDefiniteEarlyExit(catchClause.getExpression())) { return false; } } return true; } return false; } else if (expression instanceof XBlockExpression) { List<XExpression> expressions = ((XBlockExpression) expression).getExpressions(); for(int i = expressions.size() - 1; i >= 0; i--) { if (isDefiniteEarlyExit(expressions.get(i))) { return true; } } } else if (expression instanceof XSynchronizedExpression) { return isDefiniteEarlyExit(((XSynchronizedExpression) expression).getExpression()); } return expression instanceof XReturnExpression || expression instanceof XThrowExpression; }
Example 9
Source File: XbaseCompiler.java From xtext-extras with Eclipse Public License 2.0 | 4 votes |
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 10
Source File: XbaseInterpreter.java From xtext-extras with Eclipse Public License 2.0 | 4 votes |
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; }