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 com.google.common.base.Preconditions;
11 import com.google.common.base.Throwables;
12 import java.lang.invoke.MethodHandle;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.MethodHandles.Lookup;
15 import java.lang.invoke.MethodType;
16 import java.lang.reflect.Method;
17 import java.util.concurrent.Callable;
18 import org.opendaylight.mdsal.binding.dom.codec.impl.ValueTypeCodec.SchemaUnawareCodec;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
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 = Preconditions.checkNotNull(constructor);
38 this.getter = Preconditions.checkNotNull(getter);
39 this.valueType = Preconditions.checkNotNull(valueType);
42 static Callable<EncapsulatedValueCodec> loader(final Class<?> typeClz, TypeDefinition<?> typeDef) {
45 if (typeDef instanceof BooleanTypeDefinition || typeDef instanceof EmptyTypeDefinition) {
46 m = typeClz.getMethod("isValue");
48 m = typeClz.getMethod("getValue");
50 final MethodHandle getter = LOOKUP.unreflect(m).asType(OBJ_METHOD);
51 final Class<?> valueType = m.getReturnType();
53 final MethodHandle constructor = LOOKUP.findConstructor(typeClz,
54 MethodType.methodType(void.class, valueType)).asType(OBJ_METHOD);
55 return new EncapsulatedValueCodec(typeClz, constructor, getter, valueType);
60 * Quick check if a value object has a chance to deserialize using {@link #deserialize(Object)}.
62 * @param value Value to be checked
63 * @return True if the value can be encapsulated
65 boolean canAcceptObject(final Object value) {
66 return valueType.isInstance(value);
70 @SuppressWarnings("checkstyle:illegalCatch")
71 public Object deserialize(final Object input) {
73 return constructor.invokeExact(input);
74 } catch (Throwable e) {
75 Throwables.throwIfUnchecked(e);
76 throw new RuntimeException(e);
81 @SuppressWarnings("checkstyle:illegalCatch")
82 public Object serialize(final Object input) {
84 return getter.invokeExact(input);
85 } catch (Throwable e) {
86 Throwables.throwIfUnchecked(e);
87 throw new RuntimeException(e);