org.eclipse.xtext.xbase.XTryCatchFinallyExpression Java Examples

The following examples show how to use org.eclipse.xtext.xbase.XTryCatchFinallyExpression. 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: DefaultEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
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: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean bracesAreAddedByOuterStructure(XExpression expression) {
	EObject container = expression.eContainer();
	if (container instanceof XTryCatchFinallyExpression 
			|| container instanceof XIfExpression
			|| container instanceof XClosure
			|| container instanceof XSynchronizedExpression) {
		return true;
	}
	if (container instanceof XBlockExpression) {
		XBlockExpression blockExpression = (XBlockExpression) container;
		EList<XExpression> expressions = blockExpression.getExpressions();
		if (expressions.size() == 1 && expressions.get(0) == expression) {
			return bracesAreAddedByOuterStructure(blockExpression);
		}
	}
	if (!(container instanceof XExpression)) {
		return true;
	}
	return false;
}
 
Example #4
Source File: XbaseSyntacticSequencer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Syntax: '('*
 */
@Override
protected void emit_XParenthesizedExpression_LeftParenthesisKeyword_0_a(EObject semanticObject,
		ISynNavigable transition, List<INode> nodes) {

	Keyword kw = grammarAccess.getXParenthesizedExpressionAccess().getLeftParenthesisKeyword_0();

	if (nodes == null) {
		if (semanticObject instanceof XIfExpression || semanticObject instanceof XTryCatchFinallyExpression) {
			EObject cnt = semanticObject.eContainer();
			if (cnt instanceof XExpression && !(cnt instanceof XBlockExpression)
					&& !(cnt instanceof XForLoopExpression))
				acceptUnassignedKeyword(kw, kw.getValue(), null);
		}
		if (semanticObject instanceof XConstructorCall) {
			XConstructorCall call = (XConstructorCall) semanticObject;
			if (!call.isExplicitConstructorCall() && call.getArguments().isEmpty()) {
				acceptUnassignedKeyword(kw, kw.getValue(), null);
			}
		}
	}
	acceptNodes(transition, nodes);
}
 
Example #5
Source File: XtendSyntacticSequencer.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Syntax: '('*
 */
@Override
protected void emit_XParenthesizedExpression_LeftParenthesisKeyword_0_a(EObject semanticObject,
		ISynNavigable transition, List<INode> nodes) {

	Keyword kw = grammarAccess.getXParenthesizedExpressionAccess().getLeftParenthesisKeyword_0();

	if (nodes == null) {
		if (semanticObject instanceof XIfExpression || semanticObject instanceof XTryCatchFinallyExpression) {
			EObject cnt = semanticObject.eContainer();
			if (cnt instanceof XExpression && !(cnt instanceof XBlockExpression)
					&& !(cnt instanceof XForLoopExpression))
				acceptUnassignedKeyword(kw, kw.getValue(), null);
		}
		if (semanticObject instanceof XConstructorCall) {
			XConstructorCall call = (XConstructorCall) semanticObject;
			if (!call.isExplicitConstructorCall() && call.getArguments().isEmpty()) {
				acceptUnassignedKeyword(kw, kw.getValue(), null);
			}
		}
	}
	acceptNodes(transition, nodes);
}
 
Example #6
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@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 #7
Source File: SARLOperationHelper.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** 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 #8
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 #9
Source File: LinkingErrorTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testResourceNotVisibleInCatch() throws Exception {
	XtendFunction function = function("def tryCatch() {\n" + 
			"	    try(val a = []) {} catch(Exception e) a" + 
			"	}");
	XTryCatchFinallyExpression tryCatch = (XTryCatchFinallyExpression) ((XBlockExpression) function.getExpression()).getExpressions().get(0);
	XFeatureCall call = (XFeatureCall) tryCatch.getCatchClauses().get(0).getExpression();
	JvmIdentifiableElement feature = call.getFeature();
	assertTrue(feature.eIsProxy());
}
 
