Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/make/tools/anttasks/GenStubsTask.java
32285 views
/*1* Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package anttasks;2627import genstubs.GenStubs;2829import java.io.*;30import java.util.*;3132import org.apache.tools.ant.BuildException;33import org.apache.tools.ant.DirectoryScanner;34import org.apache.tools.ant.taskdefs.MatchingTask;35import org.apache.tools.ant.types.Path;36import org.apache.tools.ant.types.Reference;3738/**39* Files are specified with an implicit fileset, using srcdir as a base directory.40* The set of files to be included is specified with an includes attribute or41* nested <includes> set. However, unlike a normal fileset, an empty includes attribute42* means "no files" instead of "all files". The Ant task also accepts "fork=true" and43* classpath attribute or nested <classpath> element to run GenStubs in a separate VM44* with the specified path. This is likely necessary if a JDK 7 parser is required to read the45* JDK 7 input files.46*/47public class GenStubsTask extends MatchingTask {48private File srcDir;49private File destDir;50private boolean fork;51private Path classpath;52private String includes;5354public void setSrcDir(File dir) {55this.srcDir = dir;56}5758public void setDestDir(File dir) {59this.destDir = dir;60}6162public void setFork(boolean v) {63this.fork = v;64}6566public void setClasspath(Path cp) {67if (classpath == null)68classpath = cp;69else70classpath.append(cp);71}7273public Path createClasspath() {74if (classpath == null) {75classpath = new Path(getProject());76}77return classpath.createPath();78}7980public void setClasspathRef(Reference r) {81createClasspath().setRefid(r);82}8384public void setIncludes(String includes) {85super.setIncludes(includes);86this.includes = includes;87}8889@Override90public void execute() {91if (includes != null && includes.trim().isEmpty())92return;9394DirectoryScanner s = getDirectoryScanner(srcDir);95String[] files = s.getIncludedFiles();96// System.err.println("Ant.execute: srcDir " + srcDir);97// System.err.println("Ant.execute: destDir " + destDir);98// System.err.println("Ant.execute: files " + Arrays.asList(files));99100files = filter(srcDir, destDir, files);101if (files.length == 0)102return;103System.out.println("Generating " + files.length + " stub files to " + destDir);104105List<String> classNames = new ArrayList<String>();106for (String file: files) {107classNames.add(file.replaceAll(".java$", "").replace('/', '.'));108}109110if (!fork) {111GenStubs m = new GenStubs();112boolean ok = m.run(srcDir.getPath(), destDir, classNames);113if (!ok)114throw new BuildException("genstubs failed");115} else {116List<String> cmd = new ArrayList<String>();117String java_home = System.getProperty("java.home");118cmd.add(new File(new File(java_home, "bin"), "java").getPath());119if (classpath != null)120cmd.add("-Xbootclasspath/p:" + classpath);121cmd.add(GenStubs.class.getName());122cmd.add("-sourcepath");123cmd.add(srcDir.getPath());124cmd.add("-s");125cmd.add(destDir.getPath());126cmd.addAll(classNames);127//System.err.println("GenStubs exec " + cmd);128ProcessBuilder pb = new ProcessBuilder(cmd);129pb.redirectErrorStream(true);130try {131Process p = pb.start();132BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));133try {134String line;135while ((line = in.readLine()) != null)136System.out.println(line);137} finally {138in.close();139}140int rc = p.waitFor();141if (rc != 0)142throw new BuildException("genstubs failed");143} catch (IOException e) {144throw new BuildException("genstubs failed", e);145} catch (InterruptedException e) {146throw new BuildException("genstubs failed", e);147}148}149}150151String[] filter(File srcDir, File destDir, String[] files) {152List<String> results = new ArrayList<String>();153for (String f: files) {154long srcTime = new File(srcDir, f).lastModified();155long destTime = new File(destDir, f).lastModified();156if (srcTime > destTime)157results.add(f);158}159return results.toArray(new String[results.size()]);160}161}162163164