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.yangtools.binding.data.codec.impl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Throwables;
12 import java.lang.invoke.MethodHandle;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.MethodType;
15 import java.lang.reflect.Method;
16 import org.opendaylight.yangtools.concepts.Codec;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 final class UnionValueOptionContext {
21 private static final MethodType OBJECT_TYPE = MethodType.methodType(Object.class, Object.class);
22 private static final Logger LOG = LoggerFactory.getLogger(UnionValueOptionContext.class);
24 private final Class<?> bindingType;
25 private final Codec<Object,Object> codec;
26 private final MethodHandle getter;
27 private final MethodHandle unionCtor;
29 UnionValueOptionContext(final Class<?> unionType, final Class<?> valueType, final Method getter, final Codec<Object, Object> codec) {
30 this.bindingType = Preconditions.checkNotNull(valueType);
31 this.codec = Preconditions.checkNotNull(codec);
34 this.getter = MethodHandles.publicLookup().unreflect(getter).asType(OBJECT_TYPE);
35 } catch (IllegalAccessException e) {
36 throw new IllegalStateException("Failed to access method " + getter, e);
40 this.unionCtor = MethodHandles.publicLookup().findConstructor(unionType,
41 MethodType.methodType(void.class, valueType)).asType(OBJECT_TYPE);
42 } catch (IllegalAccessException | NoSuchMethodException e) {
43 throw new IllegalStateException(String.format("Failed to access constructor for %s in type %s", valueType,
48 Object serialize(final Object input) {
49 final Object baValue = getValueFrom(input);
50 return baValue == null ? null : codec.serialize(baValue);
53 Object deserializeUnion(final Object input) {
54 // Side-step potential exceptions by checking the type if it is available
55 if (codec instanceof EncapsulatedValueCodec && !((EncapsulatedValueCodec) codec).canAcceptObject(input)) {
61 value = codec.deserialize(input);
62 } catch (Exception e) {
63 LOG.debug("Codec {} failed to deserialize input {}", codec, input, e);
68 return unionCtor.invokeExact(value);
69 } catch (ClassCastException e) {
70 // This case can happen. e.g. NOOP_CODEC
71 LOG.debug("Failed to instantiate {} for input {} value {}", bindingType, input, value, e);
73 } catch (Throwable e) {
74 throw new IllegalArgumentException("Failed to construct union for value " + value, e);
78 Object getValueFrom(final Object input) {
80 return getter.invokeExact(input);
81 } catch (Throwable e) {
82 throw Throwables.propagate(e);
87 public int hashCode() {
88 return bindingType.hashCode();
92 public boolean equals(final Object obj) {
96 if (!(obj instanceof UnionValueOptionContext)) {
100 final UnionValueOptionContext other = (UnionValueOptionContext) obj;
101 return bindingType.equals(other.bindingType);