Skip to content

Commit

Permalink
check directory for JadCommand and fix typos (#2670)
Browse files Browse the repository at this point in the history
  • Loading branch information
Allan-QLB authored Sep 22, 2023
1 parent 6221684 commit d1586cd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
import com.taobao.arthas.core.shell.command.AnnotatedCommand;
import com.taobao.arthas.core.shell.command.CommandProcess;
import com.taobao.arthas.core.shell.command.ExitStatus;
import com.taobao.arthas.core.util.ClassUtils;
import com.taobao.arthas.core.util.ClassLoaderUtils;
import com.taobao.arthas.core.util.CommandUtils;
import com.taobao.arthas.core.util.InstrumentationUtils;
import com.taobao.arthas.core.util.SearchUtils;
import com.taobao.arthas.core.util.*;
import com.taobao.arthas.core.util.affect.RowAffect;
import com.taobao.middleware.cli.annotations.Argument;
import com.taobao.middleware.cli.annotations.DefaultValue;
Expand Down Expand Up @@ -100,18 +96,11 @@ public void setLimit(int limit) {

@Override
public void process(CommandProcess process) {
RowAffect effect = new RowAffect();
try {
if (directory != null) {
File dir = new File(directory);
if (!dir.isDirectory()) {
process.end(-1, directory + " :is not a directory, please check it");
return;
}
if (directory != null && !FileUtils.isDirectoryOrNotExist(directory)) {
process.end(-1, directory + " :is not a directory, please check it");
return;
}

ExitStatus status = null;

Instrumentation inst = process.session().getInstrumentation();
if (code == null && classLoaderClass != null) {
List<ClassLoader> matchedClassLoaders = ClassLoaderUtils.getClassLoaderByClassName(inst, classLoaderClass);
Expand All @@ -130,8 +119,9 @@ public void process(CommandProcess process) {
return;
}
}

Set<Class<?>> matchedClasses = SearchUtils.searchClass(inst, classPattern, isRegEx, code);
final RowAffect effect = new RowAffect();
final ExitStatus status;
if (matchedClasses == null || matchedClasses.isEmpty()) {
status = processNoMatch(process);
} else if (matchedClasses.size() > limit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@
import com.taobao.arthas.core.shell.command.AnnotatedCommand;
import com.taobao.arthas.core.shell.command.CommandProcess;
import com.taobao.arthas.core.shell.command.ExitStatus;
import com.taobao.arthas.core.util.ClassUtils;
import com.taobao.arthas.core.util.ClassLoaderUtils;
import com.taobao.arthas.core.util.CommandUtils;
import com.taobao.arthas.core.util.Decompiler;
import com.taobao.arthas.core.util.InstrumentationUtils;
import com.taobao.arthas.core.util.SearchUtils;
import com.taobao.arthas.core.util.*;
import com.taobao.arthas.core.util.affect.RowAffect;
import com.taobao.middleware.cli.annotations.Argument;
import com.taobao.middleware.cli.annotations.DefaultValue;
Expand Down Expand Up @@ -77,7 +72,7 @@ public void setClassPattern(String classPattern) {
}

@Argument(argName = "method-name", index = 1, required = false)
@Description("method name pattern, decompile a specific method instead of the whole class")
@Description("Method name pattern, decompile a specific method instead of the whole class")
public void setMethodName(String methodName) {
this.methodName = methodName;
}
Expand All @@ -102,7 +97,7 @@ public void setRegEx(boolean regEx) {
}

@Option(longName = "hideUnicode", flag = true)
@Description("hide unicode, default value false")
@Description("Hide unicode, default value false")
public void setHideUnicode(boolean hideUnicode) {
this.hideUnicode = hideUnicode;
}
Expand All @@ -115,20 +110,23 @@ public void setSourceOnly(boolean sourceOnly) {

@Option(longName = "lineNumber")
@DefaultValue("true")
@Description("Output source code contins line number, default value true")
@Description("Output source code contains line number, default value true")
public void setLineNumber(boolean lineNumber) {
this.lineNumber = lineNumber;
}

@Option(shortName = "d", longName = "directory")
@Description("Sets the destination directory for dummped class files required by cfr decompiler")
@Description("Sets the destination directory for dumped class files required by cfr decompiler")
public void setDirectory(String directory) {
this.directory = directory;
}

@Override
public void process(CommandProcess process) {
RowAffect affect = new RowAffect();
if (directory != null && !FileUtils.isDirectoryOrNotExist(directory)) {
process.end(-1, directory + " :is not a directory, please check it");
return;
}
Instrumentation inst = process.session().getInstrumentation();

if (code == null && classLoaderClass != null) {
Expand All @@ -152,7 +150,8 @@ public void process(CommandProcess process) {
Set<Class<?>> matchedClasses = SearchUtils.searchClassOnly(inst, classPattern, isRegEx, code);

try {
ExitStatus status = null;
final RowAffect affect = new RowAffect();
final ExitStatus status;
if (matchedClasses == null || matchedClasses.isEmpty()) {
status = processNoMatch(process);
} else if (matchedClasses.size() > 1) {
Expand All @@ -177,9 +176,8 @@ public void process(CommandProcess process) {

private ExitStatus processExactMatch(CommandProcess process, RowAffect affect, Instrumentation inst, Set<Class<?>> matchedClasses, Set<Class<?>> withInnerClasses) {
Class<?> c = matchedClasses.iterator().next();
Set<Class<?>> allClasses = new HashSet<Class<?>>(withInnerClasses);
Set<Class<?>> allClasses = new HashSet<>(withInnerClasses);
allClasses.add(c);

try {
final ClassDumpTransformer transformer;
if (directory == null) {
Expand All @@ -195,15 +193,13 @@ private ExitStatus processExactMatch(CommandProcess process, RowAffect affect, I
"\" or try with \"-d/--directory\" to specify the directory of dump files");
}
File classFile = classFiles.get(c);

Pair<String,NavigableMap<Integer,Integer>> decompileResult = Decompiler.decompileWithMappings(classFile.getAbsolutePath(), methodName, hideUnicode, lineNumber);
String source = decompileResult.getFirst();
if (source != null) {
source = pattern.matcher(source).replaceAll("");
} else {
source = "unknown";
}

JadModel jadModel = new JadModel();
jadModel.setSource(source);
jadModel.setMappings(decompileResult.getSecond());
Expand All @@ -212,7 +208,6 @@ private ExitStatus processExactMatch(CommandProcess process, RowAffect affect, I
jadModel.setLocation(ClassUtils.getCodeSource(c.getProtectionDomain().getCodeSource()));
}
process.appendResult(jadModel);

affect.rCnt(classFiles.keySet().size());
return ExitStatus.success();
} catch (Throwable t) {
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/com/taobao/arthas/core/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,15 @@ public static Properties readProperties(String file) throws IOException {
}

}

/**
* Check if the given path is a directory or not exists.
* @param path path of file.
* @return {@code true} if the path is not exist or is an existing directory, otherwise returns {@code false}.
*/
public static boolean isDirectoryOrNotExist(String path) {
File file = new File(path);
return !file.exists() || file.isDirectory();
}
}

0 comments on commit d1586cd

Please sign in to comment.