Package and visibility

*爱你&永不变心* 提交于 2019-12-29 00:59:25

问题


I'm making an SDK and I'm trying to separate classes to different packages, those classes use some other shared classes. The issue is if I made the shared classes public everyone will be able to see them, not only my classes. What's the right way to make them only accessible by my application?

Example :


Package a MyClass1

Package b MyClass2

Package c public MySharedClass


Because c is public MySharedClass will be able to access it, but the issue is that it will also will be visible to the world, how could I prevent that?


回答1:


Create a package that is documented as an internal package, not to be used by clients.

There is no way in Java to make a class public only for certain packages: It either is public for everyone or package-private (public only in the declared package).

I think there's a proposal for modules to allow better control in the visibility of classes, but we'll have to wait, at least, for Java 8.




回答2:


The packages are all "public" in Java, what you can protect is the classes within a package. For limiting the visibility of a class to only a given package, declare it like this (without the public visibility modifier):

class MyClass {
    // ...
}

In that way, only the classes in the same package as MyClass will be able to see it.




回答3:


Non trivial:

The shared classes could be defined by a generally accessible set of interfaces. The actual implementation should be loaded explicitly via a Classloader. After that, simply apply Java Security Management mechanisms to control access to the implementation classes. Anyone can see the interfaces and access to actual implementation will be restricted to your SDK.

(A varient of above is what every web/app server needs to do. How do you think Tomcat prevents you from accessing some other app's "public" classes?)

edit: note above is a runtime mechanism. There are static (post) compile approaches as well. APT for example, could be effective here. Naturally I am not addressing a restructuring of your package (in OP) and only addressing how to secure access to a general approach. But these are a bit 'hacky' -- the runtime mechanism of class loading is canonical and imo strictly more correct.




回答4:


If the class is shared by classes from two different packages, it could be a good indication that these two classes should be in the same package, along with the shared class, which wouldn't be public and would thus only be usable by classes of the same package.

If it's really not an option, just document the shared class appropriately, to indicate that it's not supposed to be used outside of the SDK internal code, that it's subject to changes in future versions, and make it even clearer by naming the package "internal" or somthing like this.




回答5:


protected modifier can use,in case of your class will access only in same package. otherwise there is no possibility.



来源:https://stackoverflow.com/questions/8647565/package-and-visibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!