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

The following examples show how to use org.eclipse.xtext.xbase.compiler.output.ITreeAppendable#openScope() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
Source File: SARLJvmGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Generate a static constructor from the given Jvm constructor.
 *
 * @param it the container of the code.
 * @param appendable the output.
 * @param config the generation configuration.
 * @return the appendable.
 */
protected ITreeAppendable generateStaticConstructor(JvmOperation it, ITreeAppendable appendable, GeneratorConfig config) {
	appendable.newLine();
	appendable.openScope();
	generateJavaDoc(it, appendable, config);
	final ITreeAppendable tracedAppendable = appendable.trace(it);
	tracedAppendable.append("static "); //$NON-NLS-1$
	generateExecutableBody(it, tracedAppendable, config);
	return appendable;
}
 
Example 8
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 9
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;
}