From 0b08a8e693bcb3ad50c3b5fd66ca9aada4a6e704 Mon Sep 17 00:00:00 2001 From: Palaniappan Muthuraman Date: Thu, 29 Aug 2024 12:39:37 +0200 Subject: [PATCH 1/8] Added new inputlocation --- .../DownloadJarAnalysisInputLocation.java | 51 +++++++++++++++++++ .../DownloadJarInputLocationTest.java | 39 ++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java create mode 100644 sootup.java.bytecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java diff --git a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java new file mode 100644 index 00000000000..582aa0d74de --- /dev/null +++ b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java @@ -0,0 +1,51 @@ +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; + +public class DownloadJarAnalysisInputLocation extends ArchiveBasedAnalysisInputLocation { + + private static final int BUFFER_SIZE = 1024; + + public DownloadJarAnalysisInputLocation( + String downloadURL, List bodyInterceptors, Collection ignoredPaths) { + super( + downloadAndConstructPath(downloadURL), SourceType.Library, bodyInterceptors, ignoredPaths); + } + + private static Path downloadAndConstructPath(String downloadURL) { + HttpURLConnection connection; + String tempDirPath = System.getProperty("java.io.tmpdir"); + String filename = downloadURL.substring(downloadURL.lastIndexOf("/") + 1); + File file = new File(tempDirPath, 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 RuntimeException(e); + } + + return file.exists() ? file.toPath() : null; + } +} diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java new file mode 100644 index 00000000000..a1e3a9fda4f --- /dev/null +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java @@ -0,0 +1,39 @@ +package sootup.java.bytecode.inputlocation; + +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.Test; +import sootup.core.model.SootMethod; +import sootup.java.core.views.JavaView; + +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 = System.getProperty("java.io.tmpdir"); + String fileName = "commons-io-2.11.0.jar"; + assert tempDirPath != null && !tempDirPath.isEmpty(); + 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); + } +} From 8bb132ed8399016c157abd8487860a071b34da13 Mon Sep 17 00:00:00 2001 From: Palaniappan Muthuraman Date: Thu, 29 Aug 2024 12:56:17 +0200 Subject: [PATCH 2/8] test tag added --- .../bytecode/inputlocation/DownloadJarInputLocationTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java index a1e3a9fda4f..902342e8f79 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java @@ -5,10 +5,14 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collections; + +import categories.TestCategories; +import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import sootup.core.model.SootMethod; import sootup.java.core.views.JavaView; +@Tag(TestCategories.JAVA_8_CATEGORY) public class DownloadJarInputLocationTest { @Test From 178df586e408603e7536da7cbb2a90f2ca663fef Mon Sep 17 00:00:00 2001 From: Palaniappan Muthuraman Date: Thu, 29 Aug 2024 14:00:07 +0200 Subject: [PATCH 3/8] fmt commit --- .../inputlocation/DownloadJarAnalysisInputLocation.java | 2 +- .../bytecode/inputlocation/DownloadJarInputLocationTest.java | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java index 582aa0d74de..e3a8135f539 100644 --- a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java +++ b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java @@ -46,6 +46,6 @@ private static Path downloadAndConstructPath(String downloadURL) { throw new RuntimeException(e); } - return file.exists() ? file.toPath() : null; + return file.toPath(); } } diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java index 902342e8f79..20fd3489bc4 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java @@ -1,12 +1,11 @@ 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 categories.TestCategories; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import sootup.core.model.SootMethod; From bd0859e0f0789b79e3f43a86d58debc18cbce60a Mon Sep 17 00:00:00 2001 From: Palaniappan Muthuraman Date: Thu, 29 Aug 2024 14:06:54 +0200 Subject: [PATCH 4/8] added license header --- .../DownloadJarAnalysisInputLocation.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java index e3a8135f539..3cb041b243e 100644 --- a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java +++ b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java @@ -11,6 +11,28 @@ import sootup.core.model.SourceType; import sootup.core.transform.BodyInterceptor; +/*- + * #%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 + * . + * #L% + */ + public class DownloadJarAnalysisInputLocation extends ArchiveBasedAnalysisInputLocation { private static final int BUFFER_SIZE = 1024; From 1c36369336423dd35aaefa8eca19e47911501ee7 Mon Sep 17 00:00:00 2001 From: Palaniappan Muthuraman Date: Thu, 29 Aug 2024 18:37:02 +0200 Subject: [PATCH 5/8] closed the connection object --- .../inputlocation/DownloadJarAnalysisInputLocation.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java index 3cb041b243e..7e3a2221a16 100644 --- a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java +++ b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java @@ -44,7 +44,7 @@ public DownloadJarAnalysisInputLocation( } private static Path downloadAndConstructPath(String downloadURL) { - HttpURLConnection connection; + HttpURLConnection connection = null; String tempDirPath = System.getProperty("java.io.tmpdir"); String filename = downloadURL.substring(downloadURL.lastIndexOf("/") + 1); File file = new File(tempDirPath, filename); @@ -66,6 +66,10 @@ private static Path downloadAndConstructPath(String downloadURL) { } } catch (IOException e) { throw new RuntimeException(e); + } finally { + if (connection != null) { + connection.disconnect(); + } } return file.toPath(); From 4bc8202d112eb9d8b245c60d275fdd094ed63eff Mon Sep 17 00:00:00 2001 From: Palaniappan Muthuraman Date: Thu, 29 Aug 2024 18:59:25 +0200 Subject: [PATCH 6/8] changed the exception type --- .../inputlocation/DownloadJarAnalysisInputLocation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java index 7e3a2221a16..355f00d741a 100644 --- a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java +++ b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java @@ -65,7 +65,7 @@ private static Path downloadAndConstructPath(String downloadURL) { } } } catch (IOException e) { - throw new RuntimeException(e); + throw new UncheckedIOException(e); } finally { if (connection != null) { connection.disconnect(); From 087d152552b146ac47aa0656bf9e88e0d6ba1efa Mon Sep 17 00:00:00 2001 From: Palaniappan Muthuraman Date: Thu, 5 Sep 2024 11:40:40 +0200 Subject: [PATCH 7/8] created tempDirectory for storing files --- .../java/bytecode/frontend/FileUtil.java | 20 +++++++++++++++++++ .../DownloadJarAnalysisInputLocation.java | 4 ++-- .../DownloadJarInputLocationTest.java | 4 ++-- 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/FileUtil.java diff --git a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/FileUtil.java b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/FileUtil.java new file mode 100644 index 00000000000..e3d2c916576 --- /dev/null +++ b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/FileUtil.java @@ -0,0 +1,20 @@ +package sootup.java.bytecode.frontend; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +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); + } + } +} diff --git a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java index 355f00d741a..d2806ab0bfd 100644 --- a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java +++ b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/inputlocation/DownloadJarAnalysisInputLocation.java @@ -10,6 +10,7 @@ import java.util.List; import sootup.core.model.SourceType; import sootup.core.transform.BodyInterceptor; +import sootup.java.bytecode.frontend.FileUtil; /*- * #%L @@ -45,9 +46,8 @@ public DownloadJarAnalysisInputLocation( private static Path downloadAndConstructPath(String downloadURL) { HttpURLConnection connection = null; - String tempDirPath = System.getProperty("java.io.tmpdir"); String filename = downloadURL.substring(downloadURL.lastIndexOf("/") + 1); - File file = new File(tempDirPath, filename); + File file = new File(FileUtil.getTempDirectory().toString(), filename); try { URL url = new URL(downloadURL); connection = (HttpURLConnection) url.openConnection(); diff --git a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java index 20fd3489bc4..16263534e9e 100644 --- a/sootup.java.bytecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java +++ b/sootup.java.bytecode/src/test/java/sootup/java/bytecode/inputlocation/DownloadJarInputLocationTest.java @@ -9,6 +9,7 @@ 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) @@ -21,9 +22,8 @@ public void testDownloadJarsInputLocation() { "https://repo1.maven.org/maven2/commons-io/commons-io/2.11.0/commons-io-2.11.0.jar", Collections.emptyList(), Collections.emptyList()); - String tempDirPath = System.getProperty("java.io.tmpdir"); + String tempDirPath = FileUtil.getTempDirectory().toString(); String fileName = "commons-io-2.11.0.jar"; - assert tempDirPath != null && !tempDirPath.isEmpty(); Path path = Paths.get(tempDirPath, fileName); assert Files.exists(path); JavaView view = new JavaView(downloadJarAnalysisInputLocation); From b15ff047e23c234fa6c2c592cd60c8b31637edbe Mon Sep 17 00:00:00 2001 From: Palaniappan Muthuraman Date: Thu, 5 Sep 2024 12:52:29 +0200 Subject: [PATCH 8/8] license header commit --- .../java/bytecode/frontend/FileUtil.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/FileUtil.java b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/FileUtil.java index e3d2c916576..a6d59358a3f 100644 --- a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/FileUtil.java +++ b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/FileUtil.java @@ -4,6 +4,28 @@ 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 + * . + * #L% + */ + public class FileUtil { private static Path tempDirectory;