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
Write a program to sort the odd elements descending order and even elements in ascending order
Implementation of several common methods of LinkedList
4.a
extends
first activity
twelveDays
Print Longest String
Sample ArrayList
1d
2