org.eclipse.xtext.xbase.XBlockExpression Java Examples

The following examples show how to use org.eclipse.xtext.xbase.XBlockExpression. 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 Project: xtext-xtend   Author: eclipse   File: LinkingTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testBug345433_02() throws Exception {
	String classAsString = 
		"import static extension org.eclipse.xtext.GrammarUtil.*\n" +
		"class Foo {" +
		"	org.eclipse.xtext.Grammar grammar\n" +
		"	def function1() {\n" + 
		"		grammar.containedRuleCalls.filter(e0 | " +
		"			!e0.isAssigned() && !e0.isEObjectRuleCall()" +
		"		).map(e1 | e1.rule)\n" + 
		"	}\n" +
		"}";
	XtendClass clazz = clazz(classAsString);
	IResourceValidator validator = ((XtextResource) clazz.eResource()).getResourceServiceProvider().getResourceValidator();
	List<Issue> issues = validator.validate(clazz.eResource(), CheckMode.ALL, CancelIndicator.NullImpl);
	assertTrue("Resource contained errors : " + issues.toString(), issues.isEmpty());
	XtendFunction function = (XtendFunction) clazz.getMembers().get(1);
	XExpression body = function.getExpression();
	LightweightTypeReference bodyType = getType(body);
	assertEquals("java.lang.Iterable<org.eclipse.xtext.AbstractRule>", bodyType.getIdentifier());
	XBlockExpression block = (XBlockExpression) body;
	XMemberFeatureCall featureCall = (XMemberFeatureCall) block.getExpressions().get(0);
	XClosure closure = (XClosure) featureCall.getMemberCallArguments().get(0);
	JvmFormalParameter e1 = closure.getFormalParameters().get(0);
	assertEquals("e1", e1.getSimpleName());
	assertEquals("org.eclipse.xtext.RuleCall", getType(e1).getIdentifier());
}
 
Example #2
Source Project: xtext-xtend   Author: eclipse   File: LinkingTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testOverloadedMethods_09() throws Exception {
	XtendFile file = file(
			"import java.util.List\n" +
			"class X {\n" +
			"  def foo() {\n" +
			"    var List<? extends CharSequence> chars = null\n" +
			"    var testdata.OverloadedMethods<Object> receiver = null\n" +
			"    receiver.overloaded2(chars, chars)\n" +
			"  }\n" +
			"}");
	XtendClass clazz = (XtendClass) file.getXtendTypes().get(0);
	XtendFunction func  = (XtendFunction) clazz.getMembers().get(0);
	XMemberFeatureCall featureCall = (XMemberFeatureCall) ((XBlockExpression) func.getExpression()).getExpressions().get(2);
	JvmIdentifiableElement overloaded = featureCall.getFeature();
	assertNotNull(overloaded);
	assertFalse(overloaded.eIsProxy());
	assertEquals("testdata.OverloadedMethods.overloaded2(java.lang.Iterable,java.lang.Iterable)", overloaded.getIdentifier());
}
 
Example #3
Source Project: xtext-xtend   Author: eclipse   File: LinkingTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testOverloadedMethods_16() throws Exception {
	XtendFile file = file(
			"import java.util.Collection\n" +
			"class X {\n" +
			"  def <T> foo(Collection<? super T> collection, Iterable<T> elements) {\n" +
			"    collection.addAll(elements.head)\n" +
			"  }\n" +
			"}");
	XtendClass clazz = (XtendClass) file.getXtendTypes().get(0);
	XtendFunction func  = (XtendFunction) clazz.getMembers().get(0);
	XMemberFeatureCall featureCall = (XMemberFeatureCall) ((XBlockExpression) func.getExpression()).getExpressions().get(0);
	JvmIdentifiableElement addAll = featureCall.getFeature();
	assertNotNull(addAll);
	assertFalse(addAll.eIsProxy());
	assertEquals("org.eclipse.xtext.xbase.lib.CollectionExtensions.addAll(java.util.Collection,T[])", addAll.getIdentifier());
}
 
