Represents result of side-effect analysis.
| 39 | * Represents result of side-effect analysis. |
| 40 | */ |
| 41 | public class SideEffect implements StmtResult<Set<Obj>> { |
| 42 | |
| 43 | /** |
| 44 | * Maps from a method to all objects directly or indirectly modified by it. |
| 45 | */ |
| 46 | private final Map<JMethod, Set<Obj>> methodMods; |
| 47 | |
| 48 | /** |
| 49 | * Maps from a stmt to the objects directly modified by it. |
| 50 | */ |
| 51 | private final Map<Stmt, Set<Obj>> stmtDirectMods; |
| 52 | |
| 53 | private final CallGraph<Invoke, JMethod> callGraph; |
| 54 | |
| 55 | SideEffect(Map<JMethod, Set<Obj>> methodMods, |
| 56 | Map<Stmt, Set<Obj>> stmtDirectMods, |
| 57 | CallGraph<Invoke, JMethod> callGraph) { |
| 58 | this.methodMods = methodMods; |
| 59 | this.stmtDirectMods = stmtDirectMods; |
| 60 | this.callGraph = callGraph; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @return set of objects that may be modified by given method. |
| 65 | */ |
| 66 | public Set<Obj> getModifiedObjects(JMethod method) { |
| 67 | return methodMods.getOrDefault(method, Set.of()); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @return set of objects that may be modified by given stmt. |
| 72 | */ |
| 73 | public Set<Obj> getModifiedObjects(Stmt stmt) { |
| 74 | if (stmt instanceof Invoke invoke) { |
| 75 | // to save space, we compute modified objects of |
| 76 | // Invoke stmt on demand, and do not cache them |
| 77 | return callGraph.getCalleesOf(invoke) |
| 78 | .stream() |
| 79 | .map(this::getModifiedObjects) |
| 80 | .flatMap(Set::stream) |
| 81 | .collect(Collectors.toUnmodifiableSet()); |
| 82 | } |
| 83 | return stmtDirectMods.getOrDefault(stmt, Set.of()); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @return {@code true} if given method does not modify any objects. |
| 88 | */ |
| 89 | public boolean isPure(JMethod method) { |
| 90 | return !methodMods.containsKey(method); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean isRelevant(Stmt stmt) { |
| 95 | return stmt instanceof Invoke || |
| 96 | stmt instanceof StoreArray || |
| 97 | (stmt instanceof StoreField storeField && |
| 98 | !storeField.isStatic()); |
nothing calls this directly
no outgoing calls
no test coverage detected