protected void writeElse() { currentAppendable = null; appendable.decreaseIndentation(); appendable.newLine(); appendable.append("} else {"); appendable.increaseIndentation(); }
protected void compileAnonymousClassBody(AnonymousClass anonymousClass, JvmDeclaredType type, ITreeAppendable b) { ITreeAppendable appendable = b.trace(anonymousClass, true); appendable.append(" "); appendable.openScope(); GeneratorConfig config = generatorConfigProvider.get(anonymousClass); jvmModelGenerator.assignThisAndSuper(appendable, type, config); jvmModelGenerator.generateMembersInBody(type, appendable, config); appendable.closeScope(); }
protected String declareSwitchResultVariable(XSwitchExpression expr, ITreeAppendable b, boolean isReferenced) { LightweightTypeReference type = getTypeForVariableDeclaration(expr); String switchResultName = b.declareSyntheticVariable(getSwitchExpressionKey(expr), "_switchResult"); if (isReferenced) { b.newLine(); b.append(type); b.append(" ").append(switchResultName).append(" = "); b.append(getDefaultValueLiteral(expr)); b.append(";"); } return switchResultName; }
/** * Close a block of code. * * @param appendable the receiver of the block closing code. */ protected void closeBlock(ITreeAppendable appendable) { appendable.decreaseIndentation(); appendable.newLine().append("}"); }
/** Generate the Java code related to the preparation statements for the break keyword. * * @param breakExpression the expression. * @param appendable the output. * @param isReferenced indicates if the expression is referenced. */ @SuppressWarnings("static-method") protected void _toJavaStatement(SarlBreakExpression breakExpression, ITreeAppendable appendable, boolean isReferenced) { appendable.newLine().append("break;"); //$NON-NLS-1$ }
protected void _toJavaStatement(final XSynchronizedExpression synchronizedExpression, final ITreeAppendable b, boolean isReferenced) { if (isReferenced) { declareSyntheticVariable(synchronizedExpression, b); } ITreeAppendable synchronizedAppendable = b.trace(synchronizedExpression, true); XExpression param = synchronizedExpression.getParam(); if (!canCompileToJavaExpression(param, b)) internalToJavaStatement(param, synchronizedAppendable, isReferenced); synchronizedAppendable.newLine().append("synchronized ("); internalToJavaExpression(param, synchronizedAppendable); synchronizedAppendable.append(") {").increaseIndentation(); synchronizedAppendable.openPseudoScope(); XExpression expression = synchronizedExpression.getExpression(); internalToJavaStatement(expression, b, isReferenced); if (isReferenced) { b.newLine().append(getVarName(synchronizedExpression, synchronizedAppendable)).append(" = "); internalToConvertedExpression(expression, b, getLightweightType(synchronizedExpression)); b.append(";"); } synchronizedAppendable.closeScope(); synchronizedAppendable.decreaseIndentation().newLine().append("}"); }
public void _toJavaStatement(RichString richString, ITreeAppendable b, boolean isReferenced) { LightweightTypeReference actualType = getLightweightType(richString); b = b.trace(richString); if (actualType.isType(StringConcatenationClient.class)) { String resultVariableName = b.declareSyntheticVariable(richString, "_client"); b.newLine(); b.append(actualType); b.append(" "); b.append(resultVariableName); b.append(" = new "); b.append(actualType); b.append("() {"); b.openScope(); reassignThisInClosure(b, actualType.getType()); b.increaseIndentation().newLine(); b.append("@"); b.append(Override.class); b.newLine().append("protected void appendTo("); b.append(StringConcatenationClient.TargetStringConcatenation.class); String variableName = b.declareSyntheticVariable(richString, "_builder"); b.append(" ").append(variableName).append(") {"); b.increaseIndentation(); RichStringPrepareCompiler compiler = new RichStringPrepareCompiler(b, variableName, richString); richStringProcessor.process(richString, compiler, indentationHandler.get()); b.closeScope(); b.decreaseIndentation().newLine().append("}").decreaseIndentation().newLine().append("};"); } else { String variableName = b.declareSyntheticVariable(richString, "_builder"); b.newLine();
public ITreeAppendable compileAsJavaExpression(XExpression obj, ITreeAppendable parentAppendable, LightweightTypeReference expectedType) { ITreeAppendable appendable = parentAppendable.trace(obj, true); if (expectedType == null) { expectedType = getLightweightReturnType(obj); boolean needsToBeWrapped = earlyExit || needsSneakyThrow || !canCompileToJavaExpression(obj, appendable); if (needsToBeWrapped) { appendable.openScope(); try { if (appendable.hasObject("this")) { Object thisElement = appendable.getObject("this"); if (thisElement instanceof JvmType) { appendable.declareVariable(thisElement, ((JvmType) thisElement).getSimpleName()+".this"); if (appendable.hasObject("super")) { Object superElement = appendable.getObject("super"); if (superElement instanceof JvmType) { appendable.declareVariable(superElement, ((JvmType) thisElement).getSimpleName()+".super"); appendable.append("new "); JvmType procedureOrFunction = null; if (isPrimitiveVoidExpected) { appendable.append(procedureOrFunction); if (!isPrimitiveVoidExpected) { appendable.append("<"); appendable.append(expectedType.getWrapperTypeIfPrimitive()); appendable.append(">"); appendable.append("Object");
appendable.openPseudoScope(); try { final List<XAbstractFeatureCall> localReferences = getReferencedExternalCalls( closure.getExpression(), closureParams, false, appendable); final LightweightTypeReference returnType = getClosureOperationReturnType(type, operation); appendable.append("new ").append(type).append("() {"); //$NON-NLS-1$ //$NON-NLS-2$ appendable.increaseIndentation(); String selfVariable = null; try { appendable.openScope(); if (needSyntheticSelfVariable(closure, type)) { appendable.newLine().append("final "); //$NON-NLS-1$ appendable.append(type).append(" "); //$NON-NLS-1$ selfVariable = appendable.declareVariable(type.getType(), "_self"); //$NON-NLS-1$ appendable.append(selfVariable); appendable.append(" = this;"); //$NON-NLS-1$ appendable.append(returnType); appendable.append(" ").append(operation.getSimpleName()); //$NON-NLS-1$ appendable.append("("); //$NON-NLS-1$ final boolean isVarArgs = operation.isVarArgs(); for (int i = 0; i < closureParams.size(); i++) { appendable.append(", "); //$NON-NLS-1$ appendable.append(")"); //$NON-NLS-1$ if (!operation.getExceptions().isEmpty()) { appendable.append(" throws "); //$NON-NLS-1$ for (int i = 0; i < operation.getExceptions().size(); ++i) {
/** * Symmetric to {@link XtendGenerator#reassignThisType(ITreeAppendable, JvmDeclaredType)} */ @Override protected void doReassignThisInClosure(ITreeAppendable b, JvmType prevType) { if (prevType instanceof JvmDeclaredType && ((JvmDeclaredType) prevType).isLocal()) { if (b.hasName(Pair.of("this", prevType))) { b.declareVariable(prevType, b.getName(Pair.of("this", prevType))); } else { b.declareSyntheticVariable(prevType, ""); } if (b.hasObject("super")) { Object superElement = b.getObject("super"); if (superElement instanceof JvmType) { // Don't reassign the super of the enclosing type if it has already been reassigned String superVariable = b.getName(superElement); if ("super".equals(superVariable)) { b.declareSyntheticVariable(superElement, prevType.getSimpleName()+".super"); } } } } else { super.doReassignThisInClosure(b, prevType); } }
protected ITreeAppendable appendCloseIfStatement(XCasePart casePart, ITreeAppendable caseAppendable, XSwitchExpressionCompilationState state) { // close surrounding if statements if (state.caseNeedsIfNotMatchedCheck()) { if (casePart.getCase() != null) { caseAppendable.decreaseIndentation().newLine().append("}"); } if (casePart.getTypeGuard() != null) { caseAppendable.decreaseIndentation().newLine().append("}"); caseAppendable.closeScope(); } } else if (casePart.getCase() != null && casePart.getTypeGuard() != null) { caseAppendable.decreaseIndentation().newLine().append("}"); caseAppendable.closeScope(); } else if (casePart.getTypeGuard() != null) { caseAppendable.closeScope(); } state.finishProcessingCase(); return caseAppendable; }
if (expectedType != null && !isJavaConformant(expectedType, type)) { String varName = getVarName(((XAbstractFeatureCall) arg).getFeature(), b); String finalVariable = b.declareSyntheticVariable(arg, "_converted_" + varName); b.newLine().append("final "); b.append(type); b.append(" ").append(finalVariable).append(" = ").append("("); b.append(type); b.append(")").append(varName).append(";"); if (b.hasName(arg)) { LightweightTypeReference expectation = getLightweightExpectedType(arg); if (expectation != null && type.isFunctionType() && !isJavaConformant(expectation, type)) { String mutableName = b.removeName(arg); String finalNameProposal = "_final" + mutableName; String finalName = b.declareSyntheticVariable(arg, finalNameProposal); b.newLine(); b.append("final "); b.append(type); b.append(" ").append(finalName).append(" = "); b.append(mutableName); b.append(";");
protected ITreeAppendable toLambda(XClosure closure, ITreeAppendable b, LightweightTypeReference type, JvmOperation operation, boolean writeExplicitTargetType) { if (writeExplicitTargetType) { b.append("(("); b.append(type); b.append(") "); b.openPseudoScope(); b.append("("); List<JvmFormalParameter> closureParams = closure.getFormalParameters(); boolean isVarArgs = operation.isVarArgs(); LightweightTypeReference parameterType = getClosureOperationParameterType(type, operation, i); if (isVarArgs && i == closureParams.size()-1 && parameterType.isArray()) { b.append(parameterType.getComponentType()); b.append("..."); } else { b.append(parameterType); b.append(" "); String proposedParamName = makeJavaIdentifier(closureParam.getName()); String name = b.declareUniqueNameVariable(closureParam, proposedParamName); b.append(name); if (i != closureParams.size() - 1) b.append(", "); b.append(") -> {"); b.increaseIndentation(); LightweightTypeReference returnType = getClosureOperationReturnType(type, operation);
protected void _toJavaStatement(final XClosure closure, final ITreeAppendable b, boolean isReferenced) { if (!isReferenced) throw new IllegalArgumentException("a closure definition does not cause any side-effects"); LightweightTypeReference type = getLightweightType(closure); JvmOperation operation = findImplementingOperation(type); if (operation != null) { b.newLine().append("final "); b.append(type); b.append(" "); String variableName = b.declareSyntheticVariable(closure, "_function"); b.append(variableName).append(" = "); GeneratorConfig config = b.getGeneratorConfig(); if (config != null && config.getJavaSourceVersion().isAtLeast(JAVA8) && canCompileToJavaLambda(closure, type, operation)) { toLambda(closure, b, type, operation, false); } else { toAnonymousClass(closure, b, type, operation); } b.append(";"); } }
@Override public void acceptIfCondition(XExpression condition) { currentAppendable = null; ifStack.add((RichStringIf) condition.eContainer()); appendable.newLine(); pushAppendable(condition.eContainer()); appendable.append("{").increaseIndentation(); writeIf(condition); }
protected void appendForLoopParameter(XForLoopExpression expr, ITreeAppendable appendable) { appendable.append("final "); JvmTypeReference paramType = getForLoopParameterType(expr); serialize(paramType, expr, appendable); appendable.append(" "); final String name = makeJavaIdentifier(expr.getDeclaredParam().getName()); String varName = appendable.declareVariable(expr.getDeclaredParam(), name); appendable.trace(expr.getDeclaredParam(), TypesPackage.Literals.JVM_FORMAL_PARAMETER__NAME, 0).append(varName); }
name = "super"; } else if(feature != null) { if (b.hasName(feature)) { name = b.getName(feature); } else { name = featureNameProvider.getSimpleName(feature); b.trace(call, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, 0).append(name); if (feature instanceof JvmExecutable) { b.append("("); List<XExpression> arguments = getActualArguments(call); if (!arguments.isEmpty()) { appendArguments(arguments, b, shouldBreakFirstArgument); b.append(")");
protected String declareAndAppendSwitchSyntheticLocalVariable(XSwitchExpression expr, String name, ITreeAppendable b) { JvmFormalParameter declaredParam = expr.getDeclaredParam(); if (declaredParam == null) { String declareSyntheticVariable = b.declareSyntheticVariable(expr, name); b.append(declareSyntheticVariable); return declareSyntheticVariable; } String declareSyntheticVariable = b.declareSyntheticVariable(declaredParam, name); b.trace(declaredParam, TypesPackage.Literals.JVM_FORMAL_PARAMETER__NAME, 0).append(declareSyntheticVariable); return declareSyntheticVariable; }
protected void doReassignThisInClosure(final ITreeAppendable b, JvmType prevType) { final String proposedName = prevType.getSimpleName()+".this"; if (!b.hasObject(proposedName)) { b.declareSyntheticVariable(prevType, proposedName); if (b.hasObject("super")) { Object superElement = b.getObject("super"); if (superElement instanceof JvmType) { // Don't reassign the super of the enclosing type if it has already been reassigned String superVariable = b.getName(superElement); if ("super".equals(superVariable)) { b.declareSyntheticVariable(superElement, prevType.getSimpleName()+".super"); } } } } }