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.yangtools.yang.model.api.TypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
25 * Derived YANG types are just immutable value holders for simple value
26 * types, which are same as in NormalizedNode model.
28 final class EncapsulatedValueCodec extends ReflectionBasedCodec implements SchemaUnawareCodec {
29 private static final Lookup LOOKUP = MethodHandles.publicLookup();
30 private static final MethodType OBJ_METHOD = MethodType.methodType(Object.class, Object.class);
31 private final MethodHandle constructor;
32 private final MethodHandle getter;
33 private final Class<?> valueType;
35 private EncapsulatedValueCodec(final Class<?> typeClz, final MethodHandle constructor, final MethodHandle getter,
36 final Class<?> valueType) {
38 this.constructor = requireNonNull(constructor);
39 this.getter = requireNonNull(getter);
40 this.valueType = requireNonNull(valueType);
43 static Callable<EncapsulatedValueCodec> loader(final Class<?> typeClz, final TypeDefinition<?> typeDef) {
46 if (typeDef instanceof BooleanTypeDefinition || typeDef instanceof EmptyTypeDefinition) {
47 m = typeClz.getMethod("isValue");
49 m = typeClz.getMethod("getValue");
51 final MethodHandle getter = LOOKUP.unreflect(m).asType(OBJ_METHOD);
52 final Class<?> valueType = m.getReturnType();
54 final MethodHandle constructor = LOOKUP.findConstructor(typeClz,
55 MethodType.methodType(void.class, valueType)).asType(OBJ_METHOD);
56 return new EncapsulatedValueCodec(typeClz, constructor, getter, valueType);
61 * Quick check if a value object has a chance to deserialize using {@link #deserialize(Object)}.
63 * @param value Value to be checked
64 * @return True if the value can be encapsulated
66 boolean canAcceptObject(final Object value) {
67 return valueType.isInstance(value);
71 @SuppressWarnings("checkstyle:illegalCatch")
72 public Object deserialize(final Object input) {
74 return constructor.invokeExact(input);
75 } catch (Throwable e) {
76 Throwables.throwIfUnchecked(e);
77 throw new IllegalStateException(e);
82 @SuppressWarnings("checkstyle:illegalCatch")
83 public Object serialize(final Object input) {
85 return getter.invokeExact(input);
86 } catch (Throwable e) {
87 Throwables.throwIfUnchecked(e);
88 throw new IllegalStateException(e);