-
-
Notifications
You must be signed in to change notification settings - Fork 347
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
feature: add support for Jar files analysis with JarLauncher (decompile then build AST with spoon) #2455
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
0d7c009
refactor(MavenLauncher)
nharrand 8c4037d
merge
nharrand c09c060
vanity
nharrand 7a2fd36
removes parameter m2repository as it has become useless
nharrand 2e88419
override last modified date when classpath is evaluated but has not c…
nharrand ee3fd53
doc(MavenLauncher)
nharrand 0932e53
untrack .tmp files and cosmetic
nharrand 11ca5a0
fixes a bug when compiler plugin exist but has no configuration child…
nharrand 1a43b2d
fixes a bug which made InheritanceModel incorectly read properties co…
nharrand c974268
re add deprecated MavenLauncher constructor and add a forceRefresh op…
nharrand 178bb9d
style
nharrand 1567cf5
Merge branch 'master' of https://github.com/INRIA/spoon
nharrand ad5414b
JarLauncher poc
nharrand 545ca73
update licenses
nharrand 4bdeddc
add a local repository for cfr
nharrand a1f72de
wip
nharrand 3440581
Refactoring(InheritanceModel, MavenLauncher)
nharrand d538953
merge
nharrand d39d2db
merge
nharrand c456307
merge upstream
nharrand 6227699
small fixes, remove local jar as it is deployed on inria maven repo
nharrand 2c68b52
add triskell repo to pom
nharrand a9457f2
Activate LarLauncherTest
nharrand 7bd26e4
merge
nharrand 0e6c5eb
add documentation
nharrand 9b75b4d
@Experimental everywhere!
nharrand 4ddfb79
dependency scope = provided for dirty cfr
monperrus 92e6602
Update pom.xml
monperrus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/** | ||
* Copyright (C) 2006-2018 INRIA and contributors | ||
* Spoon - http://spoon.gforge.inria.fr/ | ||
* | ||
* This software is governed by the CeCILL-C License under French law and | ||
* abiding by the rules of distribution of free software. You can use, modify | ||
* and/or redistribute the software under the terms of the CeCILL-C license as | ||
* circulated by CEA, CNRS and INRIA at http://www.cecill.info. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details. | ||
* | ||
* The fact that you are presently reading this means that you have had | ||
* knowledge of the CeCILL-C license and that you accept its terms. | ||
*/ | ||
package spoon; | ||
|
||
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; | ||
import spoon.decompiler.CFRDecompiler; | ||
import spoon.decompiler.Decompiler; | ||
import spoon.support.Experimental; | ||
import spoon.support.compiler.SpoonPom; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
|
||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; | ||
|
||
@Experimental | ||
public class JarLauncher extends Launcher { | ||
File pom; | ||
File jar; | ||
File decompiledRoot; | ||
File decompiledSrc; | ||
Decompiler decompiler; | ||
boolean decompile = false; | ||
|
||
/** | ||
* JarLauncher basic constructor. Uses the defauld Decompiler (CFR) | ||
* | ||
* @param jarPath path to the jar to be analyzed | ||
*/ | ||
public JarLauncher(String jarPath) { | ||
this(jarPath, null, (String) null); | ||
} | ||
|
||
|
||
/** | ||
* JarLauncher basic constructor. Uses the defauld Decompiler (CFR) | ||
* | ||
* @param jarPath path to the jar to be analyzed | ||
* @param decompiledSrcPath path to directory where decompiled source will be outputted | ||
*/ | ||
public JarLauncher(String jarPath, String decompiledSrcPath) { | ||
this(jarPath, decompiledSrcPath, (String) null); | ||
} | ||
|
||
/** | ||
* JarLauncher basic constructor. Uses the defauld Decompiler (CFR) | ||
* | ||
* @param jarPath path to the jar to be analyzed | ||
* @param decompiledSrcPath path to directory where decompiled source will be outputted | ||
* @param pom path to pom associated with the jar to be analyzed | ||
*/ | ||
public JarLauncher(String jarPath, String decompiledSrcPath, String pom) { | ||
this(jarPath, decompiledSrcPath, pom, null); | ||
} | ||
|
||
/** | ||
* JarLauncher basic constructor. Uses the defauld Decompiler (CFR) | ||
* | ||
* @param jarPath path to the jar to be analyzed | ||
* @param decompiledSrcPath path to directory where decompiled source will be outputted | ||
* @param decompiler Instance implementing {@link spoon.decompiler.Decompiler} to be used | ||
*/ | ||
public JarLauncher(String jarPath, String decompiledSrcPath, Decompiler decompiler) { | ||
this(jarPath, decompiledSrcPath, null, decompiler); | ||
} | ||
|
||
/** | ||
* JarLauncher constructor. Uses the defauld Decompiler (CFR) | ||
* | ||
* @param jarPath path to the jar to be analyzed | ||
* @param decompiledSrcPath path to directory where decompiled source will be outputted | ||
* @param pom path to pom associated with the jar to be analyzed | ||
* @param decompiler Instance implementing {@link spoon.decompiler.Decompiler} to be used | ||
*/ | ||
public JarLauncher(String jarPath, String decompiledSrcPath, String pom, Decompiler decompiler) { | ||
this.decompiler = decompiler; | ||
if (decompiledSrcPath == null) { | ||
decompiledSrcPath = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + "spoon-tmp"; | ||
decompile = true; | ||
} | ||
this.decompiledRoot = new File(decompiledSrcPath); | ||
if (decompiledRoot.exists() && !decompiledRoot.canWrite()) { | ||
throw new SpoonException("Dir " + decompiledRoot.getPath() + " already exists and is not deletable."); | ||
} else if (decompiledRoot.exists() && decompile) { | ||
decompiledRoot.delete(); | ||
} | ||
if (!decompiledRoot.exists()) { | ||
decompiledRoot.mkdirs(); | ||
decompile = true; | ||
} | ||
decompiledSrc = new File(decompiledRoot, "src/main/java"); | ||
if (!decompiledSrc.exists()) { | ||
decompiledSrc.mkdirs(); | ||
decompile = true; | ||
} | ||
|
||
if (decompiler == null) { | ||
this.decompiler = getDefaultDecompiler(); | ||
} | ||
|
||
jar = new File(jarPath); | ||
if (!jar.exists() || !jar.isFile()) { | ||
throw new SpoonException("Jar " + jar.getPath() + "not found."); | ||
} | ||
|
||
//We call the decompiler only if jar has changed since last decompilation. | ||
if (jar.lastModified() > decompiledSrc.lastModified()) { | ||
decompile = true; | ||
} | ||
init(pom); | ||
} | ||
|
||
private void init(String pomPath) { | ||
//We call the decompiler only if jar has changed since last decompilation. | ||
if (decompile) { | ||
decompiler.decompile(jar.getAbsolutePath()); | ||
} | ||
|
||
|
||
if (pomPath != null) { | ||
File srcPom = new File(pomPath); | ||
if (!srcPom.exists() || !srcPom.isFile()) { | ||
throw new SpoonException("Pom " + srcPom.getPath() + "not found."); | ||
} | ||
try { | ||
pom = new File(decompiledRoot, "pom.xml"); | ||
Files.copy(srcPom.toPath(), pom.toPath(), REPLACE_EXISTING); | ||
} catch (IOException e) { | ||
throw new SpoonException("Unable to write " + pom.getPath()); | ||
} | ||
try { | ||
SpoonPom pomModel = new SpoonPom(pom.getPath(), null, MavenLauncher.SOURCE_TYPE.APP_SOURCE, getEnvironment()); | ||
if (pomModel == null) { | ||
throw new SpoonException("Unable to create the model, pom not found?"); | ||
} | ||
getEnvironment().setComplianceLevel(pomModel.getSourceVersion()); | ||
String[] classpath = pomModel.buildClassPath(null, MavenLauncher.SOURCE_TYPE.APP_SOURCE, LOGGER, false); | ||
// dependencies | ||
this.getModelBuilder().setSourceClasspath(classpath); | ||
} catch (IOException | XmlPullParserException e) { | ||
throw new SpoonException("Failed to read classpath file."); | ||
} | ||
addInputResource(decompiledSrc.getAbsolutePath()); | ||
} else { | ||
addInputResource(decompiledSrc.getAbsolutePath()); | ||
} | ||
} | ||
|
||
protected Decompiler getDefaultDecompiler() { | ||
return new CFRDecompiler(decompiledSrc); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Copyright (C) 2006-2018 INRIA and contributors | ||
* Spoon - http://spoon.gforge.inria.fr/ | ||
* | ||
* This software is governed by the CeCILL-C License under French law and | ||
* abiding by the rules of distribution of free software. You can use, modify | ||
* and/or redistribute the software under the terms of the CeCILL-C license as | ||
* circulated by CEA, CNRS and INRIA at http://www.cecill.info. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details. | ||
* | ||
* The fact that you are presently reading this means that you have had | ||
* knowledge of the CeCILL-C license and that you accept its terms. | ||
*/ | ||
package spoon.decompiler; | ||
|
||
import java.io.File; | ||
|
||
import org.benf.cfr.reader.Main; | ||
import spoon.support.Experimental; | ||
|
||
@Experimental | ||
public class CFRDecompiler implements Decompiler { | ||
|
||
File outputDir; | ||
|
||
public CFRDecompiler(File outputDir) { | ||
this.outputDir = outputDir; | ||
} | ||
|
||
@Override | ||
public void decompile(String jarPath) { | ||
Main.main(new String[]{jarPath, "--outputdir", outputDir.getPath()}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Copyright (C) 2006-2018 INRIA and contributors | ||
* Spoon - http://spoon.gforge.inria.fr/ | ||
* | ||
* This software is governed by the CeCILL-C License under French law and | ||
* abiding by the rules of distribution of free software. You can use, modify | ||
* and/or redistribute the software under the terms of the CeCILL-C license as | ||
* circulated by CEA, CNRS and INRIA at http://www.cecill.info. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details. | ||
* | ||
* The fact that you are presently reading this means that you have had | ||
* knowledge of the CeCILL-C license and that you accept its terms. | ||
*/ | ||
package spoon.decompiler; | ||
|
||
import spoon.support.Experimental; | ||
|
||
@Experimental | ||
public interface Decompiler { | ||
|
||
/** | ||
* Sets the output directory for source generated. | ||
* | ||
* @param jarPath | ||
* Path to jar to be analyzed. | ||
*/ | ||
void decompile(String jarPath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright (C) 2006-2018 INRIA and contributors | ||
* Spoon - http://spoon.gforge.inria.fr/ | ||
* | ||
* This software is governed by the CeCILL-C License under French law and | ||
* abiding by the rules of distribution of free software. You can use, modify | ||
* and/or redistribute the software under the terms of the CeCILL-C license as | ||
* circulated by CEA, CNRS and INRIA at http://www.cecill.info. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details. | ||
* | ||
* The fact that you are presently reading this means that you have had | ||
* knowledge of the CeCILL-C license and that you accept its terms. | ||
*/ | ||
package spoon; | ||
|
||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import spoon.reflect.CtModel; | ||
import spoon.reflect.code.CtLocalVariable; | ||
import spoon.reflect.code.CtTry; | ||
import spoon.reflect.declaration.CtConstructor; | ||
|
||
import java.io.File; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class JarLauncherTest { | ||
|
||
@Test | ||
public void testJarLauncher() { | ||
|
||
File baseDir = new File("src/test/resources/jarLauncher"); | ||
File pom = new File(baseDir, "pom.xml"); | ||
File jar = new File(baseDir, "helloworld-1.0-SNAPSHOT.jar"); | ||
JarLauncher launcher = new JarLauncher(jar.getAbsolutePath(), null, pom.getAbsolutePath()); | ||
launcher.getEnvironment().setAutoImports(true); | ||
launcher.buildModel(); | ||
CtModel model = launcher.getModel(); | ||
assertEquals(model.getAllTypes().size(), 5); | ||
CtConstructor constructor = (CtConstructor) model.getRootPackage().getFactory().Type().get("se.kth.castor.UseJson").getTypeMembers().get(0); | ||
CtTry tryStmt = (CtTry) constructor.getBody().getStatement(1); | ||
CtLocalVariable var = (CtLocalVariable) tryStmt.getBody().getStatement(0); | ||
assertNotNull(var.getType().getTypeDeclaration()); | ||
} | ||
|
||
@Test | ||
public void testJarLauncherNoPom() { | ||
File baseDir = new File("src/test/resources/jarLauncher"); | ||
File jar = new File(baseDir, "helloworld-1.0-SNAPSHOT.jar"); | ||
JarLauncher launcher = new JarLauncher(jar.getAbsolutePath(), null); | ||
launcher.getEnvironment().setAutoImports(true); | ||
launcher.buildModel(); | ||
CtModel model = launcher.getModel(); | ||
assertEquals(model.getAllTypes().size(),5); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@experimental