/** * Creates an ObjectGraphNode. * * @param id the node id. * @param fieldName the field name that the parent node refers to this node by. * @param type the fully qualified java type name. * @param value the node value. */ public ObjectGraphNode(final int id, final String fieldName, final String type, final Object value) { this.id = id; this.fieldName = fieldName; this.type = type; this.value = value; this.size = getSize(type, value); }
/** * For some types, we don't care about their internals, so just summarise the size. * * @param node the node to summarise. */ private void summariseNode(final ObjectGraphNode node) { int size = node.getSize(); node.removeAll(); node.setSize(size); }
/** * Returns the sum of the size of primitive/simple value data contained within this node. This will differ from the * serialized size, as references to shared objects aren't followed/counted. * * @return the actual data size, in bytes. */ public int getSize() { int dataSize = size; for (int i = 0; i < getChildCount(); i++) { dataSize += ((ObjectGraphNode) getChildAt(i)).getSize(); } return dataSize; }
/** * Generates a flat format summary XML representation of this ObjectGraphNode. * * @param indent the indent, for formatting. * @param buffer the StringBuffer to append the summary to. */ private void toFlatSummary(final String indent, final StringBuffer buffer) { buffer.append(indent); ObjectGraphNode root = (ObjectGraphNode) getRoot(); double pct = 100.0 * getSize() / root.getSize(); buffer.append(getSize()).append(" ("); buffer.append(new DecimalFormat("0.0").format(pct)); buffer.append("%) - ").append(getFieldName()).append(" - "); if (getRefNode() != null) { buffer.append("ref (").append(getRefNode().getValue().getClass().getName()).append(')'); } else if (getValue() == null) { buffer.append("null (").append(getType()).append(')'); } else { buffer.append(getType()); } buffer.append('\n'); String newIndent = indent + " "; for (int i = 0; i < getChildCount(); i++) { ((ObjectGraphNode) getChildAt(i)).toFlatSummary(newIndent, buffer); } }
xml.append(" size=\"").append(getSize()).append('"');
@Test public void testGetSize() { ObjectGraphNode node = new ObjectGraphNode(1, "test", int.class.getName(), 1); Assert.assertEquals("Incorrect size", 4, node.getSize()); node = new ObjectGraphNode(1, "test", long.class.getName(), 1); Assert.assertEquals("Incorrect size", 8, node.getSize()); node = new ObjectGraphNode(1, "test", Object.class.getName(), new Object()); int expectedSize = ObjectGraphNode.OBJECT_SHELL_SIZE + ObjectGraphNode.OBJREF_SIZE; Assert.assertEquals("Incorrect size", expectedSize, node.getSize()); }
/** * Adjusts the overhead of the given node, for types where not all data is output (e.g. Lists and Maps). * * @param node the node to adjust. */ private void adjustOverhead(final ObjectGraphNode node) { ObjectGraphNode[] originalChildren = new ObjectGraphNode[node.getChildCount()]; int dataSize = 0; for (int i = 0; i < node.getChildCount(); i++) { ObjectGraphNode childNode = (ObjectGraphNode) node.getChildAt(i); dataSize += childNode.getSize(); originalChildren[i] = childNode; } node.removeAll(); // Figure out the list overhead. // All the original children will be included refs, as they have already been visited. visitComplexType(node); node.setSize(node.getSize() - originalChildren.length * ObjectGraphNode.OBJREF_SIZE); // re-attach the old children node.removeAll(); for (int i = 0; i < originalChildren.length; i++) { node.add(originalChildren[i]); } }
@Test public void testToFlatSummary() { WContainer component = new WContainer(); setActiveContext(createUIContext()); WLabel label = new WLabel(TEST_LABEL); component.add(label); final int nodeId = component.getIndexOfChild(label); final String fieldName = label.getId(); ObjectGraphNode node = new ObjectGraphNode(nodeId, fieldName, label.getClass().getName(), label); String flatSummary = node.toFlatSummary(); flatSummary = flatSummary.replaceAll("\n", ""); Assert.assertTrue("summary should start with size", flatSummary.startsWith( node.getSize() + " ")); Assert.assertTrue("summary should end with class name", flatSummary.endsWith(label. getClass().getName())); }