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

The following examples show how to use org.eclipse.xtext.xbase.compiler.output.ITreeAppendable#closeScope() . 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 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;
}
 
Example 2
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
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("}");
}
 
Example 3
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public void generateAnnotationMethod(final JvmOperation it, final ITreeAppendable appendable, final GeneratorConfig config) {
  appendable.increaseIndentation().newLine();
  appendable.openScope();
  this.generateJavaDoc(it, appendable, config);
  final ITreeAppendable tracedAppendable = appendable.trace(it);
  this.generateAnnotations(it.getAnnotations(), tracedAppendable, true, config);
  this.generateModifier(it, tracedAppendable, config);
  this._errorSafeExtensions.serializeSafely(it.getReturnType(), "Object", tracedAppendable);
  tracedAppendable.append(" ");
  this._treeAppendableUtil.traceSignificant(tracedAppendable, it).append(this.makeJavaIdentifier(it.getSimpleName()));
  tracedAppendable.append("()");
  this.generateDefaultExpression(it, tracedAppendable, config);
  tracedAppendable.append(";");
  appendable.decreaseIndentation();
  appendable.closeScope();
}
 
Example 4
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected ITreeAppendable _generateMember(final JvmDeclaredType it, final ITreeAppendable appendable, final GeneratorConfig config) {
  ITreeAppendable _xblockexpression = null;
  {
    appendable.newLine();
    appendable.openScope();
    this.assignThisAndSuper(appendable, it, config);
    ITreeAppendable _xtrycatchfinallyexpression = null;
    try {
      _xtrycatchfinallyexpression = this.generateBody(it, appendable, config);
    } finally {
      appendable.closeScope();
    }
    _xblockexpression = _xtrycatchfinallyexpression;
  }
  return _xblockexpression;
}
 
Example 5
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected ITreeAppendable _generateMember(final JvmConstructor it, final ITreeAppendable appendable, final GeneratorConfig config) {
  ITreeAppendable _xblockexpression = null;
  {
    appendable.newLine();
    appendable.openScope();
    this.generateJavaDoc(it, appendable, config);
    final ITreeAppendable tracedAppendable = appendable.trace(it);
    this.generateAnnotations(it.getAnnotations(), tracedAppendable, true, config);
    this.generateModifier(it, tracedAppendable, config);
    this.generateTypeParameterDeclaration(it, tracedAppendable, config);
    this._treeAppendableUtil.traceSignificant(tracedAppendable, it).append(this.makeJavaIdentifier(it.getSimpleName()));
    tracedAppendable.append("(");
    this.generateParameters(it, tracedAppendable, config);
    tracedAppendable.append(")");
    this.generateThrowsClause(it, tracedAppendable, config);
    tracedAppendable.append(" ");
    this.generateExecutableBody(it, tracedAppendable, config);
    appendable.closeScope();
    _xblockexpression = appendable;
  }
  return _xblockexpression;
}
 
Example 6
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _toJavaStatement(XBlockExpression expr, ITreeAppendable b, boolean isReferenced) {
	b = b.trace(expr, false);
	if (expr.getExpressions().isEmpty())
		return;
	if (expr.getExpressions().size()==1) {
		internalToJavaStatement(expr.getExpressions().get(0), b, isReferenced);
		return;
	}
	if (isReferenced)
		declareSyntheticVariable(expr, b);
	boolean needsBraces = isReferenced || !bracesAreAddedByOuterStructure(expr);
	if (needsBraces) {
		b.newLine().append("{").increaseIndentation();
		b.openPseudoScope();
	}
	final EList<XExpression> expressions = expr.getExpressions();
	for (int i = 0; i < expressions.size(); i++) {
		XExpression ex = expressions.get(i);
		if (i < expressions.size() - 1) {
			internalToJavaStatement(ex, b, false);
		} else {
			internalToJavaStatement(ex, b, isReferenced);
			if (isReferenced) {
				b.newLine().append(getVarName(expr, b)).append(" = ");
				internalToConvertedExpression(ex, b, getLightweightType(expr));
				b.append(";");
			}
		}
	}
	if (needsBraces) {
		b.closeScope();
		b.decreaseIndentation().newLine().append("}");
	}
}
 
Example 7
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.18
 */
