From d1586cd727f12ccac425e2615bc1020449b19b3c Mon Sep 17 00:00:00 2001 From: Allan-QLB <68638598+Allan-QLB@users.noreply.github.com> Date: Fri, 22 Sep 2023 14:08:40 +0800 Subject: [PATCH] check directory for JadCommand and fix typos (#2670) --- .../command/klass100/DumpClassCommand.java | 22 ++++---------- .../core/command/klass100/JadCommand.java | 29 ++++++++----------- .../taobao/arthas/core/util/FileUtils.java | 10 +++++++ 3 files changed, 28 insertions(+), 33 deletions(-) diff --git a/core/src/main/java/com/taobao/arthas/core/command/klass100/DumpClassCommand.java b/core/src/main/java/com/taobao/arthas/core/command/klass100/DumpClassCommand.java index d36399dfcc..26d722a0ff 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/klass100/DumpClassCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/klass100/DumpClassCommand.java @@ -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; @@ -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 matchedClassLoaders = ClassLoaderUtils.getClassLoaderByClassName(inst, classLoaderClass); @@ -130,8 +119,9 @@ public void process(CommandProcess process) { return; } } - Set> 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) { diff --git a/core/src/main/java/com/taobao/arthas/core/command/klass100/JadCommand.java b/core/src/main/java/com/taobao/arthas/core/command/klass100/JadCommand.java index 8ea8d58e81..1637421f7f 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/klass100/JadCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/klass100/JadCommand.java @@ -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; @@ -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; } @@ -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; } @@ -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) { @@ -152,7 +150,8 @@ public void process(CommandProcess process) { Set> 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) { @@ -177,9 +176,8 @@ public void process(CommandProcess process) { private ExitStatus processExactMatch(CommandProcess process, RowAffect affect, Instrumentation inst, Set> matchedClasses, Set> withInnerClasses) { Class c = matchedClasses.iterator().next(); - Set> allClasses = new HashSet>(withInnerClasses); + Set> allClasses = new HashSet<>(withInnerClasses); allClasses.add(c); - try { final ClassDumpTransformer transformer; if (directory == null) { @@ -195,7 +193,6 @@ 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> decompileResult = Decompiler.decompileWithMappings(classFile.getAbsolutePath(), methodName, hideUnicode, lineNumber); String source = decompileResult.getFirst(); if (source != null) { @@ -203,7 +200,6 @@ private ExitStatus processExactMatch(CommandProcess process, RowAffect affect, I } else { source = "unknown"; } - JadModel jadModel = new JadModel(); jadModel.setSource(source); jadModel.setMappings(decompileResult.getSecond()); @@ -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) { diff --git a/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java b/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java index ec046468cf..bae5db72dd 100644 --- a/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java +++ b/core/src/main/java/com/taobao/arthas/core/util/FileUtils.java @@ -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(); + } }