Skip to content

Commit

Permalink
SingleClassPointing#650 Fmt commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ReshmaSobhaNair committed Oct 12, 2023
1 parent c648b83 commit 64c99bf
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public enum FileType {
WAR("war"),
JIMPLE("jimple");

public static final @Nonnull EnumSet<FileType> ARCHIVE_TYPES = EnumSet.of(JAR, ZIP, APK, WAR,CLASS);
public static final @Nonnull EnumSet<FileType> ARCHIVE_TYPES =
EnumSet.of(JAR, ZIP, APK, WAR, CLASS);

private final @Nonnull String extension;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalNotification;
import com.googlecode.dex2jar.tools.Dex2jarCmd;
import java.io.*;
import java.nio.file.*;
import java.util.*;
Expand All @@ -21,8 +20,6 @@
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.checkerframework.checker.signature.qual.Identifier;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
Expand All @@ -31,9 +28,9 @@
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.inputlocation.FileType;
import sootup.core.model.AbstractClass;
import sootup.core.model.SourceType;
import sootup.core.types.ClassType;
import sootup.core.util.PathUtils;
Expand Down Expand Up @@ -84,6 +81,7 @@ protected PathBasedAnalysisInputLocation(Path path, SourceType srcType) {
this.path = path;
this.sourceType = srcType;
}

protected PathBasedAnalysisInputLocation(Path path) {
this.path = path;
this.sourceType = null;
Expand All @@ -109,11 +107,9 @@ public static PathBasedAnalysisInputLocation create(
inputLocation = new MultiReleaseJarAnalysisInputLocation(path, srcType);
} else if (PathUtils.hasExtension(path, FileType.APK)) {
inputLocation = new ApkAnalysisInputLocation(path, srcType);
} else if(PathUtils.hasExtension(path,FileType.CLASS)){
inputLocation= new ClassFileBasedAnalysisInputLocation(path);
}

else {
} else if (PathUtils.hasExtension(path, FileType.CLASS)) {
inputLocation = new ClassFileBasedAnalysisInputLocation(path, srcType);
} else {
inputLocation = new ArchiveBasedAnalysisInputLocation(path, srcType);
}
} else {
Expand Down Expand Up @@ -148,24 +144,24 @@ private static boolean isMultiReleaseJar(Path path) {

return false;
}

@Nonnull
Optional<? extends AbstractClassSource<JavaSootClass>> createClassSourceForPath(
@Nonnull Path classFilePath,
@Nonnull IdentifierFactory factory,
@Nonnull ClassProvider<JavaSootClass> classProvider) {
@Nonnull Path classFilePath,
@Nonnull IdentifierFactory factory,
@Nonnull ClassProvider<JavaSootClass> classProvider) {
final FileType handledFileType = classProvider.getHandledFileType();
final String moduleInfoFilename = JavaModuleIdentifierFactory.MODULE_INFO_FILE + ".class";

// Check if the file has the correct extension and is not a module info file
if (PathUtils.hasExtension(classFilePath, handledFileType)
&& !classFilePath.toString().endsWith(moduleInfoFilename)) {
return classProvider.createClassSource(this, classFilePath, factory.fromPath(classFilePath.getParent(), classFilePath));
&& !classFilePath.toString().endsWith(moduleInfoFilename)) {
return classProvider.createClassSource(
this, classFilePath, factory.fromPath(classFilePath.getParent(), classFilePath));
}
return Optional.empty();

}


@Nonnull
Collection<? extends AbstractClassSource<JavaSootClass>> walkDirectory(
@Nonnull Path dirPath,
Expand Down Expand Up @@ -202,51 +198,53 @@ protected Optional<? extends AbstractClassSource<JavaSootClass>> getClassSourceI
.getPath(
signature.getFullyQualifiedName().replace('.', '/')
+ classProvider.getHandledFileType().getExtensionWithDot()));

if (!Files.exists(pathToClass)) {
return Optional.empty();
}

return classProvider.createClassSource(this, pathToClass, signature);
}

public static class ClassFileBasedAnalysisInputLocation extends PathBasedAnalysisInputLocation {
protected Optional<? extends AbstractClassSource<JavaSootClass>> getSingleClass(
@Nonnull JavaClassType signature,
@Nonnull Path path,
@Nonnull ClassProvider<JavaSootClass> classProvider) {

public ClassFileBasedAnalysisInputLocation(Path classFilePath) {
super(classFilePath);
if (!classFilePath.toString().endsWith(".class")) {
throw new IllegalArgumentException("Provided path is not a .class file.");
}
Path pathToClass = Paths.get(path.toString());

if (!Files.exists(pathToClass)) {
return Optional.empty();
}

public boolean doesClassExist() {
return Files.exists(path) && Files.isRegularFile(path);
return classProvider.createClassSource(this, pathToClass, signature);
}

private static class ClassFileBasedAnalysisInputLocation extends PathBasedAnalysisInputLocation {
public ClassFileBasedAnalysisInputLocation(
@Nonnull Path classPath, @Nonnull SourceType srcType) {
super(classPath, srcType);
}

@Override
@Nonnull
public Optional<? extends AbstractClassSource<JavaSootClass>> getClassSource(@Nonnull ClassType type, @Nonnull View<?> view) {
return getClassSourceInternal((JavaClassType) type, path, new AsmJavaClassProvider(view));
@Override
@Nonnull
public Optional<? extends AbstractClassSource<JavaSootClass>> getClassSource(
@Nonnull ClassType type, @Nonnull View<?> view) {
return getSingleClass((JavaClassType) type, path, new AsmJavaClassProvider(view));
}

@Nonnull
@Override
public Collection<? extends AbstractClassSource<JavaSootClass>> getClassSources(@Nonnull View<?> view) {
return null;
public Collection<? extends AbstractClassSource<JavaSootClass>> getClassSources(
@Nonnull View<?> view) {
AsmJavaClassProvider classProvider = new AsmJavaClassProvider(view);
IdentifierFactory factory = view.getIdentifierFactory();
Path dirPath = this.path.getParent();
Optional<SootClassSource<JavaSootClass>> classSource =
classProvider.createClassSource(this, path, factory.fromPath(dirPath, path));
return Collections.singletonList(classSource.get());
}


}










private static class DirectoryBasedAnalysisInputLocation extends PathBasedAnalysisInputLocation {

private DirectoryBasedAnalysisInputLocation(@Nonnull Path path, @Nullable SourceType srcType) {
Expand Down Expand Up @@ -556,7 +554,7 @@ private String dex2jar(Path path) {
int start = apkPath.lastIndexOf(File.separator);
int end = apkPath.lastIndexOf(".apk");
String outputFile = outDir + apkPath.substring(start + 1, end) + ".jar";
//Dex2jarCmd.main("-f", apkPath, "-o", outputFile);
// Dex2jarCmd.main("-f", apkPath, "-o", outputFile);
return outputFile;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ public abstract class AnalysisInputLocationTest {
final Path mrj = Paths.get("../shared-test-resources/multi-release-jar/mrjar.jar");
final Path mmrj = Paths.get("../shared-test-resources/multi-release-jar-modular/mrjar.jar");
final Path apk = Paths.get("../shared-test-resources/apk/SimpleApk.apk");
final Path cls = Paths.get("../shared-test-resources/miniTestSuite/java6/binary/AbstractClass.class");
final Path cls =
Paths.get("../shared-test-resources/miniTestSuite/java6/binary/AssertStatement.class");


//final Path cls = Paths.get("../shared-test-resources/miniTestSuite/java6/binary/AbstractClass.class");
// final Path cls =
// Paths.get("../shared-test-resources/miniTestSuite/java6/binary/AbstractClass.class");

protected IdentifierFactory getIdentifierFactory() {
return JavaIdentifierFactory.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import categories.Java8Test;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -39,9 +38,7 @@
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import sootup.core.Project;
import sootup.core.frontend.BodySource;
import sootup.core.inputlocation.AnalysisInputLocation;
import sootup.core.inputlocation.EagerInputLocation;
import sootup.core.model.*;
import sootup.core.signatures.FieldSubSignature;
Expand Down Expand Up @@ -282,21 +279,16 @@ public void testApk() {

@Test
public void testSingleClass() {
Path classFilePath = Paths.get("../shared-test-resources/miniTestSuite/java6/binary/AbstractClass.class");
PathBasedAnalysisInputLocation location = new PathBasedAnalysisInputLocation.ClassFileBasedAnalysisInputLocation(classFilePath);
assertTrue("The .class file does not exist", ((PathBasedAnalysisInputLocation.ClassFileBasedAnalysisInputLocation) location).doesClassExist());



PathBasedAnalysisInputLocation pathBasedNamespace =
PathBasedAnalysisInputLocation.create(cls, null);
final ClassType mainClass = getIdentifierFactory().getClassType("AssertStatement");
testClassReceival(pathBasedNamespace, Collections.singletonList(mainClass), 1);

This comment has been minimized.

Copy link
@swissiety

swissiety Oct 12, 2023

Collaborator

please test it with a ClassType that contains a package name as well

}



@Test
public void testJar() {
PathBasedAnalysisInputLocation pathBasedNamespace =
PathBasedAnalysisInputLocation.create(jar, null);

ArrayList<ClassType> sigs = new ArrayList<>();
sigs.add(getIdentifierFactory().getClassType("Employee", "ds"));
sigs.add(getIdentifierFactory().getClassType("MiniApp"));
Expand Down

0 comments on commit 64c99bf

Please sign in to comment.