From 7c5eb534cbbf3959dd8fd0c66b39220e9e5a921d 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 | 1 - .../spoon/test/compilation/CompilationTest.java | 15 +++++++++++++++ src/test/resources/simple-module/module-info.java | 3 +++ .../simple-module/spoonmod/TestClass.java | 5 +++++ 4 files changed, 23 insertions(+), 1 deletion(-) 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 c4f03766dab..480de21aeb0 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTBatchCompiler.java @@ -91,7 +91,6 @@ public CompilationUnit[] getCompilationUnits() { } else { lastSlash += 1; } - if (this.module == null) { compilationUnit.module = CharOperation.subarray(modulePath, lastSlash, modulePath.length); } 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