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
8 package org.opendaylight.mdsal.binding.javav2.dom.adapter.impl.operation.invoker;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Throwables;
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.mdsal.binding.javav2.runtime.reflection.BindingReflections;
19 import org.opendaylight.mdsal.binding.javav2.spec.base.Instantiable;
20 import org.opendaylight.mdsal.binding.javav2.spec.base.Operation;
21 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
25 abstract class OperationMethodInvoker {
27 private static final Lookup LOOKUP = MethodHandles.publicLookup();
29 protected abstract <T extends Operation> Future<RpcResult<?>> invokeOn(T impl, TreeNode input);
31 protected static OperationMethodInvoker from(final Method method) {
32 final Optional<Class<? extends Instantiable<?>>> input = BindingReflections.resolveOperationInputClass(method);
34 final MethodHandle methodHandle = LOOKUP.unreflect(method);
35 if (input.isPresent()) {
36 return new OperationMethodInvokerWithInput(methodHandle);
38 return new OperationMethodInvokerWithoutInput(methodHandle);
39 } catch (final IllegalAccessException e) {
40 throw new IllegalStateException("Lookup on public method failed.", e);
44 @SuppressWarnings("checkstyle:IllegalCatch")
45 protected Future<RpcResult<?>> invoking(final MethodHandle handle, final Object... args) {
47 return (Future<RpcResult<?>>) handle.invokeExact(args);
48 } catch (final Throwable e) {
49 throw Throwables.propagate(e);