From 96f9df6fa26f46c6dd6308356acdc3ea4eeaa655 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sun, 14 Apr 2024 11:50:10 +0200 Subject: [PATCH] Replace Guava's ImmutableMap in XtextAntlrGeneratorFragment2 Part of https://github.com/eclipse/xtext/issues/2975 --- .../antlr/XtextAntlrGeneratorFragment2.xtend | 16 ++++----- .../antlr/XtextAntlrGeneratorFragment2.java | 36 ++++++++++--------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/parser/antlr/XtextAntlrGeneratorFragment2.xtend b/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/parser/antlr/XtextAntlrGeneratorFragment2.xtend index b6f76a0832e..0ac5dea187e 100644 --- a/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/parser/antlr/XtextAntlrGeneratorFragment2.xtend +++ b/org.eclipse.xtext.xtext.generator/src/org/eclipse/xtext/xtext/generator/parser/antlr/XtextAntlrGeneratorFragment2.xtend @@ -8,12 +8,12 @@ *******************************************************************************/ package org.eclipse.xtext.xtext.generator.parser.antlr -import com.google.common.collect.ImmutableMap import com.google.common.collect.Lists import com.google.inject.Inject import com.google.inject.Singleton import com.google.inject.name.Names import java.io.InputStream +import java.util.HashMap import java.util.List import java.util.Map import java.util.Set @@ -372,7 +372,7 @@ class XtextAntlrGeneratorFragment2 extends AbstractAntlrGeneratorFragment2 { */ protected def StringConcatenationClient initNameMappings(List partition) ''' «FOR element : partition» - builder.put(grammarAccess.«element.originalElement.grammarElementAccess», "«element.originalElement.containingRule.contentAssistRuleName»__«element.originalElement.gaElementIdentifier»«IF element instanceof Group»__0«ENDIF»"); + mappings.put(grammarAccess.«element.originalElement.grammarElementAccess», "«element.originalElement.containingRule.contentAssistRuleName»__«element.originalElement.gaElementIdentifier»«IF element instanceof Group»__0«ENDIF»"); «ENDFOR» ''' @@ -394,7 +394,7 @@ class XtextAntlrGeneratorFragment2 extends AbstractAntlrGeneratorFragment2 { «IF partitions.size > 1» «FOR partition : partitions.indexed» private static final class Init«partition.key» { - private static void doInit(«ImmutableMap».Builder<«AbstractElement», «String»> builder, «grammar.grammarAccess» grammarAccess) { + private static void doInit(«Map»<«AbstractElement», «String»> mappings, «grammar.grammarAccess» grammarAccess) { «partition.value.initNameMappings» } } @@ -405,19 +405,19 @@ class XtextAntlrGeneratorFragment2 extends AbstractAntlrGeneratorFragment2 { @«Inject» public NameMappings(«grammar.grammarAccess» grammarAccess) { - «ImmutableMap».Builder<«AbstractElement», «String»> builder = «ImmutableMap».builder(); - init(builder, grammarAccess); - this.mappings = builder.build(); + «Map»<«AbstractElement», «String»> mappings = new «HashMap»<>(); + init(mappings, grammarAccess); + this.mappings = «Map».copyOf(mappings); } public «String» getRuleName(«AbstractElement» element) { return mappings.get(element); } - private static void init(«ImmutableMap».Builder<«AbstractElement», «String»> builder, «grammar.grammarAccess» grammarAccess) { + private static void init(«Map»<«AbstractElement», «String»> mappings, «grammar.grammarAccess» grammarAccess) { «IF partitions.size > 1» «FOR partition : partitions.indexed» - Init«partition.key».doInit(builder, grammarAccess); + Init«partition.key».doInit(mappings, grammarAccess); «ENDFOR» «ELSE» «FOR partition : partitions» diff --git a/org.eclipse.xtext.xtext.generator/xtend-gen/org/eclipse/xtext/xtext/generator/parser/antlr/XtextAntlrGeneratorFragment2.java b/org.eclipse.xtext.xtext.generator/xtend-gen/org/eclipse/xtext/xtext/generator/parser/antlr/XtextAntlrGeneratorFragment2.java index 7c4291bb1b8..358951ea901 100644 --- a/org.eclipse.xtext.xtext.generator/xtend-gen/org/eclipse/xtext/xtext/generator/parser/antlr/XtextAntlrGeneratorFragment2.java +++ b/org.eclipse.xtext.xtext.generator/xtend-gen/org/eclipse/xtext/xtext/generator/parser/antlr/XtextAntlrGeneratorFragment2.java @@ -9,7 +9,6 @@ package org.eclipse.xtext.xtext.generator.parser.antlr; import com.google.common.base.Objects; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.inject.Inject; @@ -19,6 +18,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -841,7 +841,7 @@ protected StringConcatenationClient initNameMappings(final List protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) { { for(final AbstractElement element : partition) { - _builder.append("builder.put(grammarAccess."); + _builder.append("mappings.put(grammarAccess."); String _grammarElementAccess = XtextAntlrGeneratorFragment2.this.grammarUtil.grammarElementAccess(AntlrGrammarGenUtil.getOriginalElement(element)); _builder.append(_grammarElementAccess); _builder.append(", \""); @@ -912,12 +912,12 @@ protected void appendTo(StringConcatenationClient.TargetStringConcatenation _bui _builder.append("\t"); _builder.append("\t"); _builder.append("private static void doInit("); - _builder.append(ImmutableMap.class, "\t\t"); - _builder.append(".Builder<"); + _builder.append(Map.class, "\t\t"); + _builder.append("<"); _builder.append(AbstractElement.class, "\t\t"); _builder.append(", "); _builder.append(String.class, "\t\t"); - _builder.append("> builder, "); + _builder.append("> mappings, "); TypeReference _grammarAccess = XtextAntlrGeneratorFragment2.this.grammarUtil.getGrammarAccess(XtextAntlrGeneratorFragment2.this.getGrammar()); _builder.append(_grammarAccess, "\t\t"); _builder.append(" grammarAccess) {"); @@ -962,21 +962,23 @@ protected void appendTo(StringConcatenationClient.TargetStringConcatenation _bui _builder.append(" grammarAccess) {"); _builder.newLineIfNotEmpty(); _builder.append("\t\t"); - _builder.append(ImmutableMap.class, "\t\t"); - _builder.append(".Builder<"); + _builder.append(Map.class, "\t\t"); + _builder.append("<"); _builder.append(AbstractElement.class, "\t\t"); _builder.append(", "); _builder.append(String.class, "\t\t"); - _builder.append("> builder = "); - _builder.append(ImmutableMap.class, "\t\t"); - _builder.append(".builder();"); + _builder.append("> mappings = new "); + _builder.append(HashMap.class, "\t\t"); + _builder.append("<>();"); _builder.newLineIfNotEmpty(); _builder.append("\t\t"); - _builder.append("init(builder, grammarAccess);"); + _builder.append("init(mappings, grammarAccess);"); _builder.newLine(); _builder.append("\t\t"); - _builder.append("this.mappings = builder.build();"); - _builder.newLine(); + _builder.append("this.mappings = "); + _builder.append(Map.class, "\t\t"); + _builder.append(".copyOf(mappings);"); + _builder.newLineIfNotEmpty(); _builder.append("\t"); _builder.append("}"); _builder.newLine(); @@ -999,12 +1001,12 @@ protected void appendTo(StringConcatenationClient.TargetStringConcatenation _bui _builder.newLine(); _builder.append("\t"); _builder.append("private static void init("); - _builder.append(ImmutableMap.class, "\t"); - _builder.append(".Builder<"); + _builder.append(Map.class, "\t"); + _builder.append("<"); _builder.append(AbstractElement.class, "\t"); _builder.append(", "); _builder.append(String.class, "\t"); - _builder.append("> builder, "); + _builder.append("> mappings, "); TypeReference _grammarAccess_2 = XtextAntlrGeneratorFragment2.this.grammarUtil.getGrammarAccess(XtextAntlrGeneratorFragment2.this.getGrammar()); _builder.append(_grammarAccess_2, "\t"); _builder.append(" grammarAccess) {"); @@ -1020,7 +1022,7 @@ protected void appendTo(StringConcatenationClient.TargetStringConcatenation _bui _builder.append("Init"); Integer _key_1 = partition_1.getKey(); _builder.append(_key_1, "\t\t"); - _builder.append(".doInit(builder, grammarAccess);"); + _builder.append(".doInit(mappings, grammarAccess);"); _builder.newLineIfNotEmpty(); } }