org.eclipse.xtext.xbase.XBinaryOperation Java Examples

The following examples show how to use org.eclipse.xtext.xbase.XBinaryOperation. 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: XbaseProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean doNotProposeFeatureOfBinaryOperation(ContentAssistContext contentAssistContext,
		XBinaryOperation binaryOperation) {
	List<INode> nodesForFeature = NodeModelUtils.findNodesForFeature(binaryOperation, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE);
	if (!nodesForFeature.isEmpty()) {
		INode node = nodesForFeature.get(0);
		if (node.getOffset() < contentAssistContext.getOffset() - contentAssistContext.getPrefix().length()) {
			XExpression rightOperand = binaryOperation.getRightOperand();
			if (rightOperand == null)
				return true;
			ICompositeNode rightOperandNode = NodeModelUtils.findActualNodeFor(rightOperand);
			if (rightOperandNode != null) {
				if (rightOperandNode.getOffset() >= contentAssistContext.getOffset())
					return true;
				if (isParentOf(rightOperandNode, contentAssistContext.getLastCompleteNode()))
					return true;
			}
		}
	}
	return false;
}
 
Example #2
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkOperandTypesForTripleEquals(XBinaryOperation binaryOperation) {
	if(isTripleEqualsOperation(binaryOperation)){
		LightweightTypeReference left = getActualType(binaryOperation.getLeftOperand());
		LightweightTypeReference right = getActualType(binaryOperation.getRightOperand());
		if(left.isArray() != right.isArray()) {
			if (left.isArray()) {
				if (right.isAny() || right.isType(Object.class) || right.isType(Serializable.class) || right.isType(Cloneable.class)) {
					return;
				}
			} else {
				if (left.isAny() || left.isType(Object.class) || left.isType(Serializable.class) || left.isType(Cloneable.class)) {
					return;
				}
			}
			error("Incompatible operand types " + left.getHumanReadableName() + " and " + right.getHumanReadableName(), null, INVALID_OPERAND_TYPES);
		}
	}
}
 
Example #3
Source File: AbstractConstantExpressionsInterpreter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public Object internalEvaluate(final XExpression it, final Context ctx) {
  if (it instanceof XBinaryOperation) {
    return _internalEvaluate((XBinaryOperation)it, ctx);
  } else if (it instanceof XUnaryOperation) {
    return _internalEvaluate((XUnaryOperation)it, ctx);
  } else if (it instanceof XBooleanLiteral) {
    return _internalEvaluate((XBooleanLiteral)it, ctx);
  } else if (it instanceof XCastedExpression) {
    return _internalEvaluate((XCastedExpression)it, ctx);
  } else if (it instanceof XStringLiteral) {
    return _internalEvaluate((XStringLiteral)it, ctx);
  } else if (it instanceof XTypeLiteral) {
    return _internalEvaluate((XTypeLiteral)it, ctx);
  } else if (it instanceof XAnnotation) {
    return _internalEvaluate((XAnnotation)it, ctx);
  } else if (it != null) {
    return _internalEvaluate(it, ctx);
  } else if (it == null) {
    return _internalEvaluate((Void)null, ctx);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, ctx).toString());
  }
}
 
Example #4
Source File: FindReferencesTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testFindReferencesTypeLiteralTwice() throws Exception {
	createFile("find.references.test/src/Test." + fileExtension, "com.acme.OtherwiseUnused != com.acme.OtherwiseUnused");
	waitForBuild();
	
	XtextResourceSet set = get(XtextResourceSet.class);
	set.setClasspathURIContext(JavaCore.create(project));
	Resource resource = set.getResource(URI.createPlatformResourceURI("find.references.test/src/Test." + fileExtension, true), true);

	// obtain reference to type
	XBinaryOperation expression = (XBinaryOperation) resource.getContents().get(0);
	JvmType lookup = (JvmType) ((XAbstractFeatureCall) expression.getLeftOperand()).getFeature();

	final MockAcceptor mockAcceptor = new MockAcceptor();
	mockAcceptor.expect(expression.getLeftOperand(), lookup, XABSTRACT_FEATURE_CALL__FEATURE);
	mockAcceptor.expect(expression.getRightOperand(), lookup, XABSTRACT_FEATURE_CALL__FEATURE);
	findReferencesTester.checkFindReferences(lookup, "Java References to com.acme.OtherwiseUnused", mockAcceptor);
}
 
