Use Method.getParameterCount()
[mdsal.git] / binding / mdsal-binding-spec-util / src / main / java / org / opendaylight / mdsal / binding / spec / reflect / BindingReflections.java
index 90d8ec52dfb7e99f2dc4d99543068e856a9783c4..0da11d9945ee1a20c8528f765321f964848193ca 100644 (file)
@@ -10,7 +10,7 @@ package org.opendaylight.mdsal.binding.spec.reflect;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkState;
 
-import com.google.common.base.Optional;
+import com.google.common.annotations.Beta;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
@@ -26,17 +26,18 @@ import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.ServiceLoader;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import javax.annotation.RegEx;
+import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
 import org.opendaylight.yangtools.util.ClassLoaderUtils;
 import org.opendaylight.yangtools.yang.binding.Action;
 import org.opendaylight.yangtools.yang.binding.Augmentable;
 import org.opendaylight.yangtools.yang.binding.Augmentation;
 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
-import org.opendaylight.yangtools.yang.binding.BindingMapping;
 import org.opendaylight.yangtools.yang.binding.ChildOf;
 import org.opendaylight.yangtools.yang.binding.DataContainer;
 import org.opendaylight.yangtools.yang.binding.DataObject;
@@ -119,7 +120,7 @@ public final class BindingReflections {
      *         have name. May return null if QName is not present.
      */
     public static QName findQName(final Class<?> dataType) {
-        return CLASS_TO_QNAME.getUnchecked(dataType).orNull();
+        return CLASS_TO_QNAME.getUnchecked(dataType).orElse(null);
     }
 
     /**
@@ -136,7 +137,7 @@ public final class BindingReflections {
                 // resolveRpcInputClass() check.While RpcMethodInvoker counts with one argument for
                 // non input type and two arguments for input type, resolveRpcInputClass() counting
                 // with zero for non input and one for input type
-                && possibleMethod.getParameterTypes().length <= 2;
+                && possibleMethod.getParameterCount() <= 2;
     }
 
     /**
@@ -144,7 +145,7 @@ public final class BindingReflections {
      *
      * @param targetMethod
      *            method to scan
-     * @return Optional.absent() if result type could not be get, or return type is Void.
+     * @return Optional.empty() if result type could not be get, or return type is Void.
      */
     @SuppressWarnings("rawtypes")
     public static Optional<Class<?>> resolveRpcOutputClass(final Method targetMethod) {
@@ -155,7 +156,7 @@ public final class BindingReflections {
         if (rpcResultArgument instanceof Class && !Void.class.equals(rpcResultArgument)) {
             return Optional.of((Class) rpcResultArgument);
         }
-        return Optional.absent();
+        return Optional.empty();
     }
 
     /**
@@ -163,7 +164,7 @@ public final class BindingReflections {
      *
      * @param targetMethod
      *            method to scan
-     * @return Optional.absent() if RPC has no input, RPC input type otherwise.
+     * @return Optional.empty() if RPC has no input, RPC input type otherwise.
      */
     @SuppressWarnings("rawtypes")
     public static Optional<Class<? extends DataContainer>> resolveRpcInputClass(final Method targetMethod) {
@@ -172,7 +173,7 @@ public final class BindingReflections {
                 return Optional.of(clazz);
             }
         }
-        return Optional.absent();
+        return Optional.empty();
     }
 
     public static QName getQName(final Class<? extends BaseIdentity> context) {
@@ -240,20 +241,6 @@ public final class BindingReflections {
         }
     }
 
-    /**
-     * Extract a QNameModule from YangModuleInfo.
-     *
-     * @param modInfo Module info
-     * @return QNameModule for the module
-     * @throws NullPointerException in modInfo is null
-     *
-     * @deprecated Use {@code YangModuleInfo.getName().getModule()} instead.
-     */
-    @Deprecated
-    public static QNameModule getQNameModule(final YangModuleInfo modInfo) {
-        return modInfo.getName().getModule();
-    }
-
     /**
      * Returns instance of {@link YangModuleInfo} of declaring model for specific class.
      *
@@ -297,7 +284,7 @@ public final class BindingReflections {
      */
     public static boolean isNotificationCallback(final Method method) {
         checkArgument(method != null);
-        if (method.getName().startsWith("on") && method.getParameterTypes().length == 1) {
+        if (method.getName().startsWith("on") && method.getParameterCount() == 1) {
             Class<?> potentialNotification = method.getParameterTypes()[0];
             if (isNotification(potentialNotification)
                     && method.getName().equals("on" + potentialNotification.getSimpleName())) {
@@ -392,7 +379,8 @@ public final class BindingReflections {
         checkArgument(DataContainer.class.isAssignableFrom(type), "Supplied type must be derived from DataContainer");
         List<Class<? extends DataObject>> ret = new LinkedList<>();
         for (Method method : type.getMethods()) {
-            Optional<Class<? extends DataContainer>> entity = getYangModeledReturnType(method);
+            Optional<Class<? extends DataContainer>> entity = getYangModeledReturnType(method,
+                BindingMapping.GETTER_PREFIX);
             if (entity.isPresent()) {
                 ret.add((Class<? extends DataObject>) entity.get());
             }
@@ -408,12 +396,16 @@ public final class BindingReflections {
      * @return Iterable of all data children, which have YANG modeled entity
      */
     public static Map<Class<?>, Method> getChildrenClassToMethod(final Class<?> type) {
+        return getChildrenClassToMethod(type, BindingMapping.GETTER_PREFIX);
+    }
+
+    private static Map<Class<?>, Method> getChildrenClassToMethod(final Class<?> type, final String prefix) {
         checkArgument(type != null, "Target type must not be null");
         checkArgument(DataContainer.class.isAssignableFrom(type), "Supplied type %s must be derived from DataContainer",
             type);
         Map<Class<?>, Method> ret = new HashMap<>();
         for (Method method : type.getMethods()) {
-            Optional<Class<? extends DataContainer>> entity = getYangModeledReturnType(method);
+            Optional<Class<? extends DataContainer>> entity = getYangModeledReturnType(method, prefix);
             if (entity.isPresent()) {
                 ret.put(entity.get(), method);
             }
@@ -421,25 +413,31 @@ public final class BindingReflections {
         return ret;
     }
 
+    @Beta
+    public static Map<Class<?>, Method> getChildrenClassToNonnullMethod(final Class<?> type) {
+        return getChildrenClassToMethod(type, BindingMapping.NONNULL_PREFIX);
+    }
+
     @SuppressWarnings({ "unchecked", "rawtypes", "checkstyle:illegalCatch" })
-    private static Optional<Class<? extends DataContainer>> getYangModeledReturnType(final Method method) {
-        if ("getClass".equals(method.getName()) || !method.getName().startsWith("get")
-                || method.getParameterTypes().length > 0) {
-            return Optional.absent();
+    private static Optional<Class<? extends DataContainer>> getYangModeledReturnType(final Method method,
+            final String prefix) {
+        final String methodName = method.getName();
+        if ("getClass".equals(methodName) || !methodName.startsWith(prefix) || method.getParameterCount() > 0) {
+            return Optional.empty();
         }
 
         Class returnType = method.getReturnType();
         if (DataContainer.class.isAssignableFrom(returnType)) {
             return Optional.of(returnType);
-        } else if (List.class.isAssignableFrom(returnType)) {
+        }
+        if (List.class.isAssignableFrom(returnType)) {
             try {
                 return ClassLoaderUtils.callWithClassLoader(method.getDeclaringClass().getClassLoader(), () -> {
                     Type listResult = ClassLoaderUtils.getFirstGenericParameter(method.getGenericReturnType());
-                    if (listResult instanceof Class
-                            && DataContainer.class.isAssignableFrom((Class) listResult)) {
+                    if (listResult instanceof Class && DataContainer.class.isAssignableFrom((Class) listResult)) {
                         return Optional.of((Class) listResult);
                     }
-                    return Optional.absent();
+                    return Optional.empty();
                 });
             } catch (Exception e) {
                 /*
@@ -450,7 +448,7 @@ public final class BindingReflections {
                 LOG.debug("Unable to find YANG modeled return type for {}", method, e);
             }
         }
-        return Optional.absent();
+        return Optional.empty();
     }
 
     private static class ClassToQNameLoader extends CacheLoader<Class<?>, Optional<QName>> {
@@ -487,7 +485,7 @@ public final class BindingReflections {
                  */
                 LOG.debug("Unexpected exception during extracting QName for {}", key, e);
             }
-            return Optional.absent();
+            return Optional.empty();
         }
 
         /**
@@ -540,22 +538,6 @@ public final class BindingReflections {
         }
     }
 
-    /**
-     * Given a {@link YangModuleInfo}, create a QName representing it. The QName is formed by reusing the module's
-     * namespace and revision using the module's name as the QName's local name.
-     *
-     * @param moduleInfo
-     *            module information
-     * @return QName representing the module
-     *
-     * @deprecated Use {@link YangModuleInfo#getName()} instead.
-     */
-    @Deprecated
-    public static QName getModuleQName(final YangModuleInfo moduleInfo) {
-        checkArgument(moduleInfo != null, "moduleInfo must not be null.");
-        return moduleInfo.getName();
-    }
-
     /**
      * Extracts augmentation from Binding DTO field using reflection.
      *