[zeromq-dev] Java binding

Martin Sustrik sustrik at 250bpm.com
Mon Feb 22 15:46:20 CET 2010


gonzalo diethelm wrote:

>> One thing troubles me though. I don't like the idea of allowing the
>> application to mess with underlying pointers. No other binding allows
>> for that and neither should Java binding. Isn't there a way to
> restrict
>> access to contextHandle property so that only Socket object can access
> it?
> 
> That is why Context::getContextHandle() and Socket::getSocketHandle()
> are declared as protected; they can only be accessed from Java classes
> belonging to the org.zmq package. I believe this provides enough
> protection.
> 
> Ideally they should both be private; in fact, I was sure this would STOP
> things from working (since Socket needs to call getContextHandle()) but
> I just tested it and it works... Surprise!
> 
> I don't know if the fact that you can call through JNI a private class
> member function from member functions in another class within the same
> package, is documented to always work in Java, but it SEEMS we could
> declare these functions private.

Little digging on the web got me this:

http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

As far as I understand the text, private member shouldn't be accessible 
from another class in the package. The fact that it works with JNI may 
be just an artifact and we shouldn't use it IMO.

More interesting seems to be "package-private" (no modifier) which AFAIU 
allows accessing the function only from within the package.

This would suit the purpose better than "protected" which allows access 
from the same package and "in addition, by a subclass of its class in 
another package". The latter would mean that a subclass of Context 
created by the application would be able to get the pointer.

Thoughts?
Martin



More information about the zeromq-dev mailing list