BUG-6028: check value types for encapsulation
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / yangtools / binding / data / codec / impl / UnionValueOptionContext.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.binding.data.codec.impl;
9
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;
19
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);
23
24     private final Class<?> bindingType;
25     private final Codec<Object,Object> codec;
26     private final MethodHandle getter;
27     private final MethodHandle unionCtor;
28
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);
32
33         try {
34             this.getter = MethodHandles.publicLookup().unreflect(getter).asType(OBJECT_TYPE);
35         } catch (IllegalAccessException e) {
36             throw new IllegalStateException("Failed to access method " + getter, e);
37         }
38
39         try {
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,
44                     unionType), e);
45         }
46     }
47
48     Object serialize(final Object input) {
49         final Object baValue = getValueFrom(input);
50         return baValue == null ? null : codec.serialize(baValue);
51     }
52
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)) {
56             return null;
57         }
58
59         final Object value;
60         try {
61             value = codec.deserialize(input);
62         } catch (Exception e) {
63             LOG.debug("Codec {} failed to deserialize input {}", codec, input, e);
64             return null;
65         }
66
67         try {
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);
72             return null;
73         } catch (Throwable e) {
74             throw new IllegalArgumentException("Failed to construct union for value " + value, e);
75         }
76     }
77
78     Object getValueFrom(final Object input) {
79         try {
80             return getter.invokeExact(input);
81         } catch (Throwable e) {
82             throw Throwables.propagate(e);
83         }
84     }
85
86     @Override
87     public int hashCode() {
88         return bindingType.hashCode();
89     }
90
91     @Override
92     public boolean equals(final Object obj) {
93         if (this == obj) {
94             return true;
95         }
96         if (!(obj instanceof UnionValueOptionContext)) {
97             return false;
98         }
99
100         final UnionValueOptionContext other = (UnionValueOptionContext) obj;
101         return bindingType.equals(other.bindingType);
102     }
103 }