Example #10
Source File: LinkingErrorTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testResourceVisibleInTry() throws Exception {
	XtendFunction function = function("def tryCatch() {\n" + 
			"	    try(val a = []) a" + 
			"	}");
	XTryCatchFinallyExpression tryCatch = (XTryCatchFinallyExpression) ((XBlockExpression) function.getExpression()).getExpressions().get(0);
	XFeatureCall call = (XFeatureCall) tryCatch.getExpression();
	JvmIdentifiableElement feature = call.getFeature();
	assertFalse(feature.eIsProxy());
}
 
Example #11
Source File: ParserTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMultiCatch_02() throws Exception {
	XtendClass clazz = clazz("class Foo { def void m() { try {} catch(extension NullPointerException | IllegalArgumentException | IllegalStateException e) {} } }");
	assertEquals(1, clazz.getMembers().size());
	XtendFunction m = (XtendFunction) clazz.getMembers().get(0);
	XBlockExpression body = (XBlockExpression) m.getExpression();
	assertEquals(1, body.getExpressions().size());
	XTryCatchFinallyExpression tryCatch = (XTryCatchFinallyExpression) body.getExpressions().get(0);
	XCatchClause singleCatchClause = tryCatch.getCatchClauses().get(0);
	XtendFormalParameter parameter = (XtendFormalParameter) singleCatchClause.getDeclaredParam();
	assertTrue(parameter.isExtension());
	JvmSynonymTypeReference parameterType = (JvmSynonymTypeReference) parameter.getParameterType();
	assertEquals(3, parameterType.getReferences().size());
}
 
Example #12
Source File: ParserTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testMultiCatch_01() throws Exception {
	XtendClass clazz = clazz("class Foo { def void m() { try {} catch(NullPointerException | IllegalArgumentException e) {} } }");
	assertEquals(1, clazz.getMembers().size());
	XtendFunction m = (XtendFunction) clazz.getMembers().get(0);
	XBlockExpression body = (XBlockExpression) m.getExpression();
	assertEquals(1, body.getExpressions().size());
	XTryCatchFinallyExpression tryCatch = (XTryCatchFinallyExpression) body.getExpressions().get(0);
	XCatchClause singleCatchClause = tryCatch.getCatchClauses().get(0);
	XtendFormalParameter parameter = (XtendFormalParameter) singleCatchClause.getDeclaredParam();
	assertFalse(parameter.isExtension());
	JvmSynonymTypeReference parameterType = (JvmSynonymTypeReference) parameter.getParameterType();
	assertEquals(2, parameterType.getReferences().size());
}
 
Example #13
Source File: ParserTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testExtensionOnCatchClause_01() throws Exception {
	XtendClass clazz = clazz("class Foo { def void m() { try {} catch(extension NullPointerException e) {} } }");
	assertEquals(1, clazz.getMembers().size());
	XtendFunction m = (XtendFunction) clazz.getMembers().get(0);
	XBlockExpression body = (XBlockExpression) m.getExpression();
	assertEquals(1, body.getExpressions().size());
	XTryCatchFinallyExpression tryCatch = (XTryCatchFinallyExpression) body.getExpressions().get(0);
	XCatchClause singleCatchClause = tryCatch.getCatchClauses().get(0);
	XtendFormalParameter parameter = (XtendFormalParameter) singleCatchClause.getDeclaredParam();
	assertTrue(parameter.isExtension());
}
 
Example #14
Source File: XbaseExpectedTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@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 #15
Source File: XbaseParserTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=356722
 */
@Test public void testTryCatchExpression_3() throws Exception {
	XTryCatchFinallyExpression tryEx = (XTryCatchFinallyExpression) expression(
			"try foo catch (java.lang.Exception) {}");
	Iterator<INode> iterator = ((XtextResource)tryEx.eResource()).getParseResult().getSyntaxErrors().iterator();
	final INode errorNode = iterator.next();
	assertEquals(")",errorNode.getText());
	assertEquals("missing RULE_ID at ')'",errorNode.getSyntaxErrorMessage().getMessage());
}
 
Example #16
Source File: XbaseParserTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
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());
}
 
Example #17
Source File: XbaseParserTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
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());
}
 