Example #4
Source Project: sarl   Author: sarl   File: SARLOperationHelper.java    License: 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(XBlockExpression expression, ISideEffectContext context) {
	final List<XExpression> exprs = expression.getExpressions();
	if (exprs != null && !exprs.isEmpty()) {
		context.open();
		try {
			for (final XExpression ex : exprs) {
				if (hasSideEffects(ex, context)) {
					return true;
				}
			}
		} finally {
			context.close();
		}
	}
	return false;
}
 
Example #5
Source Project: xtext-xtend   Author: eclipse   File: LinkingTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testOverloadedMethods_18() throws Exception {
	XtendFile file = file(
			"import java.util.Collection\n" +
			"class X {\n" +
			"  def <T> foo(Collection<T> collection, Iterable<? extends T> elements) {\n" +
			"    collection.addAll(elements.head)\n" +
			"  }\n" +
			"}");
	XtendClass clazz = (XtendClass) file.getXtendTypes().get(0);
	XtendFunction func  = (XtendFunction) clazz.getMembers().get(0);
	XMemberFeatureCall featureCall = (XMemberFeatureCall) ((XBlockExpression) func.getExpression()).getExpressions().get(0);
	JvmIdentifiableElement addAll = featureCall.getFeature();
	assertNotNull(addAll);
	assertFalse(addAll.eIsProxy());
	assertEquals("org.eclipse.xtext.xbase.lib.CollectionExtensions.addAll(java.util.Collection,T[])", addAll.getIdentifier());
}
 
Example #6
Source Project: xtext-extras   Author: eclipse   File: TypeConvertingCompiler.java    License: Eclipse Public License 2.0 6 votes vote down vote up
/**
 * On Java-level the any-type is represented as java.lang.Object as there is no subtype of everything (i.e. type for null).
 * So, when the values are used we need to manually cast them to whatever is expected.
 * 
 *  This method tells us whether such a cast is needed.
 */
private boolean isToBeCastedAnyType(LightweightTypeReference actualType, XExpression obj, ITreeAppendable appendable) {
	if (actualType instanceof AnyTypeReference) {
		if (getReferenceName(obj, appendable) != null)
			return true;
		else if (obj instanceof XBlockExpression) {
			XBlockExpression blockExpression = (XBlockExpression) obj;
			EList<XExpression> expressions = blockExpression.getExpressions();
			if (expressions.isEmpty())
				return false;
			if (expressions.size() > 1)
				return true;
			XExpression last = expressions.get(0);
			return isToBeCastedAnyType(actualType, last, appendable);
		}
	}
	return false;
}
 
Example #7
Source Project: xtext-xtend   Author: eclipse   File: LinkingTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testBug403580_03() throws Exception {
	XtendFile file = file(
			"abstract class C {\n" +
			"	def void m() {\n" +
			"		overloaded [ String s | s ]\n" +
			"	}\n" +
			"	def void overloaded((String)=>String s)" +
			"	def <T> void overloaded(Comparable<T> c)" + 
			"}\n"); 
	XtendClass c = (XtendClass) file.getXtendTypes().get(0);
	XtendFunction m = (XtendFunction) c.getMembers().get(0);
	XBlockExpression body = (XBlockExpression) m.getExpression();
	XFeatureCall featureCall = (XFeatureCall) body.getExpressions().get(0);
	JvmIdentifiableElement method = featureCall.getFeature();
	assertEquals("C.overloaded(org.eclipse.xtext.xbase.lib.Functions$Function1)", method.getIdentifier());
}
 
