Hiding classes in a jar file

2019-01-15 01:19发布

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)

8条回答
仙女界的扛把子
2楼-- · 2019-01-15 01:46

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.

查看更多
不美不萌又怎样
3楼-- · 2019-01-15 01:48

You can do such magics with a custom class loader but:

  • the correct separation will be available only in a project staffed with your class loader;
  • it's really doubtful that the effort to create such loader is worthy.

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 have com.sun.xml.internal.stream.XMLInputFactoryImpl. It is perfectly compilable if you write:

new com.sun.xml.internal.stream.XMLInputFactoryImpl()

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!

查看更多
冷血范
4楼-- · 2019-01-15 01:49

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

"If you have a variable or method in your class that you don’t want clients of your class directly accessing, don’t give it a public, protected or private declaration. Due to an oversight in the design of Java, you can’t explicitly declare the default “package” accessibility. Other members of the package will be able to see it, but classes outside the package that inherit from yours, won’t. The protected accessibility attribute offers slightly more visibibily. A protected method is visible to inheriting classes, even not part of the same package. A package scope (default) method is not. That is the only difference between protected and package scope. "

查看更多
唯我独甜
5楼-- · 2019-01-15 01:51

With standard classloaders an plain old jar files this is not possible. OSGi cas this concept of making visible to an other bundle only some packages and not other (i.e. separation of public api and internal implementation).

If you are usinf eclipse you may enforce such rules with this

查看更多
我想做一个坏孩纸
6楼-- · 2019-01-15 01:54

Obfuscation can help you somehow.

查看更多
淡お忘
7楼-- · 2019-01-15 01:55

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.

查看更多
登录 后发表回答