Skip to content
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

Fix improve analysisinputlocation ctors #649

Merged
merged 5 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,5 @@ Optional<? extends AbstractClassSource<T>> getClassSource(
* @return returns null as source type
*/
@Nullable
default SourceType getSourceType() {
return null;
}
SourceType getSourceType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import sootup.core.frontend.AbstractClassSource;
import sootup.core.frontend.SootClassSource;
import sootup.core.model.SootClass;
import sootup.core.model.SourceType;
import sootup.core.types.ClassType;
import sootup.core.views.View;

Expand All @@ -41,14 +42,17 @@
public class EagerInputLocation<S extends SootClass<? extends SootClassSource<S>>>
implements AnalysisInputLocation<S> {

@Nonnull protected final SourceType sourceType;
@Nonnull private final Map<ClassType, ? extends SootClassSource<S>> map;

/** not useful for retrieval of classes via view. remove inputlocation from sootclass? */
public EagerInputLocation() {
map = Collections.emptyMap();
this(Collections.emptyMap(), SourceType.Application);
}

public EagerInputLocation(@Nonnull Map<ClassType, ? extends SootClassSource<S>> map) {
public EagerInputLocation(
@Nonnull Map<ClassType, ? extends SootClassSource<S>> map, @Nonnull SourceType sourceType) {
this.sourceType = sourceType;
this.map = ImmutableMap.copyOf(map);
}

Expand All @@ -66,6 +70,12 @@ public Collection<? extends AbstractClassSource<S>> getClassSources(@Nullable Vi
return map.values();
}

@Nonnull
@Override
public SourceType getSourceType() {
return sourceType;
}

@Override
public int hashCode() {
return map.hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void createByteCodeProject() {
// from the directory
Path pathToBinary = Paths.get("src/test/resources/BasicSetup/binary");
AnalysisInputLocation<JavaSootClass> inputLocation =
new PathBasedAnalysisInputLocation(pathToBinary, null);
PathBasedAnalysisInputLocation.create(pathToBinary, null);

// Specify the language of the JavaProject. This is especially relevant for Multi-release jars,
// where classes are loaded depending on the language level of the analysis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void test() {
// Create a AnalysisInputLocation, which points to a directory. All class files will be loaded
// from the directory
AnalysisInputLocation<JavaSootClass> inputLocation =
new PathBasedAnalysisInputLocation(
PathBasedAnalysisInputLocation.create(
Paths.get("src/test/resources/BodyInterceptor/binary"), null);

// Specify the language of the JavaProject. This is especially relevant for Multi-release jars,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public void test() {
// Create a AnalysisInputLocation, which points to a directory. All class files will be loaded
// from the directory
AnalysisInputLocation<JavaSootClass> inputLocation =
new PathBasedAnalysisInputLocation(Paths.get("src/test/resources/BasicSetup/binary"), null);
PathBasedAnalysisInputLocation.create(
Paths.get("src/test/resources/BasicSetup/binary"), null);

// Specify the language of the JavaProject. This is especially relevant for Multi-release jars,
// where classes are loaded depending on the language level of the analysis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public Optional<? extends AbstractClassSource<JavaSootClass>> getClassSource(
@Nonnull
private Optional<AnalysisInputLocation<JavaSootClass>> inputLocationForPath(@Nonnull Path path) {
if (Files.exists(path) && (Files.isDirectory(path) || PathUtils.isArchive(path))) {
return Optional.of(new PathBasedAnalysisInputLocation(path, srcType));
return Optional.of(PathBasedAnalysisInputLocation.create(path, srcType));
} else {
logger.warn("Invalid/Unknown class path entry: " + path);
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import sootup.core.IdentifierFactory;
import sootup.core.frontend.AbstractClassSource;
import sootup.core.frontend.ClassProvider;
import sootup.core.frontend.SootClassSource;
import sootup.core.inputlocation.AnalysisInputLocation;
import sootup.core.model.SourceType;
import sootup.core.types.ClassType;
import sootup.core.views.View;
import sootup.java.core.JavaModuleIdentifierFactory;
Expand All @@ -56,6 +58,7 @@
public class JavaModulePathAnalysisInputLocation implements ModuleInfoAnalysisInputLocation {

@Nonnull private final ModuleFinder moduleFinder;
@Nonnull private final SourceType sourcetype;

/**
* Creates a {@link JavaModulePathAnalysisInputLocation} which locates classes in the given module
Expand All @@ -65,7 +68,12 @@ public class JavaModulePathAnalysisInputLocation implements ModuleInfoAnalysisIn
* SootClassSource}es for the files found on the class path
*/
public JavaModulePathAnalysisInputLocation(@Nonnull String modulePath) {
this(modulePath, FileSystems.getDefault());
this(modulePath, FileSystems.getDefault(), SourceType.Application);
}

public JavaModulePathAnalysisInputLocation(
@Nonnull String modulePath, @Nonnull SourceType sourcetype) {
this(modulePath, FileSystems.getDefault(), sourcetype);
}

/**
Expand All @@ -77,7 +85,8 @@ public JavaModulePathAnalysisInputLocation(@Nonnull String modulePath) {
* @param fileSystem filesystem for the path
*/
public JavaModulePathAnalysisInputLocation(
@Nonnull String modulePath, @Nonnull FileSystem fileSystem) {
@Nonnull String modulePath, @Nonnull FileSystem fileSystem, @Nonnull SourceType sourcetype) {
this.sourcetype = sourcetype;
moduleFinder = new ModuleFinder(modulePath, fileSystem);
}

Expand Down Expand Up @@ -106,6 +115,12 @@ public Collection<? extends AbstractClassSource<JavaSootClass>> getClassSources(
.collect(Collectors.toList());
}

@Nullable
@Override
public SourceType getSourceType() {
return sourcetype;
}

@Override
@Nonnull
public Collection<? extends AbstractClassSource<JavaSootClass>> getModulesClassSources(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,18 @@

import java.io.IOException;
import java.net.URI;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import sootup.core.IdentifierFactory;
import sootup.core.frontend.AbstractClassSource;
import sootup.core.frontend.ClassProvider;
import sootup.core.frontend.ResolveException;
import sootup.core.inputlocation.AnalysisInputLocation;
import sootup.core.model.SourceType;
import sootup.core.types.ClassType;
import sootup.core.util.StreamUtils;
import sootup.core.views.View;
Expand All @@ -62,6 +59,16 @@ public class JrtFileSystemAnalysisInputLocation implements ModuleInfoAnalysisInp
Map<ModuleSignature, JavaModuleInfo> moduleInfoMap = new HashMap<>();
boolean isResolved = false;

@Nonnull private final SourceType sourceType;

public JrtFileSystemAnalysisInputLocation() {
this(SourceType.Library);
}

public JrtFileSystemAnalysisInputLocation(@Nonnull SourceType sourceType) {
this.sourceType = sourceType;
}

@Override
@Nonnull
public Optional<? extends AbstractClassSource<JavaSootClass>> getClassSource(
Expand Down Expand Up @@ -240,6 +247,12 @@ public Set<ModuleSignature> getModules(View<?> view) {
return Collections.unmodifiableSet(moduleInfoMap.keySet());
}

@Nullable
@Override
public SourceType getSourceType() {
return sourceType;
}

@Override
public boolean equals(Object o) {
return o instanceof JrtFileSystemAnalysisInputLocation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import javax.annotation.Nullable;
import sootup.core.frontend.ResolveException;
import sootup.core.inputlocation.AnalysisInputLocation;
import sootup.core.model.SourceType;
import sootup.core.util.PathUtils;
import sootup.java.bytecode.frontend.AsmModuleSource;
import sootup.java.core.JavaModuleIdentifierFactory;
Expand Down Expand Up @@ -64,6 +65,7 @@ public class ModuleFinder {
private int next = 0;

@Nonnull private final List<Path> modulePathEntries;
private SourceType sourceType = null; // FIXME !

public boolean hasMoreToResolve() {
return next < modulePathEntries.size();
Expand Down Expand Up @@ -201,7 +203,8 @@ private void discoverModulesIn(@Nonnull Path path) {

private void buildModuleForExplodedModule(@Nonnull Path dir) throws ResolveException {
// create the input location for this module dir
PathBasedAnalysisInputLocation inputLocation = new PathBasedAnalysisInputLocation(dir, null);
PathBasedAnalysisInputLocation inputLocation =
PathBasedAnalysisInputLocation.create(dir, sourceType);

Path moduleInfoFile = dir.resolve(JavaModuleIdentifierFactory.MODULE_INFO_FILE + ".class");
if (!Files.exists(moduleInfoFile) && !Files.isRegularFile(moduleInfoFile)) {
Expand All @@ -223,7 +226,8 @@ private void buildModuleForExplodedModule(@Nonnull Path dir) throws ResolveExcep
* @param jar the jar file
*/
private void buildModuleForJar(@Nonnull Path jar) {
PathBasedAnalysisInputLocation inputLocation = new PathBasedAnalysisInputLocation(jar, null);
PathBasedAnalysisInputLocation inputLocation =
PathBasedAnalysisInputLocation.create(jar, sourceType);
Path mi;
try (FileSystem zipFileSystem = FileSystems.newFileSystem(jar, (ClassLoader) null)) {
final Path archiveRoot = zipFileSystem.getPath("/");
Expand Down
Loading