2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Throwables;
13 import java.lang.invoke.MethodHandle;
14 import java.lang.invoke.MethodHandles;
15 import java.lang.invoke.MethodHandles.Lookup;
16 import java.lang.invoke.MethodType;
17 import java.lang.reflect.Method;
18 import java.util.concurrent.Callable;
19 import org.opendaylight.mdsal.binding.dom.codec.impl.ValueTypeCodec.SchemaUnawareCodec;
20 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
24 * Derived YANG types are just immutable value holders for simple value
25 * types, which are same as in NormalizedNode model.
27 final class EncapsulatedValueCodec extends ReflectionBasedCodec implements SchemaUnawareCodec {
28 private static final Lookup LOOKUP = MethodHandles.publicLookup();
29 private static final MethodType OBJ_METHOD = MethodType.methodType(Object.class, Object.class);
30 private final MethodHandle constructor;
31 private final MethodHandle getter;
32 private final Class<?> valueType;
34 private EncapsulatedValueCodec(final Class<?> typeClz, final MethodHandle constructor, final MethodHandle getter,
35 final Class<?> valueType) {
37 this.constructor = requireNonNull(constructor);
38 this.getter = requireNonNull(getter);
39 this.valueType = requireNonNull(valueType);
42 static Callable<EncapsulatedValueCodec> loader(final Class<?> typeClz, final TypeDefinition<?> typeDef) {
44 final Method m = typeClz.getMethod(BindingMapping.SCALAR_TYPE_OBJECT_GET_VALUE_NAME);
45 final MethodHandle getter = LOOKUP.unreflect(m).asType(OBJ_METHOD);
46 final Class<?> valueType = m.getReturnType();
47 final MethodHandle constructor = LOOKUP.findConstructor(typeClz,
48 MethodType.methodType(void.class, valueType)).asType(OBJ_METHOD);
49 return new EncapsulatedValueCodec(typeClz, constructor, getter, valueType);
54 * Quick check if a value object has a chance to deserialize using {@link #deserialize(Object)}.
56 * @param value Value to be checked
57 * @return True if the value can be encapsulated
59 boolean canAcceptObject(final Object value) {
60 return valueType.isInstance(value);
64 @SuppressWarnings("checkstyle:illegalCatch")
65 public Object deserialize(final Object input) {
67 return constructor.invokeExact(input);
68 } catch (Throwable e) {
69 Throwables.throwIfUnchecked(e);
70 throw new IllegalStateException(e);
75 @SuppressWarnings("checkstyle:illegalCatch")
76 public Object serialize(final Object input) {
78 return getter.invokeExact(input);
79 } catch (Throwable e) {
80 Throwables.throwIfUnchecked(e);
81 throw new IllegalStateException(e);