2 * Copyright (c) 2015 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
9 package org.opendaylight.controller.md.sal.binding.impl;
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.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 final class GetValueRouteContextExtractor extends ContextReferenceExtractor {
22 private final static Lookup PUBLIC_LOOKUP = MethodHandles.publicLookup();
23 private final MethodHandle contextHandle;
24 private final MethodHandle valueHandle;
26 private GetValueRouteContextExtractor(final MethodHandle rawContextHandle, final MethodHandle rawValueHandle) {
27 contextHandle = rawContextHandle.asType(MethodType.methodType(Object.class, DataObject.class));
28 valueHandle = rawValueHandle.asType(MethodType.methodType(InstanceIdentifier.class, Object.class));
31 public static ContextReferenceExtractor create(final Method contextGetter, final Method getValueMethod)
32 throws IllegalAccessException {
33 final MethodHandle rawContextHandle = PUBLIC_LOOKUP.unreflect(contextGetter);
34 final MethodHandle rawValueHandle = PUBLIC_LOOKUP.unreflect(getValueMethod);
35 return new GetValueRouteContextExtractor(rawContextHandle, rawValueHandle);
39 InstanceIdentifier<?> extract(final DataObject obj) {
41 final Object ctx = contextHandle.invokeExact(obj);
43 return (InstanceIdentifier<?>) valueHandle.invokeExact(ctx);
46 } catch (final Throwable e) {
47 throw Throwables.propagate(e);