Example #5
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(XBinaryOperation expression, ISideEffectContext context) {
	if (isReassignmentOperator(expression)) {
		if (!isLocalExpression(expression.getLeftOperand(), context, false)) {
			return true;
		}
	} else {
		if (expression.isTypeLiteral() || expression.isPackageFragment()) {
			return false;
		}
		if (hasSideEffects(expression.getLeftOperand(), context)) {
			return true;
		}
	}
	if (hasSideEffects(expression.getRightOperand(), context)) {
		return true;
	}
	return false;
}
 
Example #6
Source File: LinkingTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testBug464264_01() throws Exception {
	XtendFile file = file(
			"import java.util.List\n" + 
			"class C {\n" + 
			"	def m(I i, List<CharSequence> list) {\n" + 
			"		i.strings += list.map[it]\n" + 
			"	}\n" + 
			"	interface I {\n" + 
			"		def List<String> getStrings()\n" + 
			"	}\n" + 
			"}");
	XtendClass c = (XtendClass) file.getXtendTypes().get(0);
	XtendFunction m = (XtendFunction) c.getMembers().get(0);
	XBlockExpression body = (XBlockExpression) m.getExpression();
	XBinaryOperation featureCall = (XBinaryOperation) body.getExpressions().get(0);
	JvmIdentifiableElement feature = featureCall.getFeature();
	assertEquals("org.eclipse.xtext.xbase.lib.CollectionExtensions.operator_add(java.util.Collection,java.lang.Iterable)", feature.getIdentifier());
	assertNull(featureCall.getImplicitReceiver());
	assertNull(featureCall.getImplicitFirstArgument());
	List<Diagnostic> errors = c.eResource().getErrors();
	assertEquals(1, errors.size());
	assertEquals("Type mismatch: cannot convert from List<CharSequence> to Iterable<? extends String>", errors.get(0).getMessage());
}
 
Example #7
Source File: FeatureLinkHelper.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public XExpression getSyntacticReceiver(XExpression expression) {
	if (expression instanceof XAbstractFeatureCall) {
		if (expression instanceof XFeatureCall) {
			return null;
		}
		if (expression instanceof XMemberFeatureCall) {
			return ((XMemberFeatureCall) expression).getMemberCallTarget();
		}
		if (expression instanceof XAssignment) {
			return ((XAssignment) expression).getAssignable();
		}
		if (expression instanceof XBinaryOperation) {
			return ((XBinaryOperation) expression).getLeftOperand();
		}
		if (expression instanceof XUnaryOperation) {
			return ((XUnaryOperation) expression).getOperand();
		}
		if (expression instanceof XPostfixOperation) {
			return ((XPostfixOperation) expression).getOperand();
		}
	}
	return null;
}
 
Example #8
Source File: XbaseNodeModelTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBinaryOperation_01() throws Exception {
	String text = "a+b+c+d";
	XBinaryOperation operation = (XBinaryOperation) expression(text);
	List<INode> nodesForFeature = NodeModelUtils.findNodesForFeature(operation, XbasePackage.Literals.XBINARY_OPERATION__LEFT_OPERAND);
	assertEquals(1, nodesForFeature.size());
	String nodeText = nodesForFeature.get(0).getText();
	assertEquals("a+b+c", nodeText);
}
 
Example #9
Source File: SARLOperationHelper.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies if the given operator is a reassignment operator.
 * A reassignment operator changes its left operand.
 *
 * @param operator the operator.
 * @return {@code true} if the operator changes its left operand.
 */
protected boolean isReassignmentOperator(XBinaryOperation operator) {
	if (operator.isReassignFirstArgument()) {
		return true;
	}
	final QualifiedName operatorName = this.operatorMapping.getOperator(
			QualifiedName.create(operator.getFeature().getSimpleName()));
	final QualifiedName compboundOperatorName = this.operatorMapping.getSimpleOperator(operatorName);
	return compboundOperatorName != null;
}
 
