在 jar 文件中隐藏类
难道真的不可能在jar文件中隐藏一些类吗?
我不想允许直接实例化类以使其更加灵活。这个罐子只能看到工厂(或正面)。
除了创建两个项目之外,还有其他方法可以解决这个问题吗? (两个项目:第一个包含类(实现),另一个引用第一个项目并包含工厂;稍后将仅引用第二个项目)
Is it really impossible to hide some classes in a jar file?
I wanted not to allow direct instantiation of the classes to keep it more flexible. Only the factory (or a facade) should be visible of this jar.
Is there any other way than solve this problem than creating two projects?
(Two projects: the first one contains the classes (implementation) and the other one references to the first one and contains the factory; later only the second one will be referenced)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我认为如果您的公共工厂方法尝试返回“隐藏”的内容,您将会遇到编译器失败或警告。
不,如果不重新实现您自己的 ClassLoader 或使用 OSGi 或类似的东西,您就无法隐藏公共类。
您可以做的是将接口 api 与实现分开,例如,有一个仅包含接口的项目和另一个包含实现的项目。但是,您仍然无法隐藏实现类。
I think you will have either compiler failure or warning if your public factory method try to return something which is "hidden".
No, you can not hide a public class without reimplementing your own
ClassLoader
or using OSGi or anything similar.What you can do is to separate interface api from the implementation, e.g. have one project which contains only the interfaces and another porject which contains the implmentations. However, you still cannot hide the implementation classes.
混淆可以以某种方式帮助您。
Obfuscation can help you somehow.
对于标准类加载器和普通的旧 jar 文件,这是不可能的。 OSGi 的概念是仅使某些包对另一个包可见(即公共 api 和内部实现的分离)。
如果您使用的是 eclipse,则可以使用 this 强制执行此类规则
With standard classloaders and plain old jar files, this is not possible. OSGi has this concept of making visible only some packages to another bundle(i.e. separation of public api and internal implementation).
If you are using eclipse, you may enforce such rules with this
如果我正确理解你所说的“不允许直接实例化类以保持更灵活”,那么正确执行的外观模式将处理这个问题。
将要隐藏的所有类的构造函数限制在包范围内。将外观类开放到公共范围。
http://mindprod.com/jgloss/packagescope.html
If I understand you correctly when you say "not to allow direct instantiation of the classes to keep it more flexible", a properly executed facade pattern will handle this.
Restrict the constructors of all the classes you want to hide to package scope. Open the facade class to public scope.
http://mindprod.com/jgloss/packagescope.html
我知道您不想隐藏实际的类,只是防止它们在工厂类之外构建。我认为通过在类构造函数中使用包私有(默认)可见性可以很容易地实现这一点。唯一的限制是您需要将类和工厂放在同一个包中,因此在中型到大型代码库中,事情可能会变得不必要的复杂。
I'm understanding you're not looking to hide the actual classes, just prevent their construction outside a factory class. This I think can be quite easily achieved by using package private (default) visibility in the class constructors. The only limitation is that you'll need to have the classes and the factory in the same package so in a medium to large codebase things may get unnecessarily complex.
如果我正确理解你的问题,你想确保你的库的用户被迫使用你的工厂来实例化他们的对象,而不是使用构造函数本身。
在我看来,有两种可能性,其中一种很愚蠢,但在少数特定情况下可用,另一种是最实用、可能也是最常用的方法。
私有内部类
工厂。如果你有的话这会起作用
每个班级一个工厂,但几乎没有
如果你有很多的话可行
不同类别的管理
通过一个工厂。
protected
访问修饰符限制对您班级的访问
构造函数。 这很常见
使用工厂时的实践
模式。
If I understand your question correctly, you would like to make sure that users of your library are forced to use your factory to instantiate their objects rather than using the constructors themselves.
As I see it there are two possibilities, one of which is silly but usable in few, specific cases, and the other one is the most practical and probably most commonly used way of doing it.
private inner classes of the
factory. This would work if you had
one factory per class, but is hardly
workable if you have a lot of
different classes being managed
through one factory.
protected
access modifier torestrict access to your class
constructors. This is common
practice when using the factory
pattern.
您可以使用自定义类加载器实现这样的魔法,但是:
在这种情况下,我会做一些类似于我们在标准 Java 中看到的事情。例如,您看到
javax.xml.stream.XMLInputFactory
,但在某个地方您有com.sun.xml.internal.stream.XMLInputFactoryImpl
。 它是完全可以编译的如果您编写:尽管您几乎不会这样做,但 :-) 使用系统属性,您可以控制正在加载的实际实现。对我来说,这种方法在很多情况下都很好。
我希望我正确理解了你的问题;)
干杯!
You can do such magics with a custom class loader but:
In such situations I would do something similar to what we may see in the standard Java. E.g.you see
javax.xml.stream.XMLInputFactory
but somewhere you havecom.sun.xml.internal.stream.XMLInputFactoryImpl
. It is perfectly compilable if you write:though you will hardly do it :-) With a system property you may control the actual implementation that is being loaded. To me such approach is fine in many situations.
I hope I have understood your question correctly ;)
Cheers!
您的问题有两种解决方案,不涉及将所有类保留在同一个包中。
第一种是使用 Friend Accessor/Friend Package 模式(实用 API 设计,图拉赫 2008)。
第二种是使用OSGi。有一篇文章这里解释OSGi 如何实现这一点。
相关问题: 1,2,< a href="https://stackoverflow.com/questions/4647599/why-friend-directive-is-missing-in-java">3 和 4。
There are two solutions to your question that don't involve keeping all classes in the same package.
The first is to use the Friend Accessor/Friend Package pattern described in (Practical API Design, Tulach 2008).
The second is to use OSGi. There is an article here explaining how OSGi accomplishes this.
Related Questions: 1, 2, 3, and 4.