Last active
August 29, 2015 14:09
-
-
Save cuub/0b86dd57778dd100e08d to your computer and use it in GitHub Desktop.
Helper class to print any object you want
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* <b>Helper class to print any object you want</b> | |
*/ | |
public class PrintMe { | |
static StringBuilder result; | |
static String newLine = System.getProperty("line.separator"); | |
static ConcurrentLinkedQueue<Object> objects = new ConcurrentLinkedQueue<Object>(); | |
private PrintMe() { | |
} | |
public static String print(Object obj) { | |
result = new StringBuilder(); | |
objects.add(obj); | |
printAux(obj, 1); | |
objects.clear(); | |
String toReturn = result.toString(); | |
result.setLength(0); | |
return toReturn; | |
} | |
private static void printAux(Object obj, int currentLevel) { | |
result.append(getIndent(currentLevel)); | |
result.append(obj.getClass().getSimpleName()); | |
result.append(" {"); | |
result.append(newLine); | |
Field[] fields = obj.getClass().getDeclaredFields(); | |
for (Field field : fields) { | |
result.append(getIndent(currentLevel + 1)); | |
try { | |
field.setAccessible(true); | |
result.append(field.getType().getSimpleName() + " " + field.getName()); | |
result.append(": "); | |
if (field.getType().isSynthetic()) { | |
continue; | |
} else if (isPrintable(field)) { | |
result.append(field.get(obj)); | |
} else if (field.getType().isArray()) { | |
printArray(field.get(obj), currentLevel + 1); | |
} else { | |
currentLevel++; | |
result.append(newLine); | |
printAux(field.get(obj), currentLevel); | |
} | |
} catch (Exception ex) { | |
System.out.println(ex); | |
} | |
result.append(newLine); | |
} | |
result.append(getIndent(currentLevel)); | |
result.append("}"); | |
} | |
private static String getIndent(int level) { | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 0; i < level; i++) { | |
sb.append(" "); | |
} | |
return sb.toString(); | |
} | |
private static void printArray(Object array, int currentLevel) { | |
int length = Array.getLength(array); | |
for (int i = 0; i < length; i++) { | |
result.append(newLine); | |
result.append(getIndent(currentLevel + 1)); | |
Object arrayElement = Array.get(array, i); | |
if (isPrintable(arrayElement)) | |
result.append(arrayElement); | |
else | |
printAux(arrayElement, currentLevel + 2); | |
} | |
} | |
private static boolean isPrintable(Object obj) { | |
return (obj.getClass().isAssignableFrom(String.class) || obj.getClass().isAssignableFrom(BigDecimal.class) | |
|| obj.getClass().isEnum() || obj.getClass().isPrimitive() | |
|| obj.getClass().isAssignableFrom(Integer.class) || obj.getClass().isAssignableFrom(Double.class) | |
|| obj.getClass().isAssignableFrom(Long.class) || obj.getClass().isAssignableFrom(Float.class)); | |
} | |
private static boolean isPrintable(Field field) { | |
return (field.getType().isAssignableFrom(String.class) || field.getType().isAssignableFrom(BigDecimal.class) | |
|| field.getType().isEnum() || field.getType().isPrimitive() | |
|| field.getType().isAssignableFrom(Integer.class) || field.getType().isAssignableFrom(Double.class) | |
|| field.getType().isAssignableFrom(Long.class) || field.getType().isAssignableFrom(Float.class)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
PrintMe.print(YourObject);