

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
15 messages in net.java.dev.jna.usersRe: [jna-users] Override Pointer.fina...| From | Sent On | Attachments |
|---|---|---|
| bb...@cox.net | Jun 26, 2007 9:11 am | |
| Timothy Wall | Jun 26, 2007 12:52 pm | |
| Wayne Meissner | Jun 26, 2007 7:43 pm | |
| Ben Chase | Jun 26, 2007 8:39 pm | |
| Wayne Meissner | Jun 26, 2007 10:33 pm | |
| Timothy Wall | Jun 27, 2007 5:08 am | |
| Timothy Wall | Jun 27, 2007 5:20 am | |
| Rick Goldstein | Jun 27, 2007 8:50 am | |
| Timothy Wall | Jun 27, 2007 9:59 am | |
| Ben Chase | Jun 27, 2007 7:26 pm | |
| Timothy Wall | Jun 28, 2007 3:56 am | |
| bb...@cox.net | Jun 28, 2007 4:43 am | |
| bb...@cox.net | Jun 28, 2007 5:37 am | |
| bb...@cox.net | Jun 28, 2007 6:18 am | |
| Wayne Meissner | Jun 28, 2007 7:41 am |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Re: [jna-users] Override Pointer.finalize to reclaim Native resources? | Actions... |
|---|---|---|
| From: | Wayne Meissner (wmei...@gmail.com) | |
| Date: | Jun 28, 2007 7:41:30 am | |
| List: | net.java.dev.jna.users | |
bb...@cox.net wrote:
Okay, so, eating my own dogfood, I introduced this class:
import com.sun.jna.Pointer;
public class WrappedPointer {
private Pointer pointer;
protected Pointer getPointer() {
return pointer;
}
protected void setPointer(Pointer p) {
pointer = p;
}
}
And then I made all of my other appropriate classes extend it. The result of
course was that everywhere I'd said "pointer" in my code it now says
"getPointer()". (If JNA would embrace this WrappedPointer class, those would
instead have changed to "this", along with changes to the method parameters in
the .dll Interface, gaining type safety.) Everywhere I'd said variously
"foo.getPointer()" or "bar.getHandle()" or "baz.pointer" or "fubar.handle", I'm
now consistently saying "bar.getPointer()". (If JNA would embrace
WrappedPointer, I think those would become just "foo" and "bar" and "baz" and
"fubar".)
You can achieve the elimination of the getPointer() calls using a TypeMapper.
One alternative I had in mind when developing the TypeMapper stuff, based on what I did in gstreamer-java, was to have an interface:
interface NativeValue { public Object getNativeValue(); }
that any object wanting to be auto-converted to a native type could implement NativeValue.
So, in your case, WrappedPointer would implement NativeValue and override getNativeValue():
public class WrappedPointer implements NativeValue { ... public Object getNativeValue() { return pointer; } }
But, I ended up going with the TypeMapper way, because it was completely external to the type being converted - i.e. it could wrap existing java types, and did not dirty your class's namespace with potentially conflicting methods.
Another interesting change was that all (or almost all) of the classes that now
extend WrappedPointer no longer have to import com.sun.jna.Pointer. (They all
still needed ...PointerByReference, I think.)
Finally, one idiom that emerged repeatedly (but this may just be a consequence
of the particular .dll I'm using) was this:
PointerByReference pHandle = new PointerByReference();
Media.INSTANCE.createFooHandle(pHandle,...);
setPointer(pHandle.getValue()); // This line is the repeated idiom.
So, I'm always wanting setPointer() to take a PointerByReference instead of a
Pointer, because I'm always stashing the pointer retrieved from .dll. I doubt
that rates as enough of an idiom to warrant being a facet of WrappedPointer.
And I'm trying to decide if there is some harm introduced if this method
setPointer(PointerByReference pp) {
setPointer(pp.getValue());
}
was added to a JNA-internal version of WrappedPointer. And I think I can just
get that idiom myself by extending WrappedPointer, and then doing all my other
extends from that, right?
No biggy on this last point about noticing this idiom. I just thought it was
interesting to see the same scrap of code crop up repeatedly... In fact, I
don't think I have much use for the vanilla settter "setPointer(Pointer p);",
except perhaps if I wanted to set the stashed pointer to some null value.
The only thing to keep in mind with adding more specific classes to JNA, is to straddle the line between ease-of-use, and cluttering the API.
The PointerDelegate pattern is probably pretty common though, so it might be worth adding somewhere. com.sun.jna.types or something.







