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;
23 * Derived YANG types are just immutable value holders for simple value
24 * types, which are same as in NormalizedNode model.
26 final class EncapsulatedValueCodec extends ReflectionBasedCodec implements SchemaUnawareCodec {
27 private static final Lookup LOOKUP = MethodHandles.publicLookup();
28 private static final MethodType OBJ_METHOD = MethodType.methodType(Object.class, Object.class);
29 private final MethodHandle constructor;
30 private final MethodHandle getter;
31 private final Class<?> valueType;
33 private EncapsulatedValueCodec(final Class<?> typeClz, final MethodHandle constructor, final MethodHandle getter,
34 final Class<?> valueType) {
36 this.constructor = Preconditions.checkNotNull(constructor);
37 this.getter = Preconditions.checkNotNull(getter);
38 this.valueType = Preconditions.checkNotNull(valueType);
41 static Callable<EncapsulatedValueCodec> loader(final Class<?> typeClz, TypeDefinition<?> typeDef) {
44 if (typeDef instanceof BooleanTypeDefinition) {
45 m = typeClz.getMethod("isValue");
47 m = typeClz.getMethod("getValue");
49 final MethodHandle getter = LOOKUP.unreflect(m).asType(OBJ_METHOD);
50 final Class<?> valueType = m.getReturnType();
52 final MethodHandle constructor = LOOKUP.findConstructor(typeClz,
53 MethodType.methodType(void.class, valueType)).asType(OBJ_METHOD);
54 return new EncapsulatedValueCodec(typeClz, constructor, getter, valueType);
59 * Quick check if a value object has a chance to deserialize using {@link #deserialize(Object)}.
61 * @param value Value to be checked
62 * @return True if the value can be encapsulated
64 boolean canAcceptObject(final Object value) {
65 return valueType.isInstance(value);
69 @SuppressWarnings("checkstyle:illegalCatch")
70 public Object deserialize(final Object input) {
72 return constructor.invokeExact(input);
73 } catch (Throwable e) {
74 Throwables.throwIfUnchecked(e);
75 throw new RuntimeException(e);
80 @SuppressWarnings("checkstyle:illegalCatch")
81 public Object serialize(final Object input) {
83 return getter.invokeExact(input);
84 } catch (Throwable e) {
85 Throwables.throwIfUnchecked(e);
86 throw new RuntimeException(e);