From 896eeace4215844543f4d5988a7b5f04cbe2ae07 Mon Sep 17 00:00:00 2001 From: Noah Santschi-Cooney Date: Tue, 1 Sep 2020 15:32:56 +0100 Subject: [PATCH] fix: fetchin module from the JDT compiler (#3549) --- .../support/compiler/jdt/JDTBatchCompiler.java | 10 ++++++++-- .../spoon/test/compilation/CompilationTest.java | 15 +++++++++++++++ src/test/resources/simple-module/module-info.java | 3 +++ .../simple-module/spoonmod/TestClass.java | 5 +++++ 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/test/resources/simple-module/module-info.java create mode 100644 src/test/resources/simple-module/spoonmod/TestClass.java diff --git a/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java b/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java index 196d031165d..a00df752722 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java @@ -91,8 +91,14 @@ public CompilationUnit[] getCompilationUnits() { } else { lastSlash += 1; } - //TODO the module name parsed by JDK compiler is in `this.modNames` - compilationUnit.module = CharOperation.subarray(modulePath, lastSlash, modulePath.length); + + if (this.module == null) { + compilationUnit.module = CharOperation.subarray(modulePath, lastSlash, modulePath.length); + } else { + //TODO the module name parsed by JDK compiler is in `this.modNames`, consider using that instead? + compilationUnit.module = this.module.name(); + } + pathToModName.put(String.valueOf(modulePath), compilationUnit.module); } } else { diff --git a/src/test/java/spoon/test/compilation/CompilationTest.java b/src/test/java/spoon/test/compilation/CompilationTest.java index dda20f9c79e..50a87c2a7af 100644 --- a/src/test/java/spoon/test/compilation/CompilationTest.java +++ b/src/test/java/spoon/test/compilation/CompilationTest.java @@ -260,6 +260,21 @@ public CompilationUnit[] getCompilationUnits() { } + @Test + public void testModuleResolution() throws InterruptedException { + // contract: module name is set from the info extract from the module-info.java file + // when such file exists + Launcher launcher = new Launcher(); + + // contract: ComplianceLevel must be >=9 to build a model from an input resource + // that contains module-info.java file(s) + launcher.getEnvironment().setComplianceLevel(9); + launcher.addInputResource("./src/test/resources/simple-module"); + launcher.buildModel(); + + assertTrue(launcher.getModel().getAllModules().iterator().next().getSimpleName().equals("spoonmod")); + } + @Test public void testFilterResourcesDir() { // shows how to filter input java dir diff --git a/src/test/resources/simple-module/module-info.java b/src/test/resources/simple-module/module-info.java new file mode 100644 index 00000000000..30e6051068d --- /dev/null +++ b/src/test/resources/simple-module/module-info.java @@ -0,0 +1,3 @@ +module spoonmod { + +} \ No newline at end of file diff --git a/src/test/resources/simple-module/spoonmod/TestClass.java b/src/test/resources/simple-module/spoonmod/TestClass.java new file mode 100644 index 00000000000..dac04ebfb51 --- /dev/null +++ b/src/test/resources/simple-module/spoonmod/TestClass.java @@ -0,0 +1,5 @@ +package spoonmod; + +public class TestClass { + public TestClass() {} +} \ No newline at end of file