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.extractor;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Throwables;
12 import java.lang.invoke.MethodHandle;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.MethodHandles.Lookup;
15 import java.lang.invoke.MethodType;
16 import java.lang.reflect.Method;
17 import org.opendaylight.mdsal.binding.javav2.spec.base.InstanceIdentifier;
18 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
21 final class DirectGetterRouteContextExtractor extends ContextReferenceExtractor {
23 private static final Lookup PUBLIC_LOOKUP = MethodHandles.publicLookup();
25 private final MethodHandle handle;
27 private DirectGetterRouteContextExtractor(final MethodHandle rawHandle) {
28 handle = rawHandle.asType(MethodType.methodType(InstanceIdentifier.class, TreeNode.class));
31 static ContextReferenceExtractor create(final Method getterMethod) throws IllegalAccessException {
32 final MethodHandle getterHandle = PUBLIC_LOOKUP.unreflect(getterMethod);
33 return new DirectGetterRouteContextExtractor(getterHandle);
36 @SuppressWarnings("checkstyle:IllegalCatch")
38 public InstanceIdentifier<? extends TreeNode> extract(final TreeNode obj) {
40 return (InstanceIdentifier<? extends TreeNode>) handle.invokeExact(obj);
41 } catch (final Throwable e) {
42 Throwables.throwIfUnchecked(e);
43 throw new RuntimeException(e);