/** * Dumps the contents of the session attributes. * * @param obj the object to dump. * @return the dump of the given object. */ public static ObjectGraphNode dump(final Object obj) { ObjectGraphDump dump = new ObjectGraphDump(false, true); ObjectGraphNode root = new ObjectGraphNode(++dump.nodeCount, null, obj.getClass().getName(), obj); dump.visit(root); return root; }
/** * Visits all the elements of the given list. * * @param node the ObjectGraphNode containing the list. */ private void visitList(final ObjectGraphNode node) { int index = 0; for (Iterator i = ((List) node.getValue()).iterator(); i.hasNext();) { Object entry = i.next(); String entryType = entry == null ? Object.class.getName() : entry.getClass().getName(); ObjectGraphNode childNode = new ObjectGraphNode(++nodeCount, "[" + index++ + "]", entryType, entry); node.add(childNode); visit(childNode); } adjustOverhead(node); }
/** * Visits all the fields in the given complex object, noting differences. * * @param node the ObjectGraphNode containing the object. * @param otherInstance the other instance to compare to */ private void visitComplexTypeWithDiff(final ObjectGraphNode node, final Object otherInstance) { if (otherInstance == null) { // Nothing to compare against, just use the default visit visitComplexType(node); } else { Field[] fields = getAllInstanceFields(node.getValue()); for (int i = 0; i < fields.length; i++) { Object fieldValue = readField(fields[i], node.getValue()); Object otherValue = readField(fields[i], otherInstance); String fieldType = fields[i].getType().getName(); String nodeFieldName = fields[i].getName() + (Util.equals(fieldValue, otherValue) ? "" : "*"); ObjectGraphNode childNode = new ObjectGraphNode(++nodeCount, nodeFieldName, fieldType, fieldValue); node.add(childNode); visit(childNode); } } }
if (isObjectVisited(currentNode)) { ObjectGraphNode ref = visitedNodes.get(currentValue); currentNode.setRefNode(ref); markObjectVisited(currentNode); visitList(currentNode); } else if (currentValue instanceof Map) { visitMap(currentNode); } else if (currentValue instanceof ComponentModel) { visitComponentModel(currentNode); } else if (currentValue instanceof Field) { visitComplexType(currentNode); summariseNode(currentNode); } else if (currentValue.getClass().isArray()) { visitArray(currentNode); } else { visitComplexType(currentNode);
/** * Visits all the fields in the given complex object. * * @param node the ObjectGraphNode containing the object. */ private void visitComplexType(final ObjectGraphNode node) { Field[] fields = getAllInstanceFields(node.getValue()); for (int i = 0; i < fields.length; i++) { Object fieldValue = readField(fields[i], node.getValue()); String fieldType = fields[i].getType().getName(); ObjectGraphNode childNode = new ObjectGraphNode(++nodeCount, fields[i].getName(), fieldType, fieldValue); node.add(childNode); visit(childNode); } }
writer.println(ObjectGraphDump.dump(this).toFlatSummary()); } catch (Exception e) { LOG.error("Failed to dump component", e);
/** * Visits all the fields in the given ComponentModel. * * @param node the ObjectGraphNode containing the ComponentModel. */ private void visitComponentModel(final ObjectGraphNode node) { ComponentModel model = (ComponentModel) node.getValue(); ComponentModel sharedModel = null; List<Field> fieldList = ReflectionUtil.getAllFields(node.getValue(), true, false); Field[] fields = fieldList.toArray(new Field[fieldList.size()]); for (int i = 0; i < fields.length; i++) { if (ComponentModel.class.equals(fields[i].getDeclaringClass()) && "sharedModel".equals(fields[i].getName())) { sharedModel = (ComponentModel) readField(fields[i], model); } } visitComplexTypeWithDiff(node, sharedModel); }
/** * 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); } }
writer.println(ObjectGraphDump.dump(currentComp).toFlatSummary()); } catch (Exception e) { LOG.error("Failed to dump component", e);
ObjectGraphNode root = ObjectGraphDump.dump(treeAndSession);
getName(), key); node.add(keyNode); visit(keyNode); } else { ObjectGraphNode keyNode = new ObjectGraphNode(++nodeCount, "key", Object.class. getClass().getName(), value); node.add(valueNode); visit(valueNode); } else { ObjectGraphNode valueNode = new ObjectGraphNode(++nodeCount, "value", Object.class. adjustOverhead(node);
ObjectGraphNode root = ObjectGraphDump.dump(treeAndSession);
@Test public void testDump() { WPanel component = new WPanel(); component.setLayout(new BorderLayout()); component.add(new WLabel(TEST_LABEL), BorderLayout.NORTH); ObjectGraphNode graphNode = ObjectGraphDump.dump(component); String result = graphNode.toXml(); // ObjectGraphNode tested independently // for the input 'component' above - the dump result must at least contain the following // and have run without exceptions Assert.assertTrue("", result.indexOf("type=\"com.github.bordertech.wcomponents.WPanel\"") != -1); Assert.assertTrue("", result.indexOf( "field=\"label\" type=\"com.github.bordertech.wcomponents.WLabel\"") != -1); Assert.assertTrue("", result.indexOf("field=\"text\" value=\""" + TEST_LABEL + ""\" type=\"java.io.Serializable\"") != -1); Assert.assertTrue("", result.indexOf( "field=\"value\" type=\"com.github.bordertech.wcomponents.layout.BorderLayout$BorderLayoutConstraint\"") != -1); } }