diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/ClasspathComputer.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/ClasspathComputer.java index 9efcf8edc2..669c6a3280 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/ClasspathComputer.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/ClasspathComputer.java @@ -18,10 +18,10 @@ import java.util.Collections; import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Objects; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -72,10 +72,10 @@ public static void setClasspath(IProject project, IPluginModelBase model) throws public static IClasspathEntry[] getClasspath(IProject project, IPluginModelBase model, Map sourceLibraryMap, boolean clear, boolean overrideCompliance) throws CoreException { IJavaProject javaProject = JavaCore.create(project); - IClasspathEntry[] originalClasspath = clear ? new IClasspathEntry[0] : javaProject.getRawClasspath(); + List originalClasspath = clear ? List.of() : Arrays.asList(javaProject.getRawClasspath()); ClasspathConfiguration context = new ClasspathConfiguration(model, javaProject, hasTestPluginName(project), getClasspathAttributes(project, model), - mapFirstSeenByPath(Arrays.stream(originalClasspath)), new ArrayList<>()); + mapFirstSeenByPath(originalClasspath.stream()), new ArrayList<>()); // add JRE and set compliance options String ee = getExecutionEnvironment(model.getBundleDescription()); @@ -97,24 +97,23 @@ public static IClasspathEntry[] getClasspath(IProject project, IPluginModelBase return entries; } - private static IClasspathEntry[] collectInOriginalOrder(IClasspathEntry[] originalClasspath, + private static IClasspathEntry[] collectInOriginalOrder(List originalClasspath, List reloaded) { // preserve original entries which eventually weren't reloaded - var reloadedPlusOriginal = Stream.concat(reloaded.stream(), Stream.of(originalClasspath)); - Map resultByPath = mapFirstSeenByPath(reloadedPlusOriginal); - List result = new ArrayList<>(resultByPath.size()); - // using the original order, collect the resulting entries, and remove - // from the map to prevent from any duplicates (even original ones) - Arrays.stream(originalClasspath).map(e -> resultByPath.remove(pathWithoutEE(e.getPath()))) - .filter(Objects::nonNull).forEachOrdered(result::add); + Map resultingReloadedByPath = mapFirstSeenByPath(reloaded.stream()); + List result = new ArrayList<>(originalClasspath); + result.replaceAll(e -> { + IClasspathEntry replacement = resultingReloadedByPath.remove(pathWithoutEE(e.getPath())); + return replacement != null ? replacement : e; + }); // using the order of reloading, append new entries (in the map still) - reloaded.stream().filter(e -> resultByPath.remove(pathWithoutEE(e.getPath())) != null) - .forEachOrdered(result::add); + result.addAll(resultingReloadedByPath.values()); return result.toArray(IClasspathEntry[]::new); } private static Map mapFirstSeenByPath(Stream entryStream) { - return entryStream.collect(Collectors.toMap(e -> pathWithoutEE(e.getPath()), e -> e, (first, dupe) -> first)); + return entryStream.collect( + Collectors.toMap(e -> pathWithoutEE(e.getPath()), e -> e, (first, dupe) -> first, LinkedHashMap::new)); } private static IPath pathWithoutEE(IPath path) {