2 * Copyright (c) 2013 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.mdsal.binding.dom.adapter.invoke;
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.ListenableFuture;
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 org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
17 import org.opendaylight.yangtools.yang.binding.DataContainer;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.RpcService;
20 import org.opendaylight.yangtools.yang.common.RpcResult;
22 abstract class RpcMethodInvoker {
24 private static final Lookup LOOKUP = MethodHandles.publicLookup();
26 abstract ListenableFuture<RpcResult<?>> invokeOn(RpcService impl, DataObject input);
28 protected static RpcMethodInvoker from(final Method method) {
29 final MethodHandle methodHandle;
31 methodHandle = LOOKUP.unreflect(method);
32 } catch (IllegalAccessException e) {
33 throw new IllegalStateException("Lookup on public method failed.",e);
36 final Optional<Class<? extends DataContainer>> input = BindingReflections.resolveRpcInputClass(method);
37 if (input.isPresent()) {
38 return new RpcMethodInvokerWithInput(methodHandle);
40 return new RpcMethodInvokerWithoutInput(methodHandle);