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 com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Throwables;
13 import java.lang.invoke.MethodHandle;
14 import java.lang.invoke.MethodHandles;
15 import java.lang.invoke.MethodType;
16 import java.lang.reflect.Method;
17 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value.EncapsulatedValueCodec;
18 import org.opendaylight.yangtools.concepts.Codec;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Context for serializing input value of union type and deserializing objects to binding.
27 public class UnionValueOptionContext {
29 private static final Logger LOG = LoggerFactory.getLogger(UnionValueOptionContext.class);
31 private static final MethodType OBJECT_TYPE = MethodType.methodType(Object.class, Object.class);
33 private final Class<?> bindingType;
34 private final Codec<Object, Object> codec;
35 private final MethodHandle getter;
36 private final MethodHandle unionCtor;
39 * Prepare union as binding object and codec for this object, make a direct method handle of getting union
40 * type and constructor of union type for initializing it.
43 * - union as binding object
45 * - returned type of union
47 * - method for getting union type
49 * - codec for serialize/deserialize type of union
51 public UnionValueOptionContext(final Class<?> unionType, final Class<?> valueType, final Method getter,
52 final Codec<Object, Object> codec) {
53 this.bindingType = Preconditions.checkNotNull(valueType);
54 this.codec = Preconditions.checkNotNull(codec);
57 this.getter = MethodHandles.publicLookup().unreflect(getter).asType(OBJECT_TYPE);
58 } catch (final IllegalAccessException e) {
59 throw new IllegalStateException("Failed to access method " + getter, e);
63 this.unionCtor = MethodHandles.publicLookup()
64 .findConstructor(unionType, MethodType.methodType(void.class, valueType)).asType(OBJECT_TYPE);
65 } catch (IllegalAccessException | NoSuchMethodException e) {
66 throw new IllegalStateException(
67 String.format("Failed to access constructor for %s in type %s", valueType, unionType), e);
72 * Serialize input based on prepared codec.
75 * - object to serialize
76 * @return serialized objetc
78 public Object serialize(final Object input) {
79 final Object baValue = getValueFrom(input);
80 return baValue == null ? null : codec.serialize(baValue);
84 * Deserialize input object via prepared codec for invoking new object of union as binding.
87 * - input object for deserializing
88 * @return deserialized union binding type object
90 public Object deserializeUnion(final Object input) {
91 // Side-step potential exceptions by checking the type if it is available
92 if (codec instanceof EncapsulatedValueCodec && !((EncapsulatedValueCodec) codec).canAcceptObject(input)) {
98 value = codec.deserialize(input);
99 } catch (final Exception e) {
100 LOG.debug("Codec {} failed to deserialize input {}", codec, input, e);
105 return unionCtor.invokeExact(value);
106 } catch (final ClassCastException e) {
107 // This case can happen. e.g. NOOP_CODEC
108 LOG.debug("Failed to instantiate {} for input {} value {}", bindingType, input, value, e);
110 } catch (final Throwable e) {
111 throw new IllegalArgumentException("Failed to construct union for value " + value, e);
115 private Object getValueFrom(final Object input) {
117 return getter.invokeExact(input);
118 } catch (final Throwable e) {
119 throw Throwables.propagate(e);
124 public int hashCode() {
125 return bindingType.hashCode();
129 public boolean equals(final Object obj) {
133 if (!(obj instanceof UnionValueOptionContext)) {
137 final UnionValueOptionContext other = (UnionValueOptionContext) obj;
138 return bindingType.equals(other.bindingType);