@Override public void serialize(PropertyPath path, Object object, WriteContext context) { context.put(path, Persistent.object(alias)); BiConsumer<String, AccessibleProperty> propertySerializer = (name, property) -> { PropertyPath subPath = path.property(name); Object value = property.get(object); context.serialize(subPath, value); }; properties.forEach(propertySerializer); if (identifier != null && identifier.property.isWritable()) { propertySerializer.accept(identifier.name, identifier.property); } }
@Override public void bind(PropertyTree propertyTree, Object object, ReadContext context) throws Exception { for (PropertyTree child : propertyTree.getChildren()) { NodeId nodeId = child.getNodeId(); if (nodeId.isKey() && !constructor.hasParameter(nodeId.getKey())) { AccessibleProperty property = properties.get(nodeId.getKey()); if (property != null && property.isWritable()) { Object value = context.getObject(child); property.set(object, value); } } } }
private void add(String name, AccessibleProperty property, TypeContext typeContext) { if (!property.isWritable() && !creatorParameters.contains(name)) { throw new IllegalArgumentException(type.getSimpleName() + "." + name + " should have a matching setter or constructor parameter"); } SubPath subPath = path.property(name); if (!properties.containsKey(name)) { properties.put(name, property); context.describeAsync(subPath, typeContext); } }
public NodeId toNodeId(Object object, WriteContext writeContext) { if (identifier == null) { throw new UnsupportedOperationException("toNodeId is not supported for composite ids"); } Object id = identifier.property.get(object); return identifier.idType.toNodeId(id, writeContext); }
@Override public Object instantiate(PropertyTree propertyTree, Object valueObject, ReadContext context) throws Exception { Object[] params = constructor.newParametersArray(); int i=0; for (String param : constructor.getParameters()) { PropertyTree child = propertyTree.get(NodeId.property(param)); Object value = child != null ? context.getObject(child) : null; params[i++] = value; } if (constructor.hasParameters()) { // Ensure that constructor parameters are bound context.bindAll(); } Object object = constructor.newInstance(params); if (identifier != null && !constructor.hasParameter(identifier.name) && identifier.property.isWritable()) { PropertyTree child = propertyTree.get(NodeId.property(identifier.name)); if (child != null) { Object value = context.getObject(child); if (value != null) { identifier.property.set(object, value); } } } return object; }
private void setIdentifier(String name, AccessibleProperty property, TypeContext typeContext) { if (identifier != null) { throw new IllegalArgumentException(type.getSimpleName() + " should not have multiple @Id-properties"); } IdentifiableType idType; if (property.isWritable() || creatorParameters.contains(name)) { idType = (IdentifiableType) context.describeNow(path.property(name), typeContext); } else { idType = (IdentifiableType) context.describeNow(null, typeContext); } identifier = new ObjectIdentifier(property, idType, name); }
@Override public NodeId toNodeId(Object object, WriteContext context) { Object keyValue = property.get(object); return identifiableType.toNodeId(keyValue, context); } }