简体   繁体   English

如何使两个不同的程序包访问彼此的类而又不允许任​​何其他第三个程序包在Java中访问它?

[英]How to make two different packages access each other's classes without allowing any other third package to access it in java?

I am making a project in netbeans and facing a problem similar to the one already asked on this site - How to share package private data between two packages in Java? 我正在用netbeans进行一个项目,遇到的问题与该站点上已经问过的问题类似- 如何在Java中的两个软件包之间共享软件包私有数据? , with a slight difference of let's say that application developer can see my codebase. ,我们可以说应用程序开发人员可以看到我的代码库。 What I thought is to make a public class in package B say 'communicator class' with its contructor having default package access and passing its instance in the constructor of classes of package A. Basically I am stopping the developer (using package C) from instantiating any classes inside package A. This would work however this is not good approach SE-wise I guess. 我想做的是在程序包B中将一个公共类称为“通信程序类”,其构造函数具有默认程序包访问权限,并在程序包A类的构造函数中传递其实例。基本上,我正在阻止开发人员(使用程序包C)实例化包A中的任何类。这可以工作,但是我猜这不是SE的好方法。 Besides that question was asked three years ago. 此外,这个问题是三年前提出的。 Is there a better approach/technique available to this now which is NEAT and/or involves less coupling between packages A and B. 现在有没有更好的方法/技术可供使用,即NEAT和/或包装A和B之间的耦合较少。

PS: I am new with java. PS:我是Java新手。 Any help would be appreciated. 任何帮助,将不胜感激。

You can introduce a module which has a reference of the other two packages and make this module to act as a bridge. 您可以引入一个参考其他两个软件包的模块,并使该模块充当桥梁。 This is one thing that can be done till Java 8 这是Java 8之前可以做的一件事

I believe your approach would work, but you should ask yourself if it's worthwhile. 我相信您的方法会行得通,但是您应该问自己是否值得。 It will have an effect on the readability of your code. 这将影响代码的可读性。

Are you trying to protect against intentional, or accidental misuse of these packages? 您是否要防止故意或意外滥用这些软件包?

If you're trying to protect against intentional misuse, your approach wouldn't completely solve it; 如果您要防止故意滥用,您的方法将无法完全解决问题; because reflection can be used to override access on methods/constructors/etc. 因为反射可用于覆盖对方法/构造函数/等的访问。 eg 例如

Communicator communicator = ...;

Constructor<ObjA> constructor = ObjA.class.getConstructor(Communicator.class);
constructor.setAccessible(true);
ObjA objectFromA = constructor.newInstance(communicator);

If you have control over the Java runtime, then something could be done with a security manager to prevent this kind of thing. 如果您可以控制Java运行时,则可以使用安全管理器来做一些事情来防止这种情况。

There may also be things that can be done with ClassLoaders to control one module's access to another module. ClassLoader可能还可以完成某些事情,以控制一个模块对另一模块的访问。 A class loads other classes using its own ClassLoader; 一个类使用其自己的ClassLoader加载其他类。 a custom ClassLoader could prevent access to private parts of another module. 定制的ClassLoader可能会阻止访问另一个模块的私有部分。 This is (I believe) how OSGi controls access between modules. (我相信)这是OSGi如何控制模块之间的访问。

Solutions involving a security manager or a class loader would require you to have some control over the java runtime used. 涉及安全管理器或类加载器的解决方案将要求您对使用的Java运行时进行一些控制。

If you're only trying to protect against accidental misuse, you may be better to rely on package naming to make it clear to people they shouldn't use the classes. 如果您只是想防止意外的滥用,那么最好依靠软件包命名来使人们清楚他们不应该使用这些类。 eg package foo.bar.internal; 例如, package foo.bar.internal;

Are these packages that people outside of your team/company will be using? 团队/公司外部的人员会使用这些软件包吗?

If you have some influence over the build environment of the modules that will use your code, you could consider adding tools to the build configuration to ban use of your private packages. 如果您对将使用代码的模块的构建环境有一定影响,则可以考虑将工具添加到构建配置中以禁止使用私有软件包。 For example Checkstyle's Import Control check gives a lot of control over what packages can be imported from where. 例如, Checkstyle的“导入控制”检查对可以从何处导入哪些程序包进行了很多控制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:如何在第三类中创建两个不同类的实例,并通过构造函数相互传递引用 - Java: how to create instances of two different classes in a third class and pass references to each other through the constructors Java类位于同一包(不同的目录)中,但它们不能彼此访问 - Java classes are in the same package (different directories) but they can't access each other 如何从其他类(可能在其他包中)访问 arrayLists - How to access arrayLists from other classes (possibly in other packages) 访问其他 Java 类 - Access other Java classes 有没有一种方法可以在Java中使用package-private来允许其他软件包访问? - Is there a way to use package-private in java to allow other packages access? 两个Java列表元素如何相互访问? - How can two Java list elements access each other? 有什么方法可以在Java中定义一个私有类,只有同一包中的其他类才能访问它? - Is there any way to define a private class in Java which only other classes in the same package can access it? 是否有更好的方法让两个类相互访问? - Is there a better way to give two classes access to each other? Java中的兄弟嵌套类可以访问彼此的私有成员 - Sibling nested classes in Java have access to each other's private members 如何在没有 jsoup 或任何其他第三方的情况下在 java 上读取 html? - How to read html on java without jsoup or any other third party?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM