protected LightweightTypeReference getCollectionElementType(XCollectionLiteral literal) { LightweightTypeReference type = getLightweightType(literal); if (type == null) throw new IllegalStateException(); if(type.isArray()) { LightweightTypeReference result = type.getComponentType(); if (result == null) throw new IllegalStateException(); return result; } else if(type.isSubtypeOf(Collection.class) && type.hasTypeArguments()) { return type.getTypeArguments().get(0).getInvariantBoundSubstitute(); } return type.getOwner().newReferenceToObject(); }
protected void executeThenPart(XSwitchExpression expr, String switchResultName, XExpression then, ITreeAppendable b, boolean isReferenced) { final boolean canBeReferenced = isReferenced && !isPrimitiveVoid(then); internalToJavaStatement(then, b, canBeReferenced); if (canBeReferenced) { b.newLine().append(switchResultName).append(" = "); internalToConvertedExpression(then, b, getLightweightType(expr)); b.append(";"); } }
protected void _toJavaExpression(XCastedExpression expr, ITreeAppendable b) { b.append("(("); serialize(expr.getType(), expr, b); b.append(") "); internalToConvertedExpression(expr.getTarget(), b, getLightweightType(expr)); b.append(")"); }
protected LightweightTypeReference appendVariableTypeAndName(XVariableDeclaration varDeclaration, ITreeAppendable appendable) { if (!varDeclaration.isWriteable()) { appendable.append("final "); } LightweightTypeReference type = null; if (varDeclaration.getType() != null) { serialize(varDeclaration.getType(), varDeclaration, appendable); type = getLightweightType((JvmIdentifiableElement) varDeclaration); } else { type = getLightweightType(varDeclaration.getRight()); if (type.isAny()) { type = getTypeForVariableDeclaration(varDeclaration.getRight()); } appendable.append(type); } appendable.append(" "); appendable.append(appendable.declareVariable(varDeclaration, makeJavaIdentifier(varDeclaration.getName()))); return type; }
if (isReferenced) { b.newLine().append(getVarName(expr, b)).append(" = "); internalToConvertedExpression(ex, b, getLightweightType(expr)); b.append(";");
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(";"); } }
protected void _toJavaExpression(final XClosure closure, final ITreeAppendable b) { if (b.hasName(closure)) { b.trace(closure, false).append(getVarName(closure, b)); } else { LightweightTypeReference type = getLightweightType(closure); JvmOperation operation = findImplementingOperation(type); if (operation != null) { GeneratorConfig config = b.getGeneratorConfig(); if (config != null && config.getJavaSourceVersion().isAtLeast(JAVA8) && canCompileToJavaLambda(closure, type, operation)) { toLambda(closure, b.trace(closure, false), type, operation, true); } else { toAnonymousClass(closure, b.trace(closure, false), type, operation); } } } }
private LightweightTypeReference findRealReturnType(XExpression expression) { if (expression == null) return null; JvmIdentifiableElement logicalContainer = getLogicalContainerProvider().getLogicalContainer(expression); if (logicalContainer instanceof JvmOperation) { return getLightweightType(logicalContainer); } if (expression instanceof XClosure) { IResolvedTypes resolvedTypes = batchTypeResolver.resolveTypes(expression); LightweightTypeReference type = resolvedTypes.getExpectedType(expression); if (type == null) { type = resolvedTypes.getActualType(expression); } if (type == null) { return null; } FunctionTypeReference functionType = type.tryConvertToFunctionTypeReference(false); if (functionType != null) { return functionType.getReturnType(); } return null; } XExpression containerExpression = EcoreUtil2.getContainerOfType(expression.eContainer(), XExpression.class); if (containerExpression == null) { LightweightTypeReference returnType = getLightweightReturnType(expression); return returnType; } return findRealReturnType(containerExpression); }
compileAsJavaExpression(initExpression, loopAppendable, getLightweightType(initExpression));
protected void _toJavaStatement(XTryCatchFinallyExpression expr, ITreeAppendable outerAppendable, boolean isReferenced) { ITreeAppendable b = outerAppendable.trace(expr, false); if (isReferenced && !isPrimitiveVoid(expr)) { declareSyntheticVariable(expr, b); } b.newLine().append("try {").increaseIndentation(); final boolean canBeReferenced = isReferenced && !isPrimitiveVoid(expr.getExpression()); internalToJavaStatement(expr.getExpression(), b, canBeReferenced); if (canBeReferenced) { b.newLine().append(getVarName(expr, b)).append(" = "); internalToConvertedExpression(expr.getExpression(), b, getLightweightType(expr)); b.append(";"); } b.decreaseIndentation().newLine().append("}"); appendCatchAndFinally(expr, b, isReferenced); }
if (canBeReferenced) { appendable.newLine().append(getVarName(catchClause.eContainer(), appendable)).append(" = "); internalToConvertedExpression(catchClause.getExpression(), appendable, getLightweightType((XExpression) catchClause.eContainer())); appendable.append(";");
b.append(getVarName(expr, b)); b.append(" = "); internalToConvertedExpression(expr.getThen(), b, getLightweightType(expr)); b.append(";"); b.append(getVarName(expr, b)); b.append(" = "); internalToConvertedExpression(expr.getElse(), b, getLightweightType(expr)); b.append(";");
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("}"); }
if (isReferenced) { loopAppendable.newLine().append(getVarName(expr, loopAppendable)).append(" = ("); internalToConvertedExpression(initExpression, loopAppendable, getLightweightType(expr)); loopAppendable.append(");");
internalToJavaStatement(casePart.getCase(), conditionAppendable, true); conditionAppendable.newLine().append("if ("); LightweightTypeReference convertedType = getLightweightType(casePart.getCase()); if (convertedType.isType(Boolean.TYPE) || convertedType.isType(Boolean.class)) { internalToJavaExpression(casePart.getCase(), conditionAppendable);