Java Code Examples for org.eclipse.xtext.xbase.XFeatureCall#getImplicitReceiver()

The following examples show how to use org.eclipse.xtext.xbase.XFeatureCall#getImplicitReceiver() . 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: XbaseQuickfixProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private void addTypeCastToImplicitReceiver(XFeatureCall featureCall, IModificationContext context,
		JvmType declaringType) throws BadLocationException {
	String receiver;
	if (featureCall.getImplicitReceiver() instanceof XAbstractFeatureCall)
		receiver = ((XAbstractFeatureCall) featureCall.getImplicitReceiver()).getFeature().getSimpleName();
	else return;
	List<INode> nodes = NodeModelUtils.findNodesForFeature(featureCall,
			XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE);
	if (nodes.isEmpty())
		return;
	INode firstNode = IterableExtensions.head(nodes);
	int offset = firstNode.getOffset();
	ReplacingAppendable appendable = appendableFactory.create(context.getXtextDocument(),
			(XtextResource) featureCall.eResource(), offset, 0);
	appendable.append("(");
	appendable.append(receiver);
	appendable.append(" as ");
	appendable.append(declaringType);
	appendable.append(").");
	appendable.commitChanges();
}
 
Example 2
Source File: LinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testImplicitFirstArgument_01() throws Exception {
	XtendClass clazz = clazz(
			"import static extension test.ImplicitFirstArgumentStatics.*\n" +
			"class MyXtendClass {\n" + 
			"  def testExtensionMethods(CharSequence it) {\n" + 
			"    length\n" +
			"  }\n" +
			"  extension String" +
			"  def length() { null }\n" +
			"}");
	XtendFunction func= (XtendFunction) clazz.getMembers().get(0);
	
	XFeatureCall first = (XFeatureCall) ((XBlockExpression)func.getExpression()).getExpressions().get(0);
	JvmOperation firstFeature = (JvmOperation) first.getFeature();
	assertEquals("java.lang.CharSequence.length()", firstFeature.getIdentifier());
	assertEquals("java.lang.CharSequence", firstFeature.getDeclaringType().getQualifiedName());
	assertNull(first.getImplicitFirstArgument());
	XFeatureCall firstReceiver = (XFeatureCall) first.getImplicitReceiver();
	assertTrue(firstReceiver.getFeature() instanceof JvmFormalParameter);
	assertNull(first.getInvalidFeatureIssueCode());
}
 
Example 3
Source File: LinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testImplicitFirstArgument_02() throws Exception {
	XtendClass clazz = clazz(
			"import static extension test.ImplicitFirstArgumentStatics.*\n" +
			"class MyXtendClass {\n" + 
			"  def testExtensionMethods(CharSequence it) {\n" + 
			"    toUpperCase\n" +
			"  }\n" +
			"  extension String" +
			"  def toUpperCase() { null }\n" + 
			"}");
	XtendFunction func= (XtendFunction) clazz.getMembers().get(0);
	
	XFeatureCall second = (XFeatureCall) ((XBlockExpression)func.getExpression()).getExpressions().get(0);
	JvmOperation secondFeature = (JvmOperation) second.getFeature();
	assertEquals("MyXtendClass.toUpperCase()", secondFeature.getIdentifier());
	assertEquals(0, secondFeature.getParameters().size());
	assertNull(second.getImplicitFirstArgument());
	XFeatureCall secondReceiver = (XFeatureCall) second.getImplicitReceiver();
	assertTrue(secondReceiver.getFeature() instanceof JvmGenericType);
	assertNull(second.getInvalidFeatureIssueCode());
}
 
Example 4
Source File: LinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testImplicitFirstArgument_03() throws Exception {
	XtendClass clazz = clazz(
			"import static extension test.ImplicitFirstArgumentStatics.*\n" +
			"class MyXtendClass {\n" + 
			"  def testExtensionMethods(CharSequence it) {\n" + 
			"    toLowerCase\n" + 
			"  }\n" +
			"  extension String" +
			"}");
	XtendFunction func= (XtendFunction) clazz.getMembers().get(0);
	
	XFeatureCall third = (XFeatureCall) ((XBlockExpression)func.getExpression()).getExpressions().get(0);
	JvmOperation thirdFeature = (JvmOperation) third.getFeature();
	assertEquals("java.lang.String.toLowerCase()", thirdFeature.getIdentifier());
	assertNull(third.getImplicitFirstArgument());
	XMemberFeatureCall thirdReceiver = (XMemberFeatureCall) third.getImplicitReceiver();
	assertTrue(thirdReceiver.getFeature() instanceof JvmField);
	assertNull(third.getInvalidFeatureIssueCode(), third.getInvalidFeatureIssueCode());
}
 
Example 5
Source File: LinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testImplicitFirstArgument_05() throws Exception {
	XtendClass clazz = clazz(
			"import static extension test.ImplicitFirstArgumentStatics.*\n" +
			"class MyXtendClass {\n" + 
			"  def testExtensionMethods(CharSequence it) {\n" + 
			"    withObject\n" + 
			"  }\n" +
			"  extension String" +
			"  def withObject(Object obj) { null }" + 
			"}");
	XtendFunction func= (XtendFunction) clazz.getMembers().get(0);
	
	XFeatureCall fifth = (XFeatureCall) ((XBlockExpression)func.getExpression()).getExpressions().get(0);
	JvmOperation fifthFeature = (JvmOperation) fifth.getFeature();
	assertEquals("MyXtendClass.withObject(java.lang.Object)", fifthFeature.getIdentifier());
	assertEquals(1, fifthFeature.getParameters().size());
	assertNotNull(fifth.getImplicitFirstArgument());
	assertEquals("it", ((XAbstractFeatureCall) fifth.getImplicitFirstArgument()).getFeature().getSimpleName());
	XFeatureCall fifthReceiver = (XFeatureCall) fifth.getImplicitReceiver();
	assertTrue(fifthReceiver.getFeature() instanceof JvmGenericType);
	assertNull(fifth.getInvalidFeatureIssueCode(), fifth.getInvalidFeatureIssueCode());
}
 