Example #18
Source File: DefaultEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected Collection<IEarlyExitComputer.ExitPoint> exitPoints(final XExpression expression) {
  if (expression instanceof XDoWhileExpression) {
    return _exitPoints((XDoWhileExpression)expression);
  } else if (expression instanceof XWhileExpression) {
    return _exitPoints((XWhileExpression)expression);
  } else if (expression instanceof XAbstractFeatureCall) {
    return _exitPoints((XAbstractFeatureCall)expression);
  } else if (expression instanceof XBasicForLoopExpression) {
    return _exitPoints((XBasicForLoopExpression)expression);
  } else if (expression instanceof XBlockExpression) {
    return _exitPoints((XBlockExpression)expression);
  } else if (expression instanceof XConstructorCall) {
    return _exitPoints((XConstructorCall)expression);
  } else if (expression instanceof XForLoopExpression) {
    return _exitPoints((XForLoopExpression)expression);
  } else if (expression instanceof XIfExpression) {
    return _exitPoints((XIfExpression)expression);
  } else if (expression instanceof XReturnExpression) {
    return _exitPoints((XReturnExpression)expression);
  } else if (expression instanceof XSwitchExpression) {
    return _exitPoints((XSwitchExpression)expression);
  } else if (expression instanceof XSynchronizedExpression) {
    return _exitPoints((XSynchronizedExpression)expression);
  } else if (expression instanceof XThrowExpression) {
    return _exitPoints((XThrowExpression)expression);
  } else if (expression instanceof XTryCatchFinallyExpression) {
    return _exitPoints((XTryCatchFinallyExpression)expression);
  } else if (expression instanceof XVariableDeclaration) {
    return _exitPoints((XVariableDeclaration)expression);
  } else if (expression != null) {
    return _exitPoints(expression);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(expression).toString());
  }
}
 
Example #19
Source File: XbaseImplicitReturnFinder.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
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);
}
 
Example #20
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void internalToConvertedExpression(XExpression obj, ITreeAppendable appendable) {
	if (obj instanceof XBlockExpression) {
		_toJavaExpression((XBlockExpression) obj, appendable);
	} else if (obj instanceof XCastedExpression) {
		_toJavaExpression((XCastedExpression) obj, appendable);
	} else if (obj instanceof XClosure) {
		_toJavaExpression((XClosure) obj, appendable);
	} else if (obj instanceof XAnnotation) {
		_toJavaExpression((XAnnotation) obj, appendable);
	} else if (obj instanceof XConstructorCall) {
		_toJavaExpression((XConstructorCall) obj, appendable);
	} else if (obj instanceof XIfExpression) {
		_toJavaExpression((XIfExpression) obj, appendable);
	} else if (obj instanceof XInstanceOfExpression) {
		_toJavaExpression((XInstanceOfExpression) obj, appendable);
	} else if (obj instanceof XSwitchExpression) {
		_toJavaExpression((XSwitchExpression) obj, appendable);
	} else if (obj instanceof XTryCatchFinallyExpression) {
		_toJavaExpression((XTryCatchFinallyExpression) obj, appendable);
	} else if (obj instanceof XListLiteral) {
		_toJavaExpression((XListLiteral) obj, appendable);
	} else if (obj instanceof XSetLiteral) {
		_toJavaExpression((XSetLiteral) obj, appendable);
	} else if (obj instanceof XSynchronizedExpression) {
		_toJavaExpression((XSynchronizedExpression) obj, appendable);
	} else if (obj instanceof XReturnExpression) {
		_toJavaExpression((XReturnExpression) obj, appendable);
	} else if (obj instanceof XThrowExpression) {
		_toJavaExpression((XThrowExpression) obj, appendable);
	} else {
		super.internalToConvertedExpression(obj, appendable);
	}
}
 
Example #21
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.18
 */
