Java Code Examples for org.eclipse.xtext.xbase.compiler.output.ITreeAppendable#declareVariable()
The following examples show how to use
org.eclipse.xtext.xbase.compiler.output.ITreeAppendable#declareVariable() .
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: TypeConvertingCompiler.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
protected void reassignThisInClosure(final ITreeAppendable b, JvmType rawClosureType) { boolean registerClosureAsThis = rawClosureType instanceof JvmGenericType; if (b.hasObject("this")) { Object element = b.getObject("this"); if (element instanceof JvmType) { // Don't reassign if the types are equal, since the 'this' of the enclosing type is shadowed if (element != rawClosureType) doReassignThisInClosure(b, (JvmType) element); } else { registerClosureAsThis = false; } } if (registerClosureAsThis) { b.declareVariable(rawClosureType, "this"); } }
Example 2
Source File: JvmModelGenerator.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
protected ITreeAppendable _generateMember(final JvmField it, final ITreeAppendable appendable, final GeneratorConfig config) { ITreeAppendable _xblockexpression = null; { appendable.newLine(); 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.getType(), "Object", tracedAppendable); tracedAppendable.append(" "); final String name = tracedAppendable.declareVariable(it, this.makeJavaIdentifier(it.getSimpleName())); this._treeAppendableUtil.traceSignificant(tracedAppendable, it).append(name); this.generateInitialization(it, tracedAppendable, config); _xblockexpression = tracedAppendable.append(";"); } return _xblockexpression; }
Example 3
Source File: JvmModelGenerator.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
public void generateParameter(final JvmFormalParameter it, final ITreeAppendable appendable, final boolean vararg, final GeneratorConfig config) { final ITreeAppendable tracedAppendable = appendable.trace(it); this.generateAnnotations(it.getAnnotations(), tracedAppendable, false, config); tracedAppendable.append("final "); if (vararg) { JvmTypeReference _parameterType = it.getParameterType(); boolean _not = (!(_parameterType instanceof JvmGenericArrayTypeReference)); if (_not) { tracedAppendable.append("/* Internal Error: Parameter was vararg but not an array type. */"); } else { JvmTypeReference _parameterType_1 = it.getParameterType(); this._errorSafeExtensions.serializeSafely(((JvmGenericArrayTypeReference) _parameterType_1).getComponentType(), "Object", tracedAppendable); } tracedAppendable.append("..."); } else { this._errorSafeExtensions.serializeSafely(it.getParameterType(), "Object", tracedAppendable); } tracedAppendable.append(" "); final String name = tracedAppendable.declareVariable(it, this.makeJavaIdentifier(it.getSimpleName())); this._treeAppendableUtil.traceSignificant(tracedAppendable, it).append(name); }
Example 4
Source File: JvmModelGenerator.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
protected String reassignThisType(final ITreeAppendable b, final JvmDeclaredType declaredType) { String _xblockexpression = null; { boolean _hasObject = b.hasObject("this"); if (_hasObject) { final Object element = b.getObject("this"); if ((element instanceof JvmDeclaredType)) { boolean _isLocal = ((JvmDeclaredType)element).isLocal(); if (_isLocal) { b.declareVariable(element, ""); } else { String _simpleName = ((JvmDeclaredType)element).getSimpleName(); final String proposedName = (_simpleName + ".this"); b.declareVariable(element, proposedName); } } } String _xifexpression = null; if ((declaredType != null)) { _xifexpression = b.declareVariable(declaredType, "this"); } _xblockexpression = _xifexpression; } return _xblockexpression; }
Example 5
Source File: XtendCompiler.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
/** * 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); } }
Example 6
Source File: XbaseCompiler.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
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); }
Example 7
Source File: XbaseCompiler.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
protected void appendClosureParameter(JvmFormalParameter closureParam, LightweightTypeReference parameterType, ITreeAppendable appendable) { appendable.append("final "); appendable.append(parameterType); appendable.append(" "); final String proposedParamName = makeJavaIdentifier(closureParam.getName()); String name = appendable.declareVariable(closureParam, proposedParamName); appendable.append(name); }
Example 8
Source File: XbaseCompiler.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
protected void appendClosureParameterVarArgs(JvmFormalParameter closureParam, LightweightTypeReference parameterType, ITreeAppendable appendable) { appendable.append("final "); appendable.append(parameterType); appendable.append("... "); final String proposedParamName = makeJavaIdentifier(closureParam.getName()); String name = appendable.declareVariable(closureParam, proposedParamName); appendable.append(name); }
Example 9
Source File: XtendCompiler.java From xtext-xtend with Eclipse Public License 2.0 | 5 votes |
@Override public void acceptForLoop(JvmFormalParameter parameter, /* @Nullable */ XExpression expression) { currentAppendable = null; super.acceptForLoop(parameter, expression); if (expression == null) throw new IllegalArgumentException("expression may not be null"); RichStringForLoop forLoop = (RichStringForLoop) expression.eContainer(); forStack.add(forLoop); appendable.newLine(); pushAppendable(forLoop); appendable.append("{").increaseIndentation(); ITreeAppendable debugAppendable = appendable.trace(forLoop, true); internalToJavaStatement(expression, debugAppendable, true); String variableName = null; if (forLoop.getBefore() != null || forLoop.getSeparator() != null || forLoop.getAfter() != null) { variableName = debugAppendable.declareSyntheticVariable(forLoop, "_hasElements"); debugAppendable.newLine(); debugAppendable.append("boolean "); debugAppendable.append(variableName); debugAppendable.append(" = false;"); } debugAppendable.newLine(); debugAppendable.append("for(final "); // TODO tracing if parameter was explicitly declared LightweightTypeReference paramType = getLightweightType(parameter); if (paramType != null) { debugAppendable.append(paramType); } else { debugAppendable.append("Object"); } debugAppendable.append(" "); String loopParam = debugAppendable.declareVariable(parameter, parameter.getName()); debugAppendable.append(loopParam); debugAppendable.append(" : "); internalToJavaExpression(expression, debugAppendable); debugAppendable.append(") {").increaseIndentation(); }
Example 10
Source File: XtendGenerator.java From xtext-xtend with Eclipse Public License 2.0 | 5 votes |
@Override public String reassignThisType(final ITreeAppendable b, final JvmDeclaredType declaredType) { String _xblockexpression = null; { boolean _hasObject = b.hasObject("this"); if (_hasObject) { final Object element = b.getObject("this"); if ((element instanceof JvmDeclaredType)) { boolean _isLocal = ((JvmDeclaredType)element).isLocal(); if (_isLocal) { Pair<String, JvmDeclaredType> _mappedTo = Pair.<String, JvmDeclaredType>of("this", ((JvmDeclaredType)element)); boolean _hasName = b.hasName(_mappedTo); if (_hasName) { Pair<String, JvmDeclaredType> _mappedTo_1 = Pair.<String, JvmDeclaredType>of("this", ((JvmDeclaredType)element)); b.declareVariable(element, b.getName(_mappedTo_1)); } else { b.declareVariable(element, ""); } } else { String _simpleName = ((JvmDeclaredType)element).getSimpleName(); final String proposedName = (_simpleName + ".this"); b.declareVariable(element, proposedName); } } } String _xifexpression = null; if ((declaredType != null)) { _xifexpression = b.declareVariable(declaredType, "this"); } _xblockexpression = _xifexpression; } return _xblockexpression; }
Example 11
Source File: XbaseCompiler.java From xtext-extras with Eclipse Public License 2.0 | 4 votes |
protected void appendCatchClause(XCatchClause catchClause, boolean parentIsReferenced, String parentVariable, ITreeAppendable appendable) { JvmTypeReference type = catchClause.getDeclaredParam().getParameterType(); final String declaredParamName = makeJavaIdentifier(catchClause.getDeclaredParam().getName()); final String name = appendable.declareVariable(catchClause.getDeclaredParam(), declaredParamName); appendable.append("if ("); JvmTypeReference typeToUse = type; if (type instanceof JvmSynonymTypeReference) { List<JvmTypeReference> references = ((JvmSynonymTypeReference) type).getReferences(); Iterator<JvmTypeReference> iter = references.iterator(); while(iter.hasNext()) { appendable.append(parentVariable).append(" instanceof "); serialize(iter.next(), catchClause, appendable); if (iter.hasNext()) { appendable.append(" || "); } } typeToUse = resolveSynonymType((JvmSynonymTypeReference) type, catchClause); } else { appendable.append(parentVariable).append(" instanceof "); serialize(type, catchClause, appendable); } appendable.append(") ").append("{"); appendable.increaseIndentation(); ITreeAppendable withDebugging = appendable.trace(catchClause, true); if (!XbaseUsageCrossReferencer.find(catchClause.getDeclaredParam(), catchClause.getExpression()).isEmpty()) { ITreeAppendable parameterAppendable = withDebugging.trace(catchClause.getDeclaredParam()); appendCatchClauseParameter(catchClause, typeToUse, name, parameterAppendable.newLine()); withDebugging.append(" = ("); serialize(typeToUse, catchClause, withDebugging); withDebugging.append(")").append(parentVariable).append(";"); } final boolean canBeReferenced = parentIsReferenced && ! isPrimitiveVoid(catchClause.getExpression()); internalToJavaStatement(catchClause.getExpression(), withDebugging, canBeReferenced); if (canBeReferenced) { appendable.newLine().append(getVarName(catchClause.eContainer(), appendable)).append(" = "); internalToConvertedExpression(catchClause.getExpression(), appendable, getLightweightType((XExpression) catchClause.eContainer())); appendable.append(";"); } closeBlock(appendable); }
Example 12
Source File: XbaseCompiler.java From xtext-extras with Eclipse Public License 2.0 | 4 votes |
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 13
Source File: JvmModelGenerator.java From xtext-extras with Eclipse Public License 2.0 | 4 votes |
private String reassignSuperType(final ITreeAppendable b, final JvmDeclaredType declaredType, final GeneratorConfig config) { String _xblockexpression = null; { JvmTypeReference _extendedClass = declaredType.getExtendedClass(); JvmType _type = null; if (_extendedClass!=null) { _type=_extendedClass.getType(); } final JvmType superType = _type; boolean _hasObject = b.hasObject("super"); if (_hasObject) { final Object element = b.getObject("this"); if ((element instanceof JvmDeclaredType)) { final Object superElement = b.getObject("super"); final String superVariable = b.getName(superElement); boolean _equals = "super".equals(superVariable); if (_equals) { String _simpleName = ((JvmDeclaredType)element).getSimpleName(); final String proposedName = (_simpleName + ".super"); b.declareVariable(superElement, proposedName); } } } boolean _isAtLeast = config.getJavaSourceVersion().isAtLeast(JavaVersion.JAVA8); if (_isAtLeast) { Iterable<JvmTypeReference> _extendedInterfaces = declaredType.getExtendedInterfaces(); for (final JvmTypeReference interfaceRef : _extendedInterfaces) { { final JvmType interfaze = interfaceRef.getType(); String _simpleName_1 = interfaze.getSimpleName(); final String simpleVarName = (_simpleName_1 + ".super"); boolean _hasObject_1 = b.hasObject(simpleVarName); if (_hasObject_1) { final Object element_1 = b.getObject(simpleVarName); boolean _notEquals = (!Objects.equal(element_1, interfaceRef)); if (_notEquals) { String _qualifiedName = interfaze.getQualifiedName(); final String qualifiedVarName = (_qualifiedName + ".super"); b.declareVariable(interfaze, qualifiedVarName); } } else { b.declareVariable(interfaze, simpleVarName); } } } } String _xifexpression = null; if ((superType != null)) { _xifexpression = b.declareVariable(superType, "super"); } _xblockexpression = _xifexpression; } return _xblockexpression; }
Example 14
Source File: SarlCompiler.java From sarl with Apache License 2.0 | 4 votes |
@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; }