From a9bc172cde910489bfb6c09f00509cfc64d7b665 Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Thu, 9 Jun 2016 15:00:11 +0200 Subject: Make filterSubtree recursive Change-Id: I7b2b888fd7debb0aec3292a07fc35c0e6493d117 Signed-off-by: Marek Gradzki --- .../impl/read/AbstractCompositeReader.java | 30 +++++++++++++++------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/AbstractCompositeReader.java b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/AbstractCompositeReader.java index afd979167..c99e0edc4 100644 --- a/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/AbstractCompositeReader.java +++ b/v3po/translate-impl/src/main/java/io/fd/honeycomb/v3po/translate/impl/read/AbstractCompositeReader.java @@ -185,29 +185,41 @@ abstract class AbstractCompositeReader filterSubtree(@Nonnull final DataObject parent, - @Nonnull final InstanceIdentifier absolutPath, - @Nonnull final Class managedType) { - // TODO is there a better way than reflection ? e.g. convert into NN and filter out with a utility - // FIXME this needs to be recursive. right now it expects only 1 additional element in ID + test - + @Nonnull final InstanceIdentifier absolutPath, + @Nonnull final Class managedType) { final InstanceIdentifier.PathArgument nextId = - RWUtils.getNextId(absolutPath, InstanceIdentifier.create(parent.getClass())); + RWUtils.getNextId(absolutPath, InstanceIdentifier.create(parent.getClass())); + + final Optional nextParent = findNextParent(parent, nextId, managedType); + + if (Iterables.getLast(absolutPath.getPathArguments()).equals(nextId)) { + return nextParent; // we found the dataObject identified by absolutePath + } else if (nextParent.isPresent()) { + return filterSubtree(nextParent.get(), absolutPath, nextId.getType()); + } else { + return nextParent; // we can't go further, return Optional.absent() + } + } + private static Optional findNextParent(@Nonnull final DataObject parent, + @Nonnull final InstanceIdentifier.PathArgument nextId, + @Nonnull final Class managedType) { + // TODO is there a better way than reflection ? e.g. convert into NN and filter out with a utility Optional method = ReflectionUtils.findMethodReflex(managedType, "get", - Collections.>emptyList(), nextId.getType()); + Collections.>emptyList(), nextId.getType()); if (method.isPresent()) { return Optional.fromNullable(filterSingle(parent, nextId, method.get())); } else { // List child nodes method = ReflectionUtils.findMethodReflex(managedType, - "get" + nextId.getType().getSimpleName(), Collections.>emptyList(), List.class); + "get" + nextId.getType().getSimpleName(), Collections.>emptyList(), List.class); if (method.isPresent()) { return filterList(parent, nextId, method.get()); } else { throw new IllegalStateException( - "Unable to filter " + nextId + " from " + parent + " getters not found using reflexion"); + "Unable to filter " + nextId + " from " + parent + " getters not found using reflexion"); } } } -- cgit 1.2.3-korg