protected void appendFinallyWithResources(XTryCatchFinallyExpression expr, ITreeAppendable b) {
	final String throwablesStore = b.getName(Tuples.pair(expr, "_caughtThrowables"));
	List<XVariableDeclaration> resources = expr.getResources();
	if (!resources.isEmpty()) {
		for (int i = resources.size() - 1; i >= 0; i--) {
			b.openPseudoScope();
			XVariableDeclaration res = resources.get(i);
			String resName = getVarName(res, b);
			b.newLine().append("if (" + resName + " != null) {");
			b.increaseIndentation();
			b.newLine().append("try {");
			b.increaseIndentation();
			b.newLine().append(resName + ".close();");
			// close inner try
			closeBlock(b);
			String throwable = b.declareSyntheticVariable(Tuples.pair(res, "_caughtThrowable"), "_t");
			b.append(" catch (").append(Throwable.class).append(" " + throwable + ") {");
			b.increaseIndentation();
			b.newLine().append(throwablesStore);
			b.append(".add(" + throwable + ");");
			// close inner catch
			closeBlock(b);
			// close if != null check
			closeBlock(b);
			b.closeScope();
		}
		b.newLine().append("if(!");
		b.append(throwablesStore);
		b.append(".isEmpty()) ");
		appendSneakyThrow(expr, b, throwablesStore + ".get(0)");
	}
}
 
Example 8
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param isReferenced unused in this context but necessary for dispatch signature 
 */
protected void _toJavaStatement(XWhileExpression expr, ITreeAppendable b, boolean isReferenced) {
	boolean needsStatement = !canCompileToJavaExpression(expr.getPredicate(), b);
	String varName = null;
	if (needsStatement) {
		internalToJavaStatement(expr.getPredicate(), b, true);
		varName = b.declareSyntheticVariable(expr, "_while");
		b.newLine().append("boolean ").append(varName).append(" = ");
		internalToJavaExpression(expr.getPredicate(), b);
		b.append(";");
	}
	b.newLine().append("while (");
	if (needsStatement) {
		b.append(varName);
	} else {
		internalToJavaExpression(expr.getPredicate(), b);
	}
	b.append(") {").increaseIndentation();
	b.openPseudoScope();
	internalToJavaStatement(expr.getBody(), b, false);
	if (needsStatement && !isEarlyExit(expr.getBody())) {
		internalToJavaStatement(expr.getPredicate(), b, true);
		b.newLine();
		b.append(varName).append(" = ");
		internalToJavaExpression(expr.getPredicate(), b);
		b.append(";");
	}
	b.closeScope();
	b.decreaseIndentation().newLine().append("}");
}
 
Example 9
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected ITreeAppendable _generateMember(final JvmOperation it, final ITreeAppendable appendable, final GeneratorConfig config) {
  ITreeAppendable _xblockexpression = null;
  {
    appendable.newLine();
    appendable.openScope();
    this.generateJavaDoc(it, appendable, config);
    final ITreeAppendable tracedAppendable = appendable.trace(it);
    this.generateAnnotations(it.getAnnotations(), tracedAppendable, true, config);
    this.generateModifier(it, tracedAppendable, config);
    this.generateTypeParameterDeclaration(it, tracedAppendable, config);
    JvmTypeReference _returnType = it.getReturnType();
    boolean _tripleEquals = (_returnType == null);
    if (_tripleEquals) {
      tracedAppendable.append("void");
    } else {
      this._errorSafeExtensions.serializeSafely(it.getReturnType(), "Object", tracedAppendable);
    }
    tracedAppendable.append(" ");
    this._treeAppendableUtil.traceSignificant(tracedAppendable, it).append(this.makeJavaIdentifier(it.getSimpleName()));
    tracedAppendable.append("(");
    this.generateParameters(it, tracedAppendable, config);
    tracedAppendable.append(")");
    this.generateThrowsClause(it, tracedAppendable, config);
    if ((it.isAbstract() || (!this.hasBody(it)))) {
      tracedAppendable.append(";");
    } else {
      tracedAppendable.append(" ");
      this.generateExecutableBody(it, tracedAppendable, config);
    }
    appendable.closeScope();
    _xblockexpression = appendable;
  }
  return _xblockexpression;
}
 
Example 10
Source File: XtendCompiler.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
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();
}
 
