Eclipse 如何知道 com.sun 是受限制的 API?

How does Eclipse know that com.sun is a restricted API?

我正在使用 Guava 中的 ClassPath.getAllClasses 在运行时 类 中查找实现特定接口的。当与 rt.jar 一起使用时,它会从 sun.* 和类似的包中找到许多 类,我想将它们过滤掉。

我想重用 Eclipse 和其他 java 编译器使用的机制来警告受限 API。此信息驻留在 JRE 中还是硬编码到 Eclipse 中?

此信息存储在 Eclipse.

中所谓的 Execution Environments 配置文件中

因为这些配置文件最初是 OSGi 的东西(参见 Bundle-RequiredExecutionEnvironment), the profiles are stored in the org.eclipse.osgi plugin/bundle in *.profile files that each contains a whitelist of the accessible packages (org.osgi.framework.system.packages), e. g. here for JavaSE-1.8

如果您创建一个新的 Java 项目,您可以选择

  • 一个执行环境 JRE,它给你导入 e 的 访问限制 错误。 G。 sun.* 包或
  • 一个没有任何访问限制的特定 JRE 到 JRE 包

创建项目后,可以在 项目 > 属性中更改所选的 JRE:Java 构建路径 ,选项卡 Libraries 通过选择 JRE 并单击 Edit...

此外,自从 Java Platform Module System 与 Java 9 一起引入以来,JRE 本身限制了对某些包的访问(而不是文件 rt.jar,文件 modules).