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

BAEL-7881: Introduction to SootUp #18106

Open
wants to merge 3 commits into
base: master
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
21 changes: 21 additions & 0 deletions libraries-5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@
<artifactId>lanterna</artifactId>
<version>${lanterna.version}</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.core</artifactId>
<version>${sootup.version}</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.java.core</artifactId>
<version>${sootup.version}</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.java.sourcecode</artifactId>
<version>${sootup.version}</version>
</dependency>
<dependency>
<groupId>org.soot-oss</groupId>
<artifactId>sootup.java.bytecode</artifactId>
<version>${sootup.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -149,6 +169,7 @@
<armeria.version>1.29.2</armeria.version>
<yauaa.version>7.28.1</yauaa.version>
<yavi.version>0.14.1</yavi.version>
<sootup.version>1.3.0</sootup.version>
</properties>

</project>
58 changes: 58 additions & 0 deletions libraries-5/src/test/java/com/baeldung/sootup/AnalyzeUnitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.baeldung.sootup;

import org.junit.jupiter.api.Test;
import sootup.core.inputlocation.AnalysisInputLocation;
import sootup.java.bytecode.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.java.bytecode.inputlocation.JrtFileSystemAnalysisInputLocation;
import sootup.java.bytecode.inputlocation.OTFCompileAnalysisInputLocation;
import sootup.java.core.views.JavaView;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AnalyzeUnitTest {
@Test
void whenAnalyzingTheJvm_thenWeCanListClasses() {
AnalysisInputLocation inputLocation = new JrtFileSystemAnalysisInputLocation();

JavaView view = new JavaView(inputLocation);

assertThat(view.getClasses()).isNotEmpty();
}

@Test
void whenAnalyzingThisTestClass_thenWeCanListClasses() {
Path javaFile = Path.of("src/test/java/com/baeldung/sootup/AnalyzeUnitTest.java");
AnalysisInputLocation inputLocation = new OTFCompileAnalysisInputLocation(javaFile);

JavaView view = new JavaView(inputLocation);

assertEquals(1, view.getClasses().size());
}

@Test
void whenAnalyzingAString_thenWeCanListClasses() throws IOException {
Path javaFile = Path.of("src/test/java/com/baeldung/sootup/AnalyzeUnitTest.java");
String javaContents = Files.readString(javaFile);

AnalysisInputLocation inputLocation = new OTFCompileAnalysisInputLocation("AnalyzeUnitTest.java", javaContents);

JavaView view = new JavaView(inputLocation);

assertEquals(1, view.getClasses().size());
}

@Test
void whenAnalyzingCompiledByteCode_thenWeCanListClasses() {
AnalysisInputLocation inputLocation = new JavaClassPathAnalysisInputLocation("target/classes");

JavaView view = new JavaView(inputLocation);

assertThat(view.getClasses()).isNotEmpty();
}
}
72 changes: 72 additions & 0 deletions libraries-5/src/test/java/com/baeldung/sootup/ClassUnitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.baeldung.sootup;

import org.junit.jupiter.api.Test;
import sootup.core.IdentifierFactory;
import sootup.core.inputlocation.AnalysisInputLocation;
import sootup.core.model.SootClass;
import sootup.core.types.ClassType;
import sootup.java.bytecode.inputlocation.OTFCompileAnalysisInputLocation;
import sootup.java.core.JavaSootClass;
import sootup.java.core.views.JavaView;

import java.nio.file.Path;
import java.util.Optional;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

public class ClassUnitTest {
@Test
void whenAnalyzingThisTestClass_thenWeCanGetASingleClass() {
Path javaFile = Path.of("src/test/java/com/baeldung/sootup/ClassUnitTest.java");
AnalysisInputLocation inputLocation = new OTFCompileAnalysisInputLocation(javaFile);

JavaView view = new JavaView(inputLocation);

IdentifierFactory identifierFactory = view.getIdentifierFactory();
ClassType javaClass = identifierFactory.getClassType("com.baeldung.sootup.ClassUnitTest");

Optional<JavaSootClass> sootClass = view.getClass(javaClass);
assertTrue(sootClass.isPresent());

JavaSootClass classUnitTest = sootClass.get();
assertTrue(classUnitTest.isPublic());
assertTrue(classUnitTest.isConcrete());
assertFalse(classUnitTest.isFinal());
assertFalse(classUnitTest.isEnum());
}

@Test
void whenAnalyzingThisTestClass_thenWeCanGetTheSuperclass() {
Path javaFile = Path.of("src/test/java/com/baeldung/sootup/ClassUnitTest.java");
AnalysisInputLocation inputLocation = new OTFCompileAnalysisInputLocation(javaFile);

JavaView view = new JavaView(inputLocation);

IdentifierFactory identifierFactory = view.getIdentifierFactory();
ClassType javaClass = identifierFactory.getClassType("com.baeldung.sootup.ClassUnitTest");

SootClass sootClass = view.getClassOrThrow(javaClass);
Optional<? extends ClassType> superclass = sootClass.getSuperclass();

assertTrue(superclass.isPresent());
assertEquals("java.lang.Object", superclass.get().getFullyQualifiedName());
}

@Test
void whenAnalyzingThisTestClass_thenWeCanGetTheInterfaces() {
Path javaFile = Path.of("src/test/java/com/baeldung/sootup/ClassUnitTest.java");
AnalysisInputLocation inputLocation = new OTFCompileAnalysisInputLocation(javaFile);

JavaView view = new JavaView(inputLocation);

IdentifierFactory identifierFactory = view.getIdentifierFactory();
ClassType javaClass = identifierFactory.getClassType("com.baeldung.sootup.ClassUnitTest");

SootClass sootClass = view.getClassOrThrow(javaClass);
Set<? extends ClassType> interfaces = sootClass.getInterfaces();

assertTrue(interfaces.isEmpty());
}

}
54 changes: 54 additions & 0 deletions libraries-5/src/test/java/com/baeldung/sootup/FieldUnitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.baeldung.sootup;

import org.junit.jupiter.api.Test;
import sootup.core.IdentifierFactory;
import sootup.core.inputlocation.AnalysisInputLocation;
import sootup.core.model.SootClass;
import sootup.core.model.SootField;
import sootup.core.types.ClassType;
import sootup.java.bytecode.inputlocation.OTFCompileAnalysisInputLocation;
import sootup.java.core.views.JavaView;

import java.nio.file.Path;
import java.util.Optional;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

public class FieldUnitTest {
private String aField;

@Test
void whenAnalyzingThisClass_thenWeCanAccessFields() {
Path javaFile = Path.of("src/test/java/com/baeldung/sootup/FieldUnitTest.java");
AnalysisInputLocation inputLocation = new OTFCompileAnalysisInputLocation(javaFile);

JavaView view = new JavaView(inputLocation);

IdentifierFactory identifierFactory = view.getIdentifierFactory();
ClassType javaClass = identifierFactory.getClassType("com.baeldung.sootup.FieldUnitTest");

SootClass sootClass = view.getClassOrThrow(javaClass);
Set<? extends SootField> fields = sootClass.getFields();
assertEquals(1, fields.size());
}

@Test
void whenAnalyzingThisClass_thenWeCanAccessASingleField() {
Path javaFile = Path.of("src/test/java/com/baeldung/sootup/FieldUnitTest.java");
AnalysisInputLocation inputLocation = new OTFCompileAnalysisInputLocation(javaFile);

JavaView view = new JavaView(inputLocation);

IdentifierFactory identifierFactory = view.getIdentifierFactory();
ClassType javaClass = identifierFactory.getClassType("com.baeldung.sootup.FieldUnitTest");

SootClass sootClass = view.getClassOrThrow(javaClass);
Optional<? extends SootField> field = sootClass.getField("aField");
assertTrue(field.isPresent());

SootField sootField = field.get();
assertTrue(sootField.isPrivate());
assertFalse(sootField.isStatic());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.baeldung.sootup;

import org.junit.jupiter.api.Test;
import sootup.core.IdentifierFactory;
import sootup.core.graph.StmtGraph;
import sootup.core.inputlocation.AnalysisInputLocation;
import sootup.core.jimple.common.stmt.Stmt;
import sootup.core.model.Body;
import sootup.core.model.SootClass;
import sootup.core.model.SootMethod;
import sootup.core.types.ClassType;
import sootup.java.bytecode.inputlocation.OTFCompileAnalysisInputLocation;
import sootup.java.core.views.JavaView;

import java.nio.file.Path;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class MethodBodyUnitTest {
@Test
void whenAnalyzingAMethod_thenWeCanAccessTheLocals() {
Path javaFile = Path.of("src/test/java/com/baeldung/sootup/MethodBodyUnitTest.java");
AnalysisInputLocation inputLocation = new OTFCompileAnalysisInputLocation(javaFile);

JavaView view = new JavaView(inputLocation);

IdentifierFactory identifierFactory = view.getIdentifierFactory();
ClassType javaClass = identifierFactory.getClassType("com.baeldung.sootup.MethodBodyUnitTest");

SootClass sootClass = view.getClassOrThrow(javaClass);
Optional<? extends SootMethod> method = sootClass.getMethod("someMethod",
List.of(
identifierFactory.getClassType("java.lang.String")
));
assertTrue(method.isPresent());

SootMethod sootMethod = method.get();

Body methodBody = sootMethod.getBody();
assertThat(methodBody.getLocalCount()).isGreaterThan(0);
var thisLocal = methodBody.getLocals()
.stream()
.filter(local -> local.getName().equals("this"))
.findFirst();
assertTrue(thisLocal.isPresent());
assertEquals(javaClass, thisLocal.get().getType());
}

@Test
void whenAnalyzingAMethod_thenWeCanAccessTheCallGraph() {
Path javaFile = Path.of("src/test/java/com/baeldung/sootup/MethodBodyUnitTest.java");
AnalysisInputLocation inputLocation = new OTFCompileAnalysisInputLocation(javaFile);

JavaView view = new JavaView(inputLocation);

IdentifierFactory identifierFactory = view.getIdentifierFactory();
ClassType javaClass = identifierFactory.getClassType("com.baeldung.sootup.MethodBodyUnitTest");

SootClass sootClass = view.getClassOrThrow(javaClass);
Optional<? extends SootMethod> method = sootClass.getMethod("someMethod",
List.of(
identifierFactory.getClassType("java.lang.String")
));
assertTrue(method.isPresent());

SootMethod sootMethod = method.get();

Body methodBody = sootMethod.getBody();
StmtGraph<?> stmtGraph = methodBody.getStmtGraph();
List<Stmt> stmts = stmtGraph.getStmts();

assertThat(stmts).hasSize(7);
assertThat(stmts.get(0)).asString().isEqualTo("this := @this: com.baeldung.sootup.MethodBodyUnitTest");
assertThat(stmts.get(1)).asString().isEqualTo("l1 := @parameter0: java.lang.String");
assertThat(stmts.get(2)).asString().isEqualTo("l2 = virtualinvoke l1.<java.lang.String: java.lang.String toUpperCase()>()");
assertThat(stmts.get(3)).asString().isEqualTo("$stack3 = <java.lang.System: java.io.PrintStream out>");
assertThat(stmts.get(4)).asString().isEqualTo("$stack4 = dynamicinvoke \"makeConcatWithConstants\" <java.lang.String (java.lang.String)>(l2) <java.lang.invoke.StringConcatFactory: java.lang.invoke.CallSite makeConcatWithConstants(java.lang.invoke.MethodHandles$Lookup,java.lang.String,java.lang.invoke.MethodType,java.lang.String,java.lang.Object[])>(\"Hello, \\u0001\")");
assertThat(stmts.get(5)).asString().isEqualTo("virtualinvoke $stack3.<java.io.PrintStream: void println(java.lang.String)>($stack4)");
assertThat(stmts.get(6)).asString().isEqualTo("return");
}


private void someMethod(String name) {
var capitals = name.toUpperCase();
System.out.println("Hello, " + capitals);
}
}
77 changes: 77 additions & 0 deletions libraries-5/src/test/java/com/baeldung/sootup/MethodUnitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.baeldung.sootup;

import org.junit.jupiter.api.Test;
import sootup.core.IdentifierFactory;
import sootup.core.inputlocation.AnalysisInputLocation;
import sootup.core.model.SootClass;
import sootup.core.model.SootMethod;
import sootup.core.types.ClassType;
import sootup.java.bytecode.inputlocation.OTFCompileAnalysisInputLocation;
import sootup.java.core.views.JavaView;

import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class MethodUnitTest {

@Test
void whenAnalyzingThisClass_thenWeCanAccessMethods() {
Path javaFile = Path.of("src/test/java/com/baeldung/sootup/MethodUnitTest.java");
AnalysisInputLocation inputLocation = new OTFCompileAnalysisInputLocation(javaFile);

JavaView view = new JavaView(inputLocation);

IdentifierFactory identifierFactory = view.getIdentifierFactory();
ClassType javaClass = identifierFactory.getClassType("com.baeldung.sootup.MethodUnitTest");

SootClass sootClass = view.getClassOrThrow(javaClass);
Set<? extends SootMethod> methods = sootClass.getMethods();
assertThat(methods).isNotEmpty();
}

@Test
void whenAnalyzingThisClass_thenWeCanAccessASingleMethod() {
Path javaFile = Path.of("src/test/java/com/baeldung/sootup/MethodUnitTest.java");
AnalysisInputLocation inputLocation = new OTFCompileAnalysisInputLocation(javaFile);

JavaView view = new JavaView(inputLocation);

IdentifierFactory identifierFactory = view.getIdentifierFactory();
ClassType javaClass = identifierFactory.getClassType("com.baeldung.sootup.MethodUnitTest");

SootClass sootClass = view.getClassOrThrow(javaClass);
Optional<? extends SootMethod> method = sootClass.getMethod("someMethod",
List.of(
identifierFactory.getClassType("java.lang.String")
));
assertTrue(method.isPresent());

SootMethod sootMethod = method.get();
assertTrue(sootMethod.isPrivate());
assertTrue(sootMethod.isConcrete());
}

@Test
void whenAnalyzingThisClass_thenWeCanListMethodsByName() {
Path javaFile = Path.of("src/test/java/com/baeldung/sootup/MethodUnitTest.java");
AnalysisInputLocation inputLocation = new OTFCompileAnalysisInputLocation(javaFile);

JavaView view = new JavaView(inputLocation);

IdentifierFactory identifierFactory = view.getIdentifierFactory();
ClassType javaClass = identifierFactory.getClassType("com.baeldung.sootup.MethodUnitTest");

SootClass sootClass = view.getClassOrThrow(javaClass);
Set<? extends SootMethod> method = sootClass.getMethodsByName("someMethod");
assertEquals(2, method.size());
}

private void someMethod(String name) {}
private void someMethod(int value) {}
}