Skip to content

Commit

Permalink
Add static utility methods on container to add and remove beans.
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Mar 26, 2021
1 parent 57779c6 commit a86a0c2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,46 @@ default List<EventListener> getEventListeners()
return Collections.unmodifiableList(new ArrayList<>(getBeans(EventListener.class)));
}

/**
* A utility method to add a bean to a container.
* @param parent the parent container.
* @param child the child bean.
* @return true if the child was added as a bean, false if parent was not instance of {@link Container} or bean was already present.
*/
static boolean addBean(Object parent, Object child)
{
if (parent instanceof Container)
return ((Container)parent).addBean(child);
return false;
}

/**
* A utility method to add a bean to a container.
* @param parent the parent container.
* @param child the child bean.
* @param managed whether to managed the lifecycle of the bean.
* @return true if the child was added as a bean, false if parent was not instance of {@link Container} or bean was already present.
*/
static boolean addBean(Object parent, Object child, boolean managed)
{
if (parent instanceof Container)
return ((Container)parent).addBean(child, managed);
return false;
}

/**
* A utility method to remove a bean from a container.
* @param parent the parent container.
* @param child the child bean.
* @return true if parent was an instance of {@link Container} and the bean was removed.
*/
static boolean removeBean(Object parent, Object child)
{
if (parent instanceof Container)
return ((Container)parent).removeBean(child);
return false;
}

/**
* A listener for Container events.
* If an added bean implements this interface it will receive the events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,9 @@ public boolean addEventListener(EventListener listener)
if (listener instanceof InheritedListener && b.isManaged() && b._bean instanceof Container)
{
if (b._bean instanceof ContainerLifeCycle)
((ContainerLifeCycle)b._bean).addBean(listener, false);
Container.addBean(b._bean, listener, false);
else
((Container)b._bean).addBean(listener);
Container.addBean(b._bean, listener);
}
}
}
Expand All @@ -499,8 +499,8 @@ public boolean removeEventListener(EventListener listener)
{
cl.beanRemoved(this, b._bean);

if (listener instanceof InheritedListener && b.isManaged() && b._bean instanceof Container)
((Container)b._bean).removeBean(listener);
if (listener instanceof InheritedListener && b.isManaged())
Container.removeBean(b._bean, listener);
}
}
return true;
Expand Down Expand Up @@ -541,9 +541,9 @@ private void manage(Bean bean)
if (l instanceof InheritedListener)
{
if (bean._bean instanceof ContainerLifeCycle)
((ContainerLifeCycle)bean._bean).addBean(l, false);
Container.addBean(bean._bean, l, false);
else
((Container)bean._bean).addBean(l);
Container.addBean(bean._bean, l);
}
}
}
Expand Down Expand Up @@ -579,7 +579,7 @@ private void unmanage(Bean bean)
for (Container.Listener l : _listeners)
{
if (l instanceof InheritedListener)
((Container)bean._bean).removeBean(l);
Container.removeBean(bean._bean, l);
}
}
bean._managed = Managed.UNMANAGED;
Expand Down

0 comments on commit a86a0c2

Please sign in to comment.