2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Throwables;
14 import java.lang.invoke.MethodHandle;
15 import java.lang.invoke.MethodHandles;
16 import java.lang.invoke.MethodHandles.Lookup;
17 import java.lang.invoke.MethodType;
18 import java.lang.reflect.Method;
19 import java.util.concurrent.Callable;
20 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value.ValueTypeCodec.SchemaUnawareCodec;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
25 * Derived YANG types are just immutable value holders for simple value
26 * types, which are same as in NormalizedNode model.
29 public final class EncapsulatedValueCodec extends ReflectionBasedCodec implements SchemaUnawareCodec {
31 private static final Lookup LOOKUP = MethodHandles.publicLookup();
32 private static final MethodType OBJ_METHOD = MethodType.methodType(Object.class, Object.class);
33 private final MethodHandle constructor;
34 private final MethodHandle getter;
35 private final Class<?> valueType;
37 private EncapsulatedValueCodec(final Class<?> typeClz, final MethodHandle constructor, final MethodHandle getter,
38 final Class<?> valueType) {
41 this.constructor = Preconditions.checkNotNull(constructor);
42 this.getter = Preconditions.checkNotNull(getter);
43 this.valueType = Preconditions.checkNotNull(valueType);
46 static Callable<EncapsulatedValueCodec> loader(final Class<?> typeClz, TypeDefinition<?> typeDef) {
49 if (typeDef instanceof BooleanTypeDefinition) {
50 m = typeClz.getMethod("isValue");
52 m = typeClz.getMethod("getValue");
54 final MethodHandle getter = LOOKUP.unreflect(m).asType(OBJ_METHOD);
55 final Class<?> valueType = m.getReturnType();
57 final MethodHandle constructor = LOOKUP.findConstructor(typeClz,
58 MethodType.methodType(void.class, valueType)).asType(OBJ_METHOD);
59 return new EncapsulatedValueCodec(typeClz, constructor, getter, valueType);
64 * Quick check if a value object has a chance to deserialize using {@link #deserialize(Object)}.
66 * @param value Value to be checked
67 * @return True if the value can be encapsulated
69 public boolean canAcceptObject(final Object value) {
70 return valueType.isInstance(value);
74 @SuppressWarnings("checkstyle:illegalCatch")
75 public Object deserialize(final Object input) {
77 return constructor.invokeExact(input);
78 } catch (final Throwable e) {
79 throw Throwables.propagate(e);
84 @SuppressWarnings("checkstyle:illegalCatch")
85 public Object serialize(final Object input) {
87 return getter.invokeExact(input);
88 } catch (final Throwable e) {
89 throw Throwables.propagate(e);