Skip to content

Commit

Permalink
Merge pull request soot-oss#1825 from MarcMil/foundfileinterfacefix
Browse files Browse the repository at this point in the history
Add IFoundFile interface to create an abstract version of FoundFile
  • Loading branch information
StevenArzt authored Jan 10, 2022
2 parents 553e82b + 5b59e34 commit 3c8b172
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 91 deletions.
2 changes: 1 addition & 1 deletion src/main/java/soot/CoffiClassProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class CoffiClassProvider implements ClassProvider {
@Override
public ClassSource find(String className) {
String fileName = className.replace('.', '/') + ".class";
FoundFile file = SourceLocator.v().lookupInClassPath(fileName);
IFoundFile file = SourceLocator.v().lookupInClassPath(fileName);
return (file == null) ? null : new CoffiClassSource(className, file);
}
}
4 changes: 2 additions & 2 deletions src/main/java/soot/CoffiClassSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
public class CoffiClassSource extends ClassSource {
private static final Logger logger = LoggerFactory.getLogger(CoffiClassSource.class);

private FoundFile foundFile;
private IFoundFile foundFile;
private InputStream classFile;
private final String fileName;
private final String zipFileName;

public CoffiClassSource(String className, FoundFile foundFile) {
public CoffiClassSource(String className, IFoundFile foundFile) {
super(className);
if (foundFile == null) {
throw new IllegalStateException("Error: The FoundFile must not be null.");
Expand Down
38 changes: 17 additions & 21 deletions src/main/java/soot/FoundFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

import soot.util.SharedCloseable;

public class FoundFile {
public class FoundFile implements IFoundFile {
private static final Logger logger = LoggerFactory.getLogger(FoundFile.class);

protected final List<InputStream> openedInputStreams = new ArrayList<InputStream>();
Expand All @@ -56,17 +56,6 @@ public class FoundFile {
protected SharedCloseable<ZipFile> zipFile;
protected ZipEntry zipEntry;

// NOTE: this constructor cannot be supported when using the SharedClosable
// without the risk of the ZipFile closing prematurely.
// public FoundFile(ZipFile file, ZipEntry entry) {
// this();
// if (file == null || entry == null) {
// throw new IllegalArgumentException("Error: The archive and entry cannot be null.");
// }
// this.zipFile = file;
// this.zipEntry = entry;
// }

public FoundFile(String archivePath, String entryName) {
if (archivePath == null || entryName == null) {
throw new IllegalArgumentException("Error: The archive path and entry name cannot be null.");
Expand All @@ -87,23 +76,36 @@ public FoundFile(Path path) {
this.path = path;
}

@Deprecated
@Override
public String getFilePath() {
if (file == null) {
if (path != null) {
File f = path.toFile();
if (f != null) {
return f.getPath();
}
}
return null;
}
return file.getPath();
}

@Override
public boolean isZipFile() {
return entryName != null;
}

@Override
public ZipFile getZipFile() {
return zipFile != null ? zipFile.get() : null;
}

@Override
public File getFile() {
return file;
}

@Override
public String getAbsolutePath() {
try {
return file != null ? file.getCanonicalPath() : path.toRealPath().toString();
Expand All @@ -112,6 +114,7 @@ public String getAbsolutePath() {
}
}

@Override
public InputStream inputStream() {
InputStream ret = null;
if (path != null) {
Expand Down Expand Up @@ -156,14 +159,7 @@ public InputStream inputStream() {
return ret;
}

public void silentClose() {
try {
close();
} catch (Exception e) {
logger.debug(e.getMessage(), e);
}
}

@Override
public void close() {
// Try to close all opened input streams
List<Exception> errs = new ArrayList<Exception>(0);
Expand Down
97 changes: 97 additions & 0 deletions src/main/java/soot/IFoundFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package soot;

/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 1997 - 2018 Raja Vallée-Rai and others
* %%
* 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%
*/

import java.io.File;
import java.io.InputStream;
import java.util.zip.ZipFile;

import org.slf4j.LoggerFactory;

/**
* This abstraction allows to use different a implementation of file access. It contains essentially the same methods as
* {@link FoundFile}.
*
* @author Marc Miltenberger
*/
public interface IFoundFile {
@Deprecated
/**
* Returns the concrete file on disk if it is not within a zip file. Otherwise, the returned file is the zip file itself.
* Note that it may return null in some circumstances.
*
* @return the file on disk
*/
public String getFilePath();

/**
* Returns true if and only if this file is contained within a zip file.
*/
public boolean isZipFile();

/**
* Returns the zip file or null.
*
* @return the zip file
*/
public ZipFile getZipFile();

/**
* Returns the concrete file on disk if it is not within a zip file. Otherwise, the returned file is the zip file itself.
* Note that it may return null in some circumstances.
*
* @return the file on disk
*/
public File getFile();

/**
* Returns the absolute path to the file or the zip file.
*
* @return the absolute path to the file or the zip file.
*/
public String getAbsolutePath();

/**
* Opens the file and returns a new input stream containing the file contents.
*
* @return a fresh stream
*/
public InputStream inputStream();

/**
* Closes all opened input streams.
*/
public void close();

/**
* Closes the instance without throwing an exception.
*/
public default void silentClose() {
try {
close();
} catch (Exception e) {
LoggerFactory.getLogger(getClass()).debug(e.getMessage(), e);
}
}

}
2 changes: 1 addition & 1 deletion src/main/java/soot/JavaClassProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ClassSource find(String className) {
*/
boolean checkAgain = className.indexOf('$') >= 0;

FoundFile file = null;
IFoundFile file = null;
try {
final SourceLocator loc = SourceLocator.v();
String javaClassName = loc.getSourceForClass(className);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/soot/JimpleClassProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class JimpleClassProvider implements ClassProvider {
*/
@Override
public ClassSource find(String className) {
FoundFile file = SourceLocator.v().lookupInClassPath(className + ".jimple");
IFoundFile file = SourceLocator.v().lookupInClassPath(className + ".jimple");
if (file == null) {
if (Options.v().permissive_resolving()) {
file = SourceLocator.v().lookupInClassPath(className.replace('.', '/') + ".jimple");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/soot/JimpleClassSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
public class JimpleClassSource extends ClassSource {
private static final Logger logger = LoggerFactory.getLogger(JimpleClassSource.class);

private FoundFile foundFile;
private IFoundFile foundFile;

public JimpleClassSource(String className, FoundFile foundFile) {
public JimpleClassSource(String className, IFoundFile foundFile) {
super(className);
if (foundFile == null) {
throw new IllegalStateException("Error: The FoundFile must not be null.");
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/soot/ModulePathSourceLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ private Map<String, List<String>> buildModuleForJar(Path jar) {
for (ClassProvider cp : classProviders) {
if (cp instanceof AsmModuleClassProvider) {
String moduleName = ((AsmModuleClassProvider) cp).getModuleName(foundFile);
SootModuleInfo moduleInfo =
(SootModuleInfo) SootModuleResolver.v().makeClassRef(SootModuleInfo.MODULE_INFO, Optional.of(moduleName));
SootModuleInfo moduleInfo
= (SootModuleInfo) SootModuleResolver.v().makeClassRef(SootModuleInfo.MODULE_INFO, Optional.of(moduleName));
this.moduleNameToPath.put(moduleName, jar);
List<String> classesInJar = super.getClassesUnder(jar.toAbsolutePath().toString());
for (String foundClass : classesInJar) {
Expand Down Expand Up @@ -412,8 +412,8 @@ private Map<String, List<String>> buildModuleForExplodedModule(Path dir) {
for (ClassProvider cp : classProviders) {
if (cp instanceof AsmModuleClassProvider) {
String moduleName = ((AsmModuleClassProvider) cp).getModuleName(new FoundFile(mi));
SootModuleInfo moduleInfo =
(SootModuleInfo) SootModuleResolver.v().makeClassRef(SootModuleInfo.MODULE_INFO, Optional.of(moduleName));
SootModuleInfo moduleInfo
= (SootModuleInfo) SootModuleResolver.v().makeClassRef(SootModuleInfo.MODULE_INFO, Optional.of(moduleName));
this.moduleNameToPath.put(moduleName, dir);

List<String> classes = getClassesUnderDirectory(dir);
Expand Down Expand Up @@ -465,7 +465,7 @@ public Set<String> classesInDynamicPackage(String str) {
* Searches for a file with the given name in the exploded modulePath.
*/
@Override
public FoundFile lookupInClassPath(String fileName) {
public IFoundFile lookupInClassPath(String fileName) {
return lookUpInModulePath(fileName);
}

Expand All @@ -486,7 +486,7 @@ protected ClassSourceType getClassSourceType(String path) {
}
}

public FoundFile lookUpInModulePath(String fileName) {
public IFoundFile lookUpInModulePath(String fileName) {
String[] moduleAndClassName = fileName.split(":");
String className = moduleAndClassName[moduleAndClassName.length - 1];
String moduleName = moduleAndClassName[0];
Expand Down Expand Up @@ -543,8 +543,7 @@ private Path discoverModule(String moduleName) {
return null;
}


protected FoundFile lookupInDir(String dir, String fileName) {
protected IFoundFile lookupInDir(String dir, String fileName) {
Path dirPath = Paths.get(dir);
Path foundFile = dirPath.resolve(fileName);
if (foundFile != null && Files.isRegularFile(foundFile)) {
Expand All @@ -564,7 +563,7 @@ protected FoundFile lookupInDir(String dir, String fileName) {
* @return the FoundFile
*/
@Override
protected FoundFile lookupInArchive(String archivePath, String fileName) {
protected IFoundFile lookupInArchive(String archivePath, String fileName) {
Path archive = Paths.get(archivePath);
try (FileSystem zipFileSystem = FileSystems.newFileSystem(archive, this.getClass().getClassLoader())) {
Path entry = zipFileSystem.getPath(fileName);
Expand All @@ -588,7 +587,7 @@ protected FoundFile lookupInArchive(String archivePath, String fileName) {
* the file to search
* @return the FoundFile
*/
public FoundFile lookUpInVirtualFileSystem(String archivePath, String fileName) {
public IFoundFile lookUpInVirtualFileSystem(String archivePath, String fileName) {
// FileSystem fs = FileSystems.getFileSystem(URI.create(archivePath));
Path foundFile = Paths.get(URI.create(archivePath)).resolve(fileName);
if (foundFile != null && Files.isRegularFile(foundFile)) {
Expand Down
Loading

0 comments on commit 3c8b172

Please sign in to comment.