Skip to content

Commit ead5e72

Browse files
Copilotmerks
authored andcommitted
Fix IllegalArgumentException with duplicate natures
Changed ProjectDescription.hasPrivateChanges() to use HashSet instead of Set.of() to handle duplicate natures gracefully. Set.of() throws IllegalArgumentException on duplicates, but HashSet tolerates them and correctly compares nature sets. Also added test case testDuplicateNatures() to ensure this regression doesn't occur again.
1 parent c1f0dd4 commit ead5e72

File tree

2 files changed

+26
-1
lines changed
  • resources

2 files changed

+26
-1
lines changed

resources/bundles/org.eclipse.core.resources/src/org/eclipse/core/internal/resources/ProjectDescription.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Collection;
2525
import java.util.Collections;
2626
import java.util.HashMap;
27+
import java.util.HashSet;
2728
import java.util.LinkedHashSet;
2829
import java.util.LinkedList;
2930
import java.util.List;
@@ -581,7 +582,7 @@ public boolean hasPrivateChanges(ProjectDescription description) {
581582
return true;
582583
}
583584
// has natures changed?
584-
if (!Set.of(natures).equals(Set.of(description.natures))) {
585+
if (!new HashSet<>(Arrays.asList(natures)).equals(new HashSet<>(Arrays.asList(description.natures)))) {
585586
return true;
586587
}
587588
// has buildspec changed?

resources/tests/org.eclipse.core.tests.resources/src/org/eclipse/core/tests/resources/NatureTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,30 @@ public void testInvalidRemovals() throws Throwable {
167167
assertHasEnabledNature(NATURE_SNOW);
168168
}
169169

170+
/**
171+
* Tests that duplicate natures in the project description don't cause
172+
* IllegalArgumentException. This is a regression test for a bug where
173+
* Set.of() was used which throws on duplicates.
174+
*/
175+
@Test
176+
public void testDuplicateNatures() throws Throwable {
177+
createInWorkspace(project);
178+
179+
// Set initial nature
180+
setNatures(project, new String[] { NATURE_SIMPLE }, false);
181+
assertHasEnabledNature(NATURE_SIMPLE);
182+
183+
// Try to set natures with a duplicate - this should not throw IllegalArgumentException
184+
// The duplicate should be handled gracefully (deduplication happens automatically)
185+
IProjectDescription desc = project.getDescription();
186+
desc.setNatureIds(new String[] { NATURE_SIMPLE, NATURE_SIMPLE });
187+
// This should not throw IllegalArgumentException when hasPrivateChanges is called
188+
project.setDescription(desc, IResource.KEEP_HISTORY, createTestMonitor());
189+
190+
// After deduplication, only one instance of the nature should remain
191+
assertHasEnabledNature(NATURE_SIMPLE);
192+
}
193+
170194
@Test
171195
public void testNatureLifecyle() throws Throwable {
172196
createInWorkspace(project);

0 commit comments

Comments
 (0)