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
8 package org.opendaylight.mdsal.binding.dom.adapter;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Throwables;
12 import java.lang.invoke.MethodHandle;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.MethodType;
15 import java.lang.reflect.Method;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
20 import org.opendaylight.yangtools.yang.binding.contract.Naming;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 abstract sealed class ContextReferenceExtractor {
26 static final class Direct extends ContextReferenceExtractor {
27 private final MethodHandle handle;
29 private Direct(final MethodHandle rawHandle) {
30 handle = rawHandle.asType(MethodType.methodType(InstanceIdentifier.class, DataObject.class));
34 static ContextReferenceExtractor create(final Method getterMethod) throws IllegalAccessException {
35 return new Direct(MethodHandles.publicLookup().unreflect(getterMethod));
39 InstanceIdentifier<?> extractImpl(final DataObject obj) throws Throwable {
40 return (InstanceIdentifier<?>) handle.invokeExact(obj);
45 static final class GetValue extends ContextReferenceExtractor {
46 private final MethodHandle contextHandle;
47 private final MethodHandle valueHandle;
49 private GetValue(final MethodHandle rawContextHandle, final MethodHandle rawValueHandle) {
50 contextHandle = rawContextHandle.asType(MethodType.methodType(Object.class, DataObject.class));
51 valueHandle = rawValueHandle.asType(MethodType.methodType(InstanceIdentifier.class, Object.class));
54 private static ContextReferenceExtractor create(final Method contextGetter, final Method getValueMethod)
55 throws IllegalAccessException {
56 final var lookup = MethodHandles.publicLookup();
57 return new GetValue(lookup.unreflect(contextGetter), lookup.unreflect(getValueMethod));
61 InstanceIdentifier<?> extractImpl(final DataObject obj) throws Throwable {
62 final var ctx = contextHandle.invokeExact(obj);
63 return ctx == null ? null : (InstanceIdentifier<?>) valueHandle.invokeExact(ctx);
67 private static final Logger LOG = LoggerFactory.getLogger(ContextReferenceExtractor.class);
69 static @Nullable ContextReferenceExtractor of(final Class<?> type) {
70 final var contextGetter = getContextGetter(type);
71 if (contextGetter == null) {
75 final var returnType = contextGetter.getReturnType();
77 if (InstanceIdentifier.class.isAssignableFrom(returnType)) {
78 return Direct.create(contextGetter);
80 final var getValueMethod = findGetValueMethod(returnType, InstanceIdentifier.class);
81 if (getValueMethod != null) {
82 return GetValue.create(contextGetter, getValueMethod);
84 LOG.warn("Class {} can not be used to determine context, falling back to NULL_EXTRACTOR.", returnType);
86 } catch (final IllegalAccessException e) {
87 LOG.warn("Class {} does not conform to Binding Specification v1. Falling back to NULL_EXTRACTOR",
94 * Extract context-reference (Instance Identifier) from a Binding DataObject.
96 * @param obj DataObject from which context reference should be extracted.
98 * @return Instance Identifier representing context reference or null, if data object does not contain a context
101 @SuppressWarnings("checkstyle:IllegalCatch")
102 final @Nullable InstanceIdentifier<?> extract(final DataObject obj) {
104 return extractImpl(obj);
105 } catch (Throwable e) {
106 Throwables.throwIfUnchecked(e);
107 throw new IllegalStateException(e);
111 @SuppressWarnings("checkstyle:IllegalThrows")
112 abstract @Nullable InstanceIdentifier<?> extractImpl(DataObject obj) throws Throwable;
114 private static @Nullable Method findGetValueMethod(final Class<?> type, final Class<?> returnType) {
117 method = type.getMethod(Naming.SCALAR_TYPE_OBJECT_GET_VALUE_NAME);
118 } catch (NoSuchMethodException e) {
119 LOG.warn("Value class {} does not comform to Binding Specification v1.", type, e);
123 if (returnType.equals(method.getReturnType())) {
129 private static Method getContextGetter(final Class<?> type) {
130 for (var method : type.getMethods()) {
131 if (method.getAnnotation(RoutingContext.class) != null) {