Example 6
Source File: LinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testImplicitFirstArgument_06() throws Exception {
	XtendClass clazz = clazz(
			"import static extension test.ImplicitFirstArgumentStatics.*\n" +
			"class MyXtendClass {\n" + 
			"  def testExtensionMethods(CharSequence it) {\n" + 
			"    contains\n" + 
			"  }\n" +
			"  extension String" +
			"}");
	XtendFunction func= (XtendFunction) clazz.getMembers().get(0);
	
	XFeatureCall sixth = (XFeatureCall) ((XBlockExpression)func.getExpression()).getExpressions().get(0);
	JvmOperation sixthFeature = (JvmOperation) sixth.getFeature();
	assertEquals("java.lang.String.contains(java.lang.CharSequence)", sixthFeature.getIdentifier());
	assertEquals("java.lang.String", sixthFeature.getDeclaringType().getQualifiedName());
	assertNotNull(sixth.getImplicitFirstArgument());
	assertEquals("it", ((XAbstractFeatureCall) sixth.getImplicitFirstArgument()).getFeature().getSimpleName());
	XMemberFeatureCall sixthReceiver = (XMemberFeatureCall) sixth.getImplicitReceiver();
	assertTrue(sixthReceiver.getFeature() instanceof JvmField);
	assertNull(sixth.getInvalidFeatureIssueCode(), sixth.getInvalidFeatureIssueCode());
}
 
Example 7
Source File: BeforeLinkingTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testImplicitReceiver() throws Exception {
	XBlockExpression block = (XBlockExpression) parseHelper.parse("{ val it = \'\' toString }");
	BatchLinkableResource resource = (BatchLinkableResource) block.eResource();
	XFeatureCall toString = (XFeatureCall) Iterables.getLast(block.getExpressions(), null);
	XFeatureCall implicitReceiver = (XFeatureCall) toString.getImplicitReceiver();
	Assert.assertEquals("it", implicitReceiver.getFeature().getSimpleName());
	resource.update(0, 0, "");
	Assert.assertNull(reflectExtensions.get(toString, "implicitReceiver"));
	Assert.assertNotNull(toString.getImplicitReceiver());
}
 
Example 8
Source File: AbstractExpectationTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testImplicitReceiver() {
  ExpectationTestingTypeComputer _typeComputer = this.getTypeComputer();
  final Function1<XExpression, Boolean> _function = (XExpression it) -> {
    return Boolean.valueOf(false);
  };
  _typeComputer.setPredicate(_function);
  this.expects("{val it = 1L intValue}");
  final XFeatureCall intValue = IteratorExtensions.<XFeatureCall>head(Iterators.<XFeatureCall>filter(this.rootExpression.eAllContents(), XFeatureCall.class));
  XExpression _implicitReceiver = intValue.getImplicitReceiver();
  this.expressions.add(_implicitReceiver);
  this.queriedAs("Long");
}
 
Example 9
Source File: LinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testImplicitFirstArgument_00() throws Exception {
	XtendClass clazz = clazz(
			"class MyXtendClass {\n" + 
			"  def prependHello(String myString) {\n" + 
			"    'Hello '+myString\n" + 
			"  }\n" + 
			"  def testExtensionMethods(String it) {\n" + 
			"    it.prependHello\n" + 
			"    prependHello\n" + 
			"  }\n" + 
			"}");
	XtendFunction func= (XtendFunction) clazz.getMembers().get(1);
	XMemberFeatureCall first = (XMemberFeatureCall) ((XBlockExpression)func.getExpression()).getExpressions().get(0);
	JvmOperation firstFeature = (JvmOperation) first.getFeature();
	assertEquals("prependHello", firstFeature.getSimpleName());
	assertNull(first.getInvalidFeatureIssueCode(), first.getInvalidFeatureIssueCode());
	XFeatureCall firstReceiver = (XFeatureCall) first.getImplicitReceiver();
	assertEquals("MyXtendClass", firstReceiver.getFeature().getSimpleName());
	XFeatureCall second = (XFeatureCall) ((XBlockExpression)func.getExpression()).getExpressions().get(1);
	JvmOperation secondFeature = (JvmOperation) second.getFeature();
	assertEquals("prependHello", secondFeature.getSimpleName());
	assertNull(second.getInvalidFeatureIssueCode(), second.getInvalidFeatureIssueCode());
	assertFalse(secondFeature.eIsProxy());
	XFeatureCall secondReceiver = (XFeatureCall) second.getImplicitReceiver();
	assertEquals("MyXtendClass", secondReceiver.getFeature().getSimpleName());
	XFeatureCall implicitArgument = (XFeatureCall) second.getImplicitFirstArgument();
	assertNotNull(implicitArgument);
	assertEquals("it", implicitArgument.getFeature().getSimpleName());
}