Skip to content

Commit

Permalink
Merge pull request #2890 from LorenzoBettini/lb_xbase_cleanup-1
Browse files Browse the repository at this point in the history
Xbase/Xtend refactoring
  • Loading branch information
LorenzoBettini authored Jan 8, 2024
2 parents aff64b8 + 7ac3f49 commit b85670b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import org.eclipse.xtext.xbase.XbasePackage;
import org.eclipse.xtext.xbase.compiler.GeneratorConfig;
import org.eclipse.xtext.xbase.compiler.IGeneratorConfigProvider;
import org.eclipse.xtext.xbase.compiler.Later;
import org.eclipse.xtext.xbase.compiler.XbaseCompiler;
import org.eclipse.xtext.xbase.compiler.output.ITreeAppendable;
import org.eclipse.xtext.xbase.lib.Extension;
Expand Down Expand Up @@ -358,7 +357,7 @@ protected XExpression normalizeBlockExpression(XExpression expr) {
}

@Override
public void doInternalToJavaStatement(XExpression obj, ITreeAppendable appendable, boolean isReferenced) {
protected void doInternalToJavaStatement(XExpression obj, ITreeAppendable appendable, boolean isReferenced) {
prependLocalTypesIfFieldInitializer(obj, appendable, isReferenced);
if(obj instanceof AnonymousClass)
_toJavaStatement((AnonymousClass)obj, appendable, isReferenced);
Expand All @@ -375,7 +374,7 @@ private void prependLocalTypesIfFieldInitializer(XExpression obj, ITreeAppendabl
}
}

public void _toJavaStatement(RichString richString, ITreeAppendable b, boolean isReferenced) {
protected void _toJavaStatement(RichString richString, ITreeAppendable b, boolean isReferenced) {
LightweightTypeReference actualType = getLightweightType(richString);
b = b.trace(richString);
if (actualType.isType(StringConcatenationClient.class)) {
Expand Down Expand Up @@ -417,7 +416,7 @@ public void _toJavaStatement(RichString richString, ITreeAppendable b, boolean i
}

@Override
public void internalToConvertedExpression(XExpression obj, ITreeAppendable appendable) {
protected void internalToConvertedExpression(XExpression obj, ITreeAppendable appendable) {
if (obj instanceof AnonymousClass)
_toJavaExpression((AnonymousClass) obj, appendable);
else if (obj instanceof RichString)
Expand All @@ -433,8 +432,16 @@ protected void _toJavaExpression(AnonymousClass anonymousClass, ITreeAppendable
} else {
XConstructorCall constructorCall = anonymousClass.getConstructorCall();
constructorCallToJavaExpression(constructorCall, b);
JvmDeclaredType declaringType = constructorCall.getConstructor().getDeclaringType();
compileAnonymousClassBody(anonymousClass, declaringType, b);
}
}

@Override
protected void constructorCallToJavaExpression(XConstructorCall expr, ITreeAppendable b) {
super.constructorCallToJavaExpression(expr, b);
if (expr.eContainer() instanceof AnonymousClass) {
JvmConstructor constructor = expr.getConstructor();
JvmDeclaredType declaringType = constructor.getDeclaringType();
compileAnonymousClassBody((AnonymousClass) expr.eContainer(), declaringType, b);
}
}

Expand All @@ -448,7 +455,7 @@ protected void compileAnonymousClassBody(AnonymousClass anonymousClass, JvmDecla
appendable.closeScope();
}

public void _toJavaExpression(RichString richString, ITreeAppendable b) {
protected void _toJavaExpression(RichString richString, ITreeAppendable b) {
b.append(getVarName(richString, b));
if(getLightweightType(richString).isType(String.class))
b.append(".toString()");
Expand Down Expand Up @@ -531,42 +538,11 @@ public void _toJavaExpression(XStringLiteral expr, ITreeAppendable b) {
public void _toJavaStatement(final XStringLiteral expr, ITreeAppendable b, boolean isReferenced) {
toJavaStatement(expr, b, isReferenced, false);
}

protected void _toJavaStatement(final AnonymousClass anonymousClass, ITreeAppendable b, final boolean isReferenced) {
_toJavaStatement(anonymousClass.getConstructorCall(), b, isReferenced);
}

@Override
protected void _toJavaStatement(final XConstructorCall expr, ITreeAppendable b, final boolean isReferenced) {
for (XExpression arg : expr.getArguments()) {
prepareExpression(arg, b);
}

if (!isReferenced) {
b.newLine();
constructorCallToJavaExpression(expr, b);
if (expr.eContainer() instanceof AnonymousClass) {
JvmConstructor constructor = expr.getConstructor();
JvmDeclaredType declaringType = constructor.getDeclaringType();
compileAnonymousClassBody((AnonymousClass) expr.eContainer(), declaringType, b);
}
b.append(";");
} else if (isVariableDeclarationRequired(expr, b, true)) {
Later later = new Later() {
@Override
public void exec(ITreeAppendable appendable) {
constructorCallToJavaExpression(expr, appendable);
if (expr.eContainer() instanceof AnonymousClass) {
JvmConstructor constructor = expr.getConstructor();
JvmDeclaredType declaringType = constructor.getDeclaringType();
compileAnonymousClassBody((AnonymousClass) expr.eContainer(), declaringType, appendable);
}
}
};
declareFreshLocalVariable(expr, b, later);
}
}


@Override
protected boolean internalCanCompileToJavaExpression(XExpression expression, ITreeAppendable appendable) {
if(expression instanceof AnonymousClass) {
Expand All @@ -576,27 +552,7 @@ protected boolean internalCanCompileToJavaExpression(XExpression expression, ITr
} else
return super.internalCanCompileToJavaExpression(expression, appendable);
}

@Override
protected boolean isVariableDeclarationRequired(XExpression expr, ITreeAppendable b, boolean recursive) {
boolean result = super.isVariableDeclarationRequired(expr, b, recursive);
if (result && expr instanceof XConstructorCall) {
EObject container = expr.eContainer();
if (container instanceof AnonymousClass) {
AnonymousClass anonymousClass = (AnonymousClass) container;
result = isVariableDeclarationRequired(anonymousClass, b, recursive);
if (result) {
JvmConstructor constructor = anonymousClass.getConstructorCall().getConstructor();
JvmDeclaredType type = constructor.getDeclaringType();
if (((JvmGenericType) type).isAnonymous()) {
return false;
}
}
}
}
return result;
}


@Override
/* @Nullable */
protected String getReferenceName(XExpression expr, ITreeAppendable b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@

import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.inject.Inject;

Expand Down Expand Up @@ -1106,7 +1105,9 @@ protected void constructorCallToJavaExpression(final XConstructorCall expr, ITre
}
ITreeAppendable typeAppendable = appendableWithNewKeyword.trace(expr, XbasePackage.Literals.XCONSTRUCTOR_CALL__CONSTRUCTOR, 0);
appendConstructedTypeName(expr, typeAppendable);
if (hasTypeArguments || (expr.isAnonymousClassConstructorCall() && !explicitTypeArguments.isEmpty() && ((JvmGenericType) constructor.getDeclaringType()).isAnonymous())) {
if (hasTypeArguments ||
(!explicitTypeArguments.isEmpty() &&
canCompileToJavaAnonymousClass(expr))) {
if (typeArguments.isEmpty()) {
LightweightTypeReference createdType = resolvedTypes.getActualType(expr);
typeArguments = createdType.getNamedType().getTypeArguments();
Expand All @@ -1132,16 +1133,25 @@ protected void constructorCallToJavaExpression(final XConstructorCall expr, ITre
}

protected void appendConstructedTypeName(XConstructorCall constructorCall, ITreeAppendable typeAppendable) {
JvmDeclaredType type = constructorCall.getConstructor().getDeclaringType();
if (type instanceof JvmGenericType && ((JvmGenericType) type).isAnonymous()) {
typeAppendable.append(Iterables.getLast(type.getSuperTypes()).getType());
IResolvedTypes resolvedTypes = batchTypeResolver.resolveTypes(constructorCall);
LightweightTypeReference actualType = resolvedTypes.getActualType(constructorCall);
if (canCompileToJavaAnonymousClass(constructorCall)) {
typeAppendable.append(actualType.getNamedType().getRawTypeReference());
} else {
IResolvedTypes resolvedTypes = batchTypeResolver.resolveTypes(constructorCall);
LightweightTypeReference actualType = resolvedTypes.getActualType(constructorCall).getRawTypeReference();
typeAppendable.append(actualType);
typeAppendable.append(actualType.getRawTypeReference());
}
}


/**
* @since 2.34
*/
protected boolean canCompileToJavaAnonymousClass(XConstructorCall constructorCall) {
if (!constructorCall.isAnonymousClassConstructorCall())
return false;
JvmDeclaredType type = constructorCall.getConstructor().getDeclaringType();
return type instanceof JvmGenericType && ((JvmGenericType) type).isAnonymous();
}

/* @Nullable */
protected ILocationData getLocationWithNewKeyword(XConstructorCall call) {
final ICompositeNode startNode = NodeModelUtils.getNode(call);
Expand Down Expand Up @@ -2167,6 +2177,13 @@ protected boolean isVariableDeclarationRequired(XExpression expr, ITreeAppendabl
}
}
}
if (expr instanceof XConstructorCall) {
XConstructorCall constructorCall = (XConstructorCall) expr;
if (constructorCall.isAnonymousClassConstructorCall()) {
return isVariableDeclarationRequired((XExpression) container, b, recursive) &&
!canCompileToJavaAnonymousClass(constructorCall);
}
}
return super.isVariableDeclarationRequired(expr, b, recursive);
}

Expand Down

0 comments on commit b85670b

Please sign in to comment.