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

The following examples show how to use org.eclipse.xtext.xbase.compiler.output.ITreeAppendable#getName() . 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 vote down vote up
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");
				}
			}
		}
	}
}
 
Example 2
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected String getSwitchLocalVariableName(XSwitchExpression expr, ITreeAppendable b) {
	JvmFormalParameter declaredParam = expr.getDeclaredParam();
	if (declaredParam != null) {
		if (b.hasName(declaredParam)) {
			return b.getName(declaredParam);
		}
		return null;
	}
	XExpression switchExpression = expr.getSwitch();
	if (b.hasName(switchExpression)) {
		return b.getName(switchExpression);
	} 
	if (switchExpression instanceof XFeatureCall) {
		XFeatureCall featureCall = (XFeatureCall) switchExpression;
		JvmIdentifiableElement feature = featureCall.getFeature();
		if (b.hasName(feature)) {
			return b.getName(feature);
		}
	}
	return null;
}
 
Example 3
Source File: XtendCompiler.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * 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 4
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 5
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
/* @Nullable */
protected String getReferenceName(XExpression expr, ITreeAppendable b) {
	if (expr instanceof XSwitchExpression) {
		Object key = getSwitchExpressionKey((XSwitchExpression) expr);
		if (b.hasName(key))
			return b.getName(key);
	}
	return super.getReferenceName(expr, b);
}
 
Example 6
Source File: AbstractXbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @return the variable name under which the result of the expression is stored. Returns <code>null</code> if the
 *          expression hasn't been assigned to a local variable before.
 */
/* @Nullable */
protected String getReferenceName(XExpression expr, ITreeAppendable b) {
	if (b.hasName(expr))
		return b.getName(expr);
	if (expr instanceof XFeatureCall) {
		XFeatureCall featureCall = (XFeatureCall) expr;
		if (b.hasName(featureCall.getFeature()))
			return b.getName(featureCall.getFeature());
	}
	return null;
}
 
Example 7
Source File: FeatureCallCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void appendFeatureCall(XAbstractFeatureCall call, ITreeAppendable b) {
	if (expressionHelper.isInlined(call)) {
		appendInlineFeatureCall(call, b);
		return;
	}
	JvmIdentifiableElement feature = call.getFeature();
	String name = null;
	if (feature instanceof JvmConstructor) {
		JvmDeclaredType constructorContainer = ((JvmConstructor) feature).getDeclaringType();
		JvmIdentifiableElement logicalContainer = contextProvider.getNearestLogicalContainer(call);
		JvmDeclaredType contextType = ((JvmMember) logicalContainer).getDeclaringType();
		if (contextType == constructorContainer)
			name = "this";
		else
			name = "super";
	} else if(feature != null) {
		if (b.hasName(feature)) {
			name = b.getName(feature);
		} else {
			name = featureNameProvider.getSimpleName(feature);
		}
	}
	if(name == null)
		name = "/* name is null */";
	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()) {
			XExpression receiver = null;
			if (call instanceof XMemberFeatureCall) {
				receiver = ((XMemberFeatureCall) call).getMemberCallTarget();
			} else if (call instanceof XAssignment) {
				receiver = ((XAssignment) call).getAssignable();
			}
			boolean shouldBreakFirstArgument = receiver == null || arguments.get(0) != receiver;
			appendArguments(arguments, b, shouldBreakFirstArgument);
		}
		b.append(")");
	}
}
 
Example 8
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void appendCatchAndFinally(XTryCatchFinallyExpression expr, ITreeAppendable b, boolean isReferenced) {
	final EList<XCatchClause> catchClauses = expr.getCatchClauses();
	final XExpression finallyExp = expr.getFinallyExpression();
	boolean isTryWithResources = !expr.getResources().isEmpty();
	// If Java 7 or better: use Java's try-with-resources
	boolean nativeTryWithResources = isAtLeast(b, JAVA7);
	final String throwablesStore = b.getName(Tuples.pair(expr, "_caughtThrowables"));

	// Catch
	if (!catchClauses.isEmpty()) {
		String variable = b.declareSyntheticVariable(Tuples.pair(expr, "_caughtThrowable"), "_t");
		b.append(" catch (final Throwable ").append(variable).append(") ");
		b.append("{").increaseIndentation().newLine();
		Iterator<XCatchClause> iterator = catchClauses.iterator();
		while (iterator.hasNext()) {
			XCatchClause catchClause = iterator.next();
			ITreeAppendable catchClauseAppendable = b.trace(catchClause);
			appendCatchClause(catchClause, isReferenced, variable, catchClauseAppendable);
			if (iterator.hasNext()) {
				b.append(" else ");
			}
		}
		b.append(" else {");
		b.increaseIndentation().newLine();
		if (isTryWithResources && !nativeTryWithResources) {
			b.append(throwablesStore + ".add(" + variable + ");").newLine();
		}
		appendSneakyThrow(expr, b, variable);
		closeBlock(b);
		closeBlock(b);
	}

	// Finally
	if (finallyExp != null || (!nativeTryWithResources && isTryWithResources)) {
		b.append(" finally {").increaseIndentation();
		if (finallyExp != null)
			internalToJavaStatement(finallyExp, b, false);
		if (!nativeTryWithResources && isTryWithResources)
			appendFinallyWithResources(expr, b);
		b.decreaseIndentation().newLine().append("}");
	}
}
 
Example 9
Source File: AbstractXbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected String getVarName(Object ex, ITreeAppendable appendable) {
	String name = appendable.getName(ex);
	return name;
}
 
Example 10
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
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;
}