Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

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

I'm using ClassPath.getAllClasses from Guava to find in runtime classes that implement a certain interface. When used with rt.jar it finds many classes from sun.* and similar packages and I'd like to filter them out.

I want to reuse the mechanism used by Eclipse and maybe other java compilers to warn about restricted API. Does this information reside JRE or hardcoded into Eclipse?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This information is stored in so-called Execution Environments profiles inside of Eclipse.

Because these profiles are originally an OSGi thing (see 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.

If you create a new Java project you can choose either

  • an execution environment JRE which gives you access restriction errors for imports of e. g. sun.* packages or
  • a specific JRE without any access restriction to JRE packages

After project creation the chosen JRE can be changed in Project > Properties: Java Build Path, tab Libraries by selecting the JRE and clicking Edit....

In addition, since the Java Platform Module System, introduced with Java 9, the JRE itself restricts access to some packages (and instead of the file rt.jar there is the file modules).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...