X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-dom-codec%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fbinding%2Fdom%2Fcodec%2Fimpl%2FEncapsulatedValueCodec.java;h=3fce3a705273c93e5d4a90e2c5ba9c9d8a2542bc;hb=c225c8c12a35e89168c18d99e48226c141cb8cbc;hp=d52930ec451a0bb5399c3dc3e72623821d813d37;hpb=afcffc25b637b243daa0e7aad6a80048cfdeb21d;p=mdsal.git diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/EncapsulatedValueCodec.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/EncapsulatedValueCodec.java index d52930ec45..3fce3a7052 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/EncapsulatedValueCodec.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/EncapsulatedValueCodec.java @@ -7,45 +7,66 @@ */ package org.opendaylight.mdsal.binding.dom.codec.impl; -import com.google.common.base.Preconditions; +import static java.util.Objects.requireNonNull; + import com.google.common.base.Throwables; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles.Lookup; import java.lang.invoke.MethodType; import java.lang.reflect.Method; -import java.util.concurrent.Callable; -import org.opendaylight.mdsal.binding.dom.codec.impl.ValueTypeCodec.SchemaUnawareCodec; +import java.util.concurrent.ExecutionException; +import org.eclipse.jdt.annotation.NonNull; +import org.opendaylight.mdsal.binding.spec.naming.BindingMapping; /** * Derived YANG types are just immutable value holders for simple value * types, which are same as in NormalizedNode model. */ -final class EncapsulatedValueCodec extends ReflectionBasedCodec implements SchemaUnawareCodec { - private static final Lookup LOOKUP = MethodHandles.publicLookup(); +final class EncapsulatedValueCodec extends SchemaUnawareCodec { private static final MethodType OBJ_METHOD = MethodType.methodType(Object.class, Object.class); + + /* + * Use identity comparison for keys and allow classes to be GCd themselves. + * + * Since codecs can (and typically do) hold a direct or indirect strong reference to the class, they need to be also + * accessed via reference. Using a weak reference could be problematic, because the codec would quite often be only + * weakly reachable. We therefore use a soft reference, whose implementation guidance is suitable to our use case: + * + * "Virtual machine implementations are, however, encouraged to bias against clearing recently-created or + * recently-used soft references." + */ + private static final LoadingCache, EncapsulatedValueCodec> CACHE = CacheBuilder.newBuilder().weakKeys() + .softValues().build(new CacheLoader<>() { + @Override + public EncapsulatedValueCodec load(final Class key) throws ReflectiveOperationException { + final Method m = key.getMethod(BindingMapping.SCALAR_TYPE_OBJECT_GET_VALUE_NAME); + final Lookup lookup = MethodHandles.publicLookup(); + final MethodHandle getter = lookup.unreflect(m).asType(OBJ_METHOD); + final Class valueType = m.getReturnType(); + final MethodHandle constructor = lookup.findConstructor(key, + MethodType.methodType(void.class, valueType)).asType(OBJ_METHOD); + return new EncapsulatedValueCodec(constructor, getter, valueType); + } + }); + private final MethodHandle constructor; private final MethodHandle getter; private final Class valueType; - private EncapsulatedValueCodec(final Class typeClz, final MethodHandle constructor, final MethodHandle getter, + private EncapsulatedValueCodec(final MethodHandle constructor, final MethodHandle getter, final Class valueType) { - super(typeClz); - this.constructor = Preconditions.checkNotNull(constructor); - this.getter = Preconditions.checkNotNull(getter); - this.valueType = Preconditions.checkNotNull(valueType); + this.constructor = requireNonNull(constructor); + this.getter = requireNonNull(getter); + this.valueType = requireNonNull(valueType); } - static Callable loader(final Class typeClz) { - return () -> { - final Method m = typeClz.getMethod("getValue"); - final MethodHandle getter = LOOKUP.unreflect(m).asType(OBJ_METHOD); - final Class valueType = m.getReturnType(); - - final MethodHandle constructor = LOOKUP.findConstructor(typeClz, - MethodType.methodType(void.class, valueType)).asType(OBJ_METHOD); - return new EncapsulatedValueCodec(typeClz, constructor, getter, valueType); - }; + static @NonNull EncapsulatedValueCodec of(final Class typeClz) throws ExecutionException { + // FIXME: require base class to be ScalarTypeObject + return CACHE.get(typeClz); } /** @@ -59,22 +80,24 @@ final class EncapsulatedValueCodec extends ReflectionBasedCodec implements Schem } @Override - public Object deserialize(final Object input) { + @SuppressWarnings("checkstyle:illegalCatch") + protected Object deserializeImpl(final Object input) { try { return constructor.invokeExact(input); } catch (Throwable e) { Throwables.throwIfUnchecked(e); - throw new RuntimeException(e); + throw new IllegalStateException(e); } } @Override - public Object serialize(final Object input) { + @SuppressWarnings("checkstyle:illegalCatch") + protected Object serializeImpl(final Object input) { try { return getter.invokeExact(input); } catch (Throwable e) { Throwables.throwIfUnchecked(e); - throw new RuntimeException(e); + throw new IllegalStateException(e); } } } \ No newline at end of file