protected void appendFinallyWithResources(XTryCatchFinallyExpression expr, ITreeAppendable b) {
	final String throwablesStore = b.getName(Tuples.pair(expr, "_caughtThrowables"));
	List<XVariableDeclaration> resources = expr.getResources();
	if (!resources.isEmpty()) {
		for (int i = resources.size() - 1; i >= 0; i--) {
			b.openPseudoScope();
			XVariableDeclaration res = resources.get(i);
			String resName = getVarName(res, b);
			b.newLine().append("if (" + resName + " != null) {");
			b.increaseIndentation();
			b.newLine().append("try {");
			b.increaseIndentation();
			b.newLine().append(resName + ".close();");
			// close inner try
			closeBlock(b);
			String throwable = b.declareSyntheticVariable(Tuples.pair(res, "_caughtThrowable"), "_t");
			b.append(" catch (").append(Throwable.class).append(" " + throwable + ") {");
			b.increaseIndentation();
			b.newLine().append(throwablesStore);
			b.append(".add(" + throwable + ");");
			// close inner catch
			closeBlock(b);
			// close if != null check
			closeBlock(b);
			b.closeScope();
		}
		b.newLine().append("if(!");
		b.append(throwablesStore);
		b.append(".isEmpty()) ");
		appendSneakyThrow(expr, b, throwablesStore + ".get(0)");
	}
}
 
Example #22
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendSneakyThrow(XTryCatchFinallyExpression expr, ITreeAppendable b, String variable) {
	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.append("throw ");
		b.append(sneakyThrowType);
		b.append(".sneakyThrow(");
		b.append(variable);
		b.append(");");
	}
}
 
Example #23
Source File: XbaseImplicitReturnFinder.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public void findImplicitReturns(final XExpression expression, final ImplicitReturnFinder.Acceptor acceptor) {
  if (expression instanceof XAbstractFeatureCall) {
    _findImplicitReturns((XAbstractFeatureCall)expression, acceptor);
    return;
  } else if (expression instanceof XBlockExpression) {
    _findImplicitReturns((XBlockExpression)expression, acceptor);
    return;
  } else if (expression instanceof XBooleanLiteral) {
    _findImplicitReturns((XBooleanLiteral)expression, acceptor);
    return;
  } else if (expression instanceof XCastedExpression) {
    _findImplicitReturns((XCastedExpression)expression, acceptor);
    return;
  } else if (expression instanceof XClosure) {
    _findImplicitReturns((XClosure)expression, acceptor);
    return;
  } else if (expression instanceof XCollectionLiteral) {
    _findImplicitReturns((XCollectionLiteral)expression, acceptor);
    return;
  } else if (expression instanceof XConstructorCall) {
    _findImplicitReturns((XConstructorCall)expression, acceptor);
    return;
  } else if (expression instanceof XIfExpression) {
    _findImplicitReturns((XIfExpression)expression, acceptor);
    return;
  } else if (expression instanceof XInstanceOfExpression) {
    _findImplicitReturns((XInstanceOfExpression)expression, acceptor);
    return;
  } else if (expression instanceof XNullLiteral) {
    _findImplicitReturns((XNullLiteral)expression, acceptor);
    return;
  } else if (expression instanceof XNumberLiteral) {
    _findImplicitReturns((XNumberLiteral)expression, acceptor);
    return;
  } else if (expression instanceof XStringLiteral) {
    _findImplicitReturns((XStringLiteral)expression, acceptor);
    return;
  } else if (expression instanceof XSwitchExpression) {
    _findImplicitReturns((XSwitchExpression)expression, acceptor);
    return;
  } else if (expression instanceof XSynchronizedExpression) {
    _findImplicitReturns((XSynchronizedExpression)expression, acceptor);
    return;
  } else if (expression instanceof XTryCatchFinallyExpression) {
    _findImplicitReturns((XTryCatchFinallyExpression)expression, acceptor);
    return;
  } else if (expression instanceof XTypeLiteral) {
    _findImplicitReturns((XTypeLiteral)expression, acceptor);
    return;
  } else if (expression != null) {
    _findImplicitReturns(expression, acceptor);
    return;
  } else if (expression == null) {
    _findImplicitReturns((Void)null, acceptor);
    return;
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(expression, acceptor).toString());
  }
}
 
