langtools/test/tools/jdeps/CompilerUtils.java
author naoto
Tue, 29 Mar 2016 17:06:33 -0700
changeset 36739 145210aba850
parent 36526 3b41f1c69604
permissions -rw-r--r--
8152704: jlink command line output/help message improvement Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    23
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    24
import javax.tools.JavaCompiler;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    25
import javax.tools.StandardJavaFileManager;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    26
import javax.tools.StandardLocation;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import javax.tools.ToolProvider;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.IOException;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.UncheckedIOException;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.nio.file.FileVisitResult;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.nio.file.Files;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.nio.file.Path;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.nio.file.SimpleFileVisitor;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.nio.file.attribute.BasicFileAttributes;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.ArrayList;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.Arrays;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.List;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.stream.Collectors;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.stream.Stream;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    40
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    41
public final class CompilerUtils {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    42
    private CompilerUtils() { }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    43
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    44
    /**
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    45
     * Compile all the java sources in {@code <source>/**} to
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    46
     * {@code <destination>/**}. The destination directory will be created if
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    47
     * it doesn't exist.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    48
     *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    49
     * All warnings/errors emitted by the compiler are output to System.out/err.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    50
     *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    51
     * @return true if the compilation is successful
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    52
     *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    53
     * @throws IOException if there is an I/O error scanning the source tree or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    54
     *                     creating the destination directory
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    55
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    public static boolean compile(Path source, Path destination, String... options)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    57
        throws IOException
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    58
    {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    59
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    60
        StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    61
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        List<Path> sources
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    63
            = Files.find(source, Integer.MAX_VALUE,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    64
                         (file, attrs) -> (file.toString().endsWith(".java")))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    65
                   .collect(Collectors.toList());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    66
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        Files.createDirectories(destination);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    69
                                 Arrays.asList(destination));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    70
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        List<String> opts = Arrays.asList(options);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        JavaCompiler.CompilationTask task
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    73
            = compiler.getTask(null, jfm, null, opts, null,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    74
                               jfm.getJavaFileObjectsFromPaths(sources));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    75
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        return task.call();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    77
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    78
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    79
    /**
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    80
     * Compile the specified module from the given module sourcepath
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    81
     *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    82
     * All warnings/errors emitted by the compiler are output to System.out/err.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    83
     *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    84
     * @return true if the compilation is successful
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    85
     *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    86
     * @throws IOException if there is an I/O error scanning the source tree or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    87
     *                     creating the destination directory
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    88
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    89
    public static boolean compileModule(Path source, Path destination,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                                        String moduleName, String... options) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    93
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    95
            Files.createDirectories(destination);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    96
            jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    97
                                     Arrays.asList(destination));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        } catch (IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    99
            throw new UncheckedIOException(e);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   101
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        Stream<String> opts = Arrays.stream(new String[] {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            "-modulesourcepath", source.toString(), "-m", moduleName
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        });
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        List<String> javacOpts = Stream.concat(opts, Arrays.stream(options))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   106
                                        .collect(Collectors.toList());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        JavaCompiler.CompilationTask task
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   108
            = compiler.getTask(null, jfm, null, javacOpts, null, null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        return task.call();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   110
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   111
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   112
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   113
    public static void cleanDir(Path dir) throws IOException {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        if (Files.notExists(dir)) return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   115
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   117
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   118
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   119
                    throws IOException
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   120
            {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                Files.delete(file);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                return FileVisitResult.CONTINUE;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   123
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   125
            public FileVisitResult postVisitDirectory(Path dir, IOException e)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   126
                    throws IOException
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   127
            {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   128
                if (e == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   129
                    Files.delete(dir);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   130
                    return FileVisitResult.CONTINUE;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   131
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   132
                    // directory iteration failed
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   133
                    throw e;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   134
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   135
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        });
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        Files.deleteIfExists(dir);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   138
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   139
}