-
-
Notifications
You must be signed in to change notification settings - Fork 344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix #4357: spoon doesn't handle paths containinig spaces correctly #4358
Changes from 7 commits
5987064
636bdd9
f6caec6
bb92e4f
f540fbb
61e78b7
af3b36c
f00ec5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,33 @@ | ||||||||||||||||||||||||||||
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 { | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Required change: Put this test in an existing test class instead of creating a new one. Due to the phrasing of the contract, I'd put this in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @slarse done. |
||||||||||||||||||||||||||||
// 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"); | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional change: This only indirectly verifies that we correctly handle spaces in classpath URLs. I'd suggest actually verifying that the reference to
Suggested change
Another alternative would be to test this with a unit test of |
||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import bar.Bar; | ||
|
||
public class Foo { | ||
Foo() { | ||
} | ||
|
||
Bar foo() { | ||
return new Bar(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if this is better or something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am unconvinced. Now that you remind me:
Path.of(URL)
will throw an exception if the URL is not pointing to something that lies on a filesystem or if no FileSystemProvider supporting the URL's protocol is present. For example to create a Path instance that points to a file located inside a Jar file (not the Jar itself), there has to be a FileSystem instance for the Jar file. In the case of the "jar:" protocol, Path.of(URL) will make a disk access and try to read the Jar content and fail if the file is not present or cannot be read. You can try this withjshell
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I doubt the existing code can handle files inside JARs either? URL-Decoding the
getPath()
would returnfile:/hello.jar!/hello.txt
which likely isn't handled correctly. I'd need to have a look at it, but failing early instead of maybe failing later on sounds like a plus to me?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this assumes utf8 encoding. How does this work on Windows when there's a non-ASCII character in the filepath?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@slarse Maybe someone with access to a windows box could test. In any case, URLs should use UTF-8. Qutoe taken from W3.org:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not fully convinced, that JDT accepts file paths referring to files inside a jar or other weird things correctly, so I am not sure raw URL decoding really is a benefit here over stricter Path.of validation/conversion.
Either way, I tried it out @slarse and the URLDecode javadoc states that it only uses the charset for percent-encoded data.
new URL("öäüß")
is not stored percent encoded, so the string is returned unchanged in the default JVM windows charset. The UTF-8 setting has no relevance for it, but if the URL does contain other percent encoded data it could maybe end up with a String using two encodings.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xzel23 I did not know that, good to know what the spec says (although I doubt all file systems follow that, a filepath is not necessarily a URI). I just don't trust Windows not to be the odd one out on.. everything.
@I-Al-Istannen Thanks for checking it out, that's 100% confusing :D