Move BindingReflections to mdsal-binding-spec-util
[mdsal.git] / binding / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / util / BindingReflections.java
index 976175546a5280ed4c758387df870d31ba5149ae..d1c3fbdd15c4021ae707d272bf35866650a681d6 100644 (file)
@@ -27,12 +27,12 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.ServiceLoader;
-import java.util.concurrent.Callable;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import javax.annotation.RegEx;
 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;
@@ -50,6 +50,7 @@ import org.opendaylight.yangtools.yang.common.YangConstants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@Deprecated
 public final class BindingReflections {
 
     private static final long EXPIRATION_TIME = 60;
@@ -229,7 +230,8 @@ public final class BindingReflections {
 
     @SuppressWarnings("checkstyle:illegalCatch")
     public static QNameModule getQNameModule(final Class<?> clz) {
-        if (DataContainer.class.isAssignableFrom(clz) || BaseIdentity.class.isAssignableFrom(clz)) {
+        if (DataContainer.class.isAssignableFrom(clz) || BaseIdentity.class.isAssignableFrom(clz)
+                || Action.class.isAssignableFrom(clz)) {
             return findQName(clz).getModule();
         }
         try {
@@ -264,7 +266,7 @@ public final class BindingReflections {
         checkArgument(cls != null);
         String packageName = getModelRootPackageName(cls.getPackage());
         final String potentialClassName = getModuleInfoClassName(packageName);
-        return ClassLoaderUtils.withClassLoader(cls.getClassLoader(), (Callable<YangModuleInfo>) () -> {
+        return ClassLoaderUtils.callWithClassLoader(cls.getClassLoader(), () -> {
             Class<?> moduleInfoClass = Thread.currentThread().getContextClassLoader().loadClass(potentialClassName);
             return (YangModuleInfo) moduleInfoClass.getMethod("getInstance").invoke(null);
         });
@@ -408,7 +410,8 @@ public final class BindingReflections {
      */
     public static Map<Class<?>, Method> getChildrenClassToMethod(final Class<?> type) {
         checkArgument(type != null, "Target type must not be null");
-        checkArgument(DataContainer.class.isAssignableFrom(type), "Supplied type must be derived from DataContainer");
+        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);
@@ -431,15 +434,14 @@ public final class BindingReflections {
             return Optional.of(returnType);
         } else if (List.class.isAssignableFrom(returnType)) {
             try {
-                return ClassLoaderUtils.withClassLoader(method.getDeclaringClass().getClassLoader(),
-                    (Callable<Optional<Class<? extends DataContainer>>>) () -> {
-                        Type listResult = ClassLoaderUtils.getFirstGenericParameter(method.getGenericReturnType());
-                        if (listResult instanceof Class
-                                && DataContainer.class.isAssignableFrom((Class) listResult)) {
-                            return Optional.of((Class) listResult);
-                        }
-                        return Optional.absent();
-                    });
+                return ClassLoaderUtils.callWithClassLoader(method.getDeclaringClass().getClassLoader(), () -> {
+                    Type listResult = ClassLoaderUtils.getFirstGenericParameter(method.getGenericReturnType());
+                    if (listResult instanceof Class
+                            && DataContainer.class.isAssignableFrom((Class) listResult)) {
+                        return Optional.of((Class) listResult);
+                    }
+                    return Optional.absent();
+                });
             } catch (Exception e) {
                 /*
                  * It is safe to log this this exception on debug, since this
@@ -466,21 +468,23 @@ public final class BindingReflections {
          */
         private static Optional<QName> resolveQNameNoCache(final Class<?> key) {
             try {
-                Field field = key.getField(BindingMapping.QNAME_STATIC_FIELD_NAME);
-                Object obj = field.get(null);
+                final Field field;
+                try {
+                    field = key.getField(BindingMapping.QNAME_STATIC_FIELD_NAME);
+                } catch (NoSuchFieldException e) {
+                    LOG.debug("{} does not have a {} field, falling back to computation", key,
+                        BindingMapping.QNAME_STATIC_FIELD_NAME, e);
+                    return Optional.of(computeQName(key));
+                }
+
+                final Object obj = field.get(null);
                 if (obj instanceof QName) {
                     return Optional.of((QName) obj);
                 }
-
-            } catch (NoSuchFieldException e) {
-                return Optional.of(computeQName(key));
-
             } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
                 /*
-                 *
-                 * It is safe to log this this exception on debug, since this method
-                 * should not fail. Only failures are possible if the runtime /
-                 * backing.
+                 * It is safe to log this this exception on debug, since this method should not fail. Only failures are
+                 * possible if the runtime / backing is inconsistent.
                  */
                 LOG.debug("Unexpected exception during extracting QName for {}", key, e);
             }
@@ -588,7 +592,7 @@ public final class BindingReflections {
      * constructors, etc).
      *
      * @param potential
-     *            Class which is potential substition
+     *            Class which is potential substitution
      * @param target
      *            Class which should be used at particular subtree
      * @return true if and only if classes represents same data.
@@ -612,8 +616,7 @@ public final class BindingReflections {
                     return false;
                 }
             } catch (NoSuchMethodException e) {
-                // Counterpart method is missing, so classes could not be
-                // substituted.
+                // Counterpart method is missing, so classes could not be substituted.
                 return false;
             } catch (SecurityException e) {
                 throw new IllegalStateException("Could not compare methods", e);