From 1c432dbb000831bf3aca434e3dda826b53e569f2 Mon Sep 17 00:00:00 2001 From: hengyunabc Date: Fri, 26 Feb 2021 15:01:38 +0800 Subject: [PATCH] fix Decompiler jdk7 support . #1711 --- .../taobao/arthas/core/util/Decompiler.java | 12 ++++-- .../taobao/arthas/core/util/StringUtils.java | 27 ++++++++++++++ .../arthas/core/util/DecompilerTest.java | 37 +++++++++++++++++++ 3 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 core/src/test/java/com/taobao/arthas/core/util/DecompilerTest.java diff --git a/core/src/main/java/com/taobao/arthas/core/util/Decompiler.java b/core/src/main/java/com/taobao/arthas/core/util/Decompiler.java index 14f2c3057..aa9d10825 100644 --- a/core/src/main/java/com/taobao/arthas/core/util/Decompiler.java +++ b/core/src/main/java/com/taobao/arthas/core/util/Decompiler.java @@ -113,7 +113,8 @@ public class Decompiler { String emptyStr = " "; StringBuilder sb = new StringBuilder(); - String[] lines = src.split("\\R"); + + List lines = StringUtils.toLines(src); if (maxLineNumber >= 100) { formatStr = "/*%3d*/ "; @@ -122,14 +123,17 @@ public class Decompiler { formatStr = "/*%4d*/ "; emptyStr = " "; } - for (int i = 0; i < lines.length; ++i) { - Integer srcLineNumber = lineMapping.get(i + 1); + + int index = 0; + for (String line : lines) { + Integer srcLineNumber = lineMapping.get(index + 1); if (srcLineNumber != null) { sb.append(String.format(formatStr, srcLineNumber)); } else { sb.append(emptyStr); } - sb.append(lines[i]).append("\n"); + sb.append(line).append("\n"); + index++; } return sb.toString(); diff --git a/core/src/main/java/com/taobao/arthas/core/util/StringUtils.java b/core/src/main/java/com/taobao/arthas/core/util/StringUtils.java index e12fbb1e9..165b5ed64 100644 --- a/core/src/main/java/com/taobao/arthas/core/util/StringUtils.java +++ b/core/src/main/java/com/taobao/arthas/core/util/StringUtils.java @@ -1,8 +1,12 @@ package com.taobao.arthas.core.util; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.StringReader; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collection; +import java.util.List; import java.util.Properties; import java.util.Set; import java.util.StringTokenizer; @@ -885,4 +889,27 @@ public abstract class StringUtils { : bytes < 0xfffccccccccccccL ? String.format("%.1f PiB", (bytes >> 10) / 0x1p40) : String.format("%.1f EiB", (bytes >> 20) / 0x1p40); } + + public static List toLines(String text) { + List result = new ArrayList(); + BufferedReader reader = new BufferedReader(new StringReader(text)); + try { + String line = reader.readLine(); + while (line != null) { + result.add(line); + line = reader.readLine(); + } + } catch (IOException exc) { + // quit + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + // ignore + } + } + } + return result; + } } diff --git a/core/src/test/java/com/taobao/arthas/core/util/DecompilerTest.java b/core/src/test/java/com/taobao/arthas/core/util/DecompilerTest.java new file mode 100644 index 000000000..43e773a9a --- /dev/null +++ b/core/src/test/java/com/taobao/arthas/core/util/DecompilerTest.java @@ -0,0 +1,37 @@ +package com.taobao.arthas.core.util; + +import java.io.File; + +import org.assertj.core.api.Assertions; +import org.junit.Test; + +/** + * + * @author hengyunabc 2021-02-09 + * + */ +public class DecompilerTest { + + @Test + public void test() { + String dir = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); + + File classFile = new File(dir, this.getClass().getName().replace('.', '/') + ".class"); + + String code = Decompiler.decompile(classFile.getAbsolutePath(), null, true); + + System.err.println(code); + + Assertions.assertThat(code).contains("/*23*/ System.err.println(code);").contains("/*32*/ int i = 0;"); + } + + public void aaa() { + + int jjj = 0; + + for (int i = 0; i < 100; ++i) { + System.err.println(i); + } + } + +}