aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/main/java/me/lucko
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2018-11-03 14:28:42 +0000
committerLuck <git@lucko.me>2018-11-03 14:28:42 +0000
commit9e4c0edc47707fbcad34305b3cd723b08f1ab4d6 (patch)
tree354b34b04631f05b4732c82b59ee1fcdd965065b /spark-common/src/main/java/me/lucko
parenta235a054532c3d403f083be7662b7a796a309159 (diff)
downloadspark-9e4c0edc47707fbcad34305b3cd723b08f1ab4d6.tar.gz
spark-9e4c0edc47707fbcad34305b3cd723b08f1ab4d6.tar.bz2
spark-9e4c0edc47707fbcad34305b3cd723b08f1ab4d6.zip
Improve the type descriptor conversion in heap dump outputs
Diffstat (limited to 'spark-common/src/main/java/me/lucko')
-rw-r--r--spark-common/src/main/java/me/lucko/spark/memory/HeapDump.java39
-rw-r--r--spark-common/src/main/java/me/lucko/spark/util/TypeDescriptors.java81
2 files changed, 84 insertions, 36 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/memory/HeapDump.java b/spark-common/src/main/java/me/lucko/spark/memory/HeapDump.java
index dc8bfec..4007bad 100644
--- a/spark-common/src/main/java/me/lucko/spark/memory/HeapDump.java
+++ b/spark-common/src/main/java/me/lucko/spark/memory/HeapDump.java
@@ -22,6 +22,8 @@ package me.lucko.spark.memory;
import com.google.gson.stream.JsonWriter;
+import me.lucko.spark.util.TypeDescriptors;
+
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
@@ -92,7 +94,7 @@ public class HeapDump {
Integer.parseInt(matcher.group(1)),
Integer.parseInt(matcher.group(2)),
Long.parseLong(matcher.group(3)),
- getFriendlyTypeName(matcher.group(4))
+ TypeDescriptors.getJavaType(matcher.group(4))
);
})
.filter(Objects::nonNull)
@@ -134,41 +136,6 @@ public class HeapDump {
return byteOut.toByteArray();
}
- private static String getPrimitiveTypeName(char character) {
- switch (character) {
- case 'B':
- return "byte";
- case 'C':
- return "char";
- case 'D':
- return "double";
- case 'F':
- return "float";
- case 'I':
- return "int";
- case 'J':
- return "long";
- case 'S':
- return "short";
- case 'V':
- return "void";
- case 'Z':
- return "boolean";
- default:
- throw new IllegalArgumentException();
- }
- }
-
- private static String getFriendlyTypeName(String internalDesc) {
- if (internalDesc.length() == 2 && internalDesc.charAt(0) == '[') {
- return getPrimitiveTypeName(internalDesc.charAt(1)) + " array";
- }
- if (internalDesc.startsWith("[L") && internalDesc.endsWith(";")) {
- return internalDesc.substring(2, internalDesc.length() - 1) + " array";
- }
- return internalDesc;
- }
-
public static final class Entry {
private final int order;
private final int instances;
diff --git a/spark-common/src/main/java/me/lucko/spark/util/TypeDescriptors.java b/spark-common/src/main/java/me/lucko/spark/util/TypeDescriptors.java
new file mode 100644
index 0000000..20dbe17
--- /dev/null
+++ b/spark-common/src/main/java/me/lucko/spark/util/TypeDescriptors.java
@@ -0,0 +1,81 @@
+/*
+ * This file is part of spark.
+ *
+ * Copyright (c) lucko (Luck) <luck@lucko.me>
+ * Copyright (c) contributors
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package me.lucko.spark.util;
+
+/**
+ * Utilities for working with Java type descriptors.
+ */
+public final class TypeDescriptors {
+
+ /**
+ * Returns the Java type corresponding to the given type descriptor.
+ *
+ * @param typeDescriptor a type descriptor.
+ * @return the Java type corresponding to the given type descriptor.
+ */
+ public static String getJavaType(String typeDescriptor) {
+ return getJavaType(typeDescriptor.toCharArray(), 0);
+ }
+
+ private static String getJavaType(char[] buf, int offset) {
+ int len;
+ switch (buf[offset]) {
+ case 'V':
+ return "void";
+ case 'Z':
+ return "boolean";
+ case 'C':
+ return "char";
+ case 'B':
+ return "byte";
+ case 'S':
+ return "short";
+ case 'I':
+ return "int";
+ case 'F':
+ return "float";
+ case 'J':
+ return "long";
+ case 'D':
+ return "double";
+ case '[': // array
+ len = 1;
+ while (buf[offset + len] == '[') {
+ len++;
+ }
+
+ StringBuilder sb = new StringBuilder(getJavaType(buf, offset + len));
+ for (int i = len; i > 0; --i) {
+ sb.append("[]");
+ }
+ return sb.toString();
+ case 'L': // object
+ len = 1;
+ while (buf[offset + len] != ';') {
+ len++;
+ }
+ return new String(buf, offset + 1, len - 1);
+ default:
+ return new String(buf, offset, buf.length - offset);
+ }
+ }
+
+}