下面列出了javax.tools.StandardJavaFileManager#handleOption ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test(dataProvider = "versions")
public void test(String version, int expected) throws Throwable {
StandardJavaFileManager jfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
jfm.setLocation(jloc, List.of(new File("multi-release.jar")));
if (version.length() > 0) {
jfm.handleOption("--multi-release", List.of(version).iterator());
}
CustomClassLoader cldr = new CustomClassLoader(jfm);
Class<?> versionClass = cldr.loadClass("version.Version");
MethodType mt = MethodType.methodType(int.class);
MethodHandle mh = MethodHandles.lookup().findVirtual(versionClass, "getVersion", mt);
int v = (int)mh.invoke(versionClass.newInstance());
Assert.assertEquals(v, expected);
jfm.close();
}
/**
* Handles the {@code --release} option.
*
* @param additionalOptions a predicate to handle additional options implied by the
* {@code --release} option. The predicate should return true if all the additional
* options were processed successfully.
* @return true if successful, false otherwise
*/
public boolean handleReleaseOptions(Predicate<Iterable<String>> additionalOptions) {
String platformString = options.get(Option.RELEASE);
checkOptionAllowed(platformString == null,
option -> error("err.release.bootclasspath.conflict", option.getPrimaryName()),
Option.BOOT_CLASS_PATH, Option.XBOOTCLASSPATH, Option.XBOOTCLASSPATH_APPEND,
Option.XBOOTCLASSPATH_PREPEND,
Option.ENDORSEDDIRS, Option.DJAVA_ENDORSED_DIRS,
Option.EXTDIRS, Option.DJAVA_EXT_DIRS,
Option.SOURCE, Option.TARGET,
Option.SYSTEM, Option.UPGRADE_MODULE_PATH);
if (platformString != null) {
PlatformDescription platformDescription = PlatformUtils.lookupPlatformDescription(platformString);
if (platformDescription == null) {
error("err.unsupported.release.version", platformString);
return false;
}
options.put(Option.SOURCE, platformDescription.getSourceVersion());
options.put(Option.TARGET, platformDescription.getTargetVersion());
context.put(PlatformDescription.class, platformDescription);
if (!additionalOptions.test(platformDescription.getAdditionalOptions()))
return false;
Collection<File> platformCP = platformDescription.getPlatformPath();
if (platformCP != null) {
JavaFileManager fm = getFileManager();
if (!(fm instanceof StandardJavaFileManager)) {
error("err.release.not.standard.file.manager");
return false;
}
try {
StandardJavaFileManager sfm = (StandardJavaFileManager) fm;
if (Source.instance(context).allowModules()) {
sfm.handleOption("--system", Collections.singletonList("none").iterator());
sfm.setLocation(StandardLocation.UPGRADE_MODULE_PATH, platformCP);
} else {
sfm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, platformCP);
}
} catch (IOException ex) {
log.printLines(PrefixKind.JAVAC, "msg.io");
ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
return false;
}
}
}
return true;
}
/**
* Handles the {@code --release} option.
*
* @param additionalOptions a predicate to handle additional options implied by the
* {@code --release} option. The predicate should return true if all the additional
* options were processed successfully.
* @return true if successful, false otherwise
*/
public boolean handleReleaseOptions(Predicate<Iterable<String>> additionalOptions) {
String platformString = options.get(Option.RELEASE);
checkOptionAllowed(platformString == null,
option -> error("err.release.bootclasspath.conflict", option.getPrimaryName()),
Option.BOOT_CLASS_PATH, Option.XBOOTCLASSPATH, Option.XBOOTCLASSPATH_APPEND,
Option.XBOOTCLASSPATH_PREPEND,
Option.ENDORSEDDIRS, Option.DJAVA_ENDORSED_DIRS,
Option.EXTDIRS, Option.DJAVA_EXT_DIRS,
Option.SOURCE, Option.TARGET,
Option.SYSTEM, Option.UPGRADE_MODULE_PATH);
if (platformString != null) {
PlatformDescription platformDescription = PlatformUtils.lookupPlatformDescription(platformString);
if (platformDescription == null) {
error("err.unsupported.release.version", platformString);
return false;
}
options.put(Option.SOURCE, platformDescription.getSourceVersion());
options.put(Option.TARGET, platformDescription.getTargetVersion());
context.put(PlatformDescription.class, platformDescription);
if (!additionalOptions.test(platformDescription.getAdditionalOptions()))
return false;
Collection<Path> platformCP = platformDescription.getPlatformPath();
if (platformCP != null) {
JavaFileManager fm = getFileManager();
if (!(fm instanceof StandardJavaFileManager)) {
error("err.release.not.standard.file.manager");
return false;
}
try {
StandardJavaFileManager sfm = (StandardJavaFileManager) fm;
if (Source.instance(context).allowModules()) {
sfm.handleOption("--system", Arrays.asList("none").iterator());
sfm.setLocationFromPaths(StandardLocation.UPGRADE_MODULE_PATH, platformCP);
} else {
sfm.setLocationFromPaths(StandardLocation.PLATFORM_CLASS_PATH, platformCP);
}
} catch (IOException ex) {
log.printLines(PrefixKind.JAVAC, "msg.io");
ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
return false;
}
}
}
return true;
}