Skip to content

Commit f68fbad

Browse files
committed
Flatten incoming preference map so as to perform merging correctly.
- Add test case - Also revise JAXP limits based on pre JDK 24 values. Signed-off-by: Roland Grunberg <rgrunber@redhat.com>
1 parent 8926155 commit f68fbad

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

.mvn/jvm.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
-Djdk.xml.maxGeneralEntitySizeLimit=1000000 -Djdk.xml.totalEntitySizeLimit=1000000
1+
-Djdk.xml.maxGeneralEntitySizeLimit=0 -Djdk.xml.totalEntitySizeLimit=0

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/MapFlattener.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414

1515
import java.lang.reflect.Type;
1616
import java.util.Arrays;
17+
import java.util.HashMap;
1718
import java.util.List;
1819
import java.util.Map;
1920
import java.util.Set;
2021
import java.util.stream.Collectors;
22+
import java.util.stream.Stream;
2123

2224
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
2325

@@ -171,4 +173,17 @@ public static void setValue(Map<String, Object> configuration, String key, Objec
171173
configuration.put(key, value);
172174
}
173175

176+
public static void putAll(Map<String, Object> currentConfiguration, Map<String, Object> newConfiguration) {
177+
Map<String, Object> flattenedNewConfig = newConfiguration.entrySet().stream().flatMap(MapFlattener::flatten).collect(HashMap::new, (m, e) -> m.put(e.getKey(), e.getValue()), HashMap::putAll);
178+
currentConfiguration.putAll(flattenedNewConfig);
179+
}
180+
181+
private static Stream<Map.Entry<String, Object>> flatten(Map.Entry<String, Object> entry) {
182+
if (entry.getValue() instanceof Map<?, ?>) {
183+
Map<String, Object> nested = (Map<String, Object>) entry.getValue();
184+
return nested.entrySet().stream().map(e -> Map.entry(entry.getKey() + "." + e.getKey(), e.getValue())).flatMap(MapFlattener::flatten);
185+
}
186+
return Stream.of(entry);
187+
}
188+
174189
}

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/PreferenceManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import org.eclipse.jdt.ls.core.internal.StatusFactory;
5858
import org.eclipse.jdt.ls.core.internal.handlers.BaseDiagnosticsHandler;
5959
import org.eclipse.jdt.ls.core.internal.handlers.FormatterHandler;
60+
import org.eclipse.jdt.ls.core.internal.handlers.MapFlattener;
6061
import org.eclipse.jdt.ls.core.internal.preferences.Preferences.SearchScope;
6162
import org.eclipse.jface.text.templates.Template;
6263
import org.eclipse.jface.text.templates.TemplateContextType;
@@ -225,7 +226,7 @@ private static boolean updateTemplate(String templateId, String content) {
225226
*/
226227
public void update(Map<String, Object> preferences) {
227228
Map<String, Object> currMap = this.preferences.asMap();
228-
currMap.putAll(preferences);
229+
MapFlattener.putAll(currMap, preferences);
229230
Preferences newPreferences = Preferences.createFrom(currMap);
230231
// Preserve preferences stored here that are never sent after initialization
231232
newPreferences.setRootPaths(this.preferences.getRootPaths());

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/MapFlattenerTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,22 @@ public void testGetValue() throws Exception {
154154
assertSame(bottom, getValue(config, "java.bottom"));
155155
}
156156

157+
@Test
158+
public void testMergeMaps() throws Exception {
159+
Map<String, Object> result = new HashMap<>();
160+
result.put("java.configuration.runtimes", new Map[] { Map.of("name", "JavaSE-1", "path", "/path/to/java-1") });
161+
assertEquals(1, result.size());
162+
163+
assertEquals("JavaSE-1", ((Map[]) getValue(result, "java.configuration.runtimes"))[0].get("name"));
164+
assertEquals("/path/to/java-1", ((Map[]) getValue(result, "java.configuration.runtimes"))[0].get("path"));
165+
166+
Map<String, Object> updatedSettings = new HashMap<>();
167+
updatedSettings.put("java", Map.of("configuration", Map.of("runtimes",
168+
new Map[] { Map.of("name", "JavaSE-99", "path", "/path/to/java-99") })));
169+
170+
MapFlattener.putAll(result, updatedSettings);
171+
assertEquals("JavaSE-99", ((Map[]) getValue(result, "java.configuration.runtimes"))[0].get("name"));
172+
assertEquals("/path/to/java-99", ((Map[]) getValue(result, "java.configuration.runtimes"))[0].get("path"));
173+
}
174+
157175
}

0 commit comments

Comments
 (0)