Java Code Examples for org.eclipse.xtext.xbase.compiler.output.ITreeAppendable#append()

The following examples show how to use org.eclipse.xtext.xbase.compiler.output.ITreeAppendable#append() . 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: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected String declareLocalVariable(XSwitchExpression expr, ITreeAppendable b) {
	// declare local var for the switch expression
	String variableName = getSwitchLocalVariableName(expr, b); 
	if (variableName != null) {
		return variableName;
	}
	String name = createSwitchLocalVariableName(expr);
	JvmTypeReference variableType = getSwitchLocalVariableType(expr);
	b.newLine().append("final ");
	serialize(variableType, expr, b);
	b.append(" ");
	variableName = declareAndAppendSwitchSyntheticLocalVariable(expr, name, b);
	b.append(" = ");
	internalToJavaExpression(expr.getSwitch(), b);
	b.append(";");
	return variableName;
}
 
Example 2
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected ITreeAppendable appendTypeArguments(XAbstractFeatureCall call, ITreeAppendable original) {
	if (!call.getTypeArguments().isEmpty()) {
		return super.appendTypeArguments(call, original);
	}
	ILocationData completeLocationData = getLocationWithTypeArguments(call);
	ITreeAppendable completeFeatureCallAppendable = completeLocationData != null ? original.trace(completeLocationData) : original;
	IResolvedTypes resolvedTypes = batchTypeResolver.resolveTypes(call);
	List<LightweightTypeReference> typeArguments = resolvedTypes.getActualTypeArguments(call);
	if (!typeArguments.isEmpty()) {
		for(LightweightTypeReference typeArgument: typeArguments) {
			if (typeArgument.isWildcard()) {
				return completeFeatureCallAppendable;
			}
		}
		completeFeatureCallAppendable.append("<");
		for (int i = 0; i < typeArguments.size(); i++) {
			if (i != 0) {
				completeFeatureCallAppendable.append(", ");
			}
			completeFeatureCallAppendable.append(typeArguments.get(i));
		}
		completeFeatureCallAppendable.append(">");
	}
	return completeFeatureCallAppendable;
}
 
Example 3
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void _toJavaStatement(final XConstructorCall expr, ITreeAppendable b, final boolean isReferenced) {
	for (XExpression arg : expr.getArguments()) {
		prepareExpression(arg, b);
	}
	
	if (!isReferenced) {
		b.newLine();
		constructorCallToJavaExpression(expr, b);
		b.append(";");
	} else if (isVariableDeclarationRequired(expr, b, true)) {
		Later later = new Later() {
			@Override
			public void exec(ITreeAppendable appendable) {
				constructorCallToJavaExpression(expr, appendable);
			}
		};
		declareFreshLocalVariable(expr, b, later);
	}
}
 
Example 4
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected ITreeAppendable _generateModifier(final JvmField it, final ITreeAppendable appendable, final GeneratorConfig config) {
  ITreeAppendable _xblockexpression = null;
  {
    this.generateVisibilityModifier(it, appendable);
    boolean _isStatic = it.isStatic();
    if (_isStatic) {
      appendable.append("static ");
    }
    boolean _isFinal = it.isFinal();
    if (_isFinal) {
      appendable.append("final ");
    }
    boolean _isTransient = it.isTransient();
    if (_isTransient) {
      appendable.append("transient ");
    }
    ITreeAppendable _xifexpression = null;
    boolean _isVolatile = it.isVolatile();
    if (_isVolatile) {
      _xifexpression = appendable.append("volatile ");
    }
    _xblockexpression = _xifexpression;
  }
  return _xblockexpression;
}
 
