Skip to content

Commit

Permalink
top referenced calss/method 不仅显示名称还包括访问次数
Browse files Browse the repository at this point in the history
  • Loading branch information
gaopeng71 committed Sep 4, 2023
1 parent a27eda0 commit 953877d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.github.dddplus.ast.view.CallGraphRenderer;
import io.github.dddplus.bce.CallGraphConfig;
import io.github.dddplus.bce.CallGraphParser;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand Down Expand Up @@ -61,12 +62,12 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.withReport(report)
.render();
getLog().info("Top referenced callee classes:");
for (String callee : renderer.topReferencedCallee(topCalleeN)) {
getLog().info(callee);
for (Pair<String, Integer> stat: renderer.topReferencedCallee(topCalleeN)) {
getLog().info(String.format("%s %d", stat.getLeft(), stat.getRight()));
}
getLog().info("Top referenced callee methods:");
for (String method : renderer.topReferencedCalleeMethods(topCalleeN)) {
getLog().info(method);
for (Pair<String, Integer> stat: renderer.topReferencedCalleeMethods(topCalleeN)) {
getLog().info(String.format("%s %d", stat.getLeft(), stat.getRight()));
}
getLog().info(String.format("Now you can convert dot file to svg: dot -Tsvg %s -O", targetFile));
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.github.dddplus.ast.model.CallGraphEntry;
import io.github.dddplus.ast.parser.JavaParserUtil;
import io.github.dddplus.ast.report.CallGraphReport;
import org.apache.commons.lang3.tuple.Pair;

import java.io.IOException;
import java.util.*;
Expand Down Expand Up @@ -40,27 +41,28 @@ public CallGraphRenderer splines(String splines) {
return this;
}

public List<String> topReferencedCallee(int k) {
public List<Pair<String, Integer>> topReferencedCallee(int k) {
return topKByValue(calleeRefCounter, k);
}

public List<String> topReferencedCalleeMethods(int k) {
public List<Pair<String, Integer>> topReferencedCalleeMethods(int k) {
return topKByValue(calleeMethodRefCounter, k);
}

private List<String> topKByValue(Map<String, Integer> map, int k) {
private List<Pair<String, Integer>> topKByValue(Map<String, Integer> map, int k) {
List<Map.Entry<String, Integer>> sortedList = new ArrayList<>(map.entrySet());
sortedList.sort(Comparator.comparing(Map.Entry::getValue, Comparator.reverseOrder()));
List<String> result = new ArrayList<>(k);
List<Pair<String, Integer>> result = new ArrayList<>(k);
int n = 0;
for (Map.Entry<String, Integer> entry : sortedList) {
result.add(entry.getKey());
result.add(Pair.of(entry.getKey(), entry.getValue()));
n++;
if (n == k) {
break;
}
}


return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ private boolean builtinIgnoreCaller(MethodVisitor m) {
return true;
}

if (false && m.callerMethod.equals(methodConstructor)) {
return true;
}

if (m.methodGen == null) {
// 判断结束
return false;
Expand Down

0 comments on commit 953877d

Please sign in to comment.