From 7295e6f9e1f6ace202ef8bc142adbc972241f8a9 Mon Sep 17 00:00:00 2001 From: rachid chami Date: Tue, 15 Sep 2020 22:37:04 +0100 Subject: [PATCH 1/5] Fixes folder path containing space --- .../web3j/codegen/unit/gen/CompilerClassLoader.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/codegen/src/main/java/org/web3j/codegen/unit/gen/CompilerClassLoader.java b/codegen/src/main/java/org/web3j/codegen/unit/gen/CompilerClassLoader.java index ff70510c1..f88c68c5b 100644 --- a/codegen/src/main/java/org/web3j/codegen/unit/gen/CompilerClassLoader.java +++ b/codegen/src/main/java/org/web3j/codegen/unit/gen/CompilerClassLoader.java @@ -59,12 +59,20 @@ private Optional compileClass(final String name) { File sourceFile = null; for (final URL url : urls) { - final File file = new File(url.getFile(), path + ".java"); + File file = new File(url.getFile(), path + ".java"); if (file.exists()) { sourceFile = file; break; } + + if (url.getFile().contains("%20")) { + file = new File(url.getFile().replace("%20", " "), path + ".java"); + if (file.exists()) { + sourceFile = file; + break; + } + } } if (sourceFile == null) { From 5836037126966bc1e7ea27bd67a3ce2206cf6a0f Mon Sep 17 00:00:00 2001 From: rachid chami Date: Tue, 15 Sep 2020 22:40:17 +0100 Subject: [PATCH 2/5] spotless --- .../java/org/web3j/codegen/unit/gen/CompilerClassLoader.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/codegen/src/main/java/org/web3j/codegen/unit/gen/CompilerClassLoader.java b/codegen/src/main/java/org/web3j/codegen/unit/gen/CompilerClassLoader.java index f88c68c5b..e3cda6381 100644 --- a/codegen/src/main/java/org/web3j/codegen/unit/gen/CompilerClassLoader.java +++ b/codegen/src/main/java/org/web3j/codegen/unit/gen/CompilerClassLoader.java @@ -64,9 +64,7 @@ private Optional compileClass(final String name) { if (file.exists()) { sourceFile = file; break; - } - - if (url.getFile().contains("%20")) { + } else if (url.getFile().contains("%20")) { file = new File(url.getFile().replace("%20", " "), path + ".java"); if (file.exists()) { sourceFile = file; From 29315abf85ac91d0d27e2efb86dcd8f78c79d606 Mon Sep 17 00:00:00 2001 From: rachid chami Date: Tue, 15 Sep 2020 23:28:03 +0100 Subject: [PATCH 3/5] replaceAll instead of replace when path contains %20 in CompilerClassLoader --- .../java/org/web3j/codegen/unit/gen/CompilerClassLoader.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codegen/src/main/java/org/web3j/codegen/unit/gen/CompilerClassLoader.java b/codegen/src/main/java/org/web3j/codegen/unit/gen/CompilerClassLoader.java index e3cda6381..edfb4ae56 100644 --- a/codegen/src/main/java/org/web3j/codegen/unit/gen/CompilerClassLoader.java +++ b/codegen/src/main/java/org/web3j/codegen/unit/gen/CompilerClassLoader.java @@ -65,7 +65,7 @@ private Optional compileClass(final String name) { sourceFile = file; break; } else if (url.getFile().contains("%20")) { - file = new File(url.getFile().replace("%20", " "), path + ".java"); + file = new File(url.getFile().replaceAll("%20", " "), path + ".java"); if (file.exists()) { sourceFile = file; break; From adb12e95907dc95aa67fab5a3b2604b749ab4b87 Mon Sep 17 00:00:00 2001 From: rachid chami Date: Tue, 22 Sep 2020 10:34:19 +0100 Subject: [PATCH 4/5] Use URLDecoder to decode path --- .../codegen/unit/gen/CompilerClassLoader.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/codegen/src/main/java/org/web3j/codegen/unit/gen/CompilerClassLoader.java b/codegen/src/main/java/org/web3j/codegen/unit/gen/CompilerClassLoader.java index edfb4ae56..084957cd0 100644 --- a/codegen/src/main/java/org/web3j/codegen/unit/gen/CompilerClassLoader.java +++ b/codegen/src/main/java/org/web3j/codegen/unit/gen/CompilerClassLoader.java @@ -15,6 +15,7 @@ import java.io.File; import java.io.IOException; import java.net.URL; +import java.net.URLDecoder; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; @@ -59,17 +60,16 @@ private Optional compileClass(final String name) { File sourceFile = null; for (final URL url : urls) { - File file = new File(url.getFile(), path + ".java"); + File file; + try { + file = new File(URLDecoder.decode(url.getPath(), "UTF-8"), path + ".java"); + } catch (Exception e) { + file = new File(url.getFile(), path + ".java"); + } if (file.exists()) { sourceFile = file; break; - } else if (url.getFile().contains("%20")) { - file = new File(url.getFile().replaceAll("%20", " "), path + ".java"); - if (file.exists()) { - sourceFile = file; - break; - } } } From aae8585a4c5158d335f121e2f02dd78f6aea6525 Mon Sep 17 00:00:00 2001 From: rachid chami Date: Tue, 22 Sep 2020 10:35:23 +0100 Subject: [PATCH 5/5] Adds CompilerClassLoaderTest --- .../unit/gen/CompilerClassLoaderTest.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 codegen/src/test/java/org/web3j/codegen/unit/gen/CompilerClassLoaderTest.java diff --git a/codegen/src/test/java/org/web3j/codegen/unit/gen/CompilerClassLoaderTest.java b/codegen/src/test/java/org/web3j/codegen/unit/gen/CompilerClassLoaderTest.java new file mode 100644 index 000000000..7b05c9be1 --- /dev/null +++ b/codegen/src/test/java/org/web3j/codegen/unit/gen/CompilerClassLoaderTest.java @@ -0,0 +1,79 @@ +/* + * Copyright 2020 Web3 Labs Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.web3j.codegen.unit.gen; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Objects; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import org.web3j.codegen.unit.gen.java.Setup; + +public class CompilerClassLoaderTest { + + String javaSourcePath = "org/com/test/contract/"; + String greeterSourceName = "Greeter"; + URL javaSourcesUrl = Objects.requireNonNull(Setup.class.getClassLoader().getResource("java")); + @TempDir static File temp; + + @Test + public void compileClassTest() throws ClassNotFoundException { + CompilerClassLoader compilerClassLoader = new CompilerClassLoader(temp, javaSourcesUrl); + Assertions.assertNotEquals( + null, + compilerClassLoader.loadClass( + (javaSourcePath + greeterSourceName).replace(File.separator, "."))); + } + + @Test + public void compileClassWithPathContainingSpacesTest() + throws ClassNotFoundException, IOException { + File tempWithSpaces = new File(temp, "With Spaces"); + File greeterSource = + new File( + Paths.get( + javaSourcesUrl.getPath(), + javaSourcePath, + greeterSourceName + ".java") + .toString()); + Assertions.assertTrue(greeterSource.exists()); + + String javaPathWithSpaces = tempWithSpaces.toString() + File.separator + "java"; + File sourcePathWithSpaces = new File(javaPathWithSpaces + File.separator + javaSourcePath); + Assertions.assertTrue(sourcePathWithSpaces.mkdirs()); + + Files.copy( + greeterSource.toPath(), + Paths.get( + sourcePathWithSpaces.toPath() + + File.separator + + greeterSourceName + + ".java")); + File greeterSourceWithSpaces = new File(sourcePathWithSpaces, greeterSourceName + ".java"); + Assertions.assertTrue(greeterSourceWithSpaces.exists()); + + CompilerClassLoader compilerClassLoader = + new CompilerClassLoader( + tempWithSpaces, new File(javaPathWithSpaces).toURI().toURL()); + Assertions.assertNotEquals( + null, + compilerClassLoader.loadClass( + (javaSourcePath + greeterSourceName).replace(File.separator, "."))); + } +}