【问题标题】:Ensuring Java classes do not import from certain packages确保 Java 类不会从某些包中导入
【发布时间】:2014-12-28 20:52:01
【问题描述】:

我有这样的包:

com.example.pure
com.example.pure.internal
com.example.other

我想确保com.example.pure 包中的类不依赖于com.example.pure.internalcom.example.other 包中的类。

显然,我可以手动浏览每个文件并关注导入,但我想自动化它。我可以写一些代码来做,但似乎它可能是其他人已经解决的问题。

我使用 Eclipse,所以我可以配置一个 Eclipse 插件来强制执行依赖方向是绝对理想的,但命令行实用程序或 Gradle 插件也很棒。

【问题讨论】:

    标签: java eclipse gradle checkstyle findbugs


    【解决方案1】:

    在 Apache Lucene 中,他们使用 this handy tool called fobidden-apis 来检查某些 API 是否未被使用。这是一个 Ant 任务,因此您可以在 eclipse 中轻松调用它

    【讨论】:

      【解决方案2】:

      如果编译成功,你可以单独单独编译com.example.pure,那么它不依赖于任何其他包:

      -cp path or -classpath path
      

      javac -d bin -cp bin src\...\someclass.java
      

      【讨论】:

      • 它实际上确实依赖于我没有提到的其他包,但这当然是我可以采用的一种方式。感谢您的想法。
      • 不客气,兄弟,你可以用其他包编译它,而不用 com.example.pure.internalcom.example.other 包看看是否取决于他们。无论如何,我希望问题能解决。
      猜你喜欢
      • 1970-01-01
      • 2018-01-11
      • 2012-01-14
      • 2019-06-20
      • 2018-10-02
      • 2019-01-31
      • 2019-05-20
      • 2019-10-31
      • 2015-11-01
      相关资源
      最近更新 更多