Correctly print the content of map objects in packets.
Dieser Commit ist enthalten in:
Ursprung
3ae10d9123
Commit
8c0a671078
@ -21,6 +21,8 @@ import java.lang.reflect.Array;
|
|||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import com.google.common.primitives.Primitives;
|
import com.google.common.primitives.Primitives;
|
||||||
@ -80,7 +82,7 @@ public class PrettyPrinter {
|
|||||||
|
|
||||||
// Start and stop
|
// Start and stop
|
||||||
output.append("{ ");
|
output.append("{ ");
|
||||||
printObject(output, object, start, stop, previous, hierachyDepth);
|
printObject(output, object, start, stop, previous, hierachyDepth, true);
|
||||||
output.append(" }");
|
output.append(" }");
|
||||||
|
|
||||||
return output.toString();
|
return output.toString();
|
||||||
@ -99,16 +101,43 @@ public class PrettyPrinter {
|
|||||||
else
|
else
|
||||||
output.append(", ");
|
output.append(", ");
|
||||||
|
|
||||||
// Handle exceptions
|
// Print value
|
||||||
if (value != null)
|
printValue(output, value, stop, previous, hierachyIndex - 1);
|
||||||
printValue(output, value, value.getClass(), stop, previous, hierachyIndex - 1);
|
|
||||||
else
|
|
||||||
output.append("NULL");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
output.append(")");
|
output.append(")");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print the content of a maps entries.
|
||||||
|
* @param output - the output string builder.
|
||||||
|
* @param map - the map to print.
|
||||||
|
* @param current - the type of this map.
|
||||||
|
* @param stop - the class that indicates we should stop printing.
|
||||||
|
* @param previous - previous objects printed.
|
||||||
|
* @param hierachyIndex - hierachy index.
|
||||||
|
* @throws IllegalAccessException If any reflection went wrong.
|
||||||
|
*/
|
||||||
|
private static void printMap(StringBuilder output, Map<Object, Object> map, Class<?> current, Class<?> stop,
|
||||||
|
Set<Object> previous, int hierachyIndex) throws IllegalAccessException {
|
||||||
|
|
||||||
|
boolean first = true;
|
||||||
|
output.append("[");
|
||||||
|
|
||||||
|
for (Entry<Object, Object> entry : map.entrySet()) {
|
||||||
|
if (first)
|
||||||
|
first = false;
|
||||||
|
else
|
||||||
|
output.append(", ");
|
||||||
|
|
||||||
|
printValue(output, entry.getKey(), stop, previous, hierachyIndex - 1);
|
||||||
|
output.append(": ");
|
||||||
|
printValue(output, entry.getValue(), stop, previous, hierachyIndex - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
output.append("]");
|
||||||
|
}
|
||||||
|
|
||||||
private static void printArray(StringBuilder output, Object array, Class<?> current, Class<?> stop,
|
private static void printArray(StringBuilder output, Object array, Class<?> current, Class<?> stop,
|
||||||
Set<Object> previous, int hierachyIndex) throws IllegalAccessException {
|
Set<Object> previous, int hierachyIndex) throws IllegalAccessException {
|
||||||
|
|
||||||
@ -142,9 +171,7 @@ public class PrettyPrinter {
|
|||||||
|
|
||||||
// Internal recursion method
|
// Internal recursion method
|
||||||
private static void printObject(StringBuilder output, Object object, Class<?> current, Class<?> stop,
|
private static void printObject(StringBuilder output, Object object, Class<?> current, Class<?> stop,
|
||||||
Set<Object> previous, int hierachyIndex) throws IllegalAccessException {
|
Set<Object> previous, int hierachyIndex, boolean first) throws IllegalAccessException {
|
||||||
// Trickery
|
|
||||||
boolean first = true;
|
|
||||||
|
|
||||||
// See if we're supposed to skip this class
|
// See if we're supposed to skip this class
|
||||||
if (current == Object.class || (stop != null && current.equals(stop))) {
|
if (current == Object.class || (stop != null && current.equals(stop))) {
|
||||||
@ -168,10 +195,11 @@ public class PrettyPrinter {
|
|||||||
Class<?> type = field.getType();
|
Class<?> type = field.getType();
|
||||||
Object value = FieldUtils.readField(field, object, true);
|
Object value = FieldUtils.readField(field, object, true);
|
||||||
|
|
||||||
if (first)
|
if (first) {
|
||||||
first = false;
|
first = false;
|
||||||
else
|
} else {
|
||||||
output.append(", ");
|
output.append(", ");
|
||||||
|
}
|
||||||
|
|
||||||
output.append(field.getName());
|
output.append(field.getName());
|
||||||
output.append(" = ");
|
output.append(" = ");
|
||||||
@ -180,10 +208,16 @@ public class PrettyPrinter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Recurse
|
// Recurse
|
||||||
printObject(output, object, current.getSuperclass(), stop, previous, hierachyIndex);
|
printObject(output, object, current.getSuperclass(), stop, previous, hierachyIndex, first);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
private static void printValue(StringBuilder output, Object value, Class<?> stop,
|
||||||
|
Set<Object> previous, int hierachyIndex) throws IllegalAccessException {
|
||||||
|
// Handle the NULL case
|
||||||
|
printValue(output, value, value != null ? value.getClass() : null, stop, previous, hierachyIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||||
private static void printValue(StringBuilder output, Object value, Class<?> type,
|
private static void printValue(StringBuilder output, Object value, Class<?> type,
|
||||||
Class<?> stop, Set<Object> previous, int hierachyIndex) throws IllegalAccessException {
|
Class<?> stop, Set<Object> previous, int hierachyIndex) throws IllegalAccessException {
|
||||||
// Just print primitive types
|
// Just print primitive types
|
||||||
@ -197,12 +231,14 @@ public class PrettyPrinter {
|
|||||||
printArray(output, value, type, stop, previous, hierachyIndex);
|
printArray(output, value, type, stop, previous, hierachyIndex);
|
||||||
} else if (Iterable.class.isAssignableFrom(type)) {
|
} else if (Iterable.class.isAssignableFrom(type)) {
|
||||||
printIterables(output, (Iterable) value, type, stop, previous, hierachyIndex);
|
printIterables(output, (Iterable) value, type, stop, previous, hierachyIndex);
|
||||||
|
} else if (Map.class.isAssignableFrom(type)) {
|
||||||
|
printMap(output, (Map<Object, Object>) value, type, stop, previous, hierachyIndex);
|
||||||
} else if (ClassLoader.class.isAssignableFrom(type) || previous.contains(value)) {
|
} else if (ClassLoader.class.isAssignableFrom(type) || previous.contains(value)) {
|
||||||
// Don't print previous objects
|
// Don't print previous objects
|
||||||
output.append("\"" + value + "\"");
|
output.append("\"" + value + "\"");
|
||||||
} else {
|
} else {
|
||||||
output.append("{ ");
|
output.append("{ ");
|
||||||
printObject(output, value, value.getClass(), stop, previous, hierachyIndex);
|
printObject(output, value, value.getClass(), stop, previous, hierachyIndex, true);
|
||||||
output.append(" }");
|
output.append(" }");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren