1 package org.opendaylight.controller.sal.binding.dom.serializer.impl;
3 import java.lang.ref.WeakReference;
4 import java.lang.reflect.Field;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.Collections;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.Iterator;
11 import java.util.List;
13 import java.util.Map.Entry;
14 import java.util.Objects;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
18 import java.util.WeakHashMap;
20 import org.opendaylight.controller.sal.binding.dom.serializer.api.AugmentationCodec;
21 import org.opendaylight.controller.sal.binding.dom.serializer.api.ChoiceCaseCodec;
22 import org.opendaylight.controller.sal.binding.dom.serializer.api.ChoiceCodec;
23 import org.opendaylight.controller.sal.binding.dom.serializer.api.CodecRegistry;
24 import org.opendaylight.controller.sal.binding.dom.serializer.api.DataContainerCodec;
25 import org.opendaylight.controller.sal.binding.dom.serializer.api.DomCodec;
26 import org.opendaylight.controller.sal.binding.dom.serializer.api.IdentifierCodec;
27 import org.opendaylight.controller.sal.binding.dom.serializer.api.InstanceIdentifierCodec;
28 import org.opendaylight.controller.sal.binding.dom.serializer.api.ValueWithQName;
29 import org.opendaylight.controller.sal.binding.impl.util.ClassLoaderUtils;
30 import org.opendaylight.controller.sal.core.api.model.SchemaServiceListener;
31 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
32 import org.opendaylight.yangtools.binding.generator.util.Types;
33 import org.opendaylight.yangtools.concepts.Delegator;
34 import org.opendaylight.yangtools.concepts.Identifiable;
35 import org.opendaylight.yangtools.yang.binding.Augmentable;
36 import org.opendaylight.yangtools.yang.binding.Augmentation;
37 import org.opendaylight.yangtools.yang.binding.BindingCodec;
38 import org.opendaylight.yangtools.yang.binding.DataContainer;
39 import org.opendaylight.yangtools.yang.binding.DataObject;
40 import org.opendaylight.yangtools.yang.binding.Identifier;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
43 import org.opendaylight.yangtools.yang.data.api.Node;
44 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
45 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
46 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
47 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
48 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
49 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
52 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
53 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
54 import org.opendaylight.yangtools.yang.model.api.UsesNode;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 import static com.google.common.base.Preconditions.*;
59 import static org.opendaylight.controller.sal.binding.dom.serializer.impl.IntermediateMapping.*;
61 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleContext;
62 import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
63 import org.opendaylight.yangtools.sal.binding.model.api.Type;
64 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
65 import org.opendaylight.yangtools.yang.model.api.Module;
66 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
68 import com.google.common.util.concurrent.CycleDetectingLockFactory.WithExplicitOrdering;
70 public class LazyGeneratedCodecRegistry implements //
72 SchemaServiceListener, //
75 private final static Logger LOG = LoggerFactory.getLogger(LazyGeneratedCodecRegistry.class);
76 private final static LateMixinCodec NOT_READY_CODEC = new LateMixinCodec();
78 private final InstanceIdentifierCodec instanceIdentifierCodec = new InstanceIdentifierCodecImpl(this);
80 private TransformerGenerator generator;
82 // Concrete class to codecs
83 private Map<Class<?>, DataContainerCodec<?>> containerCodecs = new WeakHashMap<>();
84 private Map<Class<?>, IdentifierCodec<?>> identifierCodecs = new WeakHashMap<>();
85 private Map<Class<?>, ChoiceCodecImpl<?>> choiceCodecs = new WeakHashMap<>();
86 private Map<Class<?>, ChoiceCaseCodecImpl<?>> caseCodecs = new WeakHashMap<>();
87 private Map<Class<?>, AugmentableCompositeCodec> augmentableCodecs = new WeakHashMap<>();
89 /** Binding type to encountered classes mapping **/
90 @SuppressWarnings("rawtypes")
91 Map<Type, WeakReference<Class>> typeToClass = new ConcurrentHashMap<>();
93 @SuppressWarnings("rawtypes")
94 private ConcurrentMap<Type, ChoiceCaseCodecImpl> typeToCaseCodecs = new ConcurrentHashMap<>();
96 private CaseClassMapFacade classToCaseRawCodec = new CaseClassMapFacade();
98 Map<SchemaPath, GeneratedTypeBuilder> pathToType = new ConcurrentHashMap<>();
99 Map<List<QName>, Type> pathToInstantiatedType = new ConcurrentHashMap<>();
101 private SchemaContext currentSchema;
103 public TransformerGenerator getGenerator() {
107 public void setGenerator(TransformerGenerator generator) {
108 this.generator = generator;
112 public InstanceIdentifierCodec getInstanceIdentifierCodec() {
113 return instanceIdentifierCodec;
117 public <T extends Augmentation<?>> AugmentationCodec<T> getCodecForAugmentation(Class<T> object) {
118 // TODO Auto-generated method stub
123 public Class<?> getClassForPath(List<QName> names) {
124 DataSchemaNode node = getSchemaNode(names);
125 SchemaPath path = node.getPath();
126 Type type = pathToType.get(path);
128 type = new ReferencedTypeImpl(type.getPackageName(), type.getName());
130 type = pathToInstantiatedType.get(names);
132 @SuppressWarnings("rawtypes")
133 WeakReference<Class> weakRef = typeToClass.get(type);
134 if (weakRef == null) {
135 LOG.error("Could not find loaded class for path: {} and type: {}", path, type.getFullyQualifiedName());
137 return weakRef.get();
141 public void putPathToClass(List<QName> names, Class<?> cls) {
142 Type reference = Types.typeForClass(cls);
143 pathToInstantiatedType.put(names, reference);
144 bindingClassEncountered(cls);
148 public IdentifierCodec<?> getKeyCodecForPath(List<QName> names) {
149 @SuppressWarnings("unchecked")
150 Class<? extends Identifiable<?>> cls = (Class<? extends Identifiable<?>>) getClassForPath(names);
151 return getIdentifierCodecForIdentifiable(cls);
155 public <T extends DataContainer> DataContainerCodec<T> getCodecForDataObject(Class<T> type) {
156 @SuppressWarnings("unchecked")
157 DataContainerCodec<T> ret = (DataContainerCodec<T>) containerCodecs.get(type);
161 Class<? extends BindingCodec<Map<QName, Object>, Object>> newType = generator.transformerFor(type);
162 BindingCodec<Map<QName, Object>, Object> rawCodec = newInstanceOf(newType);
163 DataContainerCodecImpl<T> newWrapper = new DataContainerCodecImpl<>(rawCodec);
164 containerCodecs.put(type, newWrapper);
169 @SuppressWarnings("rawtypes")
170 public void bindingClassEncountered(Class cls) {
172 ConcreteType typeRef = Types.typeForClass(cls);
173 if (typeToClass.containsKey(typeRef)) {
176 LOG.info("Binding Class {} encountered.", cls);
177 WeakReference<Class> weakRef = new WeakReference<>(cls);
178 typeToClass.put(typeRef, weakRef);
179 if (Augmentation.class.isAssignableFrom(cls)) {
181 } else if (DataObject.class.isAssignableFrom(cls)) {
182 @SuppressWarnings({ "unchecked", "unused" })
183 Object cdc = getCodecForDataObject((Class<? extends DataObject>) cls);
188 public void onClassProcessed(Class<?> cls) {
189 ConcreteType typeRef = Types.typeForClass(cls);
190 if (typeToClass.containsKey(typeRef)) {
193 LOG.info("Binding Class {} encountered.", cls);
194 WeakReference<Class> weakRef = new WeakReference<>((Class) cls);
195 typeToClass.put(typeRef, weakRef);
198 private DataSchemaNode getSchemaNode(List<QName> path) {
199 QName firstNode = path.get(0);
200 DataNodeContainer previous = currentSchema.findModuleByNamespaceAndRevision(firstNode.getNamespace(),
201 firstNode.getRevision());
202 Iterator<QName> iterator = path.iterator();
203 while (iterator.hasNext()) {
204 QName arg = iterator.next();
205 DataSchemaNode currentNode = previous.getDataChildByName(arg);
206 if (currentNode == null && previous instanceof DataNodeContainer) {
207 currentNode = searchInChoices(previous, arg);
209 if (currentNode instanceof DataNodeContainer) {
210 previous = (DataNodeContainer) currentNode;
211 } else if (currentNode instanceof LeafSchemaNode || currentNode instanceof LeafListSchemaNode) {
212 checkState(!iterator.hasNext(), "Path tries to nest inside leaf node.");
216 return (DataSchemaNode) previous;
219 private DataSchemaNode searchInChoices(DataNodeContainer node, QName arg) {
220 Set<DataSchemaNode> children = node.getChildNodes();
221 for (DataSchemaNode child : children) {
222 if (child instanceof ChoiceNode) {
223 ChoiceNode choiceNode = (ChoiceNode) child;
224 DataSchemaNode potential = searchInCases(choiceNode, arg);
225 if (potential != null) {
233 private DataSchemaNode searchInCases(ChoiceNode choiceNode, QName arg) {
234 Set<ChoiceCaseNode> cases = choiceNode.getCases();
235 for (ChoiceCaseNode caseNode : cases) {
236 DataSchemaNode node = caseNode.getDataChildByName(arg);
244 private <T> T newInstanceOf(Class<?> newType) {
246 @SuppressWarnings("unchecked")
247 T ret = (T) newType.newInstance();
249 } catch (InstantiationException e) {
250 throw new IllegalStateException(e);
251 } catch (IllegalAccessException e) {
252 throw new IllegalStateException(e);
257 public <T extends Identifiable<?>> IdentifierCodec<?> getIdentifierCodecForIdentifiable(Class<T> type) {
258 IdentifierCodec<?> obj = identifierCodecs.get(type);
262 Class<? extends BindingCodec<Map<QName, Object>, Object>> newCodec = generator
263 .keyTransformerForIdentifiable(type);
264 BindingCodec<Map<QName, Object>, Object> newInstance;
265 newInstance = newInstanceOf(newCodec);
266 IdentifierCodecImpl<?> newWrapper = new IdentifierCodecImpl<>(newInstance);
267 identifierCodecs.put(type, newWrapper);
272 public void onCodecCreated(Class<?> cls) {
273 CodecMapping.setIdentifierCodec(cls, instanceIdentifierCodec);
277 public <T extends Identifier<?>> IdentifierCodec<T> getCodecForIdentifier(Class<T> object) {
278 @SuppressWarnings("unchecked")
279 IdentifierCodec<T> obj = (IdentifierCodec<T>) identifierCodecs.get(object);
283 Class<? extends BindingCodec<Map<QName, Object>, Object>> newCodec = generator
284 .keyTransformerForIdentifier(object);
285 BindingCodec<Map<QName, Object>, Object> newInstance;
286 newInstance = newInstanceOf(newCodec);
287 IdentifierCodecImpl<T> newWrapper = new IdentifierCodecImpl<>(newInstance);
288 identifierCodecs.put(object, newWrapper);
292 @SuppressWarnings("rawtypes")
293 public ChoiceCaseCodecImpl getCaseCodecFor(Class caseClass) {
294 ChoiceCaseCodecImpl<?> potential = caseCodecs.get(caseClass);
295 if (potential != null) {
298 ConcreteType typeref = Types.typeForClass(caseClass);
299 ChoiceCaseCodecImpl caseCodec = typeToCaseCodecs.get(typeref);
301 checkState(caseCodec != null, "Case Codec was not created proactivelly for %s", caseClass.getName());
302 checkState(caseCodec.getSchema() != null, "Case schema is not available for %s", caseClass.getName());
303 @SuppressWarnings("unchecked")
304 Class<? extends BindingCodec> newCodec = generator.caseCodecFor(caseClass, caseCodec.getSchema());
305 BindingCodec newInstance = newInstanceOf(newCodec);
306 caseCodec.setDelegate(newInstance);
307 caseCodecs.put(caseClass, caseCodec);
309 for (Entry<Class<?>, ChoiceCodecImpl<?>> choice : choiceCodecs.entrySet()) {
310 if (choice.getKey().isAssignableFrom(caseClass)) {
311 choice.getValue().cases.put(caseClass, caseCodec);
317 public void onModuleContextAdded(SchemaContext schemaContext, Module module, ModuleContext context) {
318 pathToType.putAll(context.getChildNodes());
319 captureCases(context.getCases(), schemaContext);
322 private void captureCases(Map<SchemaPath, GeneratedTypeBuilder> cases, SchemaContext module) {
323 for (Entry<SchemaPath, GeneratedTypeBuilder> caseNode : cases.entrySet()) {
324 ReferencedTypeImpl typeref = new ReferencedTypeImpl(caseNode.getValue().getPackageName(), caseNode
325 .getValue().getName());
327 LOG.info("Case path: {} Type : {}", caseNode.getKey(), caseNode.getValue().getFullyQualifiedName());
328 pathToType.put(caseNode.getKey(), caseNode.getValue());
330 ChoiceCaseNode node = (ChoiceCaseNode) SchemaContextUtil.findDataSchemaNode(module, caseNode.getKey());
333 LOG.error("YANGTools Bug: SchemaNode for {}, with path {} was not found in context.",
334 typeref.getFullyQualifiedName(), caseNode.getKey());
335 @SuppressWarnings("rawtypes")
336 ChoiceCaseCodecImpl value = new ChoiceCaseCodecImpl();
337 typeToCaseCodecs.putIfAbsent(typeref, value);
340 @SuppressWarnings("rawtypes")
341 ChoiceCaseCodecImpl value = new ChoiceCaseCodecImpl(node);
342 typeToCaseCodecs.putIfAbsent(typeref, value);
347 public void onGlobalContextUpdated(SchemaContext context) {
348 currentSchema = context;
351 @SuppressWarnings({ "unchecked", "rawtypes" })
353 public void onChoiceCodecCreated(Class<?> choiceClass,
354 Class<? extends BindingCodec<Map<QName, Object>, Object>> choiceCodec, ChoiceNode schema) {
355 ChoiceCodec<?> oldCodec = choiceCodecs.get(choiceClass);
356 checkState(oldCodec == null);
357 BindingCodec<Map<QName, Object>, Object> delegate = newInstanceOf(choiceCodec);
358 ChoiceCodecImpl<?> newCodec = new ChoiceCodecImpl(delegate);
359 choiceCodecs.put(choiceClass, newCodec);
360 CodecMapping.setClassToCaseMap(choiceCodec, (Map<Class<?>, BindingCodec<?, ?>>) classToCaseRawCodec);
361 CodecMapping.setCompositeNodeToCaseMap(choiceCodec, newCodec.getCompositeToCase());
363 tryToCreateCasesCodecs(schema);
367 private void tryToCreateCasesCodecs(ChoiceNode schema) {
368 for (ChoiceCaseNode caseNode : schema.getCases()) {
369 SchemaPath path = caseNode.getPath();
370 GeneratedTypeBuilder type;
371 if (path != null && (type = pathToType.get(path)) != null) {
372 ReferencedTypeImpl typeref = new ReferencedTypeImpl(type.getPackageName(), type.getName());
373 ChoiceCaseCodecImpl partialCodec = typeToCaseCodecs.get(typeref);
374 if(partialCodec.getSchema() == null ) {
375 partialCodec.setSchema(caseNode);
378 Class<?> caseClass = ClassLoaderUtils.tryToLoadClassWithTCCL(type.getFullyQualifiedName());
379 if (caseClass != null) {
380 getCaseCodecFor(caseClass);
388 public void onValueCodecCreated(Class<?> valueClass, Class<?> valueCodec) {
392 public void onCaseCodecCreated(Class<?> choiceClass,
393 Class<? extends BindingCodec<Map<QName, Object>, Object>> choiceCodec) {
397 public void onDataContainerCodecCreated(Class<?> dataClass, Class<? extends BindingCodec<?, ?>> dataCodec) {
398 if (Augmentable.class.isAssignableFrom(dataClass)) {
399 AugmentableCompositeCodec augmentableCodec = getAugmentableCodec(dataClass);
400 CodecMapping.setAugmentationCodec(dataCodec, augmentableCodec);
405 private AugmentableCompositeCodec getAugmentableCodec(Class<?> dataClass) {
406 AugmentableCompositeCodec ret = augmentableCodecs.get(dataClass);
410 ret = new AugmentableCompositeCodec(dataClass);
411 augmentableCodecs.put(dataClass, ret);
415 private static abstract class IntermediateCodec<T> implements //
416 DomCodec<T>, Delegator<BindingCodec<Map<QName, Object>, Object>> {
418 private final BindingCodec<Map<QName, Object>, Object> delegate;
421 public BindingCodec<Map<QName, Object>, Object> getDelegate() {
425 public IntermediateCodec(BindingCodec<Map<QName, Object>, Object> delegate) {
426 this.delegate = delegate;
430 public Node<?> serialize(ValueWithQName<T> input) {
431 Map<QName, Object> intermediateOutput = delegate.serialize(input);
432 return toNode(intermediateOutput);
436 private static class IdentifierCodecImpl<T extends Identifier<?>> //
437 extends IntermediateCodec<T> //
438 implements IdentifierCodec<T> {
440 public IdentifierCodecImpl(BindingCodec<Map<QName, Object>, Object> delegate) {
445 public ValueWithQName<T> deserialize(Node<?> input) {
446 QName qname = input.getNodeType();
447 @SuppressWarnings("unchecked")
448 T value = (T) getDelegate().deserialize((Map<QName, Object>) input);
449 return new ValueWithQName<T>(qname, value);
453 public CompositeNode serialize(ValueWithQName<T> input) {
454 return (CompositeNode) super.serialize(input);
458 private static class DataContainerCodecImpl<T extends DataContainer> //
459 extends IntermediateCodec<T> //
460 implements DataContainerCodec<T> {
462 public DataContainerCodecImpl(BindingCodec<Map<QName, Object>, Object> delegate) {
467 public ValueWithQName<T> deserialize(Node<?> input) {
471 QName qname = input.getNodeType();
472 @SuppressWarnings("unchecked")
473 T value = (T) getDelegate().deserialize((Map<QName, Object>) input);
474 return new ValueWithQName<T>(qname, value);
478 public CompositeNode serialize(ValueWithQName<T> input) {
479 return (CompositeNode) super.serialize(input);
483 @SuppressWarnings("rawtypes")
484 private static class ChoiceCaseCodecImpl<T extends DataContainer> implements ChoiceCaseCodec<T>, //
485 Delegator<BindingCodec> {
486 private boolean augmenting;
487 private BindingCodec delegate;
489 private Set<String> validNames;
490 private Set<QName> validQNames;
491 private ChoiceCaseNode schema;
493 public void setSchema(ChoiceCaseNode caseNode) {
494 this.schema = schema;
495 this.schema = caseNode;
496 validNames = new HashSet<>();
497 validQNames = new HashSet<>();
498 for (DataSchemaNode node : caseNode.getChildNodes()) {
499 QName qname = node.getQName();
500 validQNames.add(qname);
501 validNames.add(qname.getLocalName());
503 augmenting = caseNode.isAugmenting();
506 public ChoiceCaseCodecImpl() {
507 this.delegate = NOT_READY_CODEC;
510 public ChoiceCaseCodecImpl(ChoiceCaseNode caseNode) {
511 this.delegate = NOT_READY_CODEC;
516 public ValueWithQName<T> deserialize(Node<?> input) {
517 throw new UnsupportedOperationException("Direct invocation of this codec is not allowed.");
521 public CompositeNode serialize(ValueWithQName<T> input) {
522 throw new UnsupportedOperationException("Direct invocation of this codec is not allowed.");
525 public BindingCodec getDelegate() {
529 public void setDelegate(BindingCodec delegate) {
530 this.delegate = delegate;
533 public ChoiceCaseNode getSchema() {
538 public boolean isAcceptable(Node<?> input) {
539 if (input instanceof CompositeNode) {
541 return checkAugmenting((CompositeNode) input);
543 return checkLocal((CompositeNode) input);
549 private boolean checkLocal(CompositeNode input) {
550 QName parent = input.getNodeType();
551 for (Node<?> childNode : input.getChildren()) {
552 QName child = childNode.getNodeType();
553 if (!Objects.equals(parent.getNamespace(), child.getNamespace())
554 || !Objects.equals(parent.getRevision(), child.getRevision())) {
557 if (validNames.contains(child.getLocalName())) {
564 private boolean checkAugmenting(CompositeNode input) {
565 for (Node<?> child : input.getChildren()) {
566 if (validQNames.contains(child.getNodeType())) {
574 private static class ChoiceCodecImpl<T> implements ChoiceCodec<T> {
576 private final BindingCodec<Map<QName, Object>, Object> delegate;
578 @SuppressWarnings("rawtypes")
579 private final Map<Class, ChoiceCaseCodecImpl<?>> cases = new WeakHashMap<>();
581 private final CaseCompositeNodeMapFacade CompositeToCase;
583 public ChoiceCodecImpl(BindingCodec<Map<QName, Object>, Object> delegate) {
584 this.delegate = delegate;
585 this.CompositeToCase = new CaseCompositeNodeMapFacade(cases);
589 public ValueWithQName<T> deserialize(Node<?> input) {
590 throw new UnsupportedOperationException("Direct invocation of this codec is not allowed.");
594 public Node<?> serialize(ValueWithQName<T> input) {
595 throw new UnsupportedOperationException("Direct invocation of this codec is not allowed.");
598 public CaseCompositeNodeMapFacade getCompositeToCase() {
599 return CompositeToCase;
602 public Map<Class, ChoiceCaseCodecImpl<?>> getCases() {
606 public BindingCodec<Map<QName, Object>, Object> getDelegate() {
612 @SuppressWarnings("rawtypes")
613 private class CaseClassMapFacade extends MapFacadeBase {
616 public Set<java.util.Map.Entry<Class, BindingCodec<Object, Object>>> entrySet() {
617 return Collections.emptySet();
621 public BindingCodec get(Object key) {
622 if (key instanceof Class) {
623 Class cls = (Class) key;
624 // bindingClassEncountered(cls);
625 ChoiceCaseCodecImpl caseCodec = getCaseCodecFor(cls);
626 return caseCodec.getDelegate();
632 @SuppressWarnings("rawtypes")
633 private static class CaseCompositeNodeMapFacade extends MapFacadeBase<CompositeNode> {
635 final Map<Class, ChoiceCaseCodecImpl<?>> choiceCases;
637 public CaseCompositeNodeMapFacade(Map<Class, ChoiceCaseCodecImpl<?>> choiceCases) {
638 this.choiceCases = choiceCases;
642 public BindingCodec get(Object key) {
643 if (!(key instanceof CompositeNode)) {
646 for (java.util.Map.Entry<Class, ChoiceCaseCodecImpl<?>> entry : choiceCases.entrySet()) {
647 ChoiceCaseCodecImpl<?> codec = entry.getValue();
648 if (codec.isAcceptable((CompositeNode) key)) {
649 return codec.getDelegate();
658 * This map is used as only facade for {@link BindingCodec} in different
659 * classloaders to retrieve codec dynamicly based on provided key.
664 @SuppressWarnings("rawtypes")
665 private static abstract class MapFacadeBase<T> implements Map<T, BindingCodec<?, ?>> {
668 public boolean containsKey(Object key) {
669 return get(key) != null;
673 public void clear() {
674 throw notModifiable();
678 public boolean equals(Object obj) {
679 return super.equals(obj);
683 public BindingCodec remove(Object key) {
693 public Collection<BindingCodec<?, ?>> values() {
694 return Collections.emptySet();
697 private UnsupportedOperationException notModifiable() {
698 return new UnsupportedOperationException("Not externally modifiable.");
702 public BindingCodec<Map<QName, Object>, Object> put(T key, BindingCodec<?, ?> value) {
703 throw notModifiable();
707 public void putAll(Map<? extends T, ? extends BindingCodec<?, ?>> m) {
708 throw notModifiable();
712 public int hashCode() {
713 return super.hashCode();
717 public boolean isEmpty() {
722 public Set<T> keySet() {
723 return Collections.emptySet();
727 public Set<java.util.Map.Entry<T, BindingCodec<?, ?>>> entrySet() {
728 return Collections.emptySet();
732 public boolean containsValue(Object value) {
737 @SuppressWarnings({ "rawtypes", "unchecked" })
738 private class AugmentableCompositeCodec implements BindingCodec {
740 private final Class augmentableType;
742 Map<Class, BindingCodec> rawAugmentationCodecs = new WeakHashMap<>();
744 public AugmentableCompositeCodec(Class type) {
745 checkArgument(Augmentable.class.isAssignableFrom(type));
746 augmentableType = type;
750 public Object serialize(Object input) {
751 if (input instanceof Augmentable<?>) {
753 Map<Class, Augmentation> augmentations = getAugmentations(input);
754 return serializeImpl(augmentations);
759 private Map<Class, Augmentation> getAugmentations(Object input) {
760 Field augmentationField;
762 augmentationField = input.getClass().getDeclaredField("augmentation");
763 augmentationField.setAccessible(true);
764 Map<Class, Augmentation> augMap = (Map<Class, Augmentation>) augmentationField.get(input);
766 } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
767 LOG.debug("Could not read augmentations for {}", input, e);
769 return Collections.emptyMap();
772 private List serializeImpl(Map<Class, Augmentation> input) {
773 List ret = new ArrayList<>();
774 for (Entry<Class, Augmentation> entry : input.entrySet()) {
775 BindingCodec codec = getRawCodecForAugmentation(entry.getKey());
776 List output = (List) codec.serialize(new ValueWithQName(null, entry.getValue()));
782 private BindingCodec getRawCodecForAugmentation(Class key) {
783 BindingCodec ret = rawAugmentationCodecs.get(key);
788 Class<? extends BindingCodec> retClass = generator.augmentationTransformerFor(key);
789 ret = retClass.newInstance();
790 rawAugmentationCodecs.put(key, ret);
792 } catch (InstantiationException e) {
793 LOG.error("Can not instantiate raw augmentation codec {}", key.getSimpleName(), e);
794 } catch (IllegalAccessException e) {
795 LOG.debug("BUG: Constructor for {} is not accessible.", key.getSimpleName(), e);
801 public Map<Class, Augmentation> deserialize(Object input) {
802 Map<Class, Augmentation> ret = new HashMap<>();
803 if (input instanceof CompositeNode) {
804 for (Entry<Class, BindingCodec> codec : rawAugmentationCodecs.entrySet()) {
805 Augmentation value = (Augmentation) codec.getValue().deserialize(input);
807 ret.put(codec.getKey(), value);
814 public Map<Class, BindingCodec> getRawAugmentationCodecs() {
815 return rawAugmentationCodecs;
818 public void setRawAugmentationCodecs(Map<Class, BindingCodec> rawAugmentationCodecs) {
819 this.rawAugmentationCodecs = rawAugmentationCodecs;
822 public Class getAugmentableType() {
823 return augmentableType;
827 @SuppressWarnings({ "rawtypes", "unchecked" })
828 private static class LateMixinCodec implements BindingCodec, Delegator<BindingCodec> {
830 private BindingCodec delegate;
833 public BindingCodec getDelegate() {
834 if (delegate == null) {
835 throw new IllegalStateException("Codec not initialized yet.");
841 public Object deserialize(Object input) {
842 return getDelegate().deserialize(input);
846 public Object serialize(Object input) {
847 return getDelegate().serialize(input);