Skip to content

Commit

Permalink
add StmtVisitor doc
Browse files Browse the repository at this point in the history
  • Loading branch information
swissiety committed Jun 18, 2024
1 parent 065d77e commit 6b1ac22
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/analysisinputlocations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
An AnalysisInputLocation tells SootUp what code input it should analyze.

### Java Runtime
- Java <9: `DefaultRTJaAnalysisInputLocation` current rt.jar (or point to any rt.jar as its just a usual .jar file)
- Java >8: `JRTFilesystemAnalysisInputLocation`
- Java <=8: `DefaultRTJaAnalysisInputLocation` current rt.jar (or point to any rt.jar as its just a usual .jar file)
- Java >=9: `JRTFilesystemAnalysisInputLocation`

If you have errors like Java.lang.String, Java.lang.Object, ... you are most likely missing this AnalysisInput.

Expand Down
42 changes: 42 additions & 0 deletions docs/jimple-stmts.md
Original file line number Diff line number Diff line change
Expand Up @@ -1171,3 +1171,45 @@ Ends the execution inside the current Method if the thrown exception is not caug
MAXLOCALS = 3
}
```

## Good to know
A lot of the SootUp APIs return the `Stmt` Interface. To determine and handle its subtypes you can make use of instanceof.
=== "Stmt If-Else forest"
```java

List<Stmt> stmts = ... ;
for( Stmt stms : stmts ){
if(stmt instanceof JAssignStmt){
// found a JAssignStmt
Value rhsOp = ((JAssignStmt) stmt).getRightOp();
...
}else if(stmt instanceof JInvokeStmt){
// found a JInvokeStmt
JInvokeStmt ivkStmt = ((JInvokeStmt) stmt);
MethodSignature rhsOp = ivkStmt.getInvokeExpr().getMethodSignature();
...
}
}

```

But this could escalate to a huge if-else-tree - almost a forest. To mitigate such scenario you can implement a subclass of `AbstractStmtVisitor`.
Just subclass the methods to the respective Stmts you need to handle.
=== "StmtVisitor"
```java

List<Stmt> stmts = ...;
AbstractStmtVisitor visitor = new AbstractStmtVisitor<Integer>() {
private int countAssignStmts = 0;
@Override
public void caseIfStmt(@Nonnull JAssignStmt stmt) {
countAssignStmts++;
}
};

for( Stmt stms : stmts ){
stmt.accept(visitor);
}

int amountOfAssignStmts = visitor.getResult();
```

0 comments on commit 6b1ac22

Please sign in to comment.