forked from soot-oss/SootUp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request soot-oss#1045 from soot-oss/NewInputLocation
Added new inputlocation for downloading jars from maven
- Loading branch information
Showing
3 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/FileUtil.java
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,42 @@ | ||
package sootup.java.bytecode.frontend; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
/*- | ||
* #%L | ||
* Soot | ||
* %% | ||
* Copyright (C) 2018-2020 | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation, either version 2.1 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* 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 | ||
* GNU General Lesser Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Lesser Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
* #L% | ||
*/ | ||
|
||
public class FileUtil { | ||
|
||
private static Path tempDirectory; | ||
|
||
public static Path getTempDirectory() { | ||
try { | ||
return tempDirectory == null | ||
? (tempDirectory = Files.createTempDirectory("tempDir")) | ||
: tempDirectory; | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...de/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java
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,77 @@ | ||
package sootup.java.bytecode.inputlocation; | ||
|
||
import java.io.*; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import sootup.core.model.SourceType; | ||
import sootup.core.transform.BodyInterceptor; | ||
import sootup.java.bytecode.frontend.FileUtil; | ||
|
||
/*- | ||
* #%L | ||
* Soot | ||
* %% | ||
* Copyright (C) 2018-2020 | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation, either version 2.1 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* 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 | ||
* GNU General Lesser Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Lesser Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
* #L% | ||
*/ | ||
|
||
public class DownloadJarAnalysisInputLocation extends ArchiveBasedAnalysisInputLocation { | ||
|
||
private static final int BUFFER_SIZE = 1024; | ||
|
||
public DownloadJarAnalysisInputLocation( | ||
String downloadURL, List<BodyInterceptor> bodyInterceptors, Collection<Path> ignoredPaths) { | ||
super( | ||
downloadAndConstructPath(downloadURL), SourceType.Library, bodyInterceptors, ignoredPaths); | ||
} | ||
|
||
private static Path downloadAndConstructPath(String downloadURL) { | ||
HttpURLConnection connection = null; | ||
String filename = downloadURL.substring(downloadURL.lastIndexOf("/") + 1); | ||
File file = new File(FileUtil.getTempDirectory().toString(), filename); | ||
try { | ||
URL url = new URL(downloadURL); | ||
connection = (HttpURLConnection) url.openConnection(); | ||
connection.setRequestMethod("GET"); | ||
int responseCode = connection.getResponseCode(); | ||
if (responseCode != HttpURLConnection.HTTP_OK) { | ||
throw new IOException("HTTP request failed with response code " + responseCode); | ||
} | ||
try (InputStream inputStream = new BufferedInputStream(connection.getInputStream()); | ||
OutputStream outputStream = Files.newOutputStream(Paths.get(file.getAbsolutePath()))) { | ||
byte[] buffer = new byte[BUFFER_SIZE]; | ||
int bytesRead; | ||
while ((bytesRead = inputStream.read(buffer, 0, BUFFER_SIZE)) != -1) { | ||
outputStream.write(buffer, 0, bytesRead); | ||
} | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} finally { | ||
if (connection != null) { | ||
connection.disconnect(); | ||
} | ||
} | ||
|
||
return file.toPath(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...tecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java
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,42 @@ | ||
package sootup.java.bytecode.inputlocation; | ||
|
||
import categories.TestCategories; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Collections; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import sootup.core.model.SootMethod; | ||
import sootup.java.bytecode.frontend.FileUtil; | ||
import sootup.java.core.views.JavaView; | ||
|
||
@Tag(TestCategories.JAVA_8_CATEGORY) | ||
public class DownloadJarInputLocationTest { | ||
|
||
@Test | ||
public void testDownloadJarsInputLocation() { | ||
DownloadJarAnalysisInputLocation downloadJarAnalysisInputLocation = | ||
new DownloadJarAnalysisInputLocation( | ||
"https://repo1.maven.org/maven2/commons-io/commons-io/2.11.0/commons-io-2.11.0.jar", | ||
Collections.emptyList(), | ||
Collections.emptyList()); | ||
String tempDirPath = FileUtil.getTempDirectory().toString(); | ||
String fileName = "commons-io-2.11.0.jar"; | ||
Path path = Paths.get(tempDirPath, fileName); | ||
assert Files.exists(path); | ||
JavaView view = new JavaView(downloadJarAnalysisInputLocation); | ||
view.getClasses() | ||
.flatMap(javaSootClass -> javaSootClass.getMethods().stream()) | ||
.filter(SootMethod::hasBody) | ||
.forEach(SootMethod::getBody); | ||
// Deleting the file after our work is done | ||
try { | ||
Files.delete(path); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
assert !Files.exists(path); | ||
} | ||
} |