test/hotspot/jtreg/runtime/cds/appcds/CompilerUtils.java
changeset 57567 b000362a89a0
parent 48138 78b2ecdd3c4b
equal deleted inserted replaced
57566:ad84ae073248 57567:b000362a89a0
       
     1 /*
       
     2  * Copyright (c) 2015, 2019, 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 
       
    25 import javax.tools.JavaCompiler;
       
    26 import javax.tools.StandardJavaFileManager;
       
    27 import javax.tools.StandardLocation;
       
    28 import javax.tools.ToolProvider;
       
    29 import java.io.IOException;
       
    30 import java.nio.file.Files;
       
    31 import java.nio.file.Path;
       
    32 import java.util.Arrays;
       
    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  * This class will eventually move to jdk.testlibrary.
       
    41  */
       
    42 
       
    43 public final class CompilerUtils {
       
    44     private CompilerUtils() { }
       
    45 
       
    46     /**
       
    47      * Compile all the java sources in {@code <source>/**} to
       
    48      * {@code <destination>/**}. The destination directory will be created if
       
    49      * it doesn't exist.
       
    50      *
       
    51      * All warnings/errors emitted by the compiler are output to System.out/err.
       
    52      *
       
    53      * @return true if the compilation is successful
       
    54      *
       
    55      * @throws IOException if there is an I/O error scanning the source tree or
       
    56      *                     creating the destination directory
       
    57      */
       
    58     public static boolean compile(Path source, Path destination, String ... options)
       
    59         throws IOException
       
    60     {
       
    61         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
       
    62         StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);
       
    63 
       
    64         List<Path> sources
       
    65             = Files.find(source, Integer.MAX_VALUE,
       
    66                 (file, attrs) -> (file.toString().endsWith(".java")))
       
    67                 .collect(Collectors.toList());
       
    68 
       
    69         Files.createDirectories(destination);
       
    70         jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
       
    71                                  Arrays.asList(destination));
       
    72 
       
    73         List<String> opts = Arrays.asList(options);
       
    74         JavaCompiler.CompilationTask task
       
    75             = compiler.getTask(null, jfm, null, opts, null,
       
    76                 jfm.getJavaFileObjectsFromPaths(sources));
       
    77 
       
    78         return task.call();
       
    79     }
       
    80 }