- /**
- * Scans supplied class and returns an iterable of all data children classes.
- *
- * @param type
- * YANG Modeled Entity derived from DataContainer
- * @return Iterable of all data children, which have YANG modeled entity
- */
- @SuppressWarnings("unchecked")
- public static Iterable<Class<? extends DataObject>> getChildrenClasses(final Class<? extends DataContainer> type) {
- checkArgument(type != null, "Target type must not be null");
- 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);
- if (entity.isPresent()) {
- ret.add((Class<? extends DataObject>) entity.get());
- }
- }
- return ret;
- }
-
- /**
- * Scans supplied class and returns an iterable of all data children classes.
- *
- * @param type
- * YANG Modeled Entity derived from DataContainer
- * @return Iterable of all data children, which have YANG modeled entity
- */
- 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 %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);
- if (entity.isPresent()) {
- ret.put(entity.get(), method);
- }
- }
- return ret;
- }
-
- @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.empty();
- }
-
- Class returnType = method.getReturnType();
- if (DataContainer.class.isAssignableFrom(returnType)) {
- return Optional.of(returnType);
- } else 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)) {
- return Optional.of((Class) listResult);
- }
- return Optional.empty();
- });
- } catch (Exception 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.
- */
- LOG.debug("Unable to find YANG modeled return type for {}", method, e);
- }
- }
- return Optional.empty();
- }
-
- private static class ClassToQNameLoader extends CacheLoader<Class<?>, Optional<QName>> {