CompilerUtils.java revision 2654:6b750cdb823a
1/*
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package jdk.test.lib.compiler;
25
26import javax.tools.JavaCompiler;
27import javax.tools.StandardJavaFileManager;
28import javax.tools.StandardLocation;
29import javax.tools.ToolProvider;
30import java.io.IOException;
31import java.nio.file.Files;
32import java.nio.file.Path;
33import java.util.Arrays;
34import java.util.Collections;
35import java.util.List;
36import java.util.stream.Collectors;
37
38/**
39 * This class consists exclusively of static utility methods for invoking the
40 * java compiler.
41 */
42public final class CompilerUtils {
43    private CompilerUtils() { }
44
45    /**
46     * Compile all the java sources in {@code <source>/**} to
47     * {@code <destination>/**}. The destination directory will be created if
48     * it doesn't exist.
49     *
50     * All warnings/errors emitted by the compiler are output to System.out/err.
51     *
52     * @return true if the compilation is successful
53     *
54     * @throws IOException
55     *         if there is an I/O error scanning the source tree or
56     *         creating the destination directory
57     * @throws UnsupportedOperationException
58     *         if there is no system java compiler
59     */
60    public static boolean compile(Path source, Path destination, String... options)
61        throws IOException
62    {
63        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
64        if (compiler == null) {
65            // no compiler available
66            throw new UnsupportedOperationException("Unable to get system java compiler. "
67                    + "Perhaps, jdk.compiler module is not available.");
68        }
69        StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);
70
71        List<Path> sources
72            = Files.find(source, Integer.MAX_VALUE,
73                (file, attrs) -> (file.toString().endsWith(".java")))
74                .collect(Collectors.toList());
75
76        Files.createDirectories(destination);
77        jfm.setLocation(StandardLocation.CLASS_PATH, Collections.emptyList());
78        jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
79                Collections.singletonList(destination));
80
81        List<String> opts = Arrays.asList(options);
82        JavaCompiler.CompilationTask task
83            = compiler.getTask(null, jfm, null, opts, null,
84                jfm.getJavaFileObjectsFromPaths(sources));
85
86        return task.call();
87    }
88}
89