Bump to odlparent-9.0.0/yangtools-7.0.1-SNAPSHOT
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / ContextReferenceExtractor.java
index 1cfe005e7a02a991ce53f06f3b334f99fe8cd12e..31711cd7be0abb00918a680b9771d92fb4593639 100644 (file)
@@ -5,22 +5,21 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.mdsal.binding.dom.adapter;
 
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.lang.reflect.Method;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-
 abstract class ContextReferenceExtractor {
 
     private static final Logger LOG = LoggerFactory.getLogger(ContextReferenceExtractor.class);
@@ -32,7 +31,6 @@ abstract class ContextReferenceExtractor {
         }
     };
 
-
     private static final LoadingCache<Class<?>, ContextReferenceExtractor> EXTRACTORS = CacheBuilder.newBuilder()
             .weakKeys().build(new CacheLoader<Class<?>, ContextReferenceExtractor>() {
 
@@ -40,6 +38,40 @@ abstract class ContextReferenceExtractor {
                 public ContextReferenceExtractor load(final Class<?> key) throws Exception {
                     return create(key);
                 }
+
+                private @NonNull ContextReferenceExtractor create(final Class<?> key) {
+                    final Method contextGetter = getContextGetter(key);
+                    if (contextGetter == null) {
+                        return NULL_EXTRACTOR;
+                    }
+                    final Class<?> returnType = contextGetter.getReturnType();
+                    try {
+                        if (InstanceIdentifier.class.isAssignableFrom(returnType)) {
+                            return DirectGetterRouteContextExtractor.create(contextGetter);
+                        }
+                        final Method getValueMethod = findGetValueMethod(returnType, InstanceIdentifier.class);
+                        if (getValueMethod != null) {
+                            return GetValueRouteContextExtractor.create(contextGetter, getValueMethod);
+                        } else {
+                            LOG.warn("Class {} can not be used to determine context, falling back to NULL_EXTRACTOR.",
+                                    returnType);
+                        }
+                    } catch (final IllegalAccessException e) {
+                        LOG.warn(
+                                "Class {} does not conform to Binding Specification v1. Falling back to NULL_EXTRACTOR",
+                                returnType, e);
+                    }
+                    return NULL_EXTRACTOR;
+                }
+
+                private Method getContextGetter(final Class<?> key) {
+                    for (final Method method : key.getMethods()) {
+                        if (method.getAnnotation(RoutingContext.class) != null) {
+                            return method;
+                        }
+                    }
+                    return null;
+                }
             });
 
 
@@ -50,45 +82,21 @@ abstract class ContextReferenceExtractor {
     }
 
     /**
-     * Extract context-reference (Instance Identifier) from
-     * Binding DataObject.
+     * Extract context-reference (Instance Identifier) from a Binding DataObject.
      *
-     * @param obj DataObject from which context reference
-     * should be extracted.
+     * @param obj DataObject from which context reference should be extracted.
      *
-     * @return Instance Identifier representing context reference
-     * or null, if data object does not contain context reference.
+     * @return Instance Identifier representing context reference or null, if data object does not contain a context
+     *         reference.
      */
     abstract @Nullable InstanceIdentifier<?> extract(DataObject obj);
 
-    @Nonnull
-    private static ContextReferenceExtractor create(final Class<?> key) {
-        final Method contextGetter = getContextGetter(key);
-        if (contextGetter == null) {
-            return NULL_EXTRACTOR;
-        }
-        final Class<?> returnType = contextGetter.getReturnType();
-        try {
-            if (InstanceIdentifier.class.isAssignableFrom(returnType)) {
-                return DirectGetterRouteContextExtractor.create(contextGetter);
-            }
-            final Method getValueMethod = findGetValueMethod(returnType,InstanceIdentifier.class);
-            if (getValueMethod != null) {
-                return GetValueRouteContextExtractor.create(contextGetter, getValueMethod);
-            } else {
-                LOG.warn("Class {} can not be used to determine context, falling back to NULL_EXTRACTOR.",returnType);
-            }
-        } catch (final IllegalAccessException e) {
-            LOG.warn("Class {} does not conform to Binding Specification v1. Falling back to NULL_EXTRACTOR", e);
-        }
-        return NULL_EXTRACTOR;
-    }
-
-    @Nullable
-    private static Method findGetValueMethod(final Class<?> type, final Class<?> returnType) {
+    @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
+            justification = "https://github.com/spotbugs/spotbugs/issues/811")
+    private static @Nullable Method findGetValueMethod(final Class<?> type, final Class<?> returnType) {
         try {
             final Method method = type.getMethod(GET_VALUE_NAME);
-            if(returnType.equals(method.getReturnType())) {
+            if (returnType.equals(method.getReturnType())) {
                 return method;
             }
         } catch (final NoSuchMethodException e) {
@@ -96,16 +104,4 @@ abstract class ContextReferenceExtractor {
         }
         return null;
     }
-
-    private static Method getContextGetter(final Class<?> key) {
-        for (final Method method : key.getMethods()) {
-            if (method.getAnnotation(RoutingContext.class) != null) {
-                return method;
-            }
-        }
-        return null;
-    }
-
-
-
 }