Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Java Object structure dumper (iterative)
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; import java.lang.reflect.*; class Rextester { public static void main(String args[]) throws Exception { dump(new Thread()); } public static void dump(Object o){ Stack<Class<?>> stack = new Stack<>(); Stack<String> descr = new Stack<>(); Stack<String> tabs = new Stack<>(); HashSet<Field> printed = new HashSet<>(); stack.push(o.getClass()); descr.push("Structure of: "); tabs.push(""); while(!stack.isEmpty()){ Class<?> current = stack.pop(); String currentTabs = tabs.pop(); System.out.println(currentTabs + descr.pop() + current.getSimpleName()); //done.forEach(System.out::println); //System.out.println(); if(current != null){ if(current.isArray()){ stack.push(current.getComponentType()); descr.push("component type" + ((current.getComponentType().isInterface() || !current.getComponentType().isPrimitive()) && Modifier.isAbstract(current.getComponentType().getModifiers())? " (abstract)" : "") + ": "); tabs.push(currentTabs + " "); } else for(Class<?> c = current; c != null && !c.equals(Object.class); c = c.getSuperclass()){ Field[] fields = c.getDeclaredFields(); for(int i = fields.length-1; i >= 0; i--){ Field field = fields[i]; if(!Modifier.isStatic(field.getModifiers())){ if(printed.add(field)){ stack.push(field.getType()); descr.push(field.getName() + ((field.getType().isInterface() || !field.getType().isPrimitive()) && !field.getType().isArray() && Modifier.isAbstract(field.getType().getModifiers())? " (abstract)" : "") + ": "); tabs.push(currentTabs + " "); } } } } } } } }
run
|
edit
|
history
|
help
2
laba3
Size and signedness of Java ints and longs
classwork
Fgh
JavaQ5b
JAVA # Klavyeden girilen cümlede kaç adet "a" harfi var
JAVA # Dizi Ortalama
Quadratic_Equation.java
Coding Numbers - Duplicate - HashSet
Hello