ohrstrom@1224: /* jjh@1305: * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. ohrstrom@1224: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohrstrom@1224: * ohrstrom@1224: * This code is free software; you can redistribute it and/or modify it ohrstrom@1224: * under the terms of the GNU General Public License version 2 only, as ohrstrom@1224: * published by the Free Software Foundation. Oracle designates this ohrstrom@1224: * particular file as subject to the "Classpath" exception as provided ohrstrom@1224: * by Oracle in the LICENSE file that accompanied this code. ohrstrom@1224: * ohrstrom@1224: * This code is distributed in the hope that it will be useful, but WITHOUT ohrstrom@1224: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohrstrom@1224: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohrstrom@1224: * version 2 for more details (a copy is included in the LICENSE file that ohrstrom@1224: * accompanied this code). ohrstrom@1224: * ohrstrom@1224: * You should have received a copy of the GNU General Public License version ohrstrom@1224: * 2 along with this work; if not, write to the Free Software Foundation, ohrstrom@1224: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohrstrom@1224: * ohrstrom@1224: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohrstrom@1224: * or visit www.oracle.com if you need additional information or have any ohrstrom@1224: * questions. ohrstrom@1224: */ ohrstrom@1224: ohrstrom@1224: package anttasks; ohrstrom@1224: ohrstrom@1224: import genstubs.GenStubs; ohrstrom@1224: ohrstrom@1224: import java.io.*; ohrstrom@1224: import java.util.*; ohrstrom@1224: ohrstrom@1224: import org.apache.tools.ant.BuildException; ohrstrom@1224: import org.apache.tools.ant.DirectoryScanner; ohrstrom@1224: import org.apache.tools.ant.taskdefs.MatchingTask; ohrstrom@1224: import org.apache.tools.ant.types.Path; ohrstrom@1224: import org.apache.tools.ant.types.Reference; ohrstrom@1224: ohrstrom@1224: /** ohrstrom@1224: * Files are specified with an implicit fileset, using srcdir as a base directory. ohrstrom@1224: * The set of files to be included is specified with an includes attribute or ohrstrom@1224: * nested set. However, unlike a normal fileset, an empty includes attribute ohrstrom@1224: * means "no files" instead of "all files". The Ant task also accepts "fork=true" and ohrstrom@1224: * classpath attribute or nested element to run GenStubs in a separate VM ohrstrom@1224: * with the specified path. This is likely necessary if a JDK 7 parser is required to read the ohrstrom@1224: * JDK 7 input files. ohrstrom@1224: */ ohrstrom@1224: public class GenStubsTask extends MatchingTask { ohrstrom@1224: private File srcDir; ohrstrom@1224: private File destDir; ohrstrom@1224: private boolean fork; ohrstrom@1224: private Path classpath; ohrstrom@1224: private String includes; ohrstrom@1224: ohrstrom@1224: public void setSrcDir(File dir) { ohrstrom@1224: this.srcDir = dir; ohrstrom@1224: } ohrstrom@1224: ohrstrom@1224: public void setDestDir(File dir) { ohrstrom@1224: this.destDir = dir; ohrstrom@1224: } ohrstrom@1224: ohrstrom@1224: public void setFork(boolean v) { ohrstrom@1224: this.fork = v; ohrstrom@1224: } ohrstrom@1224: ohrstrom@1224: public void setClasspath(Path cp) { ohrstrom@1224: if (classpath == null) ohrstrom@1224: classpath = cp; ohrstrom@1224: else ohrstrom@1224: classpath.append(cp); ohrstrom@1224: } ohrstrom@1224: ohrstrom@1224: public Path createClasspath() { ohrstrom@1224: if (classpath == null) { ohrstrom@1224: classpath = new Path(getProject()); ohrstrom@1224: } ohrstrom@1224: return classpath.createPath(); ohrstrom@1224: } ohrstrom@1224: ohrstrom@1224: public void setClasspathRef(Reference r) { ohrstrom@1224: createClasspath().setRefid(r); ohrstrom@1224: } ohrstrom@1224: ohrstrom@1224: public void setIncludes(String includes) { ohrstrom@1224: super.setIncludes(includes); ohrstrom@1224: this.includes = includes; ohrstrom@1224: } ohrstrom@1224: ohrstrom@1224: @Override ohrstrom@1224: public void execute() { ohrstrom@1224: if (includes != null && includes.trim().isEmpty()) ohrstrom@1224: return; ohrstrom@1224: ohrstrom@1224: DirectoryScanner s = getDirectoryScanner(srcDir); ohrstrom@1224: String[] files = s.getIncludedFiles(); ohrstrom@1224: // System.err.println("Ant.execute: srcDir " + srcDir); ohrstrom@1224: // System.err.println("Ant.execute: destDir " + destDir); ohrstrom@1224: // System.err.println("Ant.execute: files " + Arrays.asList(files)); ohrstrom@1224: ohrstrom@1224: files = filter(srcDir, destDir, files); ohrstrom@1224: if (files.length == 0) ohrstrom@1224: return; ohrstrom@1224: System.out.println("Generating " + files.length + " stub files to " + destDir); ohrstrom@1224: ohrstrom@1224: List classNames = new ArrayList(); ohrstrom@1224: for (String file: files) { ohrstrom@1224: classNames.add(file.replaceAll(".java$", "").replace('/', '.')); ohrstrom@1224: } ohrstrom@1224: ohrstrom@1224: if (!fork) { ohrstrom@1224: GenStubs m = new GenStubs(); ohrstrom@1224: boolean ok = m.run(srcDir.getPath(), destDir, classNames); ohrstrom@1224: if (!ok) ohrstrom@1224: throw new BuildException("genstubs failed"); ohrstrom@1224: } else { ohrstrom@1224: List cmd = new ArrayList(); ohrstrom@1224: String java_home = System.getProperty("java.home"); ohrstrom@1224: cmd.add(new File(new File(java_home, "bin"), "java").getPath()); ohrstrom@1224: if (classpath != null) ohrstrom@1224: cmd.add("-Xbootclasspath/p:" + classpath); ohrstrom@1224: cmd.add(GenStubs.class.getName()); ohrstrom@1224: cmd.add("-sourcepath"); ohrstrom@1224: cmd.add(srcDir.getPath()); ohrstrom@1224: cmd.add("-s"); ohrstrom@1224: cmd.add(destDir.getPath()); ohrstrom@1224: cmd.addAll(classNames); ohrstrom@1224: //System.err.println("GenStubs exec " + cmd); ohrstrom@1224: ProcessBuilder pb = new ProcessBuilder(cmd); ohrstrom@1224: pb.redirectErrorStream(true); ohrstrom@1224: try { ohrstrom@1224: Process p = pb.start(); ohrstrom@1224: BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); ohrstrom@1224: try { ohrstrom@1224: String line; ohrstrom@1224: while ((line = in.readLine()) != null) ohrstrom@1224: System.out.println(line); ohrstrom@1224: } finally { ohrstrom@1224: in.close(); ohrstrom@1224: } ohrstrom@1224: int rc = p.waitFor(); ohrstrom@1224: if (rc != 0) ohrstrom@1224: throw new BuildException("genstubs failed"); ohrstrom@1224: } catch (IOException e) { ohrstrom@1224: throw new BuildException("genstubs failed", e); ohrstrom@1224: } catch (InterruptedException e) { ohrstrom@1224: throw new BuildException("genstubs failed", e); ohrstrom@1224: } ohrstrom@1224: } ohrstrom@1224: } ohrstrom@1224: ohrstrom@1224: String[] filter(File srcDir, File destDir, String[] files) { ohrstrom@1224: List results = new ArrayList(); ohrstrom@1224: for (String f: files) { ohrstrom@1224: long srcTime = new File(srcDir, f).lastModified(); ohrstrom@1224: long destTime = new File(destDir, f).lastModified(); ohrstrom@1224: if (srcTime > destTime) ohrstrom@1224: results.add(f); ohrstrom@1224: } ohrstrom@1224: return results.toArray(new String[results.size()]); ohrstrom@1224: } ohrstrom@1224: }