6a25542b00da459dee33c08d611fcf01cb813eaf
[mdsal.git] / binding2 / prototype / yang-binding2 / src / main / java / org / opendaylight / yangtools / yang / binding / util / RpcMethodInvoker.java
1 /*
2  * Copyright (c) 2016 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.yang.binding.util;
9
10 import static com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.XmlToken.Optional;
11
12 import java.lang.invoke.MethodHandle;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.MethodHandles.Lookup;
15 import java.lang.reflect.Method;
16 import java.util.Optional;
17 import java.util.concurrent.Future;
18 import org.opendaylight.yangtools.yang.binding.InterfaceTyped;
19 import org.opendaylight.yangtools.yang.binding.TreeNode;
20 import org.opendaylight.yangtools.yang.binding.RpcService;
21 import org.opendaylight.yangtools.yang.common.RpcResult;
22
23 abstract class RpcMethodInvoker {
24
25     private static final Lookup LOOKUP = MethodHandles.publicLookup();
26
27     protected abstract Future<RpcResult<?>> invokeOn(RpcService impl, TreeNode input);
28
29     protected static RpcMethodInvoker from(final Method method) {
30         Optional<Class<? extends InterfaceTyped>> input = BindingReflections.resolveRpcInputClass(method);
31         try {
32             MethodHandle methodHandle = LOOKUP.unreflect(method);
33             if (input.isPresent()) {
34                 return new RpcMethodInvokerWithInput(methodHandle);
35             }
36             return new RpcMethodInvokerWithoutInput(methodHandle);
37         } catch (IllegalAccessException e) {
38             throw new IllegalStateException("Lookup on public method failed.",e);
39         }
40
41     }
42 }