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.mdsal.binding.dom.adapter;
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 static final 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);
38 @SuppressWarnings("checkstyle:IllegalCatch")
40 InstanceIdentifier<?> extract(final DataObject obj) {
42 final Object ctx = contextHandle.invokeExact(obj);
44 return (InstanceIdentifier<?>) valueHandle.invokeExact(ctx);
47 } catch (final Throwable e) {
48 Throwables.throwIfUnchecked(e);
49 throw new RuntimeException(e);