Java Code Examples for org.eclipse.xtext.xbase.XIfExpression#getThen()

The following examples show how to use org.eclipse.xtext.xbase.XIfExpression#getThen() . 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: ExpressionScopeTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testReassignedType_01() throws Exception {
	XIfExpression ifExpr = (XIfExpression) IterableExtensions
			.last(((XBlockExpression) expression("{ var it = new Object() if (it instanceof String) {} }", false)).getExpressions());
	XBlockExpression block = (XBlockExpression) ifExpr.getThen();
	IExpressionScope expressionScope = batchTypeResolver.resolveTypes(block).getExpressionScope(block, IExpressionScope.Anchor.BEFORE);
	contains(expressionScope, "charAt");
	contains(expressionScope, "it");
	contains(expressionScope, "operator_lessThan");
}
 
Example 2
Source File: ExpressionScopeTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testReassignedType_02() throws Exception {
	XIfExpression ifExpr = (XIfExpression) IterableExtensions
			.last(((XBlockExpression) expression("{ var it = new Object() if (it instanceof String) { it = new Object() } }", false))
					.getExpressions());
	XBlockExpression block = (XBlockExpression) ifExpr.getThen();
	IExpressionScope expressionScope = batchTypeResolver.resolveTypes(block).getExpressionScope(block, IExpressionScope.Anchor.BEFORE);
	contains(expressionScope, "charAt");
	contains(expressionScope, "it");
	contains(expressionScope, "operator_lessThan");
}
 
Example 3
Source File: ExpressionScopeTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testReassignedType_03() throws Exception {
	XIfExpression ifExpr = (XIfExpression) IterableExtensions
			.last(((XBlockExpression) expression("{ var it = new Object() if (it instanceof String) { it = new Object() } }", false))
					.getExpressions());
	XBlockExpression block = (XBlockExpression) ifExpr.getThen();
	XExpression assignment = Iterables.getFirst(block.getExpressions(), null);
	IExpressionScope expressionScope = batchTypeResolver.resolveTypes(assignment).getExpressionScope(assignment,
			IExpressionScope.Anchor.AFTER);
	containsNot(expressionScope, "charAt");
	contains(expressionScope, "it");
	containsNot(expressionScope, "operator_lessThan");
}
 
Example 4
Source File: XbaseParserTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testIf_3() throws Exception {
	XIfExpression ie = (XIfExpression) expression("if (foo) bar else if (baz) if (apa) bpa else cpa");
	XIfExpression nestedIf = (XIfExpression) ie.getElse();
	XIfExpression secondNested = (XIfExpression) nestedIf.getThen();
	XFeatureCall cpa = (XFeatureCall) secondNested.getElse();
	assertEquals("cpa",cpa.getConcreteSyntaxFeatureName());
}
 
Example 5
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Generate the given object.
 *
 * @param ifStatement the if-then-else statement.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the statement.
 */
protected XExpression _generate(XIfExpression ifStatement, IAppendable it, IExtraLanguageGeneratorContext context) {
	it.append("if "); //$NON-NLS-1$
	generate(ifStatement.getIf(), it, context);
	it.append(":"); //$NON-NLS-1$
	it.increaseIndentation().newLine();
	if (ifStatement.getThen() != null) {
		generate(ifStatement.getThen(), context.getExpectedExpressionType(), it, context);
	} else if (context.getExpectedExpressionType() == null) {
		it.append("pass"); //$NON-NLS-1$
	} else {
		it.append("return ").append(toDefaultValue(context.getExpectedExpressionType().toJavaCompliantTypeReference())); //$NON-NLS-1$
	}
	it.decreaseIndentation();
	if (ifStatement.getElse() != null) {
		it.newLine().append("else:"); //$NON-NLS-1$
		it.increaseIndentation().newLine();
		generate(ifStatement.getElse(), context.getExpectedExpressionType(), it, context);
		it.decreaseIndentation();
	} else if (context.getExpectedExpressionType() != null) {
		it.newLine().append("else:"); //$NON-NLS-1$
		it.increaseIndentation().newLine();
		it.append("return ").append(toDefaultValue(context.getExpectedExpressionType().toJavaCompliantTypeReference())); //$NON-NLS-1$
		it.decreaseIndentation();
	}
	return ifStatement;
}
 
