Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Hiding methods from a public API

Thread view: 
Chris - 07 Aug 2007 20:08 GMT
We publish a library. Some of the methods in the public classes are
intended for consumption by our customers, but others are just for
internal use. Ok, fine, declare those methods protected.

A difficultly arises when we want to access one of these internal-use
methods from a class in another package. Example:

com.mydomain.foo.MyFoo wants to access a method in
com.mydomain.bar.MyBar

So the method has to be made public, which makes it available to our
customers. Not good.

How do people generally handle this? I've been putting a "Do not use,
for internal use only" in the Javadoc for the method, but that's not ideal.
Arne Vajhøj - 07 Aug 2007 20:27 GMT
> We publish a library. Some of the methods in the public classes are
> intended for consumption by our customers, but others are just for
[quoted text clipped - 11 lines]
> How do people generally handle this? I've been putting a "Do not use,
> for internal use only" in the Javadoc for the method, but that's not ideal.

Redesign.

If you have a bar "chunk" that needs to expose some functionality
to the foo "chunk", but you do not want other to use it, then it
indicates a design problem to me.

If you insist on just doing a workaround, then just don't generate
javadocs for that class.

Arne
Stefan Ram - 07 Aug 2007 20:35 GMT
>How do people generally handle this? I've been putting a "Do not use,
>for internal use only" in the Javadoc for the method, but that's not ideal.

 I have a macro »PRIVATE«. (I am using a preprocessor.)

 For the build of the library this is defined as

@de.dclj.ram.meta.description.Private public

 Thus, it declares as »public«, but adds an annotation to show
 the intend that this is to be considered »private« in regard
 to clients of the library.

 When I build JavaDocs from the source code,
 this macro is being redefined as

private

 So, these entries will not show up in the JavaDoc.

 I might write a Doclet to implement the same behavior with the
 annotation, but the preprocessor approach was easier for the
 moment and I can still write the doclet later. (NB: This is a
 recent modification and I still have not jet created
 documentation since, so it is not tried.)
Thomas Hawtin - 07 Aug 2007 20:50 GMT
> We publish a library. Some of the methods in the public classes are
> intended for consumption by our customers, but others are just for
> internal use. Ok, fine, declare those methods protected.

I assume you mean default access ("package private"). Can customers not
extend your classes?

> A difficultly arises when we want to access one of these internal-use
> methods from a class in another package. Example:

If you declare a public interface you expose that, but keep
implementation in a different package for which you do not publish an API.

Inner classes are also useful. Your public class can have an inner class
that uses your unpublished package interface.

Tom Hawtin
Eric Sosman - 07 Aug 2007 21:04 GMT
Chris wrote On 08/07/07 15:08,:
> We publish a library. Some of the methods in the public classes are
> intended for consumption by our customers, but others are just for
> internal use. Ok, fine, declare those methods protected.

   Are you sure you understand what "protected" means?
It means "accessible from all of this package, and also
from any class in any package anywhere at all that just
happens to extend this one, no matter who wrote it."

> A difficultly arises when we want to access one of these internal-use
> methods from a class in another package. Example:
[quoted text clipped - 4 lines]
> So the method has to be made public, which makes it available to our
> customers. Not good.

   Sounds like you've painted yourself into a corner.
Or maybe packaged yourself into a corner: Your division
of classes into packages does not reflect the ways those
classes relate to each other (if it did, you wouldn't have
this urge to make cross-package calls to package-private
methods).  Arne Vajhøj's advice is good.

> How do people generally handle this? I've been putting a "Do not use,
> for internal use only" in the Javadoc for the method, but that's not ideal.

   There are a couple of ugly hacks you might try, but
they are hacks and they are ugly:

   - Deprecate all the "hands-off" methods.  This won't
     absolutely prevent their use, but may discourage it.

   - Give each method an extra "enabler" argument whose
     value is a class the customer cannot instantiate.
     The way *you* get hold of an instance is to use a
     static factory method that checks the identity of
     its caller and throws if called from outside your
     own suite of packages.  (The consensus of another
     current thread about this technique is that it is
     a Bad Idea.)

Signature

Eric.Sosman@sun.com

Greg R. Broderick - 08 Aug 2007 17:12 GMT
Chris <spam_me_not@goaway.com> wrote in news:46b8c172$0$5472
$9a6e19ea@news.newshosting.com:

> How do people generally handle this? I've been putting a "Do not use,
> for internal use only" in the Javadoc for the method, but that's not ideal.

Publish an interface that doesn't include the methods that you wish to remain
for internal use.  Tell your clients to program to the interface instead of
to the concrete class.

Regards

Signature

---------------------------------------------------------------------
Greg R. Broderick                  usenet200707@blackholio.dyndns.org

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------

Lew - 08 Aug 2007 23:14 GMT
> Chris <spam_me_not@goaway.com> wrote in news:46b8c172$0$5472
> $9a6e19ea@news.newshosting.com:
[quoted text clipped - 5 lines]
> for internal use.  Tell your clients to program to the interface instead of
> to the concrete class.

In conjunction with that, provide a factory that produces an object of the
interface type, but with the run-time type of your actual proprietary
implementation.

Signature

Lew

Roedy Green - 08 Aug 2007 22:12 GMT
>We publish a library. Some of the methods in the public classes are
>intended for consumption by our customers, but others are just for
>internal use. Ok, fine, declare those methods protected.

First review http://mindprod.com/jgloss/scope.html
so you are clear on just what the various scope modifiers mean,
particularly protected.

Then consider using a the global rename feature of your favourite IDE
to make the names you don't want others to use ugly, and embarrassing
if caught using them.  e.g. prefix them with

"XYZCorp_use_only"
"DO_NOT_USE_"
"May_Disappear_"

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.