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

The following examples show how to use org.eclipse.xtext.xbase.XFeatureCall#getFeature() . 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: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkDelegateConstructorIsFirst(XFeatureCall featureCall) {
	JvmIdentifiableElement feature = featureCall.getFeature();
	if (feature != null && !feature.eIsProxy() && feature instanceof JvmConstructor) {
		JvmIdentifiableElement container = logicalContainerProvider.getNearestLogicalContainer(featureCall);
		if (container != null) {
			if (container instanceof JvmConstructor) {
				XExpression body = logicalContainerProvider.getAssociatedExpression(container);
				if (body == featureCall)
					return;
				if (body instanceof XBlockExpression) {
					List<XExpression> expressions = ((XBlockExpression) body).getExpressions();
					if (expressions.isEmpty() || expressions.get(0) != featureCall) {
						error("Constructor call must be the first expression in a constructor", null, INVALID_CONSTRUCTOR_INVOCATION);
					}
				}
			} else {
				error("Constructor call must be the first expression in a constructor", null, INVALID_CONSTRUCTOR_INVOCATION);
			}
		}
	}
}
 
Example 2
Source File: LinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testStaticNestedClass_02() throws Exception {
	XtendFile file = file(
			"class C {\n" + 
			"  static class D {\n" +
			"    def static void m(CharSequence c) { m('') }\n" +
			"  }\n" +
			"  def static void m(String s) {}\n" +
			"}");
	XtendClass c = (XtendClass) file.getXtendTypes().get(0);
	XtendClass d = (XtendClass) c.getMembers().get(0);
	XtendFunction m = (XtendFunction) d.getMembers().get(0);
	XBlockExpression body = (XBlockExpression) m.getExpression();
	XFeatureCall featureCall = (XFeatureCall) body.getExpressions().get(0);
	JvmIdentifiableElement feature = featureCall.getFeature();
	assertEquals("C.m(java.lang.String)", feature.getIdentifier());
}
 
Example 3
Source File: LinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testBug403580_06() throws Exception {
	XtendFile file = file(
			"abstract class C {\n" +
			"	def void m() {\n" +
			"		overloaded [ String s | s.length ]\n" +
			"	}\n" +
			"	def void overloaded((String)=>String s)" +
			"	def void overloaded(Object o)" +
			"}\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(java.lang.Object)", method.getIdentifier());
}
 
