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 1 commit
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>
57 changes: 57 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,57 @@
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.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AnalyzeUnitTest {
@Test
void whenAnalyzingTheJVM_thenWeCanListClasses() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We follow https://google.github.io/styleguide/javaguide.html#s5.3-camel-case

Suggested change
void whenAnalyzingTheJVM_thenWeCanListClasses() {
void whenAnalyzingTheJvm_thenWeCanListClasses() {

AnalysisInputLocation inputLocation = new JrtFileSystemAnalysisInputLocation();

JavaView view = new JavaView(inputLocation);

assertTrue(view.getClasses().size() > 0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For complex assertions we use AssertJ

}

@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);

assertTrue(view.getClasses().size() > 0);
}
}
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,84 @@
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.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();
assertTrue(methodBody.getLocalCount() > 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();
for (Stmt stmt : stmts) {
System.out.println(stmt);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we assert here instead of printing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, but I was concerned that this would be a bit flaky. It's possible that the exact format of the statements - or even the number of them - would change between versions of SootUp, and maybe between versions of Java.

However, I've updated it as you said so if you want to keep it then it's there :)

}
}


private void someMethod(String name) {
var capitald = name.toUpperCase();
System.out.println("Hello, " + capitald);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a guideline to use SLF4J not System.out for print statements

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method isn't being used. It's purely here so that it can be analyzed. I just needed to do something with the capitals variable was all.

}
}
75 changes: 75 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,75 @@
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.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();
assertTrue(methods.size() > 0);
}

@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(1, method.size());
}

private void someMethod(String name) {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we show multiple overloaded methods with the same name listed here perhaps?

}