From 62575d19404ec771d9c68ccd75eb861fd6fe471a Mon Sep 17 00:00:00 2001 From: LHearen Date: Tue, 16 Oct 2018 18:17:49 +0800 Subject: [PATCH] make it easier to locate the class from so many matched --- .../command/klass100/SearchClassCommand.java | 46 ++++++++++++------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/com/taobao/arthas/core/command/klass100/SearchClassCommand.java b/core/src/main/java/com/taobao/arthas/core/command/klass100/SearchClassCommand.java index c353ffddff0..6c43f4cffec 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/klass100/SearchClassCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/klass100/SearchClassCommand.java @@ -1,5 +1,15 @@ package com.taobao.arthas.core.command.klass100; +import static com.taobao.text.ui.Element.label; + +import java.lang.instrument.Instrumentation; +import java.security.CodeSource; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Set; + import com.taobao.arthas.core.command.Constants; import com.taobao.arthas.core.shell.command.AnnotatedCommand; import com.taobao.arthas.core.shell.command.CommandProcess; @@ -17,12 +27,6 @@ import com.taobao.text.ui.TableElement; import com.taobao.text.util.RenderUtil; -import java.lang.instrument.Instrumentation; -import java.security.CodeSource; -import java.util.Set; - -import static com.taobao.text.ui.Element.label; - /** * 展示类信息 * @@ -43,6 +47,14 @@ public class SearchClassCommand extends AnnotatedCommand { private boolean isRegEx = false; private Integer expand; + public static String getCodeSource(final CodeSource cs) { + if (null == cs || null == cs.getLocation() || null == cs.getLocation().getFile()) { + return com.taobao.arthas.core.util.Constants.EMPTY_STRING; + } + + return cs.getLocation().getFile(); + } + @Argument(argName = "class-pattern", index = 0) @Description("Class name pattern, use either '.' or '/' as separator") public void setClassPattern(String classPattern) { @@ -79,8 +91,18 @@ public void process(CommandProcess process) { RowAffect affect = new RowAffect(); Instrumentation inst = process.session().getInstrumentation(); Set> matchedClasses = SearchUtils.searchClass(inst, classPattern, isRegEx); - - for (Class clazz : matchedClasses) { + List> matchedClassList = new ArrayList(matchedClasses); + Collections.sort(matchedClassList, new Comparator() { + @Override + public int compare(Object o1, Object o2) { + return StringUtils.classname((Class) o1).compareTo(StringUtils.classname((Class) o2)); + } + }); + + // 1.8 or above using lambda; + // matchedClasses.stream().sorted(Comparator.comparing(StringUtils::classname)); + + for (Class clazz : matchedClassList) { processClass(process, clazz); } @@ -127,12 +149,4 @@ private Element renderClassInfo(Class clazz, boolean isPrintField) { return table; } - public static String getCodeSource(final CodeSource cs) { - if (null == cs || null == cs.getLocation() || null == cs.getLocation().getFile()) { - return com.taobao.arthas.core.util.Constants.EMPTY_STRING; - } - - return cs.getLocation().getFile(); - } - }