Example #10
Source File: XbaseParserTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testOrAndAndPrecedence() throws Exception {
	XBinaryOperation or = (XBinaryOperation) expression("foo && bar || baz");
	assertEquals(2,or.getExplicitArguments().size());
	assertEquals("||", or.getConcreteSyntaxFeatureName());
	assertEquals("baz", ((XFeatureCall)or.getExplicitArguments().get(1)).getConcreteSyntaxFeatureName());
	XBinaryOperation and = (XBinaryOperation) or.getExplicitArguments().get(0);
	assertEquals(2,and.getExplicitArguments().size());
	assertEquals("&&", and.getConcreteSyntaxFeatureName());
	assertEquals("foo", ((XFeatureCall)and.getExplicitArguments().get(0)).getConcreteSyntaxFeatureName());
	assertEquals("bar", ((XFeatureCall)and.getExplicitArguments().get(1)).getConcreteSyntaxFeatureName());
}
 
Example #11
Source File: XbaseParserTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testAssignment_RightAssociativity() throws Exception {
	XAssignment ass = (XAssignment) expression("foo = bar += baz");
	assertEquals(1,ass.getExplicitArguments().size());
	assertEquals("foo", ass.getConcreteSyntaxFeatureName());
	assertNull(ass.getAssignable());
	XBinaryOperation op = (XBinaryOperation) ass.getExplicitArguments().get(0);
	assertEquals(2,op.getExplicitArguments().size());
	assertEquals("+=", op.getConcreteSyntaxFeatureName());
	assertEquals("bar", ((XFeatureCall)op.getExplicitArguments().get(0)).getConcreteSyntaxFeatureName());
	assertEquals("baz", ((XFeatureCall)op.getExplicitArguments().get(1)).getConcreteSyntaxFeatureName());
}
 
Example #12
Source File: XbaseNodeModelTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBinaryOperation_03() throws Exception {
	String text = "a-b-c+d";
	XBinaryOperation operation = (XBinaryOperation) expression(text);
	List<INode> nodesForFeature = NodeModelUtils.findNodesForFeature(operation, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE);
	assertEquals(1, nodesForFeature.size());
	String nodeText = nodesForFeature.get(0).getText();
	assertEquals("+", nodeText);
}
 
Example #13
Source File: XbaseLocationInFileProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testAssignment_rhs_03() throws Exception {
	String text = "a = b += c";
	XBinaryOperation assignment = (XBinaryOperation) ((XAssignment) expression(text)).getValue();
	ITextRegion region = locationInFileProvider.getSignificantTextRegion(assignment, XbasePackage.Literals.XBINARY_OPERATION__RIGHT_OPERAND, 0);
	String significant = text.substring(region.getOffset(), region.getOffset() + region.getLength());
	assertEquals("c", significant);
}
 
Example #14
Source File: XbaseNodeModelTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testAssignment_rhs_03() throws Exception {
	String text = "a=b+=c";
	XBinaryOperation assignment = (XBinaryOperation) ((XAssignment) expression(text)).getValue();
	List<INode> nodesForFeature = NodeModelUtils.findNodesForFeature(assignment, XbasePackage.Literals.XBINARY_OPERATION__RIGHT_OPERAND);
	assertEquals(1, nodesForFeature.size());
	String nodeText = nodesForFeature.get(0).getText();
	assertEquals("c", nodeText);
}
 
Example #15
Source File: ParserTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testRichStringFOR_01() throws Exception {
	XtendFunction function = function("def withForLoop() '''�FOR i: 1..10��i��ENDFOR�'''");
	final RichString richString = (RichString) function.getExpression();
	final RichStringForLoop rsFor = (RichStringForLoop) richString.getExpressions().get(1);
	assertTrue(rsFor.getForExpression() instanceof XBinaryOperation);
	assertEquals("i", rsFor.getDeclaredParam().getName());
	
	RichString eachRichString = (RichString) rsFor.getEachExpression();
	assertEquals(3, eachRichString.getExpressions().size());
	XExpression variableReference = eachRichString.getExpressions().get(1);
	assertTrue(variableReference instanceof XFeatureCall);
	assertSame(rsFor.getDeclaredParam(), ((XAbstractFeatureCall) variableReference).getFeature());
}
 
Example #16
Source File: AmbiguousFeatureLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected String getSyntaxDescriptions() {
	XExpression expression = getExpression();
	if (expression instanceof XBinaryOperation) {
		return "binary operation";
	} else if (expression instanceof XUnaryOperation) {
		return "unary operation";
	} else if (expression instanceof XPostfixOperation) {
		return "postfix operation";
	} else {
		return "feature call";
	}
}
 