Example #8
Source Project: xtext-xtend   Author: eclipse   File: TypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testExpectationRelevantExpressionType_03() throws Exception {
	String clazzString = 
		"class C {\n" +
		"  def m() {\n" + 
		"    typeof(C).getMethod('', newArrayList)\n" +  
		"  }\n" +
		"}";
	XtendClass clazz = (XtendClass) file(clazzString, false).getXtendTypes().get(0);
	XtendFunction function = (XtendFunction) clazz.getMembers().get(0);
	XBlockExpression body = (XBlockExpression) function.getExpression();
	XMemberFeatureCall invocation = (XMemberFeatureCall) body.getExpressions().get(0);
	XFeatureCall newArrayList = (XFeatureCall) invocation.getActualArguments().get(1);
	assertEquals("newArrayList", newArrayList.getFeature().getSimpleName());
	assertEquals("Class<?>[]", getExpectedType(newArrayList).getSimpleName());
	assertEquals("ArrayList<Class<?>>", getType(newArrayList).getSimpleName());
}
 
Example #9
Source Project: xtext-xtend   Author: eclipse   File: LinkingTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testOverloadedMethods_07() throws Exception {
	XtendFile file = file(
			"import java.util.List\n" +
			"class X {\n" +
			"  def foo() {\n" +
			"    var List<String> strings = null\n" +
			"    var testdata.OverloadedMethods<String> receiver = null\n" +
			"    receiver.overloaded2(strings, strings)\n" +
			"  }\n" +
			"}");
	XtendClass clazz = (XtendClass) file.getXtendTypes().get(0);
	XtendFunction func  = (XtendFunction) clazz.getMembers().get(0);
	XMemberFeatureCall featureCall = (XMemberFeatureCall) ((XBlockExpression) func.getExpression()).getExpressions().get(2);
	JvmIdentifiableElement overloaded = featureCall.getFeature();
	assertNotNull(overloaded);
	assertFalse(overloaded.eIsProxy());
	assertEquals("testdata.OverloadedMethods.overloaded2(java.util.List,java.util.List)", overloaded.getIdentifier());
}
 
Example #10
Source Project: xtext-xtend   Author: eclipse   File: LinkingTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testOverloadedMethods_13() throws Exception {
	XtendFile file = file(
			"import java.util.Collection\n" +
			"class X {\n" +
			"  def <T> foo(Collection<? super T> collection, Iterable<T> elements) {\n" +
			"    collection.addAll(elements)\n" +
			"  }\n" +
			"}");
	XtendClass clazz = (XtendClass) file.getXtendTypes().get(0);
	XtendFunction func  = (XtendFunction) clazz.getMembers().get(0);
	XMemberFeatureCall featureCall = (XMemberFeatureCall) ((XBlockExpression) func.getExpression()).getExpressions().get(0);
	JvmIdentifiableElement addAll = featureCall.getFeature();
	assertNotNull(addAll);
	assertFalse(addAll.eIsProxy());
	assertEquals("org.eclipse.xtext.xbase.lib.CollectionExtensions.addAll(java.util.Collection,java.lang.Iterable)", addAll.getIdentifier());
}
 
Example #11
Source Project: xtext-xtend   Author: eclipse   File: LinkingTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testSugarOverTypeLiteral_02() throws Exception {
	XtendFile file = file(
			"import org.eclipse.emf.ecore.resource.Resource\n" +
			"import org.eclipse.emf.common.util.URI\n" +
			"class C {\n" +
			"	def m(Resource Resource, URI unused) {\n" +
			"		Resource.URI" +
			"	}\n" +
			"}\n"); 
	XtendClass c = (XtendClass) file.getXtendTypes().get(0);
	XtendFunction m = (XtendFunction) c.getMembers().get(0);
	XBlockExpression body = (XBlockExpression) m.getExpression();
	XMemberFeatureCall featureCall = (XMemberFeatureCall) body.getExpressions().get(0);
	JvmIdentifiableElement method = featureCall.getFeature();
	assertEquals("org.eclipse.emf.ecore.resource.Resource.getURI()", method.getIdentifier());
}
 
