+ @Override
+ public void leafNode(final String localName, final Object value) throws IOException {
+ if (value instanceof TypeObject) {
+ // TypeObject is a tagging interface used for generated classes which wrap derived and restricted types.
+ // They are immutable and hence we can safely wrap them in LeafNodes and reuse them, if directed to do so.
+ final TypeObject typed = (TypeObject) value;
+ final Class<? extends TypeObject> type = typed.getClass();
+ if (cacheHolder.isCached(type)) {
+ final ValueNodeCodecContext context = ((DataObjectCodecContext<?, ?>) delegate.current())
+ .getLeafChild(localName);
+ if (context instanceof OfTypeObject) {
+ final AbstractBindingNormalizedNodeCache<TypeObject, ?> cache = cacheHolder.getCachingSerializer(
+ (OfTypeObject<?>)context);
+ if (cache != null) {
+ // We have a cache hit and are thus done
+ domWriter.addChild(cache.get(typed));
+ return;
+ }
+
+ LOG.debug("Unexpected failure to acquire cache for context {}, skipping caching", context);
+ } else {
+ LOG.debug("Context {} does not match expected TypeObject {}, skipping caching", context, typed);
+ }
+ }
+ }
+ super.leafNode(localName, value);
+ }
+