Example #17
Source File: JvmAnnotationReferencePrinter.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected String internalToString(Object obj) {
	if (obj instanceof XBinaryOperation) {
		return _internalToString((XBinaryOperation) obj);
	} else if (obj instanceof XFeatureCall) {
		return _internalToString((XFeatureCall) obj);
	} else if (obj instanceof XListLiteral) {
		return _internalToString((XListLiteral) obj);
	} else if (obj instanceof XMemberFeatureCall) {
		return _internalToString((XMemberFeatureCall) obj);
	} else if (obj instanceof XBooleanLiteral) {
		return _internalToString((XBooleanLiteral) obj);
	} else if (obj instanceof XNumberLiteral) {
		return _internalToString((XNumberLiteral) obj);
	} else if (obj instanceof XStringLiteral) {
		return _internalToString((XStringLiteral) obj);
	} else if (obj instanceof XTypeLiteral) {
		return _internalToString((XTypeLiteral) obj);
	} else if (obj instanceof XAnnotation) {
		return _internalToString((XAnnotation) obj);
	} else if (obj instanceof JvmAnnotationReference) {
		return _internalToString((JvmAnnotationReference) obj);
	} else if (obj instanceof JvmAnnotationValue) {
		return _internalToString((JvmAnnotationValue) obj);
	} else {
		return _internalToString(obj);
	}
}
 
Example #18
Source File: XbaseParserTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testUnaryOperation_3() throws Exception {
	XBinaryOperation call = (XBinaryOperation) expression("foo+-bar");
	assertEquals("+",call.getConcreteSyntaxFeatureName());
	assertEquals(2,call.getExplicitArguments().size());
	XUnaryOperation unary = (XUnaryOperation) call.getExplicitArguments().get(1);
	assertEquals("-", unary.getConcreteSyntaxFeatureName());
	assertEquals("bar", ((XFeatureCall)unary.getExplicitArguments().get(0)).getConcreteSyntaxFeatureName());
}
 
Example #19
Source File: XbaseParserTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testSwitch_0() throws Exception {
	XSwitchExpression se = (XSwitchExpression) expression("switch true { case 1==0 : '1' }");
	assertNull(se.getDefault());
	assertEquals(1, se.getCases().size());
	assertNotNull(se.getSwitch());
	XCasePart casePart = se.getCases().get(0);
	assertTrue(casePart.getCase() instanceof XBinaryOperation);
	assertTrue(casePart.getThen() instanceof XStringLiteral);
}
 
Example #20
Source File: AbstractXbaseLinkingTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBinaryOperation_5() throws Exception {
	XBlockExpression block = (XBlockExpression) expression("{\n" +
			" val java.util.List list = null" +
			" list += 'bar'" +
			"}", true);
	XBinaryOperation operation = (XBinaryOperation) block.getExpressions().get(1);
	assertEquals("org.eclipse.xtext.xbase.lib.CollectionExtensions.operator_add(java.util.Collection,E)", operation.getFeature().getIdentifier());
}
 
Example #21
Source File: XbaseLocationInFileProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testAssignment_rhs_04() throws Exception {
	String text = "a = b += c";
	XBinaryOperation assignment = (XBinaryOperation) ((XAssignment) expression(text)).getValue();
	ITextRegion region = locationInFileProvider.getSignificantTextRegion(assignment, XbasePackage.Literals.XBINARY_OPERATION__LEFT_OPERAND, 0);
	String significant = text.substring(region.getOffset(), region.getOffset() + region.getLength());
	assertEquals("b", significant);
}
 
Example #22
Source File: XbaseLocationInFileProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBinaryOperation_01() throws Exception {
	String text = "a + b + c + d";
	XBinaryOperation operation = (XBinaryOperation) expression(text);
	ITextRegion region = locationInFileProvider.getSignificantTextRegion(operation, XbasePackage.Literals.XBINARY_OPERATION__LEFT_OPERAND, 0);
	String significant = text.substring(region.getOffset(), region.getOffset() + region.getLength());
	assertEquals("a + b + c", significant);
}
 
