Java Code Examples for org.eclipse.xtext.xbase.XCollectionLiteral#getElements()

The following examples show how to use org.eclipse.xtext.xbase.XCollectionLiteral#getElements() . 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: CollectionLiteralsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates a list of collection type references from the element types of a collection literal.
 */
protected List<LightweightTypeReference> computeCollectionTypeCandidates(XCollectionLiteral literal, JvmGenericType collectionType, LightweightTypeReference elementTypeExpectation, ITypeComputationState state) {
	List<XExpression> elements = literal.getElements();
	if(!elements.isEmpty()) {
		List<LightweightTypeReference> elementTypes = Lists.newArrayListWithCapacity(elements.size());
		for(XExpression element: elements) {
			ITypeComputationResult elementType = computeTypes(element, elementTypeExpectation, state);
			LightweightTypeReference actualType = elementType.getActualExpressionType();
			if(actualType != null && !actualType.isAny()) {
				ParameterizedTypeReference collectionTypeCandidate = state.getReferenceOwner().newParameterizedTypeReference(collectionType);
				collectionTypeCandidate.addTypeArgument(actualType.getWrapperTypeIfPrimitive());
				elementTypes.add(collectionTypeCandidate);
			}
		}
		return elementTypes;
	}
	return Collections.emptyList();
}
 
Example 2
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void appendImmutableCollectionExpression(XCollectionLiteral literal,
		ITreeAppendable b, String collectionsMethod, Class<?> guavaHelper, String guavaHelperMethod) {
	LightweightTypeReference collectionElementType = getCollectionElementType(literal);
	b.append(Collections.class);
	b.append(".<").append(collectionElementType).append(">").append(collectionsMethod).append("(");
	b.append(guavaHelper).append(".<").append(collectionElementType).append(">").append(guavaHelperMethod).append("(");
	boolean isFirst = true;
	for(XExpression element: literal.getElements())  {
		if(!isFirst)
			b.append(", ");
		isFirst = false;
		if(element instanceof XNullLiteral) {
			b.append("(").append(collectionElementType).append(")");
		}
		internalToJavaExpression(element, b);
	}
	b.append("))");
	return;
}
 
Example 3
Source File: CollectionLiteralsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Process all children and assign an unknown type to the literal. 
 */
protected void handleCollectionTypeNotAvailable(XCollectionLiteral literal, ITypeComputationState state, Class<?> clazz) {
	for(XExpression element: literal.getElements()) {
		state.withNonVoidExpectation().computeTypes(element);
	}
	state.acceptActualType(state.getReferenceOwner().newUnknownTypeReference(clazz.getName()));
}
 
Example 4
Source File: ShouldExtensions.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Ensure that the given type literal is equal to the given list.
 *
 * @param actual the type literal to test.
 * @param expected the name of the expected type.
 * @return the validation status
 */
public static boolean shouldBe(XCollectionLiteral actual, Object expected) {
	if (actual == null || actual.getElements() == null) {
		return false;
	}
	return shouldIterate(
			actual.getElements().iterator(),
			expected,
			!(actual instanceof XSetLiteral));
}
 
Example 5
Source File: SARLOperationHelper.java    From sarl with Apache License 2.0 5 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(XCollectionLiteral expression, ISideEffectContext context) {
	context.open();
	try {
		for (final XExpression ex : expression.getElements()) {
			if (hasSideEffects(ex, context)) {
				return true;
			}
		}
	} finally {
		context.close();
	}
	return false;
}
 
Example 6
Source File: CollectionLiteralsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void refineElementTypeExpectation(XCollectionLiteral literal, LightweightTypeReference expectation, ITypeComputationState state) {
	for (XExpression element : literal.getElements()) {
		state.refineExpectedType(element, expectation);
	}
}