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
9 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableSet;
17 import java.lang.invoke.MethodHandle;
18 import java.lang.invoke.MethodHandles;
19 import java.lang.invoke.MethodType;
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.LinkedHashMap;
27 import java.util.Map.Entry;
29 import java.util.TreeSet;
30 import java.util.concurrent.Callable;
31 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.value.ValueTypeCodec.SchemaUnawareCodec;
32 import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifier;
33 import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifierNormalizer;
34 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
38 final class BitsCodec extends ReflectionBasedCodec implements SchemaUnawareCodec {
40 private static final MethodType CONSTRUCTOR_INVOKE_TYPE = MethodType.methodType(Object.class, Boolean[].class);
41 // Ordered by position
42 private final Map<String, Method> getters;
43 // Ordered by lexical name
44 private final Set<String> ctorArgs;
45 private final MethodHandle ctor;
47 private BitsCodec(final Class<?> typeClass, final MethodHandle ctor, final Set<String> ctorArgs,
48 final Map<String, Method> getters) {
51 this.ctor = requireNonNull(ctor);
52 this.ctorArgs = ImmutableSet.copyOf(ctorArgs);
53 this.getters = ImmutableMap.copyOf(getters);
56 static Callable<BitsCodec> loader(final Class<?> returnType, final BitsTypeDefinition rootType) {
58 final Map<String, Method> getters = new LinkedHashMap<>();
59 final Set<String> ctorArgs = new TreeSet<>();
61 for (Bit bit : rootType.getBits()) {
62 final Method valueGetter = returnType.getMethod("is" + JavaIdentifierNormalizer
63 .normalizeSpecificIdentifier(bit.getName(), JavaIdentifier.CLASS));
64 ctorArgs.add(bit.getName());
65 getters.put(bit.getName(), valueGetter);
67 Constructor<?> constructor = null;
68 for (Constructor<?> cst : returnType.getConstructors()) {
69 if (!cst.getParameterTypes()[0].equals(returnType)) {
74 final MethodHandle ctor = MethodHandles.publicLookup().unreflectConstructor(constructor)
75 .asSpreader(Boolean[].class, ctorArgs.size()).asType(CONSTRUCTOR_INVOKE_TYPE);
76 return new BitsCodec(returnType, ctor, ctorArgs, getters);
81 @SuppressWarnings("checkstyle:illegalCatch")
82 public Object deserialize(Object input) {
83 Preconditions.checkArgument(input instanceof Set);
84 @SuppressWarnings("unchecked")
85 final Set<String> casted = (Set<String>) input;
88 * We can do this walk based on field set sorted by name,
89 * since constructor arguments in Java Binding are sorted by name.
91 * This means we will construct correct array for construction
94 final Boolean[] args = new Boolean[ctorArgs.size()];
96 for (String value : ctorArgs) {
97 args[currentArg++] = casted.contains(value);
101 return ctor.invokeExact(args);
102 } catch (Throwable e) {
103 throw new IllegalStateException("Failed to instantiate object for " + input, e);
108 public Object serialize(Object input) {
109 final Collection<String> result = new ArrayList<>(getters.size());
110 for (Entry<String, Method> valueGet : getters.entrySet()) {
113 value = (Boolean) valueGet.getValue().invoke(input);
114 } catch (IllegalAccessException | InvocationTargetException e) {
115 throw new IllegalArgumentException("Failed to get bit " + valueGet.getKey(), e);
119 result.add(valueGet.getKey());
122 return result.size() == getters.size() ? getters.keySet() : ImmutableSet.copyOf(result);