Example #23
Source File: XbaseLocationInFileProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBinaryOperation_02() throws Exception {
	String text = "a + b + c + d";
	XBinaryOperation operation = (XBinaryOperation) expression(text);
	ITextRegion region = locationInFileProvider.getSignificantTextRegion(operation, XbasePackage.Literals.XBINARY_OPERATION__RIGHT_OPERAND, 0);
	String significant = text.substring(region.getOffset(), region.getOffset() + region.getLength());
	assertEquals("d", significant);
}
 
Example #24
Source File: XbaseLocationInFileProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBinaryOperation_03() throws Exception {
	String text = "a - b - c + d";
	XBinaryOperation operation = (XBinaryOperation) expression(text);
	ITextRegion region = locationInFileProvider.getSignificantTextRegion(operation, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, 0);
	String significant = text.substring(region.getOffset(), region.getOffset() + region.getLength());
	assertEquals("+", significant);
}
 
Example #25
Source File: AbstractXbaseLinkingTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBinaryOperation_11() throws Exception {
	XBlockExpression block = (XBlockExpression) expression("{\n" +
			" val java.util.List list = null" +
			" list += null as Iterable<CharSequence>" +
			"}", true);
	XBinaryOperation operation = (XBinaryOperation) block.getExpressions().get(1);
	assertEquals("org.eclipse.xtext.xbase.lib.CollectionExtensions.operator_add(java.util.Collection,java.lang.Iterable)", operation.getFeature().getIdentifier());
}
 
Example #26
Source File: AbstractXbaseLinkingTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBinaryOperation_10() throws Exception {
	XBlockExpression block = (XBlockExpression) expression("{\n" +
			" val java.util.List<CharSequence> list = null" +
			" list += null as Iterable<CharSequence>" +
			"}", true);
	XBinaryOperation operation = (XBinaryOperation) block.getExpressions().get(1);
	assertEquals("org.eclipse.xtext.xbase.lib.CollectionExtensions.operator_add(java.util.Collection,java.lang.Iterable)", operation.getFeature().getIdentifier());
}
 
Example #27
Source File: AbstractXbaseLinkingTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBinaryOperation_9() throws Exception {
	XBlockExpression block = (XBlockExpression) expression("{\n" +
			" val java.util.List<CharSequence> list = null" +
			" list += null as Iterable<? extends CharSequence>" +
			"}", true);
	XBinaryOperation operation = (XBinaryOperation) block.getExpressions().get(1);
	assertEquals("org.eclipse.xtext.xbase.lib.CollectionExtensions.operator_add(java.util.Collection,java.lang.Iterable)", operation.getFeature().getIdentifier());
}
 
Example #28
Source File: AbstractXbaseLinkingTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBinaryOperation_8() throws Exception {
	XBlockExpression block = (XBlockExpression) expression("{\n" +
			" val java.util.List<? super CharSequence> list = null" +
			" list += null as Iterable<? extends CharSequence>" +
			"}", true);
	XBinaryOperation operation = (XBinaryOperation) block.getExpressions().get(1);
	assertEquals("org.eclipse.xtext.xbase.lib.CollectionExtensions.operator_add(java.util.Collection,java.lang.Iterable)", operation.getFeature().getIdentifier());
}
 
Example #29
Source File: AbstractXbaseLinkingTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBinaryOperation_7() throws Exception {
	XBlockExpression block = (XBlockExpression) expression("{\n" +
			" val java.util.List<? super CharSequence> list = null" +
			" list += null as Iterable<CharSequence>" +
			"}", true);
	XBinaryOperation operation = (XBinaryOperation) block.getExpressions().get(1);
	assertEquals("org.eclipse.xtext.xbase.lib.CollectionExtensions.operator_add(java.util.Collection,java.lang.Iterable)", operation.getFeature().getIdentifier());
}
 
Example #30
Source File: AbstractXbaseLinkingTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBinaryOperation_6() throws Exception {
	XBlockExpression block = (XBlockExpression) expression("{\n" +
			" val java.util.List<String> list = null" +
			" list += 'bar'" +
			"}", true);
	XBinaryOperation operation = (XBinaryOperation) block.getExpressions().get(1);
	assertEquals("org.eclipse.xtext.xbase.lib.CollectionExtensions.operator_add(java.util.Collection,E)", operation.getFeature().getIdentifier());
}