Use Method.getParameterCount()
[mdsal.git] / binding / mdsal-binding-spec-util / src / main / java / org / opendaylight / mdsal / binding / spec / reflect / BindingReflections.java
index 764d5a1b1e77060267c02afc3918b4fb26b85b1e..0da11d9945ee1a20c8528f765321f964848193ca 100644 (file)
@@ -10,6 +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.annotations.Beta;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
 import com.google.common.cache.LoadingCache;
@@ -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;
     }
 
     /**
@@ -283,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())) {
@@ -378,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());
             }
@@ -394,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);
             }
@@ -407,22 +413,28 @@ 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) {
+    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.empty();