Skip to content

Commit

Permalink
Merge pull request soot-oss#1045 from soot-oss/NewInputLocation
Browse files Browse the repository at this point in the history
Added new inputlocation for downloading jars from maven
  • Loading branch information
kadirayk authored Sep 5, 2024
2 parents e846a49 + b15ff04 commit 1322060
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
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);
}
}
}
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();
}
}
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);
}
}

0 comments on commit 1322060

Please sign in to comment.