f9c45b9fecf32ac89612176c64a7d482762b978e
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / DirectGetterRouteContextExtractor.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.md.sal.binding.impl;
10
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;
19
20 @Deprecated
21 final class DirectGetterRouteContextExtractor extends ContextReferenceExtractor {
22
23     private static final Lookup PUBLIC_LOOKUP = MethodHandles.publicLookup();
24     private final MethodHandle handle;
25
26     private DirectGetterRouteContextExtractor(final MethodHandle rawHandle) {
27         handle = rawHandle.asType(MethodType.methodType(InstanceIdentifier.class, DataObject.class));
28     }
29
30     static ContextReferenceExtractor create(final Method getterMethod) throws IllegalAccessException {
31         final MethodHandle getterHandle = PUBLIC_LOOKUP.unreflect(getterMethod);
32         return new DirectGetterRouteContextExtractor(getterHandle);
33     }
34
35     @Override
36     @SuppressWarnings("checkstyle:IllegalCatch")
37     InstanceIdentifier<?> extract(final DataObject obj) {
38         try {
39             return (InstanceIdentifier<?>) handle.invokeExact(obj);
40         } catch (Throwable e) {
41             throw Throwables.propagate(e);
42         }
43     }
44 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.