Skip to content

Commit

Permalink
fix: Stop adding duplicated modules when cloning them (#4747)
Browse files Browse the repository at this point in the history
  • Loading branch information
I-Al-Istannen authored Jun 12, 2022
1 parent 61b6213 commit 39bebb8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/spoon/reflect/factory/ModuleFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public CtModule getOrCreate(String moduleName) {
ctModule = factory.Core().createModule().setSimpleName(moduleName);
ctModule.setRootPackage(new CtModelImpl.CtRootPackage());
ctModule.setParent(getUnnamedModule());
getUnnamedModule().addModule(ctModule);
}

return ctModule;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/spoon/support/DefaultCoreFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ public CtTypeMemberWildcardImportReference createTypeMemberWildcardImportReferen
public CtModule createModule() {
CtModule module = new CtModuleImpl();
module.setFactory(getMainFactory());
this.getMainFactory().Module().getUnnamedModule().addModule(module);
module.setParent(this.getMainFactory().Module().getUnnamedModule());
return module;
}

Expand Down
13 changes: 13 additions & 0 deletions src/test/java/spoon/test/module/TestModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,17 @@ public void testModuleOverlappingPackages() {
);
assertNotNull(launcher.getFactory().Type().get("test.parent.nested.Foo"), "");
}

@Test
public void testModulePrintDoesNotDuplicate() {
// contract: Printing a module does not duplicate it in Model#getAllModules()
Launcher launcher = new Launcher();
launcher.getEnvironment().setComplianceLevel(9);
launcher.addInputResource(MODULE_RESOURCES_PATH + "/code-multiple-modules");
CtModel ctModel = launcher.buildModel();
assertEquals(3, ctModel.getAllModules().size());
//noinspection ResultOfMethodCallIgnored I wish it had no effect too, unspecified static analysis tool
launcher.getFactory().Module().getModule("bar").toString();
assertEquals(3, ctModel.getAllModules().size());
}
}

0 comments on commit 39bebb8

Please sign in to comment.