diff --git a/org.eclipse.xtend.core/src/org/eclipse/xtend/core/compiler/XtendCompiler.java b/org.eclipse.xtend.core/src/org/eclipse/xtend/core/compiler/XtendCompiler.java index d2273937db8..a96f6e071bc 100644 --- a/org.eclipse.xtend.core/src/org/eclipse/xtend/core/compiler/XtendCompiler.java +++ b/org.eclipse.xtend.core/src/org/eclipse/xtend/core/compiler/XtendCompiler.java @@ -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; @@ -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); @@ -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)) { @@ -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) @@ -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); } } @@ -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()"); @@ -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) { @@ -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) { diff --git a/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/XbaseCompiler.java b/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/XbaseCompiler.java index 1136e0f73ae..6e15a44ed2e 100644 --- a/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/XbaseCompiler.java +++ b/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/XbaseCompiler.java @@ -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; @@ -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(); @@ -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); @@ -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); }