/** * Marks the given object as having been visited. * * @param node the Node containing the object to mark as visited. */ private void markObjectVisited(final ObjectGraphNode node) { visitedNodes.put(node.getValue(), node); }
/** * Indicates whether the given object has been visited. * * @param node the Node containing the object to check. * @return true if the object has been visited, false if not. */ private boolean isObjectVisited(final ObjectGraphNode node) { return visitedNodes.containsKey(node.getValue()); }
/** * 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); } }
/** * 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. * * @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); } }
/** * 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 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); } } }
Map map = (Map) node.getValue();
@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()); }
Object currentValue = currentNode.getValue();