/** * 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); } }
/** * Visits all the elements of the given array. * * @param node the ObjectGraphNode containing the array. */ private void visitArray(final ObjectGraphNode node) { if (node.getValue() instanceof Object[]) { Object[] array = (Object[]) node.getValue(); for (int i = 0; i < array.length; i++) { String entryType = array[i] == null ? Object.class.getName() : array[i].getClass(). getName(); ObjectGraphNode childNode = new ObjectGraphNode(++nodeCount, "[" + i + "]", entryType, array[i]); node.add(childNode); visit(childNode); } } else { ObjectGraphNode childNode = new ObjectGraphNode(++nodeCount, "[primitive array]", node. getValue().getClass().getName(), node.getValue()); node.add(childNode); } }
/** * @return a String representation of this node's value if it is simple, otherwise null. */ private String formatSimpleValue() { if (value instanceof Character) { char c = ((Character) value); if (c < 32 || c > 127) { return "(char) " + ((int) c); } return '\'' + ((Character) value).toString() + '\''; } else if (value instanceof String) { String stringVal = ((String) value).replace('\n', ' ').replace('\r', ' '); if (stringVal.length() > 30) { return '"' + stringVal.substring(0, 27) + "...\""; } else { return '"' + stringVal + '"'; } } else if (isPrimitive() || isSimpleType()) { return value == null ? null : value.toString(); } return null; }
/** * 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; }
/** * 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]); } }
String primitiveString = formatSimpleValue(); xml.append(isPrimitive() ? "<primitive" : "<object"); xml.append(" size=\"").append(getSize()).append('"'); if (getChildCount() == 0) { xml.append("/>\n"); } else { for (int i = 0; i < getChildCount(); i++) { ((ObjectGraphNode) getChildAt(i)).toXml(newIndent, xml); xml.append(isPrimitive() ? "</primitive>\n" : "</object>\n");
Object currentValue = currentNode.getValue(); || currentNode.isPrimitive() || currentNode.isSimpleType()) { return; currentNode.setRefNode(ref); return;
@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())); }
@Test public void testSetRefNode() { ObjectGraphNode node = new ObjectGraphNode(1, "test", String.class.getName(), "test"); ObjectGraphNode refNode = new ObjectGraphNode(2, "test", String.class.getName(), "test"); node.setRefNode(refNode); Assert.assertSame("Should retrieve refNod set", refNode, node.getRefNode()); }
@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()); }
ObjectGraphNode root = ObjectGraphDump.dump(treeAndSession); LOG.info(LINE_PREFIX + "Component mem use: " + ((ObjectGraphNode) root.getChildAt(0)). getSize()); LOG.info(LINE_PREFIX + "UIC mem use: " + ((ObjectGraphNode) root.getChildAt(1)). getSize()); } finally { UIContextHolder.popContext();
/** * @return a flat format summary representation of the object tree, starting at this node. */ public String toFlatSummary() { StringBuffer buffer = new StringBuffer(); toFlatSummary("", buffer); return buffer.toString(); }
@Test public void testIsPrimitive() { ObjectGraphNode node = new ObjectGraphNode(1, "test", String.class.getName(), "test"); Assert.assertFalse("Node should not be primitive", node.isPrimitive()); node = new ObjectGraphNode(1, "test", int.class.getName(), 123); Assert.assertTrue("Node should be is primitive", node.isPrimitive()); }
@Test public void testGetFieldName() { String fieldName = "ObjectGraphNode_Test.testGetFieldName.fieldName"; ObjectGraphNode node = new ObjectGraphNode(1, fieldName, String.class.getName(), "test"); Assert.assertEquals("should retrieve correct field name", fieldName, node.getFieldName()); }
@Test public void testGetType() { ObjectGraphNode node = new ObjectGraphNode(1, "test", int.class.getName(), 1); Assert.assertEquals("Should retrieve type set", int.class.getName(), node.getType()); }
@Test public void testIsSimpleType() { ObjectGraphNode node = new ObjectGraphNode(1, "test", String.class.getName(), "test"); Assert.assertTrue("Node should be a simple type", node.isSimpleType()); node = new ObjectGraphNode(1, "test", Duplet.class.getName(), new Duplet<String, String>()); Assert.assertFalse("Node should not be a simple type", node.isSimpleType()); }
LOG.info(((ObjectGraphNode) root.getChildAt(0)).toXml()); LOG.info(((ObjectGraphNode) root.getChildAt(1)).toXml()); } catch (Exception e) { LOG.error("Failed to dump context", e);
@Test public void testGetValue() { String fieldValue = "ObjectGraphNode_Test.testGetValue.fieldValue"; ObjectGraphNode node = new ObjectGraphNode(1, "test", String.class.getName(), fieldValue); Assert.assertSame("Should retrieve value set", fieldValue, node.getValue()); }
@Test public void testToXml() { 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 xmlSummary = node.toXml(); Assert.assertTrue("should report correct ID", xmlSummary.indexOf("object id=\"0\"") != -1); Assert.assertTrue("should report correct field name", xmlSummary.indexOf( "field=\"" + fieldName + "\"") != -1); Assert.assertTrue("should report correct class name", xmlSummary.indexOf("type=\"com.github.bordertech.wcomponents.WLabel\"") != -1); Assert.assertTrue("should report correct size", xmlSummary.indexOf("size=\"12\"") != -1); } }