*/
package org.opendaylight.mdsal.binding.dom.codec.impl;
-import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Verify.verify;
import com.google.common.annotations.Beta;
-import com.google.common.collect.ImmutableClassToInstanceMap;
import java.io.IOException;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.Proxy;
import java.util.Collection;
import java.util.List;
import java.util.Map;
-import java.util.Map.Entry;
+import java.util.Set;
import org.opendaylight.mdsal.binding.dom.codec.api.BindingStreamEventWriter;
-import org.opendaylight.mdsal.binding.dom.codec.util.AugmentationReader;
-import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
import org.opendaylight.yangtools.yang.binding.Augmentable;
import org.opendaylight.yangtools.yang.binding.Augmentation;
import org.opendaylight.yangtools.yang.binding.DataContainer;
protected static final void streamAugmentations(final DataObjectSerializerRegistry registry,
final BindingStreamEventWriter writer, final Augmentable<?> obj) throws IOException {
- final Map<Class<? extends Augmentation<?>>, Augmentation<?>> augmentations;
- if (registry instanceof AugmentationReader) {
- augmentations = ((AugmentationReader) registry).getAugmentations(obj);
- } else if (Proxy.isProxyClass(obj.getClass())) {
- augmentations = getFromProxy(obj);
- } else {
- augmentations = BindingReflections.getAugmentations(obj);
- }
- for (final Entry<Class<? extends Augmentation<?>>, Augmentation<?>> aug : augmentations.entrySet()) {
+ for (final var aug : obj.augmentations().entrySet()) {
emitAugmentation(aug.getKey(), aug.getValue(), writer, registry);
}
}
- private static Map<Class<? extends Augmentation<?>>, Augmentation<?>> getFromProxy(final Augmentable<?> obj) {
- final InvocationHandler proxy = Proxy.getInvocationHandler(obj);
- if (proxy instanceof AugmentationReader) {
- return ((AugmentationReader) proxy).getAugmentations(obj);
- }
- return ImmutableClassToInstanceMap.of();
- }
-
protected static final <C extends DataContainer> void streamChoice(final Class<C> choiceClass,
final DataObjectSerializerRegistry registry, final BindingStreamEventWriter writer, final C value)
throws IOException {
}
protected static final void streamLeafList(final BindingStreamEventWriter writer, final String localName,
- final List<?> value) throws IOException {
+ final Set<?> value) throws IOException {
if (value != null) {
writer.startLeafSet(localName, value.size());
commonStreamLeafset(writer, value);
protected static final <E extends DataObject> void streamList(final Class<E> childClass,
final DataObjectStreamer<E> childStreamer, final DataObjectSerializerRegistry registry,
final BindingStreamEventWriter writer, final List<? extends E> value) throws IOException {
- if (value != null) {
- writer.startUnkeyedList(childClass, value.size());
+ final int size = nullSize(value);
+ if (size != 0) {
+ writer.startUnkeyedList(childClass, size);
commonStreamList(registry, writer, childStreamer, value);
}
}
protected static final <E extends DataObject & Identifiable<?>> void streamMap(final Class<E> childClass,
final DataObjectStreamer<E> childStreamer, final DataObjectSerializerRegistry registry,
- final BindingStreamEventWriter writer, final List<? extends E> value) throws IOException {
- if (value != null) {
- writer.startMapNode(childClass, value.size());
- commonStreamList(registry, writer, childStreamer, value);
+ final BindingStreamEventWriter writer, final Map<?, ? extends E> value) throws IOException {
+ final int size = nullSize(value);
+ if (size != 0) {
+ writer.startMapNode(childClass, size);
+ commonStreamList(registry, writer, childStreamer, value.values());
}
}
protected static final <E extends DataObject & Identifiable<?>> void streamOrderedMap(final Class<E> childClass,
final DataObjectStreamer<E> childStreamer, final DataObjectSerializerRegistry registry,
final BindingStreamEventWriter writer, final List<? extends E> value) throws IOException {
- if (value != null) {
- writer.startOrderedMapNode(childClass, value.size());
+ final int size = nullSize(value);
+ if (size != 0) {
+ writer.startOrderedMapNode(childClass, size);
commonStreamList(registry, writer, childStreamer, value);
}
}
writer.endNode();
}
- private static void commonStreamLeafset(final BindingStreamEventWriter writer, final List<?> value)
+ private static void commonStreamLeafset(final BindingStreamEventWriter writer, final Collection<?> value)
throws IOException {
for (Object entry : value) {
writer.leafSetEntryNode(entry);
writer.endNode();
}
- @SuppressWarnings("rawtypes")
- private static void emitAugmentation(final Class type, final Augmentation<?> value,
+ private static void emitAugmentation(final Class<? extends Augmentation<?>> type, final Augmentation<?> value,
final BindingStreamEventWriter writer, final DataObjectSerializerRegistry registry) throws IOException {
/*
- * Binding Specification allowed to insert augmentation with null for
- * value, which effectively could be used to remove augmentation
- * from builder / DTO.
+ * Binding Specification allowed to insert augmentation with null for value, which effectively could be used to
+ * remove augmentation from builder / DTO.
*/
if (value != null) {
- checkArgument(value instanceof DataObject);
- @SuppressWarnings("unchecked")
final DataObjectSerializer serializer = registry.getSerializer(type);
if (serializer != null) {
- serializer.serialize((DataObject) value, writer);
+ serializer.serialize(value, writer);
} else {
LOG.warn("DataObjectSerializer is not present for {} in registry {}", type, registry);
}
@SuppressWarnings("unchecked")
private static <T extends DataObject> boolean tryCache(final BindingStreamEventWriter writer, final T value) {
- return writer instanceof BindingSerializer ? ((BindingSerializer<?, T>) writer).serialize(value) == null : true;
+ // Force serialization if writer is not a BindingSerializer, otherwise defer to it for a decision
+ return !(writer instanceof BindingSerializer) || ((BindingSerializer<?, T>) writer).serialize(value) == null;
+ }
+
+ private static int nullSize(final Collection<?> list) {
+ return list == null ? 0 : list.size();
+ }
+
+ private static int nullSize(final Map<?, ?> map) {
+ return map == null ? 0 : map.size();
}
}