@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); } }
@Override public Boolean caseXTryCatchFinallyExpression(XTryCatchFinallyExpression object) { List<XCatchClause> clauses = object.getCatchClauses(); if (clauses.isEmpty()) { return Boolean.TRUE; } 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; } caughtExceptions.add(caughtException); } delegate.collectThrownExceptions(clause.getExpression()); } delegate.collectThrownExceptions(object.getFinallyExpression()); if (wasThrowable) { return Boolean.FALSE; } delegate.catchExceptions(caughtExceptions).collectThrownExceptions(object.getExpression()); return Boolean.FALSE; }
protected void appendCatchClause(XCatchClause catchClause, boolean parentIsReferenced, String parentVariable, ITreeAppendable appendable) { JvmTypeReference type = catchClause.getDeclaredParam().getParameterType(); final String declaredParamName = makeJavaIdentifier(catchClause.getDeclaredParam().getName()); final String name = appendable.declareVariable(catchClause.getDeclaredParam(), declaredParamName); appendable.append("if (").append(parentVariable).append(" instanceof "); serialize(type, catchClause, appendable); appendable.append(") ").append("{"); appendable.increaseIndentation(); ITreeAppendable withDebugging = appendable.trace(catchClause, true); if (!XbaseUsageCrossReferencer.find(catchClause.getDeclaredParam(), catchClause.getExpression()).isEmpty()) { ITreeAppendable parameterAppendable = withDebugging.trace(catchClause.getDeclaredParam()); appendCatchClauseParameter(catchClause, type, name, parameterAppendable.newLine()); withDebugging.append(" = ("); serialize(type, catchClause, withDebugging); withDebugging.append(")").append(parentVariable).append(";"); } final boolean canBeReferenced = parentIsReferenced && ! isPrimitiveVoid(catchClause.getExpression()); internalToJavaStatement(catchClause.getExpression(), withDebugging, canBeReferenced); if (canBeReferenced) { appendable.newLine().append(getVarName(catchClause.eContainer(), appendable)).append(" = "); internalToConvertedExpression(catchClause.getExpression(), appendable, getLightweightType((XExpression) catchClause.eContainer())); appendable.append(";"); } closeBlock(appendable); }
/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ @SuppressWarnings("unchecked") @Override public void eSet(int featureID, Object newValue) { switch (featureID) { case XbasePackage.XTRY_CATCH_FINALLY_EXPRESSION__EXPRESSION: setExpression((XExpression)newValue); return; case XbasePackage.XTRY_CATCH_FINALLY_EXPRESSION__FINALLY_EXPRESSION: setFinallyExpression((XExpression)newValue); return; case XbasePackage.XTRY_CATCH_FINALLY_EXPRESSION__CATCH_CLAUSES: getCatchClauses().clear(); getCatchClauses().addAll((Collection<? extends XCatchClause>)newValue); return; } super.eSet(featureID, newValue); }
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()); }
/** * Contexts: * XCatchClause returns XCatchClause * * Constraint: * (declaredParam=FullJvmFormalParameter expression=XExpression) */ protected void sequence_XCatchClause(ISerializationContext context, XCatchClause semanticObject) { if (errorAcceptor != null) { if (transientValues.isValueTransient(semanticObject, XbasePackage.Literals.XCATCH_CLAUSE__DECLARED_PARAM) == ValueTransient.YES) errorAcceptor.accept(diagnosticProvider.createFeatureValueMissing(semanticObject, XbasePackage.Literals.XCATCH_CLAUSE__DECLARED_PARAM)); if (transientValues.isValueTransient(semanticObject, XbasePackage.Literals.XCATCH_CLAUSE__EXPRESSION) == ValueTransient.YES) errorAcceptor.accept(diagnosticProvider.createFeatureValueMissing(semanticObject, XbasePackage.Literals.XCATCH_CLAUSE__EXPRESSION)); } SequenceFeeder feeder = createSequencerFeeder(context, semanticObject); feeder.accept(grammarAccess.getXCatchClauseAccess().getDeclaredParamFullJvmFormalParameterParserRuleCall_2_0(), semanticObject.getDeclaredParam()); feeder.accept(grammarAccess.getXCatchClauseAccess().getExpressionXExpressionParserRuleCall_4_0(), semanticObject.getExpression()); feeder.finish(); }
@Check public void checkTypes(XCatchClause catchClause) { LightweightTypeReference parameterType = getActualType(catchClause, catchClause.getDeclaredParam()); if (parameterType != null && !parameterType.isSubtypeOf(Throwable.class)) { error("No exception of type " + parameterType.getHumanReadableName() + " can be thrown; an exception type must be a subclass of Throwable", catchClause.getDeclaredParam(), TypesPackage.Literals.JVM_FORMAL_PARAMETER__PARAMETER_TYPE, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, INCOMPATIBLE_TYPES); } }
protected void appendCatchAndFinally(XTryCatchFinallyExpression expr, ITreeAppendable b, boolean isReferenced) { final EList<XCatchClause> catchClauses = expr.getCatchClauses(); if (!catchClauses.isEmpty()) { String variable = b.declareSyntheticVariable(Tuples.pair(expr, "_catchedThrowable"), "_t"); b.append(" catch (final Throwable ").append(variable).append(") "); b.append("{").increaseIndentation(); b.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(); final JvmType sneakyThrowType = findKnownTopLevelType(Exceptions.class, expr); if (sneakyThrowType == null) { b.append("COMPILE ERROR : '"+Exceptions.class.getCanonicalName()+"' could not be found on the classpath!"); } else { b.newLine().append("throw "); b.append(sneakyThrowType); b.append(".sneakyThrow("); b.append(variable); b.append(");"); } closeBlock(b); closeBlock(b); } final XExpression finallyExp = expr.getFinallyExpression(); if (finallyExp != null) { b.append(" finally {").increaseIndentation(); internalToJavaStatement(finallyExp, b, false); b.decreaseIndentation().newLine().append("}"); } }
/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ public EList<XCatchClause> getCatchClauses() { if (catchClauses == null) { catchClauses = new EObjectContainmentEList<XCatchClause>(XCatchClause.class, this, XbasePackage.XTRY_CATCH_FINALLY_EXPRESSION__CATCH_CLAUSES); } return catchClauses; }
protected void _findImplicitReturns(final XTryCatchFinallyExpression expression, final ImplicitReturnFinder.Acceptor acceptor) { this.findImplicitReturns(expression.getExpression(), acceptor); final Consumer<XCatchClause> _function = (XCatchClause it) -> { this.findImplicitReturns(it.getExpression(), acceptor); }; expression.getCatchClauses().forEach(_function); }
protected void doTestTryCatchExpression(XTryCatchFinallyExpression tryEx) { assertFeatureCall("foo", ((XThrowExpression)tryEx.getExpression()).getExpression()); assertFeatureCall("baz", tryEx.getFinallyExpression()); assertEquals(1,tryEx.getCatchClauses().size()); XCatchClause clause = tryEx.getCatchClauses().get(0); assertFeatureCall("bar", clause.getExpression()); assertEquals("java.lang.Exception", clause.getDeclaredParam().getParameterType().getIdentifier()); assertEquals("e", clause.getDeclaredParam().getName()); }
protected void doTestTryCatchExpression_2(XTryCatchFinallyExpression tryEx) { assertFeatureCall("foo", tryEx.getExpression()); assertNull(tryEx.getFinallyExpression()); assertEquals(1,tryEx.getCatchClauses().size()); XCatchClause clause = tryEx.getCatchClauses().get(0); assertFeatureCall("bar", clause.getExpression()); assertEquals("java.lang.Exception", clause.getDeclaredParam().getParameterType().getIdentifier()); assertEquals("e", clause.getDeclaredParam().getName()); }
@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()); }
@Test public void testTryCatch() { try { StringConcatenation _builder = new StringConcatenation(); _builder.append("try 1 catch(Exception e) 2"); XExpression _expression = this.expression(_builder); final XTryCatchFinallyExpression expr = ((XTryCatchFinallyExpression) _expression); this.hasImplicitReturns(expr, expr.getExpression(), IterableExtensions.<XCatchClause>head(expr.getCatchClauses()).getExpression()); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
/** * Constraint: * (declaredParam=FullJvmFormalParameter expression=XExpression) */ protected void sequence_XCatchClause(EObject context, XCatchClause semanticObject) { if(errorAcceptor != null) { if(transientValues.isValueTransient(semanticObject, XbasePackage.Literals.XCATCH_CLAUSE__EXPRESSION) == ValueTransient.YES) errorAcceptor.accept(diagnosticProvider.createFeatureValueMissing(semanticObject, XbasePackage.Literals.XCATCH_CLAUSE__EXPRESSION)); if(transientValues.isValueTransient(semanticObject, XbasePackage.Literals.XCATCH_CLAUSE__DECLARED_PARAM) == ValueTransient.YES) errorAcceptor.accept(diagnosticProvider.createFeatureValueMissing(semanticObject, XbasePackage.Literals.XCATCH_CLAUSE__DECLARED_PARAM)); } INodesForEObjectProvider nodes = createNodeProvider(semanticObject); SequenceFeeder feeder = createSequencerFeeder(semanticObject, nodes); feeder.accept(grammarAccess.getXCatchClauseAccess().getDeclaredParamFullJvmFormalParameterParserRuleCall_2_0(), semanticObject.getDeclaredParam()); feeder.accept(grammarAccess.getXCatchClauseAccess().getExpressionXExpressionParserRuleCall_4_0(), semanticObject.getExpression()); feeder.finish(); }
@Deprecated protected void sequence_XCatchClause(EObject context, XCatchClause semanticObject) { sequence_XCatchClause(createContext(context, semanticObject), semanticObject); }
protected boolean isValueExpectedRecursive(XExpression expr) { EStructuralFeature feature = expr.eContainingFeature(); EObject container = expr.eContainer(); // is part of block if (container instanceof XBlockExpression) { XBlockExpression blockExpression = (XBlockExpression) container; final List<XExpression> expressions = blockExpression.getExpressions(); if (expressions.get(expressions.size()-1) != expr) { return false; } } // no expectation cases if (feature == XbasePackage.Literals.XTRY_CATCH_FINALLY_EXPRESSION__FINALLY_EXPRESSION || feature == XbasePackage.Literals.XABSTRACT_WHILE_EXPRESSION__BODY || feature == XbasePackage.Literals.XFOR_LOOP_EXPRESSION__EACH_EXPRESSION) { return false; } // is value expected if (container instanceof XAbstractFeatureCall || container instanceof XConstructorCall || container instanceof XAssignment || container instanceof XVariableDeclaration || container instanceof XReturnExpression || container instanceof XThrowExpression || feature == XbasePackage.Literals.XFOR_LOOP_EXPRESSION__FOR_EXPRESSION || feature == XbasePackage.Literals.XSWITCH_EXPRESSION__SWITCH || feature == XbasePackage.Literals.XCASE_PART__CASE || feature == XbasePackage.Literals.XIF_EXPRESSION__IF || feature == XbasePackage.Literals.XABSTRACT_WHILE_EXPRESSION__PREDICATE || feature == XbasePackage.Literals.XBASIC_FOR_LOOP_EXPRESSION__EXPRESSION || feature == XbasePackage.Literals.XSYNCHRONIZED_EXPRESSION__PARAM) { return true; } if (isLocalClassSemantics(container) || logicalContainerProvider.getLogicalContainer(expr) != null) { LightweightTypeReference expectedReturnType = typeResolver.resolveTypes(expr).getExpectedReturnType(expr); return expectedReturnType == null || !expectedReturnType.isPrimitiveVoid(); } if (container instanceof XCasePart || container instanceof XCatchClause) { container = container.eContainer(); } if (container instanceof XExpression) { return isValueExpectedRecursive((XExpression) container); } return true; }
@Override public Boolean caseXCatchClause(XCatchClause object) { return Boolean.TRUE; }
/** * 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; }
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; }
protected void appendCatchClauseParameter(XCatchClause catchClause, JvmTypeReference parameterType, final String parameterName, ITreeAppendable appendable) { appendable.append("final "); serialize(parameterType, catchClause, appendable); appendable.append(" "); appendable.trace(catchClause.getDeclaredParam(), TypesPackage.Literals.JVM_FORMAL_PARAMETER__NAME, 0).append(parameterName); }
@Test public void testTryCatch_0() throws Exception { XTryCatchFinallyExpression exp = (XTryCatchFinallyExpression) expression("try 'foo' catch (Exception e) e",true); XCatchClause xCatchClause = exp.getCatchClauses().get(0); assertSame(xCatchClause.getDeclaredParam(), ((XFeatureCall)xCatchClause.getExpression()).getFeature()); }