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