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

feature: add support for Jar files analysis with JarLauncher (decompile then build AST with spoon) #2455

Merged
merged 28 commits into from
Sep 27, 2018

Conversation

nharrand
Copy link
Collaborator

@nharrand nharrand commented Sep 7, 2018

This PR aims at providing support for JAR annalysis. (Related to #675 ).
My typical usecase is to be able to download an artifact's jar from maven central (and optionally its pom to get dependency information) to decompile it and build a spoon AST.

This code provide a small wrapper named JarLauncher that does just that.

Still lack:

  • (fixing git commits)
  • some proper testing
  • documentation
  • an option to not decompile but instead build only shadow types?
  • support for alternative decompiler?
  • use maven install plugin for cleaner deployement of CFR?

As the decompiler I use ( CFR ) is not avilable on maven central I propose to add its jar in /libs.
WDYT?

@nharrand nharrand changed the title (WiP) JarLauncher (decompile than build AST with spoon) (WiP) JarLauncher (decompile then build AST with spoon) Sep 7, 2018
public class CFRDecompiler implements Decompiler {
@Override
public void decompile(File jar, File outputDir) {
Main.main(new String[]{jar.getAbsolutePath(), "--outputdir", outputDir.getAbsolutePath()});
Copy link
Collaborator

Choose a reason for hiding this comment

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

is their a proper Java API?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not as far as I know.

* @param outputDir
* Path for the output directory for decompilation.
*/
void decompile(File jar, File outputDir);
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about

List<SpoonFile> decompile(String jarPath); 
List<SpoonResource> decompile(String jarPath); 
CtModel decompile(String jarPath); 

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not convinced by either of this solutions. My plan was to call the decompiler only if the input jar has been modified after last decompilation or if user forces it. I would prefer having directory declaration in JarLauncher than in Decompiler.


import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

public class JarLauncher extends Launcher {
Copy link
Collaborator

Choose a reason for hiding this comment

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

pom.xml Outdated
<groupId>org.benf</groupId>
<artifactId>cfr</artifactId>
<version>${version.cfr}</version>
<scope>system</scope>
Copy link
Contributor

Choose a reason for hiding this comment

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

Scope system will not package the dependency into the resulting jar. You need to create a local maven repository and add it to git. See: https://sookocheff.com/post/java/local-maven-repository/

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks. I'll do that.

@nharrand nharrand closed this Sep 10, 2018
@nharrand nharrand deleted the jar-launcher-wip branch September 10, 2018 12:29
@nharrand nharrand restored the jar-launcher-wip branch September 10, 2018 12:29
@nharrand nharrand reopened this Sep 10, 2018
@nharrand nharrand changed the title (WiP) JarLauncher (decompile then build AST with spoon) JarLauncher (decompile then build AST with spoon) Sep 20, 2018
@nharrand
Copy link
Collaborator Author

Ready for review.

@monperrus
Copy link
Collaborator

LGTM.

Could you add the corresponding documentation in doc?

@monperrus monperrus changed the title JarLauncher (decompile then build AST with spoon) feature: add support for Jar files analysis with JarLauncher (decompile then build AST with spoon) Sep 21, 2018
pom.xml Outdated
@@ -306,6 +306,9 @@
<groupId>org.benf</groupId>
<artifactId>cfr</artifactId>
<version>0.132.0</version>
<!-- since it is not on Maven central, we don't package it -->
<!-- if you need it, download it at http://www.benf.org/other/cfr/ and put the jar in the classpath yourself -->
<scope>provided</scope>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is not on maven central, but I've upload it on maven.inria.fr, so it is still publicly available. (So we could package it if we wanted it.)

@monperrus
Copy link
Collaborator

What's the license of CFR? Is it compatible with LGPL?

@nharrand
Copy link
Collaborator Author

nharrand commented Sep 26, 2018

It's MIT.

Copy link
Collaborator

@monperrus monperrus left a comment

Choose a reason for hiding this comment

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

Thanks a lot, this opens interesting and fun new perspectives for Spoon

@monperrus monperrus merged commit 409f519 into INRIA:master Sep 27, 2018
@nharrand nharrand deleted the jar-launcher-wip branch October 2, 2018 12:18
@monperrus monperrus mentioned this pull request Oct 10, 2018
4 tasks
monperrus added a commit that referenced this pull request Dec 4, 2018
Dear all,

I'd like to nominate Nicolas Harrand (@nharrand) to become an integrator in Spoon. Nicolas has done several key contributions over the past year:

* he has added support for paths in order to uniquely identify AST nodes (#1874), this feature is unique (to my knowledge, it does not exist in any other source code analysis library for Java), and enables original source code analysis over time and versions.
* he has added support for analysis of binary files with decompilation (#2455)
* he has laid down the foundations of a modularized Spoon, which will likely be key in the future
* he has ported Spoon to JDK 11 (#2789). This kind of infrastructure contribution is also very important in the long term.

In addition, Nicolas has proven his ability to communicate constructively and respectfully over issues and pull-requests.

What do you think?
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.

3 participants