jdk/test/lib/testlibrary/CompilerUtils.java
changeset 45418 8d478df92153
parent 45417 f7479ee8de69
parent 45412 358c26742cc9
child 45419 1e5f410302d2
equal deleted inserted replaced
45417:f7479ee8de69 45418:8d478df92153
     1 /*
       
     2  * Copyright (c) 2015, 2016, 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 
       
    24 import javax.tools.JavaCompiler;
       
    25 import javax.tools.StandardJavaFileManager;
       
    26 import javax.tools.StandardLocation;
       
    27 import javax.tools.ToolProvider;
       
    28 import java.io.IOException;
       
    29 import java.nio.file.Files;
       
    30 import java.nio.file.Path;
       
    31 import java.util.Arrays;
       
    32 import java.util.Collections;
       
    33 import java.util.List;
       
    34 import java.util.stream.Collectors;
       
    35 
       
    36 /**
       
    37  * This class consists exclusively of static utility methods for invoking the
       
    38  * java compiler.
       
    39  */
       
    40 
       
    41 public final class CompilerUtils {
       
    42     private CompilerUtils() { }
       
    43 
       
    44     /**
       
    45      * Compile all the java sources in {@code <source>/**} to
       
    46      * {@code <destination>/**}. The destination directory will be created if
       
    47      * it doesn't exist.
       
    48      *
       
    49      * All warnings/errors emitted by the compiler are output to System.out/err.
       
    50      *
       
    51      * @return true if the compilation is successful
       
    52      *
       
    53      * @throws IOException
       
    54      *         if there is an I/O error scanning the source tree or
       
    55      *         creating the destination directory
       
    56      * @throws UnsupportedOperationException
       
    57      *         if there is no system java compiler
       
    58      */
       
    59     public static boolean compile(Path source, Path destination, String ... options)
       
    60         throws IOException
       
    61     {
       
    62         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
       
    63         if (compiler == null) {
       
    64             // no compiler available
       
    65             throw new UnsupportedOperationException("Unable to get system java compiler. " +
       
    66                 "Perhaps, jdk.compiler module is not available.");
       
    67         }
       
    68         StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);
       
    69 
       
    70         List<Path> sources
       
    71             = Files.find(source, Integer.MAX_VALUE,
       
    72                 (file, attrs) -> (file.toString().endsWith(".java")))
       
    73                 .collect(Collectors.toList());
       
    74 
       
    75         Files.createDirectories(destination);
       
    76         jfm.setLocation(StandardLocation.CLASS_PATH, Collections.EMPTY_LIST);
       
    77         jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
       
    78                                  Arrays.asList(destination));
       
    79 
       
    80         List<String> opts = Arrays.asList(options);
       
    81         JavaCompiler.CompilationTask task
       
    82             = compiler.getTask(null, jfm, null, opts, null,
       
    83                 jfm.getJavaFileObjectsFromPaths(sources));
       
    84 
       
    85         return task.call();
       
    86     }
       
    87 }