From 5987064d0968773d3e920b69fc2c6539782c2622 Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Thu, 16 Dec 2021 08:43:09 +0100 Subject: [PATCH 1/8] fix #4357: spoon doesn't handle paths containinig spaces correctly --- src/main/java/spoon/support/StandardEnvironment.java | 4 +++- .../visitor/java/JavaReflectionTreeBuilderTest.java | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/spoon/support/StandardEnvironment.java b/src/main/java/spoon/support/StandardEnvironment.java index 30008b2f8e7..ce76b43117d 100644 --- a/src/main/java/spoon/support/StandardEnvironment.java +++ b/src/main/java/spoon/support/StandardEnvironment.java @@ -43,7 +43,9 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; +import java.net.URLDecoder; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -418,7 +420,7 @@ public void setInputClassLoader(ClassLoader aClassLoader) { if (onlyFileURLs) { List classpath = new ArrayList<>(); for (URL url : urls) { - classpath.add(url.getPath()); + classpath.add(URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8)); } setSourceClasspath(classpath.toArray(new String[0])); } else { diff --git a/src/test/java/spoon/support/visitor/java/JavaReflectionTreeBuilderTest.java b/src/test/java/spoon/support/visitor/java/JavaReflectionTreeBuilderTest.java index 66a1a914765..a34c0d301fb 100644 --- a/src/test/java/spoon/support/visitor/java/JavaReflectionTreeBuilderTest.java +++ b/src/test/java/spoon/support/visitor/java/JavaReflectionTreeBuilderTest.java @@ -28,6 +28,8 @@ import java.lang.annotation.Retention; import java.net.CookieManager; import java.net.URLClassLoader; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.time.format.TextStyle; import java.util.ArrayList; import java.util.Arrays; @@ -603,10 +605,11 @@ public void testPartialShadow() { @Test public void testInnerClassWithConstructorParameterAnnotated() { Launcher launcher = new Launcher(); - launcher.addInputResource(JavaReflectionTreeBuilderTest.class + launcher.addInputResource(URLDecoder.decode(JavaReflectionTreeBuilderTest.class .getClassLoader() .getResource("annotated-parameter-on-nested-class-constructor/Caller.java") - .getPath()); + .getPath(), + StandardCharsets.UTF_8)); launcher.getEnvironment().setSourceClasspath( new String[]{ "src/test/resources" From 636bdd90042bad78077bdcf6e0d4139b0e8d18eb Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Mon, 20 Dec 2021 13:48:11 +0100 Subject: [PATCH 2/8] add testcase --- .../spoon/test/issue4357/TestIssue4357.java | 27 +++++++++++++++++++ src/test/resources/path with spaces/Foo.java | 4 +++ 2 files changed, 31 insertions(+) create mode 100644 src/test/java/spoon/test/issue4357/TestIssue4357.java create mode 100644 src/test/resources/path with spaces/Foo.java diff --git a/src/test/java/spoon/test/issue4357/TestIssue4357.java b/src/test/java/spoon/test/issue4357/TestIssue4357.java new file mode 100644 index 00000000000..2b36e086134 --- /dev/null +++ b/src/test/java/spoon/test/issue4357/TestIssue4357.java @@ -0,0 +1,27 @@ +package spoon.test.issue4357; + +import org.junit.jupiter.api.Test; +import spoon.Launcher; + +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.Paths; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +public class TestIssue4357 { + + @Test + public void testClasspathURLWithSpaces() throws MalformedURLException { + // contract: launcher can handle spaces in classpath URL + Launcher launcher = new Launcher(); + URL[] classpath = { + Paths.get("src/test/resources/path with spaces").toUri().toURL() + }; + ClassLoader classLoader = new URLClassLoader(classpath); + launcher.getEnvironment().setInputClassLoader(classLoader); + launcher.getEnvironment().setNoClasspath(false); + assertDoesNotThrow(() -> launcher.buildModel()); + } +} diff --git a/src/test/resources/path with spaces/Foo.java b/src/test/resources/path with spaces/Foo.java new file mode 100644 index 00000000000..51356587a87 --- /dev/null +++ b/src/test/resources/path with spaces/Foo.java @@ -0,0 +1,4 @@ +class Foo { + void bar() { + } +} From f6caec6a6d77765f62bbbfed93586b6a0be9e632 Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Wed, 29 Dec 2021 09:16:09 +0100 Subject: [PATCH 3/8] change the test case to make sure that the model cannot be built without the fix --- .../java/spoon/test/issue4357/TestIssue4357.java | 14 ++++++++++---- src/test/resources/path with spaces/Foo.java | 4 ---- .../resources/path with spaces/lib/bar/Bar.java | 9 +++++++++ 3 files changed, 19 insertions(+), 8 deletions(-) delete mode 100644 src/test/resources/path with spaces/Foo.java create mode 100644 src/test/resources/path with spaces/lib/bar/Bar.java diff --git a/src/test/java/spoon/test/issue4357/TestIssue4357.java b/src/test/java/spoon/test/issue4357/TestIssue4357.java index 2b36e086134..910a9762025 100644 --- a/src/test/java/spoon/test/issue4357/TestIssue4357.java +++ b/src/test/java/spoon/test/issue4357/TestIssue4357.java @@ -2,13 +2,14 @@ import org.junit.jupiter.api.Test; import spoon.Launcher; +import spoon.reflect.CtModel; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Paths; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertTrue; public class TestIssue4357 { @@ -17,11 +18,16 @@ public void testClasspathURLWithSpaces() throws MalformedURLException { // contract: launcher can handle spaces in classpath URL Launcher launcher = new Launcher(); URL[] classpath = { - Paths.get("src/test/resources/path with spaces").toUri().toURL() + Paths.get("./src/test/resources/path with spaces/lib/bar.jar").toAbsolutePath().toUri().toURL() }; + launcher.getEnvironment().setNoClasspath(false); + launcher.getEnvironment().setShouldCompile(true); ClassLoader classLoader = new URLClassLoader(classpath); launcher.getEnvironment().setInputClassLoader(classLoader); - launcher.getEnvironment().setNoClasspath(false); - assertDoesNotThrow(() -> launcher.buildModel()); + launcher.addInputResource(Paths.get("./src/test/resources/path with spaces/Foo.java").toAbsolutePath().toString()); + CtModel model = launcher.buildModel(); + + assertTrue(model.getAllTypes().stream().anyMatch(ct -> ct.getQualifiedName().equals("Foo")), + "CtTxpe 'Foo' not present in model"); } } diff --git a/src/test/resources/path with spaces/Foo.java b/src/test/resources/path with spaces/Foo.java deleted file mode 100644 index 51356587a87..00000000000 --- a/src/test/resources/path with spaces/Foo.java +++ /dev/null @@ -1,4 +0,0 @@ -class Foo { - void bar() { - } -} diff --git a/src/test/resources/path with spaces/lib/bar/Bar.java b/src/test/resources/path with spaces/lib/bar/Bar.java new file mode 100644 index 00000000000..6a063464533 --- /dev/null +++ b/src/test/resources/path with spaces/lib/bar/Bar.java @@ -0,0 +1,9 @@ +package bar; + +public class Bar { + public Bar() { + } + + public void bar() { + } +} From bb92e4f3381d72188a2d895f63524b1fdf848d40 Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Wed, 29 Dec 2021 09:30:40 +0100 Subject: [PATCH 4/8] change the test case to make sure that the model cannot be built without the fix --- src/test/resources/path with spaces/Foo.java | 10 ++++++++++ src/test/resources/path with spaces/lib/bar.jar | Bin 0 -> 893 bytes 2 files changed, 10 insertions(+) create mode 100644 src/test/resources/path with spaces/Foo.java create mode 100644 src/test/resources/path with spaces/lib/bar.jar diff --git a/src/test/resources/path with spaces/Foo.java b/src/test/resources/path with spaces/Foo.java new file mode 100644 index 00000000000..72af540cdc3 --- /dev/null +++ b/src/test/resources/path with spaces/Foo.java @@ -0,0 +1,10 @@ +import bar.Bar; + +public class Foo { + Foo() { + } + + Bar foo() { + return new Bar(); + } +} diff --git a/src/test/resources/path with spaces/lib/bar.jar b/src/test/resources/path with spaces/lib/bar.jar new file mode 100644 index 0000000000000000000000000000000000000000..5f3d52ba50a12f32f17a50fd8cd29cdc4328fc40 GIT binary patch literal 893 zcmWIWW@Zs#;Nak35cixL%zy+q8CV#6T|*poJ^kGD|D9rBU}gyLX6FE@V1gyhz*`&lGeS|An&{Xh1RX8OU>18FBC2D*4ob&VX5B2cU+4Ss@mWJ=ClRjY{=R9@v z!V0`}f>u0x)Y8RRDK$%>PFFl;jgGxB>k?tLz|*W?M;+elw$c)4pg+`6kWfW;6fcsM z$vKI|#lF6le1{x(WS-v=ICL>+88g=>#sv(WT#k&3?(=F-(NGDR@ouZX~EG$h{=F~E;FKVV!&Wjr-_j5GupS8i^nSOHn z@(%67(&hXY=1P|(@I-Aq{L<+1-;?fZnf?c@SFJg=q4a$%BPbP25}FdT3FwuVV9zo# zi7=ohB3KFmB_dP+PZgjf72u7k6**!-NeBUKflRnoqyz;rfr|lm0zjB>5Xgii2ZWuV z1c4lHpag*ckAX~(4p{O)b}cAgkplq~uLuyvh|md+pF literal 0 HcmV?d00001 From f540fbbfe9c6c63fa54cdacd03cde4e36da16ec1 Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Wed, 29 Dec 2021 09:41:13 +0100 Subject: [PATCH 5/8] recompile with JDK 8 compatibility --- src/test/resources/path with spaces/lib/bar.jar | Bin 893 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/test/resources/path with spaces/lib/bar.jar diff --git a/src/test/resources/path with spaces/lib/bar.jar b/src/test/resources/path with spaces/lib/bar.jar deleted file mode 100644 index 5f3d52ba50a12f32f17a50fd8cd29cdc4328fc40..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 893 zcmWIWW@Zs#;Nak35cixL%zy+q8CV#6T|*poJ^kGD|D9rBU}gyLX6FE@V1gyhz*`&lGeS|An&{Xh1RX8OU>18FBC2D*4ob&VX5B2cU+4Ss@mWJ=ClRjY{=R9@v z!V0`}f>u0x)Y8RRDK$%>PFFl;jgGxB>k?tLz|*W?M;+elw$c)4pg+`6kWfW;6fcsM z$vKI|#lF6le1{x(WS-v=ICL>+88g=>#sv(WT#k&3?(=F-(NGDR@ouZX~EG$h{=F~E;FKVV!&Wjr-_j5GupS8i^nSOHn z@(%67(&hXY=1P|(@I-Aq{L<+1-;?fZnf?c@SFJg=q4a$%BPbP25}FdT3FwuVV9zo# zi7=ohB3KFmB_dP+PZgjf72u7k6**!-NeBUKflRnoqyz;rfr|lm0zjB>5Xgii2ZWuV z1c4lHpag*ckAX~(4p{O)b}cAgkplq~uLuyvh|md+pF From 61e78b7229378290f507452e4612ceaf75240f32 Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Wed, 29 Dec 2021 09:56:11 +0100 Subject: [PATCH 6/8] recompile with JDK 8 compatibility --- src/test/resources/path with spaces/lib/bar.jar | Bin 0 -> 893 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/test/resources/path with spaces/lib/bar.jar diff --git a/src/test/resources/path with spaces/lib/bar.jar b/src/test/resources/path with spaces/lib/bar.jar new file mode 100644 index 0000000000000000000000000000000000000000..3e0c344d2e2f4ec15dc8127742e5bcde79f2e521 GIT binary patch literal 893 zcmWIWW@Zs#;Nak35b&KF%zy+q8CV#6T|*poJ^kGD|D9rBU}gyLX6FE@V1gga_Pc4>ax;Xe9LPi~TcFh}SARMm(djCE6X zOn17|dh_nhn>W8d|H)+FZr{KfeSq`fA!Q!p%E^U#-QIh`XZmF|ZTINj5-0TL;}ew! z!Yz|R_e>H!7&$%NS0d-K)B9PgFI^2>HAgafW@&-OY^{s4uLRh|pXsqx;`oyBKVYv)j&ccalT<0#+q(>~wT<+pPE@!mV_gXH0Bd%v@SQh~YWwGW$sUU>=j zEF+T$18O3Ir4UdeLIvo#0pw@MdKLNwWf>3Q*D+96$hE C=;prw literal 0 HcmV?d00001 From af3b36c6ce4c0217df82db097d7901c4c0219f0a Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Wed, 29 Dec 2021 09:58:03 +0100 Subject: [PATCH 7/8] remove source file --- src/test/resources/path with spaces/lib/bar/Bar.java | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 src/test/resources/path with spaces/lib/bar/Bar.java diff --git a/src/test/resources/path with spaces/lib/bar/Bar.java b/src/test/resources/path with spaces/lib/bar/Bar.java deleted file mode 100644 index 6a063464533..00000000000 --- a/src/test/resources/path with spaces/lib/bar/Bar.java +++ /dev/null @@ -1,9 +0,0 @@ -package bar; - -public class Bar { - public Bar() { - } - - public void bar() { - } -} From f00ec5dc4fa412b2d56c5c2d7172004a0b58b919 Mon Sep 17 00:00:00 2001 From: Axel Howind Date: Wed, 5 Jan 2022 09:36:44 +0100 Subject: [PATCH 8/8] move test to LauncherTest --- src/test/java/spoon/LauncherTest.java | 21 ++++++++++++ .../spoon/test/issue4357/TestIssue4357.java | 33 ------------------- 2 files changed, 21 insertions(+), 33 deletions(-) delete mode 100644 src/test/java/spoon/test/issue4357/TestIssue4357.java diff --git a/src/test/java/spoon/LauncherTest.java b/src/test/java/spoon/LauncherTest.java index 7c36843ed8a..b4c05cd76fc 100644 --- a/src/test/java/spoon/LauncherTest.java +++ b/src/test/java/spoon/LauncherTest.java @@ -25,8 +25,12 @@ import spoon.support.compiler.VirtualFile; import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; @@ -139,4 +143,21 @@ public void testPrettyPrintWithVirtualFileInput() throws Exception { assertDoesNotThrow(() -> launcher.prettyprint()); } + + @Test + public void testClasspathURLWithSpaces() throws MalformedURLException { + // contract: launcher can handle spaces in classpath URL + Launcher launcher = new Launcher(); + URL[] classpath = { + Paths.get("./src/test/resources/path with spaces/lib/bar.jar").toAbsolutePath().toUri().toURL() + }; + launcher.getEnvironment().setNoClasspath(false); + launcher.getEnvironment().setShouldCompile(true); + ClassLoader classLoader = new URLClassLoader(classpath); + launcher.getEnvironment().setInputClassLoader(classLoader); + launcher.addInputResource(Paths.get("./src/test/resources/path with spaces/Foo.java").toAbsolutePath().toString()); + CtModel model = launcher.buildModel(); + + assertTrue("CtTxpe 'Foo' not present in model", model.getAllTypes().stream().anyMatch(ct -> ct.getQualifiedName().equals("Foo"))); + } } diff --git a/src/test/java/spoon/test/issue4357/TestIssue4357.java b/src/test/java/spoon/test/issue4357/TestIssue4357.java deleted file mode 100644 index 910a9762025..00000000000 --- a/src/test/java/spoon/test/issue4357/TestIssue4357.java +++ /dev/null @@ -1,33 +0,0 @@ -package spoon.test.issue4357; - -import org.junit.jupiter.api.Test; -import spoon.Launcher; -import spoon.reflect.CtModel; - -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLClassLoader; -import java.nio.file.Paths; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class TestIssue4357 { - - @Test - public void testClasspathURLWithSpaces() throws MalformedURLException { - // contract: launcher can handle spaces in classpath URL - Launcher launcher = new Launcher(); - URL[] classpath = { - Paths.get("./src/test/resources/path with spaces/lib/bar.jar").toAbsolutePath().toUri().toURL() - }; - launcher.getEnvironment().setNoClasspath(false); - launcher.getEnvironment().setShouldCompile(true); - ClassLoader classLoader = new URLClassLoader(classpath); - launcher.getEnvironment().setInputClassLoader(classLoader); - launcher.addInputResource(Paths.get("./src/test/resources/path with spaces/Foo.java").toAbsolutePath().toString()); - CtModel model = launcher.buildModel(); - - assertTrue(model.getAllTypes().stream().anyMatch(ct -> ct.getQualifiedName().equals("Foo")), - "CtTxpe 'Foo' not present in model"); - } -}