From 5ff85035ca7f894389a77989a09bed9adfeb4d5c Mon Sep 17 00:00:00 2001 From: Noah Santschi-Cooney Date: Tue, 25 Aug 2020 19:08:33 +0100 Subject: [PATCH 1/4] Fetching compilation unit module from the JDT compiler --- src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java b/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java index 196d031165d..d80bff647cf 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java @@ -91,8 +91,7 @@ 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); + compilationUnit.module = this.module.name(); pathToModName.put(String.valueOf(modulePath), compilationUnit.module); } } else { From 1f9876d2cc914861340b974d8931e65b4c857c4c Mon Sep 17 00:00:00 2001 From: Noah Santschi-Cooney Date: Wed, 26 Aug 2020 00:37:33 +0100 Subject: [PATCH 2/4] Fallback to resolving compilation unit from filename as previously --- .../spoon/support/compiler/jdt/JDTBatchCompiler.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java b/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java index d80bff647cf..a00df752722 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java @@ -91,7 +91,14 @@ public CompilationUnit[] getCompilationUnits() { } else { lastSlash += 1; } - compilationUnit.module = this.module.name(); + + 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 { From 00485cd5d5cea0835bca7522e3abbd6466133d30 Mon Sep 17 00:00:00 2001 From: Noah Santschi-Cooney Date: Tue, 1 Sep 2020 12:41:47 +0100 Subject: [PATCH 3/4] Added CompilationUnit module test --- .../java/spoon/test/compilation/CompilationTest.java | 12 ++++++++++++ src/test/resources/simple-module/module-info.java | 3 +++ .../resources/simple-module/spoonmod/TestClass.java | 5 +++++ 3 files changed, 20 insertions(+) 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/test/java/spoon/test/compilation/CompilationTest.java b/src/test/java/spoon/test/compilation/CompilationTest.java index dda20f9c79e..03b20da8350 100644 --- a/src/test/java/spoon/test/compilation/CompilationTest.java +++ b/src/test/java/spoon/test/compilation/CompilationTest.java @@ -260,6 +260,18 @@ public CompilationUnit[] getCompilationUnits() { } + @Test + public void testModuleResolution() throws InterruptedException { + Launcher launcher = new Launcher(); + + launcher.getEnvironment().setComplianceLevel(9); + launcher.getEnvironment().setSpoonProgress(new ProgressLogger(launcher.getEnvironment())); + 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 From 6b30d183c750417c52163e10a0af34cfe00f18b3 Mon Sep 17 00:00:00 2001 From: Noah Santschi-Cooney Date: Tue, 1 Sep 2020 14:58:32 +0100 Subject: [PATCH 4/4] Added natural language contract to module resolution test --- src/test/java/spoon/test/compilation/CompilationTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/test/java/spoon/test/compilation/CompilationTest.java b/src/test/java/spoon/test/compilation/CompilationTest.java index 03b20da8350..50a87c2a7af 100644 --- a/src/test/java/spoon/test/compilation/CompilationTest.java +++ b/src/test/java/spoon/test/compilation/CompilationTest.java @@ -262,10 +262,13 @@ 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.getEnvironment().setSpoonProgress(new ProgressLogger(launcher.getEnvironment())); launcher.addInputResource("./src/test/resources/simple-module"); launcher.buildModel();