Hi Xavier,

iterating over all instances - iterator() - will just create a list of
instances so remove() would remove it from the list which is likely a noop
for you since you don't see the list but its iterator.

ApplicationScoped is a normal scope and an AlterableContext so the bean
instance would be destroyed (ie next usage will recreate it).


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com>

2016-02-24 8:54 GMT+01:00 Xavier Dury <[email protected]>:

> Nevermind, if had to do something like that:
>
> Instance<Bar> barInstance = fooInstance.select(Bar.class);
> Bar bar = barInstance.get();
> barInstance.destroy(bar);
>
> ----------------------------------------
> > From: [email protected]
> > To: [email protected]
> > Subject: NPE on Instance.destroy()
> > Date: Wed, 24 Feb 2016 08:49:31 +0100
> >
> > Hi,
> >
> > I've come accross a NPE while calling Instance.destroy() whith
> @Dependent beans I got from the same Instance, is it normal? (I am using
> TomEE 7M1 + OWB 1.6.3)
> >
> > More questions about Instance:
> > - does the remove() method on the iterator obtained via
> Instance.iterator() do anything (like calling Instance.destroy() with the
> current object)?
> > - what happens if destroy() is called on an @ApplicationScoped bean
> >
> > Thanks,
> >
> > Xavier
> >
> > import java.util.Iterator;
> >
> > import javax.enterprise.inject.Instance;
> > import javax.inject.Inject;
> >
> > import org.apache.openejb.junit.ApplicationComposerRule;
> > import org.apache.openejb.testing.CdiExtensions;
> > import org.apache.openejb.testing.Module;
> > import org.junit.Rule;
> > import org.junit.Test;
> > import org.junit.rules.TestRule;
> >
> > public @CdiExtensions class InstanceTest {
> >
> > public static class Bar implements Foo {}
> >
> > public static class Baz implements Foo {}
> >
> > public interface Foo {}
> >
> > public final @Rule TestRule application = new
> ApplicationComposerRule(this);
> >
> > private @Inject Instance<Bar> barInstance;
> > private @Inject Instance<Foo> fooInstance;
> >
> > public @Module Class<?>[] classes() {
> > return new Class<?>[] {Bar.class, Baz.class};
> > }
> >
> > public @Test void getDestroy() {
> > Bar bar = this.barInstance.get();
> > System.out.println(bar.getClass().getName());
> > this.fooInstance.destroy(bar); // NPE
> > }
> >
> > public @Test void iterateDestroy() {
> > for (Foo foo : this.fooInstance) {
> > System.out.println(foo.getClass().getName());
> > this.fooInstance.destroy(foo);
> > }
> > }
> >
> > public @Test void iterateRemove() {
> > Iterator<Foo> iterator = this.fooInstance.iterator();
> > while (iterator.hasNext()) {
> > System.out.println(iterator.next().getClass().getName());
> > iterator.remove();
> > }
> > }
> >
> > public @Test void selectGetDestroy() {
> > Bar bar = this.fooInstance.select(Bar.class).get();
> > System.out.println(bar.getClass().getName());
> > this.fooInstance.destroy(bar); // NPE
> > }
> >
> > public @Test void selectIterateDestroy() {
> > for (Bar bar : this.fooInstance.select(Bar.class)) {
> > System.out.println(bar.getClass().getName());
> > this.fooInstance.destroy(bar); // NPE
> > }
> > }
> >
> > public @Test void selectIterateRemove() {
> > Iterator<Bar> iterator = this.fooInstance.select(Bar.class).iterator();
> > while (iterator.hasNext()) {
> > System.out.println(iterator.next().getClass().getName());
> > iterator.remove();
> > }
> > }
> > }
> >
> >
>
>

Reply via email to