2 * Copyright (c) 2016 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.yang.binding.util;
10 import static com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.XmlToken.Optional;
12 import com.google.common.base.Optional;
13 import java.lang.invoke.MethodHandle;
14 import java.lang.invoke.MethodHandles;
15 import java.lang.invoke.MethodHandles.Lookup;
16 import java.lang.reflect.Method;
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;
23 abstract class RpcMethodInvoker {
25 private static final Lookup LOOKUP = MethodHandles.publicLookup();
27 protected abstract Future<RpcResult<?>> invokeOn(RpcService impl, TreeNode input);
29 protected static RpcMethodInvoker from(final Method method) {
30 Optional<Class<? extends InterfaceTyped>> input = BindingReflections.resolveRpcInputClass(method);
32 MethodHandle methodHandle = LOOKUP.unreflect(method);
33 if (input.isPresent()) {
34 return new RpcMethodInvokerWithInput(methodHandle);
36 return new RpcMethodInvokerWithoutInput(methodHandle);
37 } catch (IllegalAccessException e) {
38 throw new IllegalStateException("Lookup on public method failed.",e);