Skip to content

Commit

Permalink
make it easier to locate the class from so many matched
Browse files Browse the repository at this point in the history
  • Loading branch information
Hearen committed Oct 17, 2018
1 parent d9ed410 commit 0757b41
Showing 1 changed file with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

/**
* 展示类信息
*
Expand All @@ -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) {
Expand Down Expand Up @@ -78,7 +90,13 @@ public void process(CommandProcess process) {
// TODO: null check
RowAffect affect = new RowAffect();
Instrumentation inst = process.session().getInstrumentation();
Set<Class<?>> matchedClasses = SearchUtils.searchClass(inst, classPattern, isRegEx);
List<Class<?>> matchedClasses = new ArrayList(SearchUtils.searchClass(inst, classPattern, isRegEx));
Collections.sort(matchedClasses, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
return StringUtils.classname((Class) o1).compareTo(StringUtils.classname((Class) o2));
}
});

for (Class<?> clazz : matchedClasses) {
processClass(process, clazz);
Expand Down Expand Up @@ -127,12 +145,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();
}

}

0 comments on commit 0757b41

Please sign in to comment.