Example #12
Source Project: xtext-xtend   Author: eclipse   File: LinkingTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testOverloadedMethods_19() throws Exception {
	XtendFile file = file(
			"import java.util.Collection\n" +
			"class X {\n" +
			"  def <T> foo(Collection<? super T> collection, Iterable<? extends T> elements) {\n" +
			"    collection.<T>addAll(elements)\n" +
			"  }\n" +
			"}");
	XtendClass clazz = (XtendClass) file.getXtendTypes().get(0);
	XtendFunction func  = (XtendFunction) clazz.getMembers().get(0);
	XMemberFeatureCall featureCall = (XMemberFeatureCall) ((XBlockExpression) func.getExpression()).getExpressions().get(0);
	JvmIdentifiableElement addAll = featureCall.getFeature();
	assertNotNull(addAll);
	assertFalse(addAll.eIsProxy());
	assertEquals("org.eclipse.xtext.xbase.lib.CollectionExtensions.addAll(java.util.Collection,T[])", addAll.getIdentifier());
}
 
Example #13
Source Project: xtext-xtend   Author: eclipse   File: XtendSyntacticSequencer.java    License: 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 #14
Source Project: xtext-xtend   Author: eclipse   File: LinkingTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testImplicitFirstArgument_07() throws Exception {
	XtendClass clazz = clazz(
			"import static extension test.ImplicitFirstArgumentStatics.*\n" +
			"class MyXtendClass {\n" + 
			"  def testExtensionMethods(CharSequence it) {\n" + 
			"    withCharSequence\n" + 
			"  }\n" +
			"}");
	XtendFunction func= (XtendFunction) clazz.getMembers().get(0);
	
	XFeatureCall seventh = (XFeatureCall) ((XBlockExpression)func.getExpression()).getExpressions().get(0);
	JvmOperation seventhFeature = (JvmOperation) seventh.getFeature();
	assertEquals("test.ImplicitFirstArgumentStatics.withCharSequence(java.lang.CharSequence)", 
			seventhFeature.getIdentifier());
	// static methods don't have receiver but an implicit argument
	assertNotNull(seventh.getImplicitFirstArgument());
	assertNull(seventh.getImplicitReceiver());
	assertNull(seventh.getInvalidFeatureIssueCode(), seventh.getInvalidFeatureIssueCode());
	assertEquals("it", ((XAbstractFeatureCall) seventh.getImplicitFirstArgument()).getFeature().getSimpleName());
}
 
Example #15
Source Project: xtext-xtend   Author: eclipse   File: TypeProviderTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testExpectationRelevantExpressionType_06() throws Exception {
	String clazzString = 
			"class C<T> {\n" +
			"  def String m(String s, Class<? extends T>[] types) {\n" + 
			"    this.m('', newArrayList)\n" +  
			"  }\n" +
			"}";
	XtendClass clazz = (XtendClass) file(clazzString, false).getXtendTypes().get(0);
	XtendFunction function = (XtendFunction) clazz.getMembers().get(0);
	XBlockExpression body = (XBlockExpression) function.getExpression();
	XMemberFeatureCall invocation = (XMemberFeatureCall) body.getExpressions().get(0);
	XFeatureCall newArrayList = (XFeatureCall) invocation.getActualArguments().get(1);
	assertEquals("newArrayList", newArrayList.getFeature().getSimpleName());
	assertEquals("Class<? extends T>[]", getExpectedType(newArrayList).getSimpleName());
	assertEquals("ArrayList<Class<? extends T>>", getType(newArrayList).getSimpleName());
}
 
Example #16
Source Project: sarl   Author: sarl   File: SARLEcoreDocumentationSyntacticSequencer.java    License: Apache License 2.0 5 votes vote down vote up
protected void emitUnassignedTokens(EObject semanticObject, ISynTransition transition,
			INode fromNode, INode toNode) {
	super.emitUnassignedTokens(semanticObject, transition, fromNode, toNode);
	emitDocumentation(semanticObject);
	if (semanticObject instanceof XBlockExpression) {
		this.lastInnerBlock = getInnerDocumentation(semanticObject);
	}
}
 