Example 6
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected boolean isVariableDeclarationRequired(XExpression expr, ITreeAppendable b, boolean recursive) {
	if (expr instanceof XAnnotation) {
		return false;
	}
	if (expr instanceof XListLiteral) {
		return false;
	}
	if (expr instanceof XSetLiteral) {
		return false;
	}
	if (expr instanceof XCastedExpression) {
		return false;
	}
	if (expr instanceof XInstanceOfExpression) {
		return false;
	}
	if (expr instanceof XMemberFeatureCall && isVariableDeclarationRequired((XMemberFeatureCall) expr, b))
		return true;
	EObject container = expr.eContainer();
	if ((container instanceof XVariableDeclaration)
		|| (container instanceof XReturnExpression) 
		|| (container instanceof XThrowExpression)) {
		return false;
	}
	if (container instanceof XIfExpression) {
		XIfExpression ifExpression = (XIfExpression) container;
		if (ifExpression.getThen() == expr || ifExpression.getElse() == expr) {
			return false;
		}
	}
	if (container instanceof XCasePart) {
		XCasePart casePart = (XCasePart) container;
		if (casePart.getThen() == expr) {
			return false;
		}
	}
	if (container instanceof XSwitchExpression) {
		XSwitchExpression switchExpression = (XSwitchExpression) container;
		if (switchExpression.getDefault() == expr) {
			return false;
		}
	}
	if (container instanceof XBlockExpression) {
		List<XExpression> siblings = ((XBlockExpression) container).getExpressions();
		if (siblings.get(siblings.size() - 1) == expr) {
			return isVariableDeclarationRequired(getFeatureCall(expr), expr, b);
		}
	}
	if (container instanceof XClosure) {
		if (((XClosure) container).getExpression() == expr) {
			return false;
		}
	}
	if (expr instanceof XAssignment) {
		XAssignment a = (XAssignment) expr;
		for (XExpression arg : getActualArguments(a)) {
			if (isVariableDeclarationRequired(arg, b, recursive)) {
				return true;
			}
		}
	}
	return super.isVariableDeclarationRequired(expr, b, recursive);
}
 
Example 7
Source File: XbaseShufflingInjectorProvider.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected XExpression getElse(XIfExpression ifExpression) {
	if (ifExpression.getElse() != null)
		return ifExpression.getThen();
	return super.getElse(ifExpression);
}
 
