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;
21 final class GetValueRouteContextExtractor extends ContextReferenceExtractor {
23 private static final Lookup PUBLIC_LOOKUP = MethodHandles.publicLookup();
24 private final MethodHandle contextHandle;
25 private final MethodHandle valueHandle;
27 private GetValueRouteContextExtractor(final MethodHandle rawContextHandle, final MethodHandle rawValueHandle) {
28 contextHandle = rawContextHandle.asType(MethodType.methodType(Object.class, DataObject.class));
29 valueHandle = rawValueHandle.asType(MethodType.methodType(InstanceIdentifier.class, Object.class));
32 public static ContextReferenceExtractor create(final Method contextGetter, final Method getValueMethod)
33 throws IllegalAccessException {
34 final MethodHandle rawContextHandle = PUBLIC_LOOKUP.unreflect(contextGetter);
35 final MethodHandle rawValueHandle = PUBLIC_LOOKUP.unreflect(getValueMethod);
36 return new GetValueRouteContextExtractor(rawContextHandle, rawValueHandle);
40 @SuppressWarnings("checkstyle:IllegalCatch")
41 InstanceIdentifier<?> extract(final DataObject obj) {
43 final Object ctx = contextHandle.invokeExact(obj);
45 return (InstanceIdentifier<?>) valueHandle.invokeExact(ctx);
48 } catch (Throwable e) {
49 throw Throwables.propagate(e);