Example #17
Source Project: xtext-xtend   Author: eclipse   File: AnonymousClassTypeTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testOverriddenMethodTypeArgumentInference() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("def foo() {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("new Iterable<String>() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("override iterator() {}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFunction function = this.function(_builder.toString());
    final JvmOperation operation = this._iXtendJvmAssociations.getDirectlyInferredOperation(function);
    final IResolvedTypes resolvedTypes = this._iBatchTypeResolver.resolveTypes(operation.eResource());
    Assert.assertEquals("Iterable<String>", resolvedTypes.getActualType(operation).toString());
    XExpression _expression = function.getExpression();
    XExpression _head = IterableExtensions.<XExpression>head(((XBlockExpression) _expression).getExpressions());
    final AnonymousClass anonymousClass = ((AnonymousClass) _head);
    XtendMember _last = IterableExtensions.<XtendMember>last(anonymousClass.getMembers());
    final JvmOperation overriding = this._iXtendJvmAssociations.getDirectlyInferredOperation(((XtendFunction) _last));
    Assert.assertEquals("Iterator<String>", resolvedTypes.getActualType(overriding).toString());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #18
Source Project: xtext-xtend   Author: eclipse   File: XtendUnsugaredHoverTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testUnsuagaredVersionForXtendFunction_3() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("package testpackage");
  _builder.newLine();
  _builder.append("class Foo {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("extension Extension");
  _builder.newLine();
  _builder.append("\t");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("def baz(Integer it){");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("val a = 42");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("bar(it, 42 + a)\t");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("}");
  _builder.newLine();
  _builder.append("\t");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final XtendFile xtendFile = this.testHelper.xtendFile(XtendUnsugaredHoverTest.FILEPATH, _builder.toString());
  IResourcesSetupUtil.waitForBuild();
  XtendMember _get = IterableExtensions.<XtendClass>head(Iterables.<XtendClass>filter(xtendFile.getXtendTypes(), XtendClass.class)).getMembers().get(1);
  final XtendFunction function = ((XtendFunction) _get);
  XExpression _expression = function.getExpression();
  final XBlockExpression block = ((XBlockExpression) _expression);
  final XExpression call = block.getExpressions().get(1);
  Assert.assertEquals("this._extension.bar(it, 42 + a)", this.serializer.computeUnsugaredExpression(call));
}
 
Example #19
Source Project: xtext-xtend   Author: eclipse   File: XtendHoverSignatureProviderTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testSignatureForXtendDefaultConstructor() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package testPackage");
    _builder.newLine();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def bar(){");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("new Foo()");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFile xtendFile = this.parseHelper.parse(_builder, this.getResourceSet());
    final XtendClass clazz = IterableExtensions.<XtendClass>head(Iterables.<XtendClass>filter(xtendFile.getXtendTypes(), XtendClass.class));
    XtendMember _get = clazz.getMembers().get(0);
    final XtendFunction xtendFunction = ((XtendFunction) _get);
    XExpression _expression = xtendFunction.getExpression();
    XExpression _get_1 = ((XBlockExpression) _expression).getExpressions().get(0);
    final XConstructorCall constructorCall = ((XConstructorCall) _get_1);
    final String signature = this.signatureProvider.getSignature(constructorCall.getConstructor());
    Assert.assertEquals("Foo()", signature);
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #20
Source Project: xtext-xtend   Author: eclipse   File: XtendCopyQualifiedNameServiceTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testJvmOperation() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class Foo {");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def foo(java.util.List<String> list) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("list.clear");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("list.add(null)");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFile xtendFile = this.parseHelper.parse(_builder, this.getResourceSet());
    final XtendTypeDeclaration xtendType = IterableExtensions.<XtendTypeDeclaration>head(xtendFile.getXtendTypes());
    XtendMember _head = IterableExtensions.<XtendMember>head(xtendType.getMembers());
    XExpression _expression = ((XtendFunction) _head).getExpression();
    final XBlockExpression expression = ((XBlockExpression) _expression);
    this.assertCopyQualifiedName(expression.getExpressions().get(0), "java.util.List.clear()");
    this.assertCopyQualifiedName(expression.getExpressions().get(1), "java.util.List.add(String)");
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #21
Source Project: sarl   Author: sarl   File: AbstractExpressionGenerator.java    License: Apache License 2.0 5 votes vote down vote up
private XExpression normalizeBlockExpression(XExpression expr) {
	if (expr instanceof XBlockExpression) {
		final XBlockExpression block = (XBlockExpression) expr;
		if (block.getExpressions().size() == 1) {
			return normalizeBlockExpression(block.getExpressions().get(0));
		}
	}
	return expr;
}
 
Example #22
Source Project: xtext-extras   Author: eclipse   File: XbaseExpectedTypeProviderTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testTypeParamInference_09() throws Exception {
	XBlockExpression block = (XBlockExpression) expression("{ var this = new testdata.ClosureClient() var Integer i = invoke1([e|null],'foo') }");
	XVariableDeclaration var = (XVariableDeclaration) block.getExpressions().get(1);
	XFeatureCall fc = (XFeatureCall) var.getRight();
	final XExpression closure = fc.getFeatureCallArguments().get(0);
	assertExpected(Functions.class.getCanonicalName()+"$Function1<java.lang.String, java.lang.Integer>", closure);
}
 
Example #23
Source Project: xtext-extras   Author: eclipse   File: AbstractXbaseLinkingTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testParameterizedInnerTypes_01() throws Exception {
	XBlockExpression block = (XBlockExpression) expression("{ val nested.ParameterizedInnerTypes<String>.Inner x = null }", true);
	XVariableDeclaration variableDecl = (XVariableDeclaration) block.getExpressions().get(0);
	JvmType type = variableDecl.getType().getType();
	assertEquals("nested.ParameterizedInnerTypes$Inner", type.getIdentifier());
	assertEquals("nested.ParameterizedInnerTypes<java.lang.String>$Inner", variableDecl.getType().getIdentifier());
}
 
Example #24
Source Project: xtext-xtend   Author: eclipse   File: LinkingTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testStaticImports_03() throws Exception {
	String fileAsText= "import static extension java.util.Collections.* class Clazz { def void method() {''.singletonList()} }";
	XtendFile file = file(fileAsText, true);
	XtendFunction function = (XtendFunction) ((XtendClass) file.getXtendTypes().get(0)).getMembers().get(0);
	XMemberFeatureCall featureCall = (XMemberFeatureCall) ((XBlockExpression)function.getExpression()).getExpressions().get(0);
	String identifier = featureCall.getFeature().getIdentifier();
	assertEquals("java.util.Collections.singletonList(T)", identifier);
}
 
Example #25
Source Project: xtext-xtend   Author: eclipse   File: XtendHoverSignatureProviderTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testSignatureForAnonymousClassLocalVarTypeTest_03() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package testPackage");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class C {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def m() {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("val r = newArrayList(new Runnable { override run() {} })");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("r");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFile xtendFile = this.parseHelper.parse(_builder, this.getResourceSet());
    final XtendClass clazz = IterableExtensions.<XtendClass>head(Iterables.<XtendClass>filter(xtendFile.getXtendTypes(), XtendClass.class));
    XtendMember _get = clazz.getMembers().get(0);
    final XtendFunction function = ((XtendFunction) _get);
    XExpression _expression = function.getExpression();
    final XBlockExpression body = ((XBlockExpression) _expression);
    XExpression _head = IterableExtensions.<XExpression>head(body.getExpressions());
    final XVariableDeclaration variable = ((XVariableDeclaration) _head);
    final String signature = this.signatureProvider.getSignature(variable);
    Assert.assertEquals("ArrayList<new Runnable(){}> r", signature);
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #26
Source Project: xtext-xtend   Author: eclipse   File: XtendUnsugaredHoverTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testUnsuagaredVersionForXtendFunction_9() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("package testpackage");
  _builder.newLine();
  _builder.append("import java.util.ArrayList");
  _builder.newLine();
  _builder.append("class Foo {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("extension Extension");
  _builder.newLine();
  _builder.append("\t");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("def baz(String it){");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("substring(0)");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("}");
  _builder.newLine();
  _builder.append("\t");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final XtendFile xtendFile = this.testHelper.xtendFile(XtendUnsugaredHoverTest.FILEPATH, _builder.toString());
  IResourcesSetupUtil.waitForBuild();
  XtendMember _get = IterableExtensions.<XtendClass>head(Iterables.<XtendClass>filter(xtendFile.getXtendTypes(), XtendClass.class)).getMembers().get(1);
  final XtendFunction function = ((XtendFunction) _get);
  XExpression _expression = function.getExpression();
  final XBlockExpression block = ((XBlockExpression) _expression);
  final XExpression call = block.getExpressions().get(0);
  Assert.assertEquals("it.substring(0)", this.serializer.computeUnsugaredExpression(call));
}
 
Example #27
Source Project: xtext-extras   Author: eclipse   File: EarlyExitValidator.java    License: Eclipse Public License 2.0 5 votes vote down vote up
private boolean markAsDeadCode(XExpression expression) {
	if (expression instanceof XBlockExpression) {
		List<XExpression> expressions = ((XBlockExpression) expression).getExpressions();
		if (markAsDeadCode(expressions)) {
			return true;
		}
	} 
	if (expression != null) {
		error("Unreachable expression.", expression, null, IssueCodes.UNREACHABLE_CODE);
		return true;
	} else {
		return false;
	}
}
 
Example #28
Source Project: xtext-xtend   Author: eclipse   File: ParserTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testFunction_6() throws Exception {
	XtendFunction func = function("override dispatch foo(String s) { foo('x')}");
	assertEquals("foo", func.getName());
	assertTrue(((XBlockExpression)func.getExpression()).getExpressions().get(0) instanceof XFeatureCall);
	assertEquals(1, func.getParameters().size());
	assertNull(func.getReturnType());
	assertTrue(func.isOverride());
	assertTrue(func.isDispatch());
	assertEquals(0, func.getTypeParameters().size());
}
 
Example #29
Source Project: xtext-xtend   Author: eclipse   File: XtendUnsugaredHoverTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testBug380361_1() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("package testpackage");
  _builder.newLine();
  _builder.append("import static java.lang.Integer.*");
  _builder.newLine();
  _builder.append("import static extension java.lang.String.*");
  _builder.newLine();
  _builder.append("class Foo {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("def void foo(String it) {");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("valueOf()");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("valueOf(it)");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("}");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final XtendFile xtendFile = this.testHelper.xtendFile(XtendUnsugaredHoverTest.FILEPATH, _builder.toString());
  IResourcesSetupUtil.waitForBuild();
  XtendMember _get = IterableExtensions.<XtendClass>head(Iterables.<XtendClass>filter(xtendFile.getXtendTypes(), XtendClass.class)).getMembers().get(0);
  final XtendFunction function = ((XtendFunction) _get);
  XExpression _expression = function.getExpression();
  final XBlockExpression block = ((XBlockExpression) _expression);
  final XExpression call = block.getExpressions().get(0);
  final XExpression call2 = block.getExpressions().get(1);
  Assert.assertEquals("String.valueOf(it)", this.serializer.computeUnsugaredExpression(call));
  Assert.assertEquals("", this.serializer.computeUnsugaredExpression(call2));
}
 
Example #30
Source Project: xtext-extras   Author: eclipse   File: ExpressionScopeTest.java    License: 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");
}