Example #24
Source File: AbstractXbaseSemanticSequencer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Deprecated
protected void sequence_XTryCatchFinallyExpression(EObject context, XTryCatchFinallyExpression semanticObject) {
	sequence_XTryCatchFinallyExpression(createContext(context, semanticObject), semanticObject);
}
 
Example #25
Source File: XtendImplicitReturnFinder.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
public void findImplicitReturns(final XExpression expression, final ImplicitReturnFinder.Acceptor acceptor) {
  if (expression instanceof AnonymousClass) {
    _findImplicitReturns((AnonymousClass)expression, acceptor);
    return;
  } else if (expression instanceof RichString) {
    _findImplicitReturns((RichString)expression, acceptor);
    return;
  } else if (expression instanceof XAbstractFeatureCall) {
    _findImplicitReturns((XAbstractFeatureCall)expression, acceptor);
    return;
  } else if (expression instanceof XBlockExpression) {
    _findImplicitReturns((XBlockExpression)expression, acceptor);
    return;
  } else if (expression instanceof XBooleanLiteral) {
    _findImplicitReturns((XBooleanLiteral)expression, acceptor);
    return;
  } else if (expression instanceof XCastedExpression) {
    _findImplicitReturns((XCastedExpression)expression, acceptor);
    return;
  } else if (expression instanceof XClosure) {
    _findImplicitReturns((XClosure)expression, acceptor);
    return;
  } else if (expression instanceof XCollectionLiteral) {
    _findImplicitReturns((XCollectionLiteral)expression, acceptor);
    return;
  } else if (expression instanceof XConstructorCall) {
    _findImplicitReturns((XConstructorCall)expression, acceptor);
    return;
  } else if (expression instanceof XIfExpression) {
    _findImplicitReturns((XIfExpression)expression, acceptor);
    return;
  } else if (expression instanceof XInstanceOfExpression) {
    _findImplicitReturns((XInstanceOfExpression)expression, acceptor);
    return;
  } else if (expression instanceof XNullLiteral) {
    _findImplicitReturns((XNullLiteral)expression, acceptor);
    return;
  } else if (expression instanceof XNumberLiteral) {
    _findImplicitReturns((XNumberLiteral)expression, acceptor);
    return;
  } else if (expression instanceof XStringLiteral) {
    _findImplicitReturns((XStringLiteral)expression, acceptor);
    return;
  } else if (expression instanceof XSwitchExpression) {
    _findImplicitReturns((XSwitchExpression)expression, acceptor);
    return;
  } else if (expression instanceof XSynchronizedExpression) {
    _findImplicitReturns((XSynchronizedExpression)expression, acceptor);
    return;
  } else if (expression instanceof XTryCatchFinallyExpression) {
    _findImplicitReturns((XTryCatchFinallyExpression)expression, acceptor);
    return;
  } else if (expression instanceof XTypeLiteral) {
    _findImplicitReturns((XTypeLiteral)expression, acceptor);
    return;
  } else if (expression != null) {
    _findImplicitReturns(expression, acceptor);
    return;
  } else if (expression == null) {
    _findImplicitReturns((Void)null, acceptor);
    return;
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(expression, acceptor).toString());
  }
}
 
