Move BindingReflections to mdsal-binding-spec-util
[mdsal.git] / binding / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / util / BindingReflections.java
index 160077cb92cdf83bd82bad65bde42ee829bccaa3..d1c3fbdd15c4021ae707d272bf35866650a681d6 100644 (file)
@@ -32,6 +32,7 @@ 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;
@@ -49,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;
@@ -228,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 {
@@ -407,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);
@@ -464,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);
             }
@@ -610,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);