public class ComObjectCollector extends Object implements ComObjectListener
ComObjectListener
implementation that collects all
newly created Com4jObject
s
The intended use of this class is to record objects created in a certain block and then dispose them all (except a few marked explicitly) at once at some later point.
See the following code example for a typical usage:
void foo() { // we will start using COM objects. // so we'll register the listener and start keeping // track of COM objects we create. ComObjectCollector col = new ComObjectCollector(); COM4J.addListener(col); try { // use COM objects as much as you want IFoo foo = doALotOfComStuff(); // do this to avoid COM objects from disposed by the disposeAll method. col.remove(foo); } finally { // dispose all the COM objects created in this thread // since the listener is registered. // But "foo" won't be disposed because of the remove method. col.disposeAll(); // make sure to remove the listener COM4J.removeListener(col); } }
Modifier and Type | Field and Description |
---|---|
protected List<WeakReference<Com4jObject>> |
objects
The collected
Com4jObject s |
Constructor and Description |
---|
ComObjectCollector() |
Modifier and Type | Method and Description |
---|---|
void |
disposeAll()
|
void |
onNewObject(Com4jObject obj)
Called when a new COM object is created.
|
void |
remove(Com4jObject obj)
Removes the given object from the list of
Com4jObject s that
this class keeps. |
protected final List<WeakReference<Com4jObject>> objects
Com4jObject
spublic void onNewObject(Com4jObject obj)
ComObjectListener
onNewObject
in interface ComObjectListener
obj
- the newly created Com4jObjectpublic void remove(Com4jObject obj)
Com4jObject
s that
this class keeps.
If the application knows certain Com4jObject
needs to live after
the disposeAll()
method, this method can be called to avoid the object
from being disposed.
If the object passed in is not known to this ComObjectCollector
,
it is a no-op.
obj
- The object to removepublic void disposeAll()
Com4jObject.dispose()
method for all the Com4jObject
s
known to this ComObjectCollector
.
Each time this method is called, it forgets all the disposed objects.
Copyright © 2014. All rights reserved.