Java Code Examples for javax.tools.StandardJavaFileManager#handleOption()

The following examples show how to use javax.tools.StandardJavaFileManager#handleOption() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultiReleaseJarAwareSJFM.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@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();
}
 
Example 2
Source File: Arguments.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: Arguments.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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;
}