Example 5
Source File: FeatureCallCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void featureCalltoJavaExpression(final XAbstractFeatureCall call, ITreeAppendable b, boolean isExpressionContext) {
	if (call instanceof XAssignment) {
		assignmentToJavaExpression((XAssignment) call, b, isExpressionContext);
	} else {
		if (needMultiAssignment(call)) {
			appendLeftOperand(call, b, isExpressionContext).append(" = ");
		}
		final JvmAnnotationReference annotationRef = this.expressionHelper.findInlineAnnotation(call);
		if (annotationRef == null || !isConstantExpression(annotationRef)) {
			boolean hasReceiver = appendReceiver(call, b, isExpressionContext);
			if (hasReceiver) {
				b.append(".");
				b = appendTypeArguments(call, b);
			}
		}
		appendFeatureCall(call, b);
	}
}
 
Example 6
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendConstructedTypeName(XConstructorCall constructorCall, ITreeAppendable typeAppendable) {
	JvmDeclaredType type = constructorCall.getConstructor().getDeclaringType();
	if (type instanceof JvmGenericType && ((JvmGenericType) type).isAnonymous()) {
		typeAppendable.append(Iterables.getLast(type.getSuperTypes()).getType());
	} else {
		typeAppendable.append(constructorCall.getConstructor().getDeclaringType());
	}
}
 
Example 7
Source File: FeatureCallCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendArguments(List<? extends XExpression> arguments, ITreeAppendable b, boolean shouldWrapLine) {
	for (int i = 0; i < arguments.size(); i++) {
		XExpression argument = arguments.get(i);
		appendArgument(argument, b, shouldWrapLine || i > 0);
		if (i + 1 < arguments.size())
			b.append(", ");
	}
}
 
Example 8
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void executeThenPart(XSwitchExpression expr, String switchResultName, XExpression then,
		ITreeAppendable b, boolean isReferenced) {
	final boolean canBeReferenced = isReferenced && !isPrimitiveVoid(then);
	internalToJavaStatement(then, b, canBeReferenced);
	if (canBeReferenced) {
		b.newLine().append(switchResultName).append(" = ");
		internalToConvertedExpression(then, b, getLightweightType(expr));
		b.append(";");
	}
}
 
Example 9
Source File: FeatureCallCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendNullValueUntyped(LightweightTypeReference type, @SuppressWarnings("unused") EObject context, ITreeAppendable b) {
	if (!type.isPrimitive()) {
		b.append("null");
	} else {
		b.append(getDefaultLiteral((JvmPrimitiveType) type.getType()));
	}
}
 
Example 10
Source File: LoopExtensions.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Uses curly braces and comma as delimiters. Doesn't use them for single
 * valued iterables.
 */
public <T> void forEachWithShortcut(ITreeAppendable appendable, Iterable<T> elements,
		Procedure1<? super T> procedure) {
	if (IterableExtensions.size(elements) == 1) {
		procedure.apply(Iterables.getFirst(elements, null));
	} else {
		appendable.append("{");
		forEach(appendable, elements, (LoopParams it) -> {
			it.setPrefix(" ");
			it.setSeparator(", ");
			it.setSuffix(" ");
		}, procedure);
		appendable.append("}");
	}
}
 
Example 11
Source File: XtendGenerator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
private ITreeAppendable generateJavaConstant(final Object value, final ITreeAppendable appendable) {
  ITreeAppendable _xifexpression = null;
  if ((value instanceof Float)) {
    _xifexpression = appendable.append(((Float)value).toString()).append("f");
  } else {
    ITreeAppendable _xifexpression_1 = null;
    if ((value instanceof Long)) {
      _xifexpression_1 = appendable.append(((Long)value).toString()).append("l");
    } else {
      ITreeAppendable _xifexpression_2 = null;
      if ((value instanceof Character)) {
        _xifexpression_2 = appendable.append(Integer.toString(((Character)value).charValue()));
      } else {
        ITreeAppendable _xifexpression_3 = null;
        if ((value instanceof CharSequence)) {
          _xifexpression_3 = appendable.append("\"").append(this.doConvertToJavaString(((CharSequence)value).toString())).append("\"");
        } else {
          ITreeAppendable _xifexpression_4 = null;
          if (((value instanceof Number) || (value instanceof Boolean))) {
            _xifexpression_4 = appendable.append(value.toString());
          } else {
            _xifexpression_4 = appendable.append("null /* ERROR: illegal constant value */");
          }
          _xifexpression_3 = _xifexpression_4;
        }
        _xifexpression_2 = _xifexpression_3;
      }
      _xifexpression_1 = _xifexpression_2;
    }
    _xifexpression = _xifexpression_1;
  }
  return _xifexpression;
}
 
