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 @SuppressWarnings("checkstyle:illegalCatch")
91 public Object deserializeUnion(final Object input) {
92 // Side-step potential exceptions by checking the type if it is available
93 if (codec instanceof EncapsulatedValueCodec && !((EncapsulatedValueCodec) codec).canAcceptObject(input)) {
99 value = codec.deserialize(input);
100 } catch (final Exception e) {
101 LOG.debug("Codec {} failed to deserialize input {}", codec, input, e);
106 return unionCtor.invokeExact(value);
107 } catch (final ClassCastException e) {
108 // This case can happen. e.g. NOOP_CODEC
109 LOG.debug("Failed to instantiate {} for input {} value {}", bindingType, input, value, e);
111 } catch (final Throwable e) {
112 throw new IllegalArgumentException("Failed to construct union for value " + value, e);
116 @SuppressWarnings("checkstyle:illegalCatch")
117 private Object getValueFrom(final Object input) {
119 return getter.invokeExact(input);
120 } catch (final Throwable e) {
121 throw Throwables.propagate(e);
126 public int hashCode() {
127 return bindingType.hashCode();
131 public boolean equals(final Object obj) {
135 if (!(obj instanceof UnionValueOptionContext)) {
139 final UnionValueOptionContext other = (UnionValueOptionContext) obj;
140 return bindingType.equals(other.bindingType);