From 828b0326a8a50a57bb143cc76a75aab9fcac566d Mon Sep 17 00:00:00 2001 From: Lorenzo Bettini Date: Sun, 31 Dec 2023 18:28:30 +0100 Subject: [PATCH 1/4] simplified appendConstructedTypeName The handling of anonymous class is already taken care by org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReferenceSerializer.doVisitParameterizedTypeReference(ParameterizedTypeReference) so there's no need to consider anonymous class here --- .../eclipse/xtext/xbase/compiler/XbaseCompiler.java | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) 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..3510a1f866b 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; @@ -1132,14 +1131,9 @@ 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()); - } else { - IResolvedTypes resolvedTypes = batchTypeResolver.resolveTypes(constructorCall); - LightweightTypeReference actualType = resolvedTypes.getActualType(constructorCall).getRawTypeReference(); - typeAppendable.append(actualType); - } + IResolvedTypes resolvedTypes = batchTypeResolver.resolveTypes(constructorCall); + LightweightTypeReference actualType = resolvedTypes.getActualType(constructorCall).getRawTypeReference(); + typeAppendable.append(actualType); } /* @Nullable */ From 3cae7d2eee326885f53b89f24af1df5309829c81 Mon Sep 17 00:00:00 2001 From: Lorenzo Bettini Date: Sun, 31 Dec 2023 18:35:08 +0100 Subject: [PATCH 2/4] don't retrieve the ImportManager by reflection Since we already assume that the import manager is part of the appendable, which is a TreeAppendable, I think it's better to do the case and expose getImportManager in TreeAppendable than to call that method with two reflective calls. This would also allow clients to provide a custom TreeAppendable; with the reflective call this latter use-case would not be supported --- .../xbase/compiler/JvmModelGenerator.xtend | 27 ++++++++----------- .../xbase/compiler/output/TreeAppendable.java | 5 +++- .../xbase/compiler/JvmModelGenerator.java | 17 +----------- 3 files changed, 16 insertions(+), 33 deletions(-) diff --git a/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/JvmModelGenerator.xtend b/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/JvmModelGenerator.xtend index ceca3665c9e..d0ee1c5346f 100644 --- a/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/JvmModelGenerator.xtend +++ b/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/JvmModelGenerator.xtend @@ -804,7 +804,7 @@ class JvmModelGenerator implements IGenerator { generateDocumentation(adapter.documentation, emptyList, appendable, config) } } - } + } def addJavaDocImports(EObject it, ITreeAppendable appendable,List documentationNodes) { for(node : documentationNodes){ @@ -839,12 +839,7 @@ class JvmModelGenerator implements IGenerator { } def getImportManager(ITreeAppendable appendable) { - val stateField = appendable.getClass.getDeclaredField("state") - stateField.setAccessible(true) - val stateValue = stateField.get(appendable) - val importManagerField = stateValue.getClass.getDeclaredField("importManager") - importManagerField.setAccessible(true) - importManagerField.get(stateValue) as ImportManager + (appendable as TreeAppendable).getImportManager() } def protected generateDocumentation(String text, List documentationNodes, ITreeAppendable appendable, GeneratorConfig config) { @@ -886,7 +881,7 @@ class JvmModelGenerator implements IGenerator { toJava(appendable, config) ]) } - + def void toJava(JvmAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) { if (operation !== null) { if (operation.simpleName === null) { @@ -899,11 +894,11 @@ class JvmModelGenerator implements IGenerator { } toJavaLiteral(appendable, config) } - + def dispatch void toJavaLiteral(JvmAnnotationAnnotationValue value, ITreeAppendable appendable, GeneratorConfig config) { appendable.forEachWithShortcut(value.values, [generateAnnotation(appendable, config)]) } - + def dispatch void toJavaLiteral(JvmShortAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) { appendable.forEachWithShortcut(values, [appendable.append(toString)]) } @@ -955,12 +950,12 @@ class JvmModelGenerator implements IGenerator { appendable.append('"' + doConvertToJavaString(toString) + '"') ]) } - + def dispatch void toJavaLiteral(JvmTypeAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) { appendable.forEachWithShortcut(values, [ appendable.append(type).append(".class") ]) - } + } def dispatch void toJavaLiteral(JvmEnumAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) { appendable.forEachWithShortcut(values, [ @@ -968,13 +963,13 @@ class JvmModelGenerator implements IGenerator { appendable.append(".") appendable.append(simpleName.makeJavaIdentifier) ]) - } - + } + def dispatch void toJavaLiteral(JvmBooleanAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) { appendable.forEachWithShortcut(values, [ appendable.append(toString) ]) - } + } def dispatch void toJavaLiteral(JvmCustomAnnotationValue it, ITreeAppendable appendable, GeneratorConfig config) { if(values.isEmpty) @@ -984,7 +979,7 @@ class JvmModelGenerator implements IGenerator { compiler.toJavaExpression(it, appendable) ]) } - + def TreeAppendable createAppendable(EObject context, ImportManager importManager, GeneratorConfig config) { val cachingConverter = new ITraceURIConverter() { diff --git a/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/output/TreeAppendable.java b/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/output/TreeAppendable.java index 126a05b826f..6b772972a39 100644 --- a/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/output/TreeAppendable.java +++ b/org.eclipse.xtext.xbase/src/org/eclipse/xtext/xbase/compiler/output/TreeAppendable.java @@ -589,7 +589,10 @@ public SharedAppendableState getState() { return state; } - ImportManager getImportManager() { + /** + * @since 2.34 + */ + public ImportManager getImportManager() { return state.getImportManager(); } diff --git a/org.eclipse.xtext.xbase/xtend-gen/org/eclipse/xtext/xbase/compiler/JvmModelGenerator.java b/org.eclipse.xtext.xbase/xtend-gen/org/eclipse/xtext/xbase/compiler/JvmModelGenerator.java index 88a6cdd58d1..06adf343abe 100644 --- a/org.eclipse.xtext.xbase/xtend-gen/org/eclipse/xtext/xbase/compiler/JvmModelGenerator.java +++ b/org.eclipse.xtext.xbase/xtend-gen/org/eclipse/xtext/xbase/compiler/JvmModelGenerator.java @@ -12,7 +12,6 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Maps; import com.google.inject.Inject; -import java.lang.reflect.Field; import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; @@ -1226,21 +1225,7 @@ public void addJavaDocImports(final EObject it, final ITreeAppendable appendable } public ImportManager getImportManager(final ITreeAppendable appendable) { - try { - ImportManager _xblockexpression = null; - { - final Field stateField = appendable.getClass().getDeclaredField("state"); - stateField.setAccessible(true); - final Object stateValue = stateField.get(appendable); - final Field importManagerField = stateValue.getClass().getDeclaredField("importManager"); - importManagerField.setAccessible(true); - Object _get = importManagerField.get(stateValue); - _xblockexpression = ((ImportManager) _get); - } - return _xblockexpression; - } catch (Throwable _e) { - throw Exceptions.sneakyThrow(_e); - } + return ((TreeAppendable) appendable).getImportManager(); } protected ITreeAppendable generateDocumentation(final String text, final List documentationNodes, final ITreeAppendable appendable, final GeneratorConfig config) { From fdad9842c53185582bc15547e96118b3cedf82e0 Mon Sep 17 00:00:00 2001 From: Lorenzo Bettini Date: Sun, 31 Dec 2023 19:29:00 +0100 Subject: [PATCH 3/4] don't handle anonymous class in constructorCallToJavaExpression either type arguments are already serialized by appendConstructedTypeName now --- .../src/org/eclipse/xtext/xbase/compiler/XbaseCompiler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 3510a1f866b..c721822a6af 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 @@ -1105,7 +1105,7 @@ 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) { if (typeArguments.isEmpty()) { LightweightTypeReference createdType = resolvedTypes.getActualType(expr); typeArguments = createdType.getNamedType().getTypeArguments(); From 28a2e11d74f3c671e6b18f0d24c7af99046b6f36 Mon Sep 17 00:00:00 2001 From: Lorenzo Bettini Date: Sun, 31 Dec 2023 19:50:37 +0100 Subject: [PATCH 4/4] further simplification: in that part typeArguments cannot be empty --- .../xtext/xbase/compiler/XbaseCompiler.java | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) 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 c721822a6af..94d4eaaf56e 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 @@ -1106,24 +1106,18 @@ protected void constructorCallToJavaExpression(final XConstructorCall expr, ITre ITreeAppendable typeAppendable = appendableWithNewKeyword.trace(expr, XbasePackage.Literals.XCONSTRUCTOR_CALL__CONSTRUCTOR, 0); appendConstructedTypeName(expr, typeAppendable); if (hasTypeArguments) { - if (typeArguments.isEmpty()) { - LightweightTypeReference createdType = resolvedTypes.getActualType(expr); - typeArguments = createdType.getNamedType().getTypeArguments(); - } - if (!typeArguments.isEmpty()) { - typeAppendable.append("<"); - for(int i = 0; i < typeArguments.size(); i++) { - if (i != 0) { - typeAppendable.append(", "); - } - if (explicitTypeArguments.isEmpty()) { - typeAppendable.append(typeArguments.get(i)); - } else { - typeAppendable.trace(explicitTypeArguments.get(i), false).append(typeArguments.get(i)); - } + typeAppendable.append("<"); + for(int i = 0; i < typeArguments.size(); i++) { + if (i != 0) { + typeAppendable.append(", "); + } + if (explicitTypeArguments.isEmpty()) { + typeAppendable.append(typeArguments.get(i)); + } else { + typeAppendable.trace(explicitTypeArguments.get(i), false).append(typeArguments.get(i)); } - typeAppendable.append(">"); } + typeAppendable.append(">"); } b.append("("); appendArguments(expr.getArguments(), b);