Skip to content
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: Fix getOriginalSourceFragment and getRawJavadoc for zip file inputs #4850

Merged
merged 3 commits into from
Sep 11, 2022

Conversation

I-Al-Istannen
Copy link
Collaborator

Description of the problem

Previously, zip files were flattened and stored as <name><random number>.java. This caused all methods that retrieve code
at runtime, such as getOriginalSourceFragment or getRawJavadoc, to fail.

Chosen solution

This patch keeps the structure of the zip file intact while extracting it into a temporary folder. Consequently, the aforementioned methods are now able to find the source files.

There were multiple ways to proceed here. As JDT needs a copy anyways, I chose to proactively copy the files to disk instead of storing them in memory and copying them when the sources are prepared. Then I set isActualFile to true, tricking the relevant parts of spoon into treating the zip file as a regular file (which it mostly is by now, as it has an on-disk representation). The old constructor is kept in place (but will cause problems) as the class is public. Nobody should have used it in the first place, so maybe we could also delete it?

Instead of changing the folder structure we could change the compilation unit name named passed to JDT, This sounds a bit deadly and is also absolutely not compatible with existing code looking at the compilation unit layer. Every compilation unit would be named /tmp/MyClass381891.java or similiar, which is not that helpful.

Further possible features

Allow the user to specify where the temporary files should be stored when creating a ZipFolder. This can be easily retrofitted in the future though.

Previously, zip files were flattened and stored as
`<name><random number>.java`. This caused all methods that retrieve code
at runtime, such as getOriginalSourceFragment or getRawJavadoc, to fail.

This patch keeps the structure of the zip file intact while extracting
it into a temporary folder. Consequently, the aforementioned methods are
now able to find the source files.
Copy link
Collaborator Author

@I-Al-Istannen I-Al-Istannen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could add the reviews here as comments, but they are probably a bit too meta.

src/main/java/spoon/support/Internal.java Show resolved Hide resolved
output.reset();
try (FileSystem zip = FileSystems.newFileSystem(URI.create("jar:" + file.toURI()), Map.of())) {
Path tempFolder = Files.createTempDirectory("spoon-zip-file-proxy");
// Try to clean up - not guaranteed to work!
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't guaranteed before as well and depends on the mood of the JVM. File#deleteOnExit() which was used before in Sources also registers a shutdown hook.

zipInput.transferTo(output);
files.add(new ZipFile(this, entry.getName(), output.toByteArray()));
output.reset();
try (FileSystem zip = FileSystems.newFileSystem(URI.create("jar:" + file.toURI()), Map.of())) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be made nicer in Java 13+, but for now we are stuck with it. URI.toString() should round-trip, so this should be safe for arbitrary paths.

}
} catch (Exception e) {
Launcher.LOGGER.error(e.getMessage(), e);
Launcher.LOGGER.error("Error copying zip file contents", e);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOGGER.error crashes with null messages, which is not ideal if you are trying to log such a message.

@I-Al-Istannen I-Al-Istannen changed the title wip: fix: Fix getOriginalSourceFragment and getRawJavadoc for zip file inputs review: fix: Fix getOriginalSourceFragment and getRawJavadoc for zip file inputs Aug 19, 2022
Copy link
Collaborator

@SirYwell SirYwell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MartinWitt MartinWitt changed the title review: fix: Fix getOriginalSourceFragment and getRawJavadoc for zip file inputs fix: Fix getOriginalSourceFragment and getRawJavadoc for zip file inputs Sep 11, 2022
@MartinWitt MartinWitt merged commit f3ae9b5 into INRIA:master Sep 11, 2022
@MartinWitt
Copy link
Collaborator

Thanks @I-Al-Istannen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants