Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Java Object Graph 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 String NEWLINE = String.format("%n"); public static void main(String args[]) { try{ System.out.println(dumpClass(Class.forName(new Scanner(System.in).nextLine()))); }catch(ClassNotFoundException e){ e.printStackTrace(); } } public static String dumpClass(Class<?> c){ TreeMap<Class<?>, String> content = new TreeMap<>((c1, c2) -> c1.getSimpleName().compareTo(c2.getSimpleName())); HashSet<Class<?>> visited = new HashSet<>(); Stack<Class<?>> toVisit = new Stack<>(); visited.add(Object.class); visited.add(Integer.class); visited.add(Boolean.class); visited.add(Double.class); visited.add(Byte.class); visited.add(String.class); visited.add(Short.class); visited.add(Long.class); visited.add(Character.class); visited.add(Float.class); visited.add(int.class); visited.add(boolean.class); visited.add(double.class); visited.add(byte.class); visited.add(short.class); visited.add(long.class); visited.add(float.class); visited.add(char.class); toVisit.push(c); visited.add(c); while(!toVisit.isEmpty()){ Class<?> current = toVisit.pop(); StringBuilder sb = new StringBuilder(); if(current.isInterface()) sb.append(current + NEWLINE); else{ sb.append(current + ":" + NEWLINE); for(Class<?> temp = current; temp != null && !temp.equals(Object.class); temp = temp.getSuperclass()){ sb.append(String.format(">> " + (temp == current? "this" : "superclass " + temp.getName()) + ":%n")); for(Field f : temp.getDeclaredFields()){ if(!Modifier.isStatic(f.getModifiers()) && !f.isSynthetic()){ Class<?> toAdd = f.getType(); while(toAdd.isArray()) toAdd = toAdd.getComponentType(); if(visited.add(toAdd)){ toVisit.push(toAdd); } sb.append(" ").append(f.getName()).append(": ").append(f.getType().getSimpleName()).append(String.format("%n")); } } } } sb.append(NEWLINE + NEWLINE); content.put(current, sb.toString()); } StringBuilder text = new StringBuilder(); text.append(content.get(c)); content.remove(c); content.forEach((arg1, arg2) -> text.append(arg2)); return text.toString(); } }
run
|
edit
|
history
|
help
0
Given two sorted arrays, merge them such that the elements are not repeated
contoh 43
Find kth element from last in a singly linked list
else weird
assign
1.6
UserDefinedBar
linked lists
Sum of ODD and EVEN numbers in an array
1