-
Notifications
You must be signed in to change notification settings - Fork 54.4k
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() { | ||
AnalysisInputLocation inputLocation = new JrtFileSystemAnalysisInputLocation(); | ||
|
||
JavaView view = new JavaView(inputLocation); | ||
|
||
assertTrue(view.getClasses().size() > 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
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()); | ||
} | ||
|
||
} |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we assert here instead of printing? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
} |
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) {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
} |
There was a problem hiding this comment.
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