@Test public void testDeepCopy() { // Set all non-default fields in an Interop instance: Interop.Builder interopBuilder = Interop.newBuilder(); interopBuilder.setArrayField(Arrays.asList(new Double[] {1.1, 1.2, 1.3, 1.4})); interopBuilder.setBoolField(true); interopBuilder.setBytesField(ByteBuffer.wrap(new byte[] {1, 2, 3, 4})); interopBuilder.setDoubleField(3.14d); interopBuilder.setEnumField(Kind.B); interopBuilder.setFixedField( new MD5(new byte[] {4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1})); interopBuilder.setFloatField(6.022f); interopBuilder.setIntField(32); interopBuilder.setLongField(64L); Map<java.lang.String, org.apache.avro.Foo> map = new HashMap<java.lang.String, org.apache.avro.Foo>(1); map.put("foo", Foo.newBuilder().setLabel("bar").build()); interopBuilder.setMapField(map); interopBuilder.setNullField(null); Node.Builder rootBuilder = Node.newBuilder().setLabel("/"); Node.Builder homeBuilder = Node.newBuilder().setLabel("home"); homeBuilder.setChildren(new ArrayList<Node>(0)); rootBuilder.setChildren(Arrays.asList(new Node[] {homeBuilder.build()})); interopBuilder.setRecordField(rootBuilder.build()); interopBuilder.setStringField("Hello"); interopBuilder.setUnionField(true); Interop interop = interopBuilder.build(); // Verify that deepCopy works for all fields: for (Field field : Interop.SCHEMA$.getFields()) { // Original field and deep copy should be equivalent: if (interop.get(field.pos()) instanceof ByteBuffer) { assertTrue( Arrays.equals( ((ByteBuffer) interop.get(field.pos())).array(), ((ByteBuffer) GenericData.get().deepCopy(field.schema(), interop.get(field.pos()))) .array())); } else { assertEquals( interop.get(field.pos()), SpecificData.get().deepCopy(field.schema(), interop.get(field.pos()))); } // Original field and deep copy should be different instances: if ((field.schema().getType() != Type.ENUM) && (field.schema().getType() != Type.NULL) && (field.schema().getType() != Type.STRING)) { assertFalse( "Field " + field.name() + " is same instance in deep copy", interop.get(field.pos()) == GenericData.get().deepCopy(field.schema(), interop.get(field.pos()))); } } }
/** * Inspects the currently edited object and clones plugins if they are needed. * * @throws EClassEditorMissing Will be thrown if there is no EditPlugin registered for a certain * class but the currently edited object has an attribute of that class. Note that this won' t * happen in this implementation because by default an editor for missing classes is * registered. */ private void inspectEditedObject() throws EClassEditorMissing { Method[] tempMethods = editedObject.getClass().getDeclaredMethods(); // methods = new HashMap<GSMethod, EditPlugin>(); methods = new HashMap<GSMethod, EditPlugin>(); Map<String, GSMethod> map = this.editedObject.getAllAttribs(); Set<String> set = map.keySet(); Iterator<String> iter = set.iterator(); while (iter.hasNext()) { String key = iter.next(); GSMethod x = map.get(key); EditPlugin plug = this.getNewPlugin(x.getMethod.getReturnType()); if (!x.show) continue; if (plug == null) plug = this.getNewPlugin(Object.class); plug.setName(key); try { Object obj = null; try { obj = x.getMethod.invoke(this.editedObject, new Object[] {}); } catch (Exception e) { obj = null; } plug.setValue(obj); } catch (Exception e) { JOptionPane.showMessageDialog( null, "Error: " + e.toString(), "Error!", JOptionPane.ERROR_MESSAGE); } if (x instanceof GSMethodForeign) { EditPlugin original = plug; plug = new ForeignKeyEditPlugin(); ((ForeignKeyEditPlugin) plug).setEditPlugin(original); ((ForeignKeyEditPlugin) plug).setEditedTable(((GSMethodForeign) x).tableName); ((ForeignKeyEditPlugin) plug).setEditedAttrib(((GSMethodForeign) x).attribName); ((ForeignKeyEditPlugin) plug).setEditedClass(((GSMethodForeign) x).objectClass); ((ForeignKeyEditPlugin) plug).setParentFrame(this.parentFrame); } if (x instanceof GSMethodPrimary) { // do nothing } methods.put(x, plug); } }