在 jar 文件中隐藏类 [英] Hiding classes in a jar file

查看:20
本文介绍了在 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)

推荐答案

我认为,如果您的公共工厂方法尝试返回隐藏"的内容,您将遇到编译器失败或警告.

I think you will have either compiler failure or warning if your public factory method try to return something which is "hidden".

不,如果不重新实现自己的 ClassLoader 或使用 OSGi 或任何类似的东西,就不能隐藏公共类.

No, you can not hide a public class without reimplementing your own ClassLoader or using OSGi or anything similar.

您可以做的是将接口 api 与实现分开,例如有一个只包含接口的项目和另一个包含实现的项目.但是,您仍然无法隐藏实现类.

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.

这篇关于在 jar 文件中隐藏类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