Example 11
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 12
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 13
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void toJavaWhileStatement(XBasicForLoopExpression expr, ITreeAppendable b, boolean isReferenced) {
	ITreeAppendable loopAppendable = b.trace(expr);
	
	boolean needBraces = !bracesAreAddedByOuterStructure(expr);
	if (needBraces) {
		loopAppendable.newLine().increaseIndentation().append("{");
		loopAppendable.openPseudoScope();
	}
	
	EList<XExpression> initExpressions = expr.getInitExpressions();
	for (int i = 0; i < initExpressions.size(); i++) {
		XExpression initExpression = initExpressions.get(i);
		if (i < initExpressions.size() - 1) {
			internalToJavaStatement(initExpression, loopAppendable, false);
		} else {
			internalToJavaStatement(initExpression, loopAppendable, isReferenced);
			if (isReferenced) {
				loopAppendable.newLine().append(getVarName(expr, loopAppendable)).append(" = (");
				internalToConvertedExpression(initExpression, loopAppendable, getLightweightType(expr));
				loopAppendable.append(");");
			}
		}
	}

	final String varName = loopAppendable.declareSyntheticVariable(expr, "_while");
	
	XExpression expression = expr.getExpression();
	if (expression != null) {
		internalToJavaStatement(expression, loopAppendable, true);
		loopAppendable.newLine().append("boolean ").append(varName).append(" = ");
		internalToJavaExpression(expression, loopAppendable);
		loopAppendable.append(";");
	} else {
		loopAppendable.newLine().append("boolean ").append(varName).append(" = true;");
	}
	loopAppendable.newLine();
	loopAppendable.append("while (");
	loopAppendable.append(varName);
	loopAppendable.append(") {").increaseIndentation();
	loopAppendable.openPseudoScope();
	
	XExpression eachExpression = expr.getEachExpression();
	internalToJavaStatement(eachExpression, loopAppendable, false);
	
	EList<XExpression> updateExpressions = expr.getUpdateExpressions();
	if (!updateExpressions.isEmpty()) {
		for (XExpression updateExpression : updateExpressions) {
			internalToJavaStatement(updateExpression, loopAppendable, false);
		}
	}
	
	if (!isEarlyExit(eachExpression)) {
		if (expression != null) {
			internalToJavaStatement(expression, loopAppendable, true);
			loopAppendable.newLine().append(varName).append(" = ");
			internalToJavaExpression(expression, loopAppendable);
			loopAppendable.append(";");
		} else {
			loopAppendable.newLine().append(varName).append(" = true;");
		}
	}
	
	loopAppendable.closeScope();
	loopAppendable.decreaseIndentation().newLine().append("}");
	
	if (needBraces) {
		loopAppendable.closeScope();
		loopAppendable.decreaseIndentation().newLine().append("}");
	}
}
 
Example 14
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected ITreeAppendable toAnonymousClass(final XClosure closure, final ITreeAppendable b, LightweightTypeReference type,
		JvmOperation operation) {
	b.append("new ");
	b.append(type);
	b.append("() {");
	b.increaseIndentation();
	try {
		b.openScope();
		String selfVariable = null;
		if (needSyntheticSelfVariable(closure, type)) {
			b.newLine().append("final ");
			b.append(type).append(" ");
			selfVariable = b.declareVariable(type.getType(), "_self");
			b.append(selfVariable);
			b.append(" = this;");
		}
		final LightweightTypeReference returnType = getClosureOperationReturnType(type, operation);
		appendOperationVisibility(b, operation);
		if (!operation.getTypeParameters().isEmpty()) {
			appendTypeParameters(b, operation, type);
		}
		b.append(returnType);
		b.append(" ").append(operation.getSimpleName());
		b.append("(");
		List<JvmFormalParameter> closureParams = closure.getFormalParameters();
		boolean isVarArgs = operation.isVarArgs();
		for (int i = 0; i < closureParams.size(); i++) {
			JvmFormalParameter closureParam = closureParams.get(i);
			LightweightTypeReference parameterType = getClosureOperationParameterType(type, operation, i);
			if (isVarArgs && i == closureParams.size()-1 && parameterType.isArray()) {
				appendClosureParameterVarArgs(closureParam, parameterType.getComponentType(), b);
			} else {					
				appendClosureParameter(closureParam, parameterType, b);
			}
			if (i != closureParams.size() - 1)
				b.append(", ");
		}
		b.append(")");
		if(!operation.getExceptions().isEmpty()) {
			b.append(" throws ");
			for (int i = 0; i < operation.getExceptions().size(); ++i) {
				serialize(operation.getExceptions().get(i), closure, b, false, false, false, false);
				if(i != operation.getExceptions().size() -1)
					b.append(", ");
			}
		}
		b.append(" {");
		b.increaseIndentation();
		if (selfVariable == null) {
			reassignThisInClosure(b, type.getType());
		} else {
			// We have already assigned the closure type to _self, so don't assign it again
			reassignThisInClosure(b, null);
		}
		compile(closure.getExpression(), b, returnType, newHashSet(operation.getExceptions()));
		closeBlock(b);
	} finally {
		b.closeScope();
	}
	return b.decreaseIndentation().newLine().append("}");
}
 
