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
8 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Throwables;
14 import java.lang.invoke.MethodHandle;
15 import java.lang.invoke.MethodHandles;
16 import java.lang.invoke.MethodType;
17 import java.lang.reflect.Method;
18 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value.EncapsulatedValueCodec;
19 import org.opendaylight.yangtools.concepts.Codec;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Context for serializing input value of union type and deserializing objects to binding.
28 public class UnionValueOptionContext {
30 private static final Logger LOG = LoggerFactory.getLogger(UnionValueOptionContext.class);
32 private static final MethodType OBJECT_TYPE = MethodType.methodType(Object.class, Object.class);
34 private final Class<?> bindingType;
35 private final Codec<Object, Object> codec;
36 private final MethodHandle getter;
37 private final MethodHandle unionCtor;
40 * Prepare union as binding object and codec for this object, make a direct method handle of getting union
41 * type and constructor of union type for initializing it.
44 * - union as binding object
46 * - returned type of union
48 * - method for getting union type
50 * - codec for serialize/deserialize type of union
52 public UnionValueOptionContext(final Class<?> unionType, final Class<?> valueType, final Method getter,
53 final Codec<Object, Object> codec) {
54 this.bindingType = requireNonNull(valueType);
55 this.codec = requireNonNull(codec);
58 this.getter = MethodHandles.publicLookup().unreflect(getter).asType(OBJECT_TYPE);
59 } catch (final IllegalAccessException e) {
60 throw new IllegalStateException("Failed to access method " + getter, e);
64 this.unionCtor = MethodHandles.publicLookup()
65 .findConstructor(unionType, MethodType.methodType(void.class, valueType)).asType(OBJECT_TYPE);
66 } catch (IllegalAccessException | NoSuchMethodException e) {
67 throw new IllegalStateException(
68 String.format("Failed to access constructor for %s in type %s", valueType, unionType), e);
73 * Serialize input based on prepared codec.
76 * - object to serialize
77 * @return serialized objetc
79 public Object serialize(final Object input) {
80 final Object baValue = getValueFrom(input);
81 return baValue == null ? null : codec.serialize(baValue);
85 * Deserialize input object via prepared codec for invoking new object of union as binding.
88 * - input object for deserializing
89 * @return deserialized union binding type object
91 @SuppressWarnings("checkstyle:illegalCatch")
92 public Object deserializeUnion(final Object input) {
93 // Side-step potential exceptions by checking the type if it is available
94 if (codec instanceof EncapsulatedValueCodec && !((EncapsulatedValueCodec) codec).canAcceptObject(input)) {
100 value = codec.deserialize(input);
101 } catch (final Exception e) {
102 LOG.debug("Codec {} failed to deserialize input {}", codec, input, e);
107 return unionCtor.invokeExact(value);
108 } catch (final ClassCastException e) {
109 // This case can happen. e.g. NOOP_CODEC
110 LOG.debug("Failed to instantiate {} for input {} value {}", bindingType, input, value, e);
112 } catch (final Throwable e) {
113 throw new IllegalArgumentException("Failed to construct union for value " + value, e);
117 @SuppressWarnings("checkstyle:illegalCatch")
118 private Object getValueFrom(final Object input) {
120 return getter.invokeExact(input);
121 } catch (final Throwable e) {
122 throw Throwables.propagate(e);
127 public int hashCode() {
128 return bindingType.hashCode();
132 public boolean equals(final Object obj) {
136 if (!(obj instanceof UnionValueOptionContext)) {
140 final UnionValueOptionContext other = (UnionValueOptionContext) obj;
141 return bindingType.equals(other.bindingType);