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

Experimental Spoon Setup for AST creation and traversal #41

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
57 changes: 57 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,65 @@
<version>4.13.1</version>
<scope>test</scope>
</dependency>

<!-- http://spoon.gforge.inria.fr/ -->
<dependency>
<groupId>fr.inria.gforge.spoon</groupId>
<artifactId>spoon-core</artifactId>
<version>10.4.1</version>
</dependency>

<!-- sootup all module dependency (remove unnecessary later)-->

<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.core</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.java.core</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.java.sourcecode</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.java.bytecode</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.jimple.parser</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.callgraph</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.analysis</artifactId>
<version>1.2.0</version>
</dependency>

</dependencies>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>/maven.google.com</id>
<url>https://maven.google.com</url>
</repository>
</repositories>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/tum/dpid/App.java

This file was deleted.

81 changes: 81 additions & 0 deletions src/main/java/tum/dpid/Runner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package tum.dpid;

import spoon.Launcher;
import spoon.reflect.CtModel;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.filter.TypeFilter;
import tum.dpid.services.DatabaseMethodFinder;
import tum.dpid.services.processors.ClassHierarchyOrder;
import tum.dpid.services.processors.MethodCallChain;
import tum.dpid.services.processors.MethodOrder;

import java.util.List;
import java.util.Map;
import java.util.Set;


/**
* Runner class of method call chain processor and analyzer by utilizing spoon
*
*/
public class Runner
{

public static void main( String[] args )
{
Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);

//String testDirectory = "src/main/resources/tester"; //"../../../resources/tester";
String directoryPath = "../../fromItestra/LoopAntiPattern";
launcher.addInputResource(directoryPath);
launcher.buildModel();
CtModel model = launcher.getModel();

//All Java Classes in the project
List<CtClass<?>> allClasses = model.getElements(new TypeFilter<>(CtClass.class));

//All Methods in the project
List<CtMethod<?>> allMethods = model.getElements(new TypeFilter<>(CtMethod.class));

// Methods which makes request to database in project
Set<CtMethod<?>> databaseMethods = DatabaseMethodFinder.findDatabaseMethods(model, allClasses);
for (CtMethod<?> method : databaseMethods) {
System.out.println("Database Method is: " + method.getSignature() + " (" + method.getDeclaringType().getQualifiedName() + ")");
}

//Initialize class hierarchy order processor and start processing it
ClassHierarchyOrder classHierarchyOrder = new ClassHierarchyOrder();
launcher.addProcessor(classHierarchyOrder);
launcher.process();

//Initialize method execution order processor (call chain of method) and start processing it
MethodOrder methodOrder = new MethodOrder();
launcher.addProcessor(methodOrder);
launcher.process();

Map<CtExecutableReference<?>, List<CtExecutableReference<?>>> callList = methodOrder.getCallList();
Map<CtTypeReference<?>, Set<CtTypeReference<?>>> classHierarchy = classHierarchyOrder.getClassImplementors() ;

//Process each method in the project and print out their call chain
for (CtMethod<?> ctMethod: allMethods) {
List<MethodCallChain> methodCallHierarchies = MethodCallChain.processMethod(ctMethod, callList, classHierarchy);
if (methodCallHierarchies.isEmpty()) {
System.out.println("No method `" + ctMethod.getDeclaringType() + "` found. \n");
}
if (methodCallHierarchies.size() > 1) {
System.out.println("Found " + methodCallHierarchies.size() + " matching methods...\n");
}
for (MethodCallChain methodCallHierarchy : methodCallHierarchies) {
methodCallHierarchy.printCallChain();
System.out.println();
}
}
}


}

88 changes: 88 additions & 0 deletions src/main/java/tum/dpid/cfg/CfgExtractor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package tum.dpid.cfg;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Optional;

import sootup.callgraph.CallGraph;
import sootup.callgraph.CallGraphAlgorithm;
import sootup.callgraph.ClassHierarchyAnalysisAlgorithm;
import sootup.core.inputlocation.AnalysisInputLocation;
import sootup.core.jimple.common.expr.JVirtualInvokeExpr;
import sootup.core.jimple.common.stmt.JInvokeStmt;
import sootup.core.model.SootClass;
import sootup.core.model.SootMethod;
import sootup.core.signatures.MethodSignature;
import sootup.core.typehierarchy.ViewTypeHierarchy;
import sootup.core.types.ClassType;
import sootup.core.types.VoidType;
import sootup.core.util.DotExporter;
import sootup.core.views.View;
import sootup.java.bytecode.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.java.bytecode.inputlocation.PathBasedAnalysisInputLocation;
import sootup.java.core.JavaIdentifierFactory;
import sootup.java.core.JavaSootMethod;
import sootup.java.core.language.JavaJimple;
import sootup.java.core.types.JavaClassType;
import sootup.java.core.views.JavaView;
import sootup.java.sourcecode.inputlocation.JavaSourcePathAnalysisInputLocation;

