• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

access permissions for a method.

 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi;
Hoping new annotations will help me do what I want to do.

I have 2 classes (Foo and Bar) in 2 different packages

Foo does not/cannot inherit from Bar.


Foo needs to call a method on bar.
The problem is the method need to allow access only to it from a call from the class Foo or Bar. Preferably hide it all together from other classes.

Any thoughts???
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The clean approach would be to put them together into the same package and have no other classes in that package. And then make the method protected or package protected.

If that's not an option somehow, the dirty (and comparatively slow) approach might be to check the stack trace that the calling method or class is what you want it to be.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

peter cooke wrote:The problem is the method need to allow access only to it from a call from the class Foo or Bar. Preferably hide it all together from other classes.
Any thoughts???


Yes: why do you need to do this? It sounds very arcane.

However, if the reason for calling the method is to get a result of some kind, one possibility would be to add a proxy class in Bar's package that can call this method on behalf of a Foo, and update the Foo with its result. Alternatively, you could prevent any other class from calling it by supplying an object that can ONLY be created by a Foo. Either way, if you set it up right, no other class would ever be able to see the result.

But it's incredibly tortuous, so my advice would be to back up and work out WHY you want to do this. Don't leap to the HOW until you understand that.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:Alternatively, you could prevent any other class from calling it by supplying an object that can ONLY be created by a Foo.


Actually, you could do it directly:but, like I say, incredibly tortuous.

Winston
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:
If that's not an option somehow, the dirty (and comparatively slow) approach might be to check the stack trace that the calling method or class is what you want it to be.



Although called "dirty", I actually like this option -- it seems "clean" to me...

peter cooke wrote:
I have 2 classes (Foo and Bar) in 2 different packages

Foo does not/cannot inherit from Bar.


Foo needs to call a method on bar.
The problem is the method need to allow access only to it from a call from the class Foo or Bar. Preferably hide it all together from other classes.



I guess theMethod() on Bar can be written like so...


And it can be called from Foo, like so...



Henry
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Although called "dirty", I actually like this option -- it seems "clean" to me...


Hmmm. My main worry is that it relies on parsing a structure that is very slow to create and from which you can only get Strings. It seems to me that my set-up - cumbersome though it is - could possibly be developed into a class-specific method Pattern; maybe involving a proxy class so as not to disturb the original.

Personally, I don't like the idea of class-specific methods in general; but if you're going to do it, it seems to me that passing a class-specific key is more self-documenting. Whether or not you use that key to pass back results is up to you.

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic