@Test public void testWalksAGraph() { final Map<String, Long> map = new HashMap<String, Long>(); ObjectGraphWalker walker = new ObjectGraphWalker( new ObjectGraphWalker.Visitor() { public long visit(final Object object) { increment(object.getClass().getName()); return 1; } public void increment(String value) { if (value != null) { Long previousValue = map.get(value); if (previousValue == null) { previousValue = 0L; } map.put(value, ++previousValue); } } }, new PassThroughFilter()); String javaVersion = System.getProperty("java.version"); if (javaVersion.startsWith("1.5")) { assertThat(walker.walk(MAX_SIZEOF_DEPTH, false, new ReentrantReadWriteLock()), is(4L)); } else if (javaVersion.startsWith("1.6") || javaVersion.startsWith("1.7")) { assertThat(walker.walk(MAX_SIZEOF_DEPTH, false, new ReentrantReadWriteLock()), is(5L)); assertThat(map.remove("java.util.concurrent.locks.ReentrantReadWriteLock$Sync$ThreadLocalHoldCounter"), is(1L)); } else { throw new AssertionError("Unexpected Java Version : " + javaVersion); } assertThat(map.remove(ReentrantReadWriteLock.class.getName()), is(1L)); assertThat(map.remove("java.util.concurrent.locks.ReentrantReadWriteLock$NonfairSync"), is(1L)); assertThat(map.remove(ReentrantReadWriteLock.ReadLock.class.getName()), is(1L)); assertThat(map.remove(ReentrantReadWriteLock.WriteLock.class.getName()), is(1L)); assertThat(map.isEmpty(), is(true)); if (javaVersion.startsWith("1.5")) { assertThat(walker.walk(MAX_SIZEOF_DEPTH, false, new SomeInnerClass()), is(13L)); } else if (javaVersion.startsWith("1.6") || javaVersion.startsWith("1.7")) { assertThat(walker.walk(MAX_SIZEOF_DEPTH, false, new SomeInnerClass()), is(14L)); assertThat(map.remove("java.util.concurrent.locks.ReentrantReadWriteLock$Sync$ThreadLocalHoldCounter"), is(1L)); } else { throw new AssertionError("Unexpected Java Version : " + javaVersion); } assertThat(map.remove(SomeInnerClass.class.getName()), is(1L)); assertThat(map.remove(this.getClass().getName()), is(1L)); assertThat(map.remove(Object.class.getName()), is(5L)); assertThat(map.remove(ReentrantReadWriteLock.class.getName()), is(1L)); assertThat(map.remove("java.util.concurrent.locks.ReentrantReadWriteLock$NonfairSync"), is(1L)); assertThat(map.remove(ReentrantReadWriteLock.ReadLock.class.getName()), is(1L)); assertThat(map.remove(ReentrantReadWriteLock.WriteLock.class.getName()), is(1L)); assertThat(map.remove(Object[].class.getName()), is(1L)); assertThat(map.remove(Integer.class.getName()), nullValue()); assertThat(map.remove(int[].class.getName()), is(1L)); assertThat(map.isEmpty(), is(true)); assertThat(walker.walk(MAX_SIZEOF_DEPTH, false, (Object) null), is(0L)); assertThat(walker.walk(MAX_SIZEOF_DEPTH, false), is(0L)); }
long walk(int maxDepth, boolean abortWhenMaxDepthExceeded, Object... root) { StringBuilder traversalDebugMessage = null; long result = 0; boolean warned = false; try { Stack<Object> toVisit = new Stack<Object>(); IdentityHashMap<Object, Object> visited = new IdentityHashMap<Object, Object>(); if (root != null) { if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage = new StringBuilder(); traversalDebugMessage.append("visiting "); } for (Object object : root) { nullSafeAdd(toVisit, object); if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled() && object != null) { traversalDebugMessage.append(object.getClass().getName()) .append("@").append(System.identityHashCode(object)).append(", "); } } if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.deleteCharAt(traversalDebugMessage.length() - 2).append("\n"); } } while (!toVisit.isEmpty()) { warned = checkMaxDepth(maxDepth, abortWhenMaxDepthExceeded, warned, visited); Object ref = toVisit.pop(); if (visited.containsKey(ref)) { continue; } Class<?> refClass = ref.getClass(); if (!isSharedFlyweight(ref) && shouldWalkClass(refClass)) { if (refClass.isArray() && !refClass.getComponentType().isPrimitive()) { for (int i = 0; i < Array.getLength(ref); i++) { nullSafeAdd(toVisit, Array.get(ref, i)); } } else { for (Field field : getFilteredFields(refClass)) { try { nullSafeAdd(toVisit, field.get(ref)); } catch (IllegalAccessException ex) { throw new RuntimeException(ex); } } } long visitSize = calculateSize(ref); if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.append(" ").append(visitSize).append("b\t\t") .append(ref.getClass().getName()).append("@").append(System.identityHashCode(ref)).append("\n"); } result += visitSize; } else if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.append(" ignored\t") .append(ref.getClass().getName()).append("@").append(System.identityHashCode(ref)).append("\n"); } visited.put(ref, null); } if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.append("Total size: ").append(result).append(" bytes\n"); LOG.debug(traversalDebugMessage.toString()); } return result; } catch (MaxDepthExceededException we) { we.addToMeasuredSize(result); throw we; } }
ObjectGraphWalker { long walk(int maxDepth, boolean abortWhenMaxDepthExceeded, Object... root) { StringBuilder traversalDebugMessage = null; long result = 0; boolean warned = false; try { Stack<Object> toVisit = new Stack<Object>(); IdentityHashMap<Object, Object> visited = new IdentityHashMap<Object, Object>(); if (root != null) { if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage = new StringBuilder(); traversalDebugMessage.append("visiting "); } for (Object object : root) { nullSafeAdd(toVisit, object); if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled() && object != null) { traversalDebugMessage.append(object.getClass().getName()) .append("@").append(System.identityHashCode(object)).append(", "); } } if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.deleteCharAt(traversalDebugMessage.length() - 2).append("\n"); } } while (!toVisit.isEmpty()) { warned = checkMaxDepth(maxDepth, abortWhenMaxDepthExceeded, warned, visited); Object ref = toVisit.pop(); if (visited.containsKey(ref)) { continue; } Class<?> refClass = ref.getClass(); if (!isSharedFlyweight(ref) && shouldWalkClass(refClass)) { if (refClass.isArray() && !refClass.getComponentType().isPrimitive()) { for (int i = 0; i < Array.getLength(ref); i++) { nullSafeAdd(toVisit, Array.get(ref, i)); } } else { for (Field field : getFilteredFields(refClass)) { try { nullSafeAdd(toVisit, field.get(ref)); } catch (IllegalAccessException ex) { throw new RuntimeException(ex); } } } long visitSize = calculateSize(ref); if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.append(" ").append(visitSize).append("b\t\t") .append(ref.getClass().getName()).append("@").append(System.identityHashCode(ref)).append("\n"); } result += visitSize; } else if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.append(" ignored\t") .append(ref.getClass().getName()).append("@").append(System.identityHashCode(ref)).append("\n"); } visited.put(ref, null); } if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.append("Total size: ").append(result).append(" bytes\n"); LOG.debug(traversalDebugMessage.toString()); } return result; } catch (MaxDepthExceededException we) { we.addToMeasuredSize(result); throw we; } } }
ObjectGraphWalker { long walk(int maxDepth, boolean abortWhenMaxDepthExceeded, Object... root) { StringBuilder traversalDebugMessage = null; long result = 0; boolean warned = false; try { Stack<Object> toVisit = new Stack<Object>(); IdentityHashMap<Object, Object> visited = new IdentityHashMap<Object, Object>(); if (root != null) { if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage = new StringBuilder(); traversalDebugMessage.append("visiting "); } for (Object object : root) { nullSafeAdd(toVisit, object); if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled() && object != null) { traversalDebugMessage.append(object.getClass().getName()) .append("@").append(System.identityHashCode(object)).append(", "); } } if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.deleteCharAt(traversalDebugMessage.length() - 2).append("\n"); } } while (!toVisit.isEmpty()) { warned = checkMaxDepth(maxDepth, abortWhenMaxDepthExceeded, warned, visited); Object ref = toVisit.pop(); if (visited.containsKey(ref)) { continue; } Class<?> refClass = ref.getClass(); if (!isSharedFlyweight(ref) && shouldWalkClass(refClass)) { if (refClass.isArray() && !refClass.getComponentType().isPrimitive()) { for (int i = 0; i < Array.getLength(ref); i++) { nullSafeAdd(toVisit, Array.get(ref, i)); } } else { for (Field field : getFilteredFields(refClass)) { try { nullSafeAdd(toVisit, field.get(ref)); } catch (IllegalAccessException ex) { throw new RuntimeException(ex); } } } long visitSize = calculateSize(ref); if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.append(" ").append(visitSize).append("b\t\t") .append(ref.getClass().getName()).append("@").append(System.identityHashCode(ref)).append("\n"); } result += visitSize; } else if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.append(" ignored\t") .append(ref.getClass().getName()).append("@").append(System.identityHashCode(ref)).append("\n"); } visited.put(ref, null); } if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.append("Total size: ").append(result).append(" bytes\n"); LOG.debug(traversalDebugMessage.toString()); } return result; } catch (MaxDepthExceededException we) { we.addToMeasuredSize(result); throw we; } } ObjectGraphWalker(Visitor visitor, SizeOfFilter filter); }
ObjectGraphWalker { long walk(int maxDepth, boolean abortWhenMaxDepthExceeded, Object... root) { StringBuilder traversalDebugMessage = null; long result = 0; boolean warned = false; try { Stack<Object> toVisit = new Stack<Object>(); IdentityHashMap<Object, Object> visited = new IdentityHashMap<Object, Object>(); if (root != null) { if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage = new StringBuilder(); traversalDebugMessage.append("visiting "); } for (Object object : root) { nullSafeAdd(toVisit, object); if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled() && object != null) { traversalDebugMessage.append(object.getClass().getName()) .append("@").append(System.identityHashCode(object)).append(", "); } } if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.deleteCharAt(traversalDebugMessage.length() - 2).append("\n"); } } while (!toVisit.isEmpty()) { warned = checkMaxDepth(maxDepth, abortWhenMaxDepthExceeded, warned, visited); Object ref = toVisit.pop(); if (visited.containsKey(ref)) { continue; } Class<?> refClass = ref.getClass(); if (!isSharedFlyweight(ref) && shouldWalkClass(refClass)) { if (refClass.isArray() && !refClass.getComponentType().isPrimitive()) { for (int i = 0; i < Array.getLength(ref); i++) { nullSafeAdd(toVisit, Array.get(ref, i)); } } else { for (Field field : getFilteredFields(refClass)) { try { nullSafeAdd(toVisit, field.get(ref)); } catch (IllegalAccessException ex) { throw new RuntimeException(ex); } } } long visitSize = calculateSize(ref); if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.append(" ").append(visitSize).append("b\t\t") .append(ref.getClass().getName()).append("@").append(System.identityHashCode(ref)).append("\n"); } result += visitSize; } else if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.append(" ignored\t") .append(ref.getClass().getName()).append("@").append(System.identityHashCode(ref)).append("\n"); } visited.put(ref, null); } if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.append("Total size: ").append(result).append(" bytes\n"); LOG.debug(traversalDebugMessage.toString()); } return result; } catch (MaxDepthExceededException we) { we.addToMeasuredSize(result); throw we; } } ObjectGraphWalker(Visitor visitor, SizeOfFilter filter);  }
ObjectGraphWalker { long walk(int maxDepth, boolean abortWhenMaxDepthExceeded, Object... root) { StringBuilder traversalDebugMessage = null; long result = 0; boolean warned = false; try { Stack<Object> toVisit = new Stack<Object>(); IdentityHashMap<Object, Object> visited = new IdentityHashMap<Object, Object>(); if (root != null) { if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage = new StringBuilder(); traversalDebugMessage.append("visiting "); } for (Object object : root) { nullSafeAdd(toVisit, object); if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled() && object != null) { traversalDebugMessage.append(object.getClass().getName()) .append("@").append(System.identityHashCode(object)).append(", "); } } if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.deleteCharAt(traversalDebugMessage.length() - 2).append("\n"); } } while (!toVisit.isEmpty()) { warned = checkMaxDepth(maxDepth, abortWhenMaxDepthExceeded, warned, visited); Object ref = toVisit.pop(); if (visited.containsKey(ref)) { continue; } Class<?> refClass = ref.getClass(); if (!isSharedFlyweight(ref) && shouldWalkClass(refClass)) { if (refClass.isArray() && !refClass.getComponentType().isPrimitive()) { for (int i = 0; i < Array.getLength(ref); i++) { nullSafeAdd(toVisit, Array.get(ref, i)); } } else { for (Field field : getFilteredFields(refClass)) { try { nullSafeAdd(toVisit, field.get(ref)); } catch (IllegalAccessException ex) { throw new RuntimeException(ex); } } } long visitSize = calculateSize(ref); if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.append(" ").append(visitSize).append("b\t\t") .append(ref.getClass().getName()).append("@").append(System.identityHashCode(ref)).append("\n"); } result += visitSize; } else if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.append(" ignored\t") .append(ref.getClass().getName()).append("@").append(System.identityHashCode(ref)).append("\n"); } visited.put(ref, null); } if (USE_VERBOSE_DEBUG_LOGGING && LOG.isDebugEnabled()) { traversalDebugMessage.append("Total size: ").append(result).append(" bytes\n"); LOG.debug(traversalDebugMessage.toString()); } return result; } catch (MaxDepthExceededException we) { we.addToMeasuredSize(result); throw we; } } ObjectGraphWalker(Visitor visitor, SizeOfFilter filter);   }