Mon, 03 Oct 2016 11:21:14 -0700
Added tag jdk8u122-b02 for changeset 2baeb96fa198
ohrstrom@1224 | 1 | /* |
jjh@1305 | 2 | * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
ohrstrom@1224 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohrstrom@1224 | 4 | * |
ohrstrom@1224 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohrstrom@1224 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohrstrom@1224 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohrstrom@1224 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohrstrom@1224 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohrstrom@1224 | 10 | * |
ohrstrom@1224 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohrstrom@1224 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohrstrom@1224 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohrstrom@1224 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohrstrom@1224 | 15 | * accompanied this code). |
ohrstrom@1224 | 16 | * |
ohrstrom@1224 | 17 | * You should have received a copy of the GNU General Public License version |
ohrstrom@1224 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohrstrom@1224 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohrstrom@1224 | 20 | * |
ohrstrom@1224 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohrstrom@1224 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohrstrom@1224 | 23 | * questions. |
ohrstrom@1224 | 24 | */ |
ohrstrom@1224 | 25 | |
ohrstrom@1224 | 26 | package anttasks; |
ohrstrom@1224 | 27 | |
ohrstrom@1224 | 28 | import genstubs.GenStubs; |
ohrstrom@1224 | 29 | |
ohrstrom@1224 | 30 | import java.io.*; |
ohrstrom@1224 | 31 | import java.util.*; |
ohrstrom@1224 | 32 | |
ohrstrom@1224 | 33 | import org.apache.tools.ant.BuildException; |
ohrstrom@1224 | 34 | import org.apache.tools.ant.DirectoryScanner; |
ohrstrom@1224 | 35 | import org.apache.tools.ant.taskdefs.MatchingTask; |
ohrstrom@1224 | 36 | import org.apache.tools.ant.types.Path; |
ohrstrom@1224 | 37 | import org.apache.tools.ant.types.Reference; |
ohrstrom@1224 | 38 | |
ohrstrom@1224 | 39 | /** |
ohrstrom@1224 | 40 | * Files are specified with an implicit fileset, using srcdir as a base directory. |
ohrstrom@1224 | 41 | * The set of files to be included is specified with an includes attribute or |
ohrstrom@1224 | 42 | * nested <includes> set. However, unlike a normal fileset, an empty includes attribute |
ohrstrom@1224 | 43 | * means "no files" instead of "all files". The Ant task also accepts "fork=true" and |
ohrstrom@1224 | 44 | * classpath attribute or nested <classpath> element to run GenStubs in a separate VM |
ohrstrom@1224 | 45 | * with the specified path. This is likely necessary if a JDK 7 parser is required to read the |
ohrstrom@1224 | 46 | * JDK 7 input files. |
ohrstrom@1224 | 47 | */ |
ohrstrom@1224 | 48 | public class GenStubsTask extends MatchingTask { |
ohrstrom@1224 | 49 | private File srcDir; |
ohrstrom@1224 | 50 | private File destDir; |
ohrstrom@1224 | 51 | private boolean fork; |
ohrstrom@1224 | 52 | private Path classpath; |
ohrstrom@1224 | 53 | private String includes; |
ohrstrom@1224 | 54 | |
ohrstrom@1224 | 55 | public void setSrcDir(File dir) { |
ohrstrom@1224 | 56 | this.srcDir = dir; |
ohrstrom@1224 | 57 | } |
ohrstrom@1224 | 58 | |
ohrstrom@1224 | 59 | public void setDestDir(File dir) { |
ohrstrom@1224 | 60 | this.destDir = dir; |
ohrstrom@1224 | 61 | } |
ohrstrom@1224 | 62 | |
ohrstrom@1224 | 63 | public void setFork(boolean v) { |
ohrstrom@1224 | 64 | this.fork = v; |
ohrstrom@1224 | 65 | } |
ohrstrom@1224 | 66 | |
ohrstrom@1224 | 67 | public void setClasspath(Path cp) { |
ohrstrom@1224 | 68 | if (classpath == null) |
ohrstrom@1224 | 69 | classpath = cp; |
ohrstrom@1224 | 70 | else |
ohrstrom@1224 | 71 | classpath.append(cp); |
ohrstrom@1224 | 72 | } |
ohrstrom@1224 | 73 | |
ohrstrom@1224 | 74 | public Path createClasspath() { |
ohrstrom@1224 | 75 | if (classpath == null) { |
ohrstrom@1224 | 76 | classpath = new Path(getProject()); |
ohrstrom@1224 | 77 | } |
ohrstrom@1224 | 78 | return classpath.createPath(); |
ohrstrom@1224 | 79 | } |
ohrstrom@1224 | 80 | |
ohrstrom@1224 | 81 | public void setClasspathRef(Reference r) { |
ohrstrom@1224 | 82 | createClasspath().setRefid(r); |
ohrstrom@1224 | 83 | } |
ohrstrom@1224 | 84 | |
ohrstrom@1224 | 85 | public void setIncludes(String includes) { |
ohrstrom@1224 | 86 | super.setIncludes(includes); |
ohrstrom@1224 | 87 | this.includes = includes; |
ohrstrom@1224 | 88 | } |
ohrstrom@1224 | 89 | |
ohrstrom@1224 | 90 | @Override |
ohrstrom@1224 | 91 | public void execute() { |
ohrstrom@1224 | 92 | if (includes != null && includes.trim().isEmpty()) |
ohrstrom@1224 | 93 | return; |
ohrstrom@1224 | 94 | |
ohrstrom@1224 | 95 | DirectoryScanner s = getDirectoryScanner(srcDir); |
ohrstrom@1224 | 96 | String[] files = s.getIncludedFiles(); |
ohrstrom@1224 | 97 | // System.err.println("Ant.execute: srcDir " + srcDir); |
ohrstrom@1224 | 98 | // System.err.println("Ant.execute: destDir " + destDir); |
ohrstrom@1224 | 99 | // System.err.println("Ant.execute: files " + Arrays.asList(files)); |
ohrstrom@1224 | 100 | |
ohrstrom@1224 | 101 | files = filter(srcDir, destDir, files); |
ohrstrom@1224 | 102 | if (files.length == 0) |
ohrstrom@1224 | 103 | return; |
ohrstrom@1224 | 104 | System.out.println("Generating " + files.length + " stub files to " + destDir); |
ohrstrom@1224 | 105 | |
ohrstrom@1224 | 106 | List<String> classNames = new ArrayList<String>(); |
ohrstrom@1224 | 107 | for (String file: files) { |
ohrstrom@1224 | 108 | classNames.add(file.replaceAll(".java$", "").replace('/', '.')); |
ohrstrom@1224 | 109 | } |
ohrstrom@1224 | 110 | |
ohrstrom@1224 | 111 | if (!fork) { |
ohrstrom@1224 | 112 | GenStubs m = new GenStubs(); |
ohrstrom@1224 | 113 | boolean ok = m.run(srcDir.getPath(), destDir, classNames); |
ohrstrom@1224 | 114 | if (!ok) |
ohrstrom@1224 | 115 | throw new BuildException("genstubs failed"); |
ohrstrom@1224 | 116 | } else { |
ohrstrom@1224 | 117 | List<String> cmd = new ArrayList<String>(); |
ohrstrom@1224 | 118 | String java_home = System.getProperty("java.home"); |
ohrstrom@1224 | 119 | cmd.add(new File(new File(java_home, "bin"), "java").getPath()); |
ohrstrom@1224 | 120 | if (classpath != null) |
ohrstrom@1224 | 121 | cmd.add("-Xbootclasspath/p:" + classpath); |
ohrstrom@1224 | 122 | cmd.add(GenStubs.class.getName()); |
ohrstrom@1224 | 123 | cmd.add("-sourcepath"); |
ohrstrom@1224 | 124 | cmd.add(srcDir.getPath()); |
ohrstrom@1224 | 125 | cmd.add("-s"); |
ohrstrom@1224 | 126 | cmd.add(destDir.getPath()); |
ohrstrom@1224 | 127 | cmd.addAll(classNames); |
ohrstrom@1224 | 128 | //System.err.println("GenStubs exec " + cmd); |
ohrstrom@1224 | 129 | ProcessBuilder pb = new ProcessBuilder(cmd); |
ohrstrom@1224 | 130 | pb.redirectErrorStream(true); |
ohrstrom@1224 | 131 | try { |
ohrstrom@1224 | 132 | Process p = pb.start(); |
ohrstrom@1224 | 133 | BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); |
ohrstrom@1224 | 134 | try { |
ohrstrom@1224 | 135 | String line; |
ohrstrom@1224 | 136 | while ((line = in.readLine()) != null) |
ohrstrom@1224 | 137 | System.out.println(line); |
ohrstrom@1224 | 138 | } finally { |
ohrstrom@1224 | 139 | in.close(); |
ohrstrom@1224 | 140 | } |
ohrstrom@1224 | 141 | int rc = p.waitFor(); |
ohrstrom@1224 | 142 | if (rc != 0) |
ohrstrom@1224 | 143 | throw new BuildException("genstubs failed"); |
ohrstrom@1224 | 144 | } catch (IOException e) { |
ohrstrom@1224 | 145 | throw new BuildException("genstubs failed", e); |
ohrstrom@1224 | 146 | } catch (InterruptedException e) { |
ohrstrom@1224 | 147 | throw new BuildException("genstubs failed", e); |
ohrstrom@1224 | 148 | } |
ohrstrom@1224 | 149 | } |
ohrstrom@1224 | 150 | } |
ohrstrom@1224 | 151 | |
ohrstrom@1224 | 152 | String[] filter(File srcDir, File destDir, String[] files) { |
ohrstrom@1224 | 153 | List<String> results = new ArrayList<String>(); |
ohrstrom@1224 | 154 | for (String f: files) { |
ohrstrom@1224 | 155 | long srcTime = new File(srcDir, f).lastModified(); |
ohrstrom@1224 | 156 | long destTime = new File(destDir, f).lastModified(); |
ohrstrom@1224 | 157 | if (srcTime > destTime) |
ohrstrom@1224 | 158 | results.add(f); |
ohrstrom@1224 | 159 | } |
ohrstrom@1224 | 160 | return results.toArray(new String[results.size()]); |
ohrstrom@1224 | 161 | } |
ohrstrom@1224 | 162 | } |