Example 12
Source File: SARLJvmGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
protected ITreeAppendable _generateMember(JvmOperation it, ITreeAppendable appendable, GeneratorConfig config) {
	if (Utils.STATIC_CONSTRUCTOR_NAME.equals(it.getSimpleName())) {
		// The constructor name is not the same as the declaring type.
		// We assume that the constructor is a static constructor.
		return generateStaticConstructor(it, appendable, config);
	}
	// The code below is adapted from the code of the Xtend super type.
	appendable.newLine();
	appendable.openScope();
	generateJavaDoc(it, appendable, config);
	final ITreeAppendable tracedAppendable = appendable.trace(it);
	generateAnnotations(it.getAnnotations(), tracedAppendable, true, config);
	// Specific case: automatic generation
	if (this.operationHelper.isPureOperation(it)
			&& this.annotations.findAnnotation(it, Pure.class) == null) {
		tracedAppendable.append("@").append(Pure.class).newLine(); //$NON-NLS-1$
	}
	generateModifier(it, tracedAppendable, config);
	generateTypeParameterDeclaration(it, tracedAppendable, config);
	if (it.getReturnType() == null) {
		tracedAppendable.append("void"); //$NON-NLS-1$
	} else {
		this._errorSafeExtensions.serializeSafely(it.getReturnType(), Object.class.getSimpleName(), tracedAppendable);
	}
	tracedAppendable.append(" "); //$NON-NLS-1$
	this._treeAppendableUtil.traceSignificant(tracedAppendable, it).append(makeJavaIdentifier(it.getSimpleName()));
	tracedAppendable.append("("); //$NON-NLS-1$
	generateParameters(it, tracedAppendable, config);
	tracedAppendable.append(")"); //$NON-NLS-1$
	generateThrowsClause(it, tracedAppendable, config);
	if (it.isAbstract() || !hasBody(it)) {
		tracedAppendable.append(";"); //$NON-NLS-1$
	} else {
		tracedAppendable.append(" "); //$NON-NLS-1$
		generateExecutableBody(it, tracedAppendable, config);
	}
	appendable.closeScope();
	return appendable;
}
 
Example 13
Source File: TypeConvertingCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private void convertListToArray(
		final LightweightTypeReference arrayTypeReference, 
		final ITreeAppendable appendable,
		final Later expression) {
	appendable.append("((");
	appendable.append(arrayTypeReference);
	appendable.append(")");
	appendable.append(Conversions.class);
	appendable.append(".unwrapArray(");
	expression.exec(appendable);
	LightweightTypeReference rawTypeArrayReference = arrayTypeReference.getRawTypeReference();
	appendable.append(", ");
	appendable.append(rawTypeArrayReference.getComponentType());
	appendable.append(".class))");
}
 
Example 14
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _toJavaLiteral(final JvmFloatAnnotationValue it, final ITreeAppendable appendable, final GeneratorConfig config) {
  final Procedure1<Float> _function = (Float it_1) -> {
    String _switchResult = null;
    boolean _matched = false;
    boolean _isNaN = Float.isNaN((it_1).floatValue());
    if (_isNaN) {
      _matched=true;
      _switchResult = "Float.NaN";
    }
    if (!_matched) {
      if (Objects.equal(it_1, Float.POSITIVE_INFINITY)) {
        _matched=true;
        _switchResult = "Float.POSITIVE_INFINITY";
      }
    }
    if (!_matched) {
      if (Objects.equal(it_1, Float.NEGATIVE_INFINITY)) {
        _matched=true;
        _switchResult = "Float.NEGATIVE_INFINITY";
      }
    }
    if (!_matched) {
      String _string = it_1.toString();
      _switchResult = (_string + "f");
    }
    appendable.append(_switchResult);
  };
  this._loopExtensions.<Float>forEachWithShortcut(appendable, it.getValues(), _function);
}
 
