From e0a5cad879d96fd11edf0c67020e84d937356803 Mon Sep 17 00:00:00 2001 From: liyiwei <979621500@qq.com> Date: Sat, 7 Dec 2024 23:03:48 +0800 Subject: [PATCH] use stream and group by --- .../src/main/java/sootup/core/model/Body.java | 23 ++++++++----------- .../java/sootup/interceptors/Aggregator.java | 2 +- .../DeadAssignmentEliminator.java | 2 +- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/model/Body.java b/sootup.core/src/main/java/sootup/core/model/Body.java index 7e8606a9088..720e9d82ee2 100644 --- a/sootup.core/src/main/java/sootup/core/model/Body.java +++ b/sootup.core/src/main/java/sootup/core/model/Body.java @@ -26,9 +26,11 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.util.*; +import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import org.apache.commons.lang3.tuple.Pair; import sootup.core.graph.MutableBlockStmtGraph; import sootup.core.graph.MutableStmtGraph; import sootup.core.graph.StmtGraph; @@ -497,21 +499,16 @@ public static Map> collectDefs(Collection stmts) } /** - * Collects all using statements of a Local from a list of statements + * Collects all using statements of a Values from a list of statements * * @param stmts The searched list of statements - * @return A map of Locals and their using statements + * @return A map of Values and their using statements */ - public static Map> collectUses(Collection stmts) { - Map> allUses = new HashMap<>(); - for (Stmt stmt : stmts) { - for (Iterator iterator = stmt.getUses().iterator(); iterator.hasNext(); ) { - Value value = iterator.next(); - Collection localUses = allUses.computeIfAbsent(value, key -> new ArrayList<>()); - localUses.add(stmt); - allUses.put(value, localUses); - } - } - return allUses; + public static Map> collectUses(Collection stmts) { + return stmts.stream() + .flatMap(stmt -> stmt.getUses().map(value -> (Pair.of(value, stmt)))) + .collect( + Collectors.groupingBy( + Pair::getLeft, Collectors.mapping(Pair::getRight, Collectors.toList()))); } } diff --git a/sootup.interceptors/src/main/java/sootup/interceptors/Aggregator.java b/sootup.interceptors/src/main/java/sootup/interceptors/Aggregator.java index aaa1cab3727..d054bcc2b38 100644 --- a/sootup.interceptors/src/main/java/sootup/interceptors/Aggregator.java +++ b/sootup.interceptors/src/main/java/sootup/interceptors/Aggregator.java @@ -69,7 +69,7 @@ public Aggregator(boolean dontAggregateFieldLocals) { public void interceptBody(@Nonnull Body.BodyBuilder builder, @Nonnull View view) { MutableStmtGraph graph = builder.getStmtGraph(); List stmts = builder.getStmts(); - Map> usesMap = Body.collectUses(stmts); + Map> usesMap = Body.collectUses(stmts); for (Stmt stmt : stmts) { if (!(stmt instanceof JAssignStmt)) { diff --git a/sootup.interceptors/src/main/java/sootup/interceptors/DeadAssignmentEliminator.java b/sootup.interceptors/src/main/java/sootup/interceptors/DeadAssignmentEliminator.java index a77a0df6bc8..ca2b1acea45 100644 --- a/sootup.interceptors/src/main/java/sootup/interceptors/DeadAssignmentEliminator.java +++ b/sootup.interceptors/src/main/java/sootup/interceptors/DeadAssignmentEliminator.java @@ -211,7 +211,7 @@ public void interceptBody(@Nonnull Body.BodyBuilder builder, @Nonnull View view) return; } - Map> essentialUses = Body.collectUses(essentialStmts); + Map> essentialUses = Body.collectUses(essentialStmts); // Eliminate dead assignments from invokes such as x = f(), where x is no longer used List postProcess = new ArrayList<>(); for (Stmt stmt : stmts) {