Example 8
Source File: XtendHoverSignatureProviderTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testAutcastExpressions() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package testPackage");
    _builder.newLine();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def foo() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val CharSequence c = \"\"");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (c instanceof String) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("c.substring(1, 1)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("switch(c){");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("String : c.length");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFile xtendFile = this.parseHelper.parse(_builder, this.getResourceSet());
    XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(xtendFile.getXtendTypes());
    XtendMember _head_1 = IterableExtensions.<XtendMember>head(((XtendClass) _head).getMembers());
    final XtendFunction func = ((XtendFunction) _head_1);
    XExpression _expression = func.getExpression();
    final XBlockExpression block = ((XBlockExpression) _expression);
    final XExpression dec = IterableExtensions.<XExpression>head(block.getExpressions());
    Assert.assertEquals("CharSequence c", this.signatureProvider.getSignature(dec));
    XExpression _get = block.getExpressions().get(1);
    final XIfExpression ifexpr = ((XIfExpression) _get);
    final XExpression then = ifexpr.getThen();
    XExpression _head_2 = IterableExtensions.<XExpression>head(((XBlockExpression) then).getExpressions());
    final XExpression target = ((XMemberFeatureCall) _head_2).getMemberCallTarget();
    Assert.assertEquals("String c", this.signatureProvider.getSignature(target));
    XExpression _get_1 = block.getExpressions().get(2);
    final XSwitchExpression switchExpr = ((XSwitchExpression) _get_1);
    XExpression _then = IterableExtensions.<XCasePart>head(switchExpr.getCases()).getThen();
    final XExpression expr = ((XMemberFeatureCall) _then).getMemberCallTarget();
    Assert.assertEquals("String c", this.signatureProvider.getSignature(expr));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 9
Source File: XtendHoverSignatureProviderTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testAutcastExpressions_2() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package testPackage");
    _builder.newLine();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("CharSequence c = \"\"");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def foo() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (c instanceof String) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("c.substring(1, 1)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("switch(c){");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("String : c.length");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFile xtendFile = this.parseHelper.parse(_builder, this.getResourceSet());
    XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(xtendFile.getXtendTypes());
    Assert.assertEquals("CharSequence c", this.signatureProvider.getSignature(IterableExtensions.<XtendMember>head(((XtendClass) _head).getMembers())));
    XtendTypeDeclaration _head_1 = IterableExtensions.<XtendTypeDeclaration>head(xtendFile.getXtendTypes());
    XtendMember _get = ((XtendClass) _head_1).getMembers().get(1);
    final XtendFunction func = ((XtendFunction) _get);
    XExpression _expression = func.getExpression();
    final XBlockExpression block = ((XBlockExpression) _expression);
    XExpression _head_2 = IterableExtensions.<XExpression>head(block.getExpressions());
    final XIfExpression ifexpr = ((XIfExpression) _head_2);
    final XExpression then = ifexpr.getThen();
    XExpression _head_3 = IterableExtensions.<XExpression>head(((XBlockExpression) then).getExpressions());
    final XExpression target = ((XMemberFeatureCall) _head_3).getMemberCallTarget();
    Assert.assertEquals("String Foo.c", this.signatureProvider.getSignature(target));
    XExpression _get_1 = block.getExpressions().get(1);
    final XSwitchExpression switchExpr = ((XSwitchExpression) _get_1);
    XExpression _then = IterableExtensions.<XCasePart>head(switchExpr.getCases()).getThen();
    final XExpression expr = ((XMemberFeatureCall) _then).getMemberCallTarget();
    Assert.assertEquals("String Foo.c", this.signatureProvider.getSignature(expr));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 10
Source File: XtendHoverSignatureProviderTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testAutcastExpressions_3() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package testPackage");
    _builder.newLine();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def foo(CharSequence c) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("if (c instanceof String) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("c.substring(1, 1)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("switch(c){");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("String : c.length");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFile xtendFile = this.parseHelper.parse(_builder, this.getResourceSet());
    XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(xtendFile.getXtendTypes());
    XtendMember _head_1 = IterableExtensions.<XtendMember>head(((XtendClass) _head).getMembers());
    final XtendFunction func = ((XtendFunction) _head_1);
    Assert.assertEquals("CharSequence c - foo(CharSequence)", this.signatureProvider.getSignature(IterableExtensions.<XtendParameter>head(func.getParameters())));
    XExpression _expression = func.getExpression();
    final XBlockExpression block = ((XBlockExpression) _expression);
    XExpression _head_2 = IterableExtensions.<XExpression>head(block.getExpressions());
    final XIfExpression ifexpr = ((XIfExpression) _head_2);
    final XExpression then = ifexpr.getThen();
    XExpression _head_3 = IterableExtensions.<XExpression>head(((XBlockExpression) then).getExpressions());
    final XExpression target = ((XMemberFeatureCall) _head_3).getMemberCallTarget();
    Assert.assertEquals("String c", this.signatureProvider.getSignature(target));
    XExpression _get = block.getExpressions().get(1);
    final XSwitchExpression switchExpr = ((XSwitchExpression) _get);
    XExpression _then = IterableExtensions.<XCasePart>head(switchExpr.getCases()).getThen();
    final XExpression expr = ((XMemberFeatureCall) _then).getMemberCallTarget();
    Assert.assertEquals("String c", this.signatureProvider.getSignature(expr));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 11
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Only for testing purpose.
 * @nooverride This method is not intended to be re-implemented or extended by clients.
 * @noreference This method is not intended to be referenced by clients.
 */
/* @Nullable */
protected XExpression getThen(XIfExpression ifExpression) {
	return ifExpression.getThen();
}