Example #26
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void computeTypes(XExpression expression, ITypeComputationState state) {
	if (expression instanceof XAssignment) {
		_computeTypes((XAssignment)expression, state);
	} else if (expression instanceof XAbstractFeatureCall) {
		_computeTypes((XAbstractFeatureCall)expression, state);
	} else if (expression instanceof XDoWhileExpression) {
		_computeTypes((XDoWhileExpression)expression, state);
	} else if (expression instanceof XWhileExpression) {
		_computeTypes((XWhileExpression)expression, state);
	} else if (expression instanceof XBlockExpression) {
		_computeTypes((XBlockExpression)expression, state);
	} else if (expression instanceof XBooleanLiteral) {
		_computeTypes((XBooleanLiteral)expression, state);
	} else if (expression instanceof XCastedExpression) {
		_computeTypes((XCastedExpression)expression, state);
	} else if (expression instanceof XClosure) {
		_computeTypes((XClosure)expression, state);
	} else if (expression instanceof XConstructorCall) {
		_computeTypes((XConstructorCall)expression, state);
	} else if (expression instanceof XForLoopExpression) {
		_computeTypes((XForLoopExpression)expression, state);
	} else if (expression instanceof XBasicForLoopExpression) {
		_computeTypes((XBasicForLoopExpression)expression, state);
	} else if (expression instanceof XIfExpression) {
		_computeTypes((XIfExpression)expression, state);
	} else if (expression instanceof XInstanceOfExpression) {
		_computeTypes((XInstanceOfExpression)expression, state);
	} else if (expression instanceof XNumberLiteral) {
		_computeTypes((XNumberLiteral)expression, state);
	} else if (expression instanceof XNullLiteral) {
		_computeTypes((XNullLiteral)expression, state);
	} else if (expression instanceof XReturnExpression) {
		_computeTypes((XReturnExpression)expression, state);
	} else if (expression instanceof XStringLiteral) {
		_computeTypes((XStringLiteral)expression, state);
	} else if (expression instanceof XSwitchExpression) {
		_computeTypes((XSwitchExpression)expression, state);
	} else if (expression instanceof XThrowExpression) {
		_computeTypes((XThrowExpression)expression, state);
	} else if (expression instanceof XTryCatchFinallyExpression) {
		_computeTypes((XTryCatchFinallyExpression)expression, state);
	} else if (expression instanceof XTypeLiteral) {
		_computeTypes((XTypeLiteral)expression, state);
	} else if (expression instanceof XVariableDeclaration) {
		_computeTypes((XVariableDeclaration)expression, state);
	} else if (expression instanceof XListLiteral) {
		_computeTypes((XListLiteral)expression, state);
	} else if (expression instanceof XSetLiteral) {
		_computeTypes((XSetLiteral)expression, state);
	} else if (expression instanceof XSynchronizedExpression) {
		_computeTypes((XSynchronizedExpression)expression, state);
	} else {
		throw new UnsupportedOperationException("Missing type computation for expression type: " + expression.eClass().getName() + " / " + state);
	}
}
 
Example #27
Source File: ThrownExceptionSwitch.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@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 #28
Source File: ExtendedEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * 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 #29
Source File: ExtendedEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
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 #30
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected void doInternalToJavaStatement(XExpression obj, ITreeAppendable appendable, boolean isReferenced) {
	if (obj instanceof XBlockExpression) {
		_toJavaStatement((XBlockExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XCastedExpression) {
		_toJavaStatement((XCastedExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XClosure) {
		_toJavaStatement((XClosure) obj, appendable, isReferenced);
	} else if (obj instanceof XConstructorCall) {
		_toJavaStatement((XConstructorCall) obj, appendable, isReferenced);
	} else if (obj instanceof XDoWhileExpression) {
		_toJavaStatement((XDoWhileExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XForLoopExpression) {
		_toJavaStatement((XForLoopExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XBasicForLoopExpression) {
		_toJavaStatement((XBasicForLoopExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XIfExpression) {
		_toJavaStatement((XIfExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XInstanceOfExpression) {
		_toJavaStatement((XInstanceOfExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XReturnExpression) {
		_toJavaStatement((XReturnExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XSwitchExpression) {
		_toJavaStatement((XSwitchExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XThrowExpression) {
		_toJavaStatement((XThrowExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XTryCatchFinallyExpression) {
		_toJavaStatement((XTryCatchFinallyExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XVariableDeclaration) {
		_toJavaStatement((XVariableDeclaration) obj, appendable, isReferenced);
	} else if (obj instanceof XWhileExpression) {
		_toJavaStatement((XWhileExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XListLiteral) {
		_toJavaStatement((XListLiteral) obj, appendable, isReferenced);
	} else if (obj instanceof XSetLiteral) {
		_toJavaStatement((XSetLiteral) obj, appendable, isReferenced);
	} else if (obj instanceof XSynchronizedExpression) {
		_toJavaStatement((XSynchronizedExpression) obj, appendable, isReferenced);
	} else {
		super.doInternalToJavaStatement(obj, appendable, isReferenced);
	}
}