/**
* Experimental class to extract control flow graph of project by using SootUp
*/

public class CfgExtractor {

public void CfgExtractorFunc(String inputPath){

AnalysisInputLocation inputLocation =
new JavaClassPathAnalysisInputLocation("target/classes");

JavaView view = new JavaView(inputLocation);

System.out.println("All Class: " + view.getClasses());

JavaClassType classType =
view.getIdentifierFactory().getClassType("tum.dpid.App");

if (!view.getClass(classType).isPresent()) {
System.out.println("Class not found!");
return;
}

// Retrieve the specified class from the project.
SootClass sootClass = view.getClass(classType).get();


MethodSignature methodSignature = view.getIdentifierFactory().getMethodSignature(
classType, "main", "void", Collections.singletonList("java.lang.String[]"));

Optional<JavaSootMethod> opt = view.getMethod(methodSignature);

// Create type hierarchy and CHA
final ViewTypeHierarchy typeHierarchy = new ViewTypeHierarchy(view);

CallGraphAlgorithm cha = new ClassHierarchyAnalysisAlgorithm(view);

// Create CG by initializing CHA with entry method(s)
CallGraph cg = cha.initialize(Collections.singletonList(methodSignature));

cg.callsFrom(methodSignature).forEach(System.out::println);

var x = DotExporter.createUrlToWebeditor( opt.get().getBody().getStmtGraph());

System.out.println("Dot Graph is " + x);

//ystem.out.println(cg);
}

/* //CFG with sootup
String sourcePath = "../../fromItestra/LoopAntiPattern";
String binaryPath = "../../fromItestra/LoopAntiPattern/build/classes/java/main/com/example/LoopAntiPattern" ; //LoopAntiPattern-0.0.1-SNAPSHOT.jar"
CfgExtractor cfgExtractor = new CfgExtractor();
cfgExtractor.CfgExtractorFunc(binaryPath);
System.out.println("*************************************************************************************************************************************");

*/
}
49 changes: 49 additions & 0 deletions src/main/java/tum/dpid/services/DatabaseMethodFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package tum.dpid.services;

import spoon.reflect.CtModel;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.ModifierKind;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DatabaseMethodFinder {

public static Set<CtMethod<?>> findDatabaseMethods(CtModel model, List<CtClass<?>> allClasses) {
Set<CtMethod<?>> databaseMethods = new HashSet<>();

for (CtClass<?> ctClass : allClasses) {
if (isDatabaseClass(ctClass)) {
for(CtMethod<?> method : ctClass.getMethods()) {
if (!isGetterOrSetter(method)) {
databaseMethods.add(method);
}
}
}
}
return databaseMethods;
}

private static boolean isDatabaseClass(CtClass<?> ctClass) {
// Define the package name where database-related classes are located
String databasePackage = "com.example.LoopAntiPattern.data.repository";
return ctClass.getPackage().getQualifiedName().startsWith(databasePackage);
}

private static boolean isGetterOrSetter(CtMethod<?> method) {
String methodName = method.getSimpleName();
boolean isStatic = method.getModifiers().contains(ModifierKind.STATIC);

// Check for getter method
boolean isGetter = !isStatic && method.getParameters().isEmpty() &&
(methodName.startsWith("get") || methodName.startsWith("is"));

// Check for setter method
boolean isSetter = !isStatic && method.getParameters().size() == 1 &&
methodName.startsWith("set");

return isGetter || isSetter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package tum.dpid.services.processors;

import spoon.processing.AbstractProcessor;
import spoon.reflect.reference.CtTypeReference;
import spoon.support.reflect.declaration.CtClassImpl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ClassHierarchyOrder extends AbstractProcessor<CtClassImpl<?>> {

private final Map<CtTypeReference<?>, Set<CtTypeReference<?>>> classImplementors = new HashMap<>();

public void findInheritance(CtTypeReference<?> classRef, CtTypeReference<?> superClass) {
Set<CtTypeReference<?>> subclasses = classImplementors.computeIfAbsent(superClass, k -> new HashSet<>());
subclasses.add(classRef);
}

@Override
public void process(CtClassImpl ctClass) {
if (ctClass.getReference().isAnonymous()) {
return;
}
if (ctClass.getSuperclass() != null) {
findInheritance(ctClass.getReference(), ctClass.getSuperclass());
}
for (Object o : ctClass.getSuperInterfaces()) {
CtTypeReference<?> superclass = (CtTypeReference<?>) o;
findInheritance(ctClass.getReference(), superclass);
}
}

public Map<CtTypeReference<?>, Set<CtTypeReference<?>>> getClassImplementors() {
return classImplementors;
}

}
Loading