Example 15
Source File: CheckCompiler.java    From dsl-devkit with Eclipse Public License 1.0 4 votes vote down vote up
/** {@inheritDoc} */
// CHECKSTYLE:OFF
protected void _toJavaExpression(final XIssueExpression expr, final ITreeAppendable b) {
  // CHECKSTYLE:ON
  b.append(getVarName(expr, b));
}
 
Example 16
Source File: CheckCompiler.java    From dsl-devkit with Eclipse Public License 1.0 4 votes vote down vote up
/** {@inheritDoc} */
// CHECKSTYLE:OFF
protected void _toJavaExpression(final XGuardExpression expr, final ITreeAppendable b) {
  // CHECKSTYLE:ON
  b.append(getVarName(expr, b));
}
 
Example 17
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 toJavaBasicForStatement(XBasicForLoopExpression expr, ITreeAppendable b, boolean isReferenced) {
	ITreeAppendable loopAppendable = b.trace(expr);
	loopAppendable.openPseudoScope();
	loopAppendable.newLine().append("for (");
	
	EList<XExpression> initExpressions = expr.getInitExpressions();
	XExpression firstInitExpression = IterableExtensions.head(initExpressions);
	if (firstInitExpression instanceof XVariableDeclaration) {
		XVariableDeclaration variableDeclaration = (XVariableDeclaration) firstInitExpression;
		LightweightTypeReference type = appendVariableTypeAndName(variableDeclaration, loopAppendable);
		loopAppendable.append(" = ");
		if (variableDeclaration.getRight() != null) {
			compileAsJavaExpression(variableDeclaration.getRight(), loopAppendable, type);
		} else {
			appendDefaultLiteral(loopAppendable, type);
		}
	} else {
		for (int i = 0; i < initExpressions.size(); i++) {
			if (i != 0) {
				loopAppendable.append(", ");
			}
			XExpression initExpression = initExpressions.get(i);
			compileAsJavaExpression(initExpression, loopAppendable, getLightweightType(initExpression));
		}
	}
	
	loopAppendable.append(";");
	
	XExpression expression = expr.getExpression();
	if (expression != null) {
		loopAppendable.append(" ");
		internalToJavaExpression(expression, loopAppendable);
	}
	loopAppendable.append(";");
	
	EList<XExpression> updateExpressions = expr.getUpdateExpressions();
	for (int i = 0; i < updateExpressions.size(); i++) {
		if (i != 0) {
			loopAppendable.append(",");
		}
		loopAppendable.append(" ");
		XExpression updateExpression = updateExpressions.get(i);
		internalToJavaExpression(updateExpression, loopAppendable);
	}
	loopAppendable.append(") {").increaseIndentation();
	
	XExpression eachExpression = expr.getEachExpression();
	internalToJavaStatement(eachExpression, loopAppendable, false);
	
	loopAppendable.decreaseIndentation().newLine().append("}");
	loopAppendable.closeScope();
}
 
Example 18
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public ITreeAppendable generateVisibilityModifier(final JvmMember it, final ITreeAppendable result) {
  return result.append(this.javaName(it.getVisibility()));
}
 
Example 19
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void _toJavaLiteral(final JvmLongAnnotationValue it, final ITreeAppendable appendable, final GeneratorConfig config) {
  final Procedure1<Long> _function = (Long it_1) -> {
    appendable.append(it_1.toString());
  };
  this._loopExtensions.<Long>forEachWithShortcut(appendable, it.getValues(), _function);
}
 
Example 20
Source File: LiteralsCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @param expr the expression. Used by the dispatch strategy.
 */
public void _toJavaExpression(XNullLiteral expr, ITreeAppendable b) {
	b.append("null");
}