Example 4
Source File: LinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testBug403580_05() 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 void overloaded(Object o)" +
			"}\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 5
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 6
Source File: Bug409780Test.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testConstraintsInfluenceType() throws Exception {
	XtendFile file = file(
			"class C {\n" + 
			"	def private a(Iterable<CharSequence> it) {\n" + 
			"		map[ b ]\n" + 
			"	}\n" + 
			"	def private <T extends Appendable> T b(CharSequence 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("org.eclipse.xtext.xbase.lib.IterableExtensions.map(java.lang.Iterable,org.eclipse.xtext.xbase.lib.Functions$Function1)", method.getIdentifier());
	assertTrue(featureCall.isStatic());
	assertTrue(featureCall.isExtension());
	assertFalse(featureCall.isTypeLiteral());
	LightweightTypeReference type = getType(featureCall);
	assertEquals("java.lang.Iterable<java.lang.Appendable>", type.getIdentifier());
}
 
Example 7
Source File: Bug409780Test.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testMissingTypeArgumentInference() throws Exception {
	XtendFile file = file(
			"class C {\n" + 
			"	def private <T extends Appendable> Iterable<T> a(Iterable<CharSequence> it) {\n" + 
			"		map[ b ]\n" + 
			"	}\n" + 
			"	def private <T extends Appendable> T b(CharSequence 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("org.eclipse.xtext.xbase.lib.IterableExtensions.map(java.lang.Iterable,org.eclipse.xtext.xbase.lib.Functions$Function1)", method.getIdentifier());
	assertTrue(featureCall.isStatic());
	assertTrue(featureCall.isExtension());
	assertFalse(featureCall.isTypeLiteral());
	LightweightTypeReference type = getType(featureCall);
	assertEquals("java.lang.Iterable<T>", type.getIdentifier());
	LightweightTypeReference expectedType = getExpectedType(featureCall);
	assertEquals("java.lang.Iterable<T>", expectedType.getIdentifier());
}
 
Example 8
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 9
Source File: LinkingTest.java    From xtext-xtend with 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 10
Source File: LinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testSugarOverTypeLiteral_04() throws Exception {
	XtendFile file = file(
			"import org.eclipse.emf.ecore.EPackage\n" +
			"class C {\n" +
			"	def m(Object it) {\n" +
			"		EPackage" +
			"	}" +
			"	def void getEPackage(String s) {}\n" +
			"}\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("org.eclipse.emf.ecore.EPackage", method.getIdentifier());
}
 
Example 11
Source File: AbstractXbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean hasJvmConstructorCall(XExpression obj) {
	if (!(obj instanceof XBlockExpression)) {
		return false;
	}
	XBlockExpression blockExpression = (XBlockExpression) obj;
	EList<XExpression> expressions = blockExpression.getExpressions();
	if (expressions.isEmpty()) {
		return false;
	}
	XExpression expr = expressions.get(0);
	if (!(expr instanceof XFeatureCall)) {
		return false;
	}
	XFeatureCall featureCall = (XFeatureCall) expr;
	return featureCall.getFeature() instanceof JvmConstructor;
}
 
Example 12
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected String getSwitchLocalVariableName(XSwitchExpression expr, ITreeAppendable b) {
	JvmFormalParameter declaredParam = expr.getDeclaredParam();
	if (declaredParam != null) {
		if (b.hasName(declaredParam)) {
			return b.getName(declaredParam);
		}
		return null;
	}
	XExpression switchExpression = expr.getSwitch();
	if (b.hasName(switchExpression)) {
		return b.getName(switchExpression);
	} 
	if (switchExpression instanceof XFeatureCall) {
		XFeatureCall featureCall = (XFeatureCall) switchExpression;
		JvmIdentifiableElement feature = featureCall.getFeature();
		if (b.hasName(feature)) {
			return b.getName(feature);
		}
	}
	return null;
}
 
Example 13
Source File: LinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testDeclaredConstructor_01() throws Exception {
	XtendClass clazz = clazz(
			"class Foo { " +
			"  int i" +
			"  new(int i) { this.i = i }" +
			"}");
	assertEquals(2, clazz.getMembers().size());
	
	XtendConstructor constructor = (XtendConstructor) clazz.getMembers().get(1);
	XAssignment assignment = (XAssignment) ((XBlockExpression)constructor.getExpression()).getExpressions().get(0);
	JvmField field = (JvmField) assignment.getFeature();
	assertEquals("i", field.getSimpleName());
	XFeatureCall target = (XFeatureCall) assignment.getAssignable();
	JvmDeclaredType identifiableElement = (JvmDeclaredType) target.getFeature();
	assertEquals("Foo", identifiableElement.getSimpleName());
	XFeatureCall value = (XFeatureCall) assignment.getValue();
	JvmFormalParameter parameter = (JvmFormalParameter) value.getFeature();
	assertEquals("i", parameter.getSimpleName());
}
 
Example 14
Source File: LinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testBug403580_01() throws Exception {
	XtendFile file = file(
			"abstract class C {\n" +
			"	def void m() {\n" +
			"		overloaded [ String s | s ]\n" +
			"	}\n" +
			"	def void overloaded(Object o)" +
			"	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(java.lang.Object)", method.getIdentifier());
}
 
Example 15
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean hasConstructorCallWithThis(JvmConstructor constr) {
	XExpression associatedExpression = logicalContainerProvider.getAssociatedExpression(constr);
	if (associatedExpression == null) {
		return false;
	}
	TreeIterator<EObject> contents = associatedExpression.eAllContents();
	while (contents.hasNext()) {
		EObject next = contents.next();
		if (next instanceof XFeatureCall) {
			XFeatureCall featureCall = (XFeatureCall) next;
			if (featureCall.getFeature() instanceof JvmConstructor && featureCall.getConcreteSyntaxFeatureName().equals(IFeatureNames.THIS.toString())) {
				return true;
			}
		}
	}
	return false;
}
 
Example 16
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());
}
 
Example 17
Source File: UnicodeEscapeLinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testIdentifiersWithUnicode_01() throws Exception {
	XtendClass clazz = clazz(
			  "class A {\n"
			+ " String a\n"
			+ " def m() {\n" 
			+ "	    \\u0061\n" 
			+ "	}\n"
			+ "}");
	XFeatureCall call = (XFeatureCall) ((XBlockExpression)((XtendFunction)clazz.getMembers().get(1)).getExpression()).getExpressions().get(0);
	JvmIdentifiableElement feature = call.getFeature();
	assertFalse(feature.eIsProxy());
	assertSame(feature, associator.getJvmField((XtendField) clazz.getMembers().get(0)));
}
 
Example 18
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected String getSwitchLocalVariableSimpleName(XSwitchExpression expr) {
	IdentifiableSimpleNameProvider nameProvider = getNameProvider();
	String varName = nameProvider.getSimpleName(expr.getDeclaredParam());
	if (varName != null) {
		return varName;
	}
	XExpression expression = expr.getSwitch();
	if (!(expression instanceof XFeatureCall)) {
		return null;
	}
	XFeatureCall featureCall = (XFeatureCall) expression;
	JvmIdentifiableElement feature = featureCall.getFeature();
	return nameProvider.getSimpleName(feature);
}
 
Example 19
Source File: UnicodeEscapeLinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testIdentifiersWithUnicode_02() throws Exception {
	XtendClass clazz = clazz(
			  "class A {\n"
			+ " String \\u0061\n"
			+ " def m() {\n" 
			+ "	    a\n" 
			+ "	}\n"
			+ "}");
	XFeatureCall call = (XFeatureCall) ((XBlockExpression)((XtendFunction)clazz.getMembers().get(1)).getExpression()).getExpressions().get(0);
	JvmIdentifiableElement feature = call.getFeature();
	assertFalse(feature.eIsProxy());
	assertSame(feature, associator.getJvmField((XtendField) clazz.getMembers().get(0)));
}
 
Example 20
Source File: ExtractMethodRefactoring.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public RefactoringStatus checkInitialConditions(final IProgressMonitor pm) throws CoreException,
		OperationCanceledException {
	StatusWrapper status = statusProvider.get();
	IResolvedTypes resolvedTypes = typeResolver.resolveTypes(firstExpression, new CancelIndicator() {
		@Override
		public boolean isCanceled() {
			return pm.isCanceled();
		}
	});
	try {
		Set<String> calledExternalFeatureNames = newHashSet();
		returnType = calculateReturnType(resolvedTypes);
		if (returnType != null && !equal("void", returnType.getIdentifier()))
			returnExpression = lastExpression;
		boolean isReturnAllowed = isEndOfOriginalMethod(); 
		for (EObject element : EcoreUtil2.eAllContents(originalMethod.getExpression())) {
			if (pm.isCanceled()) {
				throw new OperationCanceledException();
			}
			boolean isLocalExpression = EcoreUtil.isAncestor(expressions, element);
			if (element instanceof XFeatureCall) {
				XFeatureCall featureCall = (XFeatureCall) element;
				JvmIdentifiableElement feature = featureCall.getFeature();
				LightweightTypeReference featureType = resolvedTypes.getActualType(featureCall);
				boolean isLocalFeature = EcoreUtil.isAncestor(expressions, feature);
				if (!isLocalFeature && isLocalExpression) {
					// call-out
					if (feature instanceof JvmFormalParameter || feature instanceof XVariableDeclaration) {
						if (!calledExternalFeatureNames.contains(feature.getSimpleName())) {
							calledExternalFeatureNames.add(feature.getSimpleName());
							ParameterInfo parameterInfo = new ParameterInfo(featureType.getIdentifier(), 
									feature.getSimpleName(), 
									parameterInfos.size());
							parameterInfos.add(parameterInfo);
							parameter2type.put(parameterInfo, featureType);
						}
						externalFeatureCalls.put(feature.getSimpleName(), featureCall);
					}
				} else if (isLocalFeature && !isLocalExpression) {
					// call-in
					if (returnExpression != null) {
						status.add(RefactoringStatus.FATAL,
								"Ambiguous return value: Multiple local variables are accessed in subsequent code.");
						break;
					}
					returnExpression = featureCall;
					returnType = featureType;
				}
			} else if(isLocalExpression) {
				if(element instanceof XReturnExpression && !isReturnAllowed) {
					status.add(RefactoringStatus.FATAL,
						"Extracting method would break control flow due to return statements.");
					break;
				} else if (element instanceof JvmTypeReference) {
					JvmType type = ((JvmTypeReference) element).getType();
					if (type instanceof JvmTypeParameter) {
						JvmOperation operation = associations.getDirectlyInferredOperation(originalMethod);
						if (operation != null) {
							List<JvmTypeParameter> typeParameters = operation.getTypeParameters();
							if (typeParameters.contains(type)) 
								neededTypeParameters.add((JvmTypeParameter) type);
						}
					}
				} else if (element instanceof JvmFormalParameter)
					localFeatureNames.add(((JvmFormalParameter) element).getName());
				else if (element instanceof XVariableDeclaration)
					localFeatureNames.add(((XVariableDeclaration) element).getIdentifier());
			}
		}
	} catch (OperationCanceledException e) {
		throw e;
	} catch (Exception exc) {
		handleException(exc, status);
	}
	return status.getRefactoringStatus();
}