2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the terms of the Eclipse
5 * Public License v1.0 which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.md.sal.binding.impl;
10 import com.google.common.base.Throwables;
11 import java.lang.invoke.MethodHandle;
12 import java.lang.invoke.MethodHandles;
13 import java.lang.invoke.MethodHandles.Lookup;
14 import java.lang.invoke.MethodType;
15 import java.lang.reflect.Method;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 final class DirectGetterRouteContextExtractor extends ContextReferenceExtractor {
21 private final static Lookup PUBLIC_LOOKUP = MethodHandles.publicLookup();
22 private final MethodHandle handle;
24 private DirectGetterRouteContextExtractor(final MethodHandle rawHandle) {
25 handle = rawHandle.asType(MethodType.methodType(InstanceIdentifier.class, DataObject.class));
28 static final ContextReferenceExtractor create(final Method getterMethod) throws IllegalAccessException {
29 final MethodHandle getterHandle = PUBLIC_LOOKUP.unreflect(getterMethod);
30 return new DirectGetterRouteContextExtractor(getterHandle);
34 InstanceIdentifier<?> extract(final DataObject obj) {
36 return (InstanceIdentifier<?>) handle.invokeExact(obj);
37 } catch (final Throwable e) {
38 throw Throwables.propagate(e);