Example 15
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected ITreeAppendable toLambda(XClosure closure, ITreeAppendable b, LightweightTypeReference type,
		JvmOperation operation, boolean writeExplicitTargetType) {
	if (writeExplicitTargetType) {
		b.append("((");
		b.append(type);
		b.append(") ");
	}
	try {
		b.openPseudoScope();
		b.append("(");
		List<JvmFormalParameter> closureParams = closure.getFormalParameters();
		boolean isVarArgs = operation.isVarArgs();
		for (int i = 0; i < closureParams.size(); i++) {
			JvmFormalParameter closureParam = closureParams.get(i);
			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());	
			// Usually a normal variable would suffice here. The 'unique name' variable is a workaround for
			// https://bugs.eclipse.org/bugs/show_bug.cgi?id=445949
			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);
		compile(closure.getExpression(), b, returnType, newHashSet(operation.getExceptions()));
		closeBlock(b);
	} finally {
		b.closeScope();
	}
	if (writeExplicitTargetType) {
		b.append(")");
	}
	return b;
}
 
Example 16
Source File: SarlCompiler.java    From sarl with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("checkstyle:npathcomplexity")
private ITreeAppendable toSerializableAnonymousClass(XClosure closure, ITreeAppendable appendable,
		LightweightTypeReference type, JvmOperation operation) {
	// This function is implemented in order to generate static inner class when the closure
	// cannot be represented neither by a Java 8 lambda nor a not-static inner class.
	// It solves the issues related to the serialization and deserialization of the closures.
	final List<JvmFormalParameter> closureParams = closure.getFormalParameters();
	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$
			}
			appendOperationVisibility(appendable, operation);
			if (!operation.getTypeParameters().isEmpty()) {
				appendTypeParameters(appendable, operation, type);
			}
			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++) {
				final JvmFormalParameter closureParam = closureParams.get(i);
				final LightweightTypeReference parameterType = getClosureOperationParameterType(type, operation, i);
				if (isVarArgs && i == closureParams.size() - 1 && parameterType.isArray()) {
					appendClosureParameterVarArgs(closureParam, parameterType.getComponentType(), appendable);
				} else {
					appendClosureParameter(closureParam, parameterType, appendable);
				}
				if (i != closureParams.size() - 1) {
					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) {
					serialize(operation.getExceptions().get(i), closure, appendable, false, false, false, false);
					if (i != operation.getExceptions().size() - 1) {
						appendable.append(", "); //$NON-NLS-1$
					}
				}
			}
			appendable.append(" {"); //$NON-NLS-1$
			appendable.increaseIndentation();
			if (selfVariable == null) {
				reassignThisInClosure(appendable, type.getType());
			} else {
				// We have already assigned the closure type to _self, so don't assign it again
				reassignThisInClosure(appendable, null);
			}
			compile(closure.getExpression(), appendable, returnType, newHashSet(operation.getExceptions()));
			closeBlock(appendable);
		} catch (Exception exception) {
			throw new RuntimeException(exception);
		} finally {
			appendable.closeScope();
		}
		appendable.newLine().append("private ").append(Object.class).append(" writeReplace() throws ");  //$NON-NLS-1$//$NON-NLS-2$
		appendable.append(ObjectStreamException.class).append(" {").increaseIndentation().newLine(); //$NON-NLS-1$
		if (selfVariable == null) {
			reassignThisInClosure(appendable, type.getType());
		} else {
			// We have already assigned the closure type to _self, so don't assign it again
			reassignThisInClosure(appendable, null);
		}
		appendable.append("return new ").append(SerializableProxy.class); //$NON-NLS-1$
		appendable.append("(").append(appendable.getName(type)).append(".class"); //$NON-NLS-1$ //$NON-NLS-2$
		for (final XAbstractFeatureCall call : localReferences) {
			appendable.append(", "); //$NON-NLS-1$
			compileAsJavaExpression(call, appendable, returnType);
		}

		appendable.append(");").decreaseIndentation().newLine().append("}"); //$NON-NLS-1$ //$NON-NLS-2$
		appendable.decreaseIndentation().newLine().append("}"); //$NON-NLS-1$
	} finally {
		appendable.closeScope();
	}
	return appendable;
}