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

The following examples show how to use org.eclipse.xtext.xbase.XListLiteral#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
protected void computeArrayLiteralType(XListLiteral literal, LightweightTypeReference expectedArrayType, ITypeExpectation expectation,
		ITypeComputationState state) {
	LightweightTypeReference elementTypeExpectation = getElementOrComponentType(expectedArrayType, state);
	int allFlags = 0;
	for(XExpression element: literal.getElements()) {
		ITypeComputationResult elementTypeResult = computeTypes(element, elementTypeExpectation, state);
		deferredBindTypeArgument(elementTypeExpectation, elementTypeResult.getActualExpressionType(), state);
		allFlags |= elementTypeResult.getCheckedConformanceFlags();
	}
	if ((allFlags & ConformanceFlags.INCOMPATIBLE) != 0) {
		allFlags &= (~ConformanceFlags.SUCCESS);
		allFlags |= ConformanceFlags.SEALED | ConformanceFlags.CHECKED | ConformanceFlags.PROPAGATED_TYPE;
		expectation.acceptActualType(expectedArrayType, allFlags);
	} else if ((allFlags & ConformanceFlags.SUCCESS) != 0) {
		allFlags |= ConformanceFlags.SEALED | ConformanceFlags.CHECKED;
		expectation.acceptActualType(expectedArrayType, allFlags);
	} else {
		expectation.acceptActualType(expectedArrayType, ConformanceFlags.CHECKED_SUCCESS | ConformanceFlags.SEALED);
	}
}
 
Example 2
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected Object _doEvaluate(XListLiteral literal, IEvaluationContext context, CancelIndicator indicator) {
	IResolvedTypes resolveTypes = typeResolver.resolveTypes(literal);
	LightweightTypeReference type = resolveTypes.getActualType(literal);
	List<Object> list = newArrayList();
	for(XExpression element: literal.getElements()) {
		if (indicator.isCanceled())
			throw new InterpreterCanceledException();
		list.add(internalEvaluate(element, context, indicator));
	}
	if(type != null && type.isArray()) {
		try {
			LightweightTypeReference componentType = type.getComponentType();
			return Conversions.unwrapArray(list, getJavaType(componentType.getType()));
		} catch (ClassNotFoundException e) {
		}
	}
	return Collections.unmodifiableList(list);
}
 
Example 3
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Generate the given object.
 *
 * @param literal the list literal.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the literal.
 */
protected XExpression _generate(XListLiteral literal, IAppendable it, IExtraLanguageGeneratorContext context) {
	appendReturnIfExpectedReturnedExpression(it, context);
	it.append("["); //$NON-NLS-1$
	boolean first = true;
	for (final XExpression value : literal.getElements()) {
		if (first) {
			first = false;
		} else {
			it.append(", "); //$NON-NLS-1$
		}
		generate(value, it, context);
	}
	it.append("]"); //$NON-NLS-1$
	return literal;
}
 
Example 4
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean canUseArrayInitializerImpl(XListLiteral literal, ITreeAppendable appendable) {
	for(XExpression element: literal.getElements()) {
		if (isVariableDeclarationRequired(element, appendable, true))
			return false;
	}
	return true;
}
 
Example 5
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @param isReferenced unused in this context but necessary for dispatch signature 
 */
protected void _toJavaStatement(XListLiteral literal, ITreeAppendable b, boolean isReferenced) {
	for(XExpression element: literal.getElements()) 
		internalToJavaStatement(element, b, true);
}
 
Example 6
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void _toJavaExpression(XListLiteral literal, ITreeAppendable b) {
	LightweightTypeReference literalType = batchTypeResolver.resolveTypes(literal).getActualType(literal);
	if (literalType == null) {
		b.append("error - couldn't compute type for literal : "+literal);
		return;
	} 
	if (literalType.isArray()) {
		LightweightTypeReference expectedType = batchTypeResolver.resolveTypes(literal).getExpectedType(literal);
		boolean skipTypeName = false;
		if (expectedType != null && expectedType.isArray()) {
			if (canUseArrayInitializer(literal, b)) {
				skipTypeName = true;
			}
		}
		if (!skipTypeName) {
			if (literalType instanceof CompoundTypeReference) {
				for (LightweightTypeReference c : literalType.getMultiTypeComponents()) {
					if (c.isArray()) {
						b.append("new ")
						.append(c.getType()) // append raw type since we cannot create generic arrays
						.append(" ");
						break;
					}
				}
			} else {
				b.append("new ")
					.append(literalType.getType()) // append raw type since we cannot create generic arrays
					.append(" ");
			}
		}
		if (literal.getElements().isEmpty()) {
			b.append("{}");
		} else {
			b.append("{ ");
			boolean isFirst = true;
			for(XExpression element: literal.getElements())  {
				if(!isFirst)
					b.append(", ");
				isFirst = false;
				internalToJavaExpression(element, b);
			}
			b.append(" }");
		}
		return;
	} else {
		appendImmutableCollectionExpression(literal, b, "unmodifiableList", CollectionLiterals.class, "newArrayList");
	}
}