6902337: fix langtools build to allow forward refs into jdk/ repo

Fri, 20 Nov 2009 11:18:43 -0800

author
jjg
date
Fri, 20 Nov 2009 11:18:43 -0800
changeset 441
4325b440eb3e
parent 440
243d0be1ba99
child 442
b1508b6affd8

6902337: fix langtools build to allow forward refs into jdk/ repo
Reviewed-by: ohair, mcimadamore

make/Makefile file | annotate | diff | comparison | revisions
make/build.properties file | annotate | diff | comparison | revisions
make/build.xml file | annotate | diff | comparison | revisions
make/tools/GenStubs/GenStubs.java file | annotate | diff | comparison | revisions
     1.1 --- a/make/Makefile	Fri Nov 20 10:15:54 2009 -0800
     1.2 +++ b/make/Makefile	Fri Nov 20 11:18:43 2009 -0800
     1.3 @@ -133,6 +133,23 @@
     1.4    ANT_JAVA_HOME = JAVA_HOME=$(ALT_BOOTDIR)
     1.5  endif
     1.6  
     1.7 +# To facilitate bootstrapping, much of langtools can be compiled with (just)
     1.8 +# a boot JDK. However, some source files need to be compiled against 
     1.9 +# new JDK API. In a bootstrap build, an import JDK may not be available,
    1.10 +# so build.xml can also build against the source files in a jdk repo,
    1.11 +# in which case it will automatically generate stub files for the new JDK API.
    1.12 +ifdef JDK_TOPDIR
    1.13 +  ANT_OPTIONS += -Dimport.jdk=$(JDK_TOPDIR)
    1.14 +else 
    1.15 +  ifdef ALT_JDK_TOPDIR
    1.16 +    ANT_OPTIONS += -Dimport.jdk=$(ALT_JDK_TOPDIR)
    1.17 +  else 
    1.18 +    ifdef ALT_JDK_IMPORT_DIR
    1.19 +      ANT_OPTIONS += -Dimport.jdk=$(ALT_JDK_IMPORT_DIR)
    1.20 +    endif
    1.21 +  endif
    1.22 +endif
    1.23 +
    1.24  ifdef ALT_OUTPUTDIR
    1.25    OUTPUTDIR = $(ALT_OUTPUTDIR)
    1.26    ANT_OPTIONS += -Dbuild.dir=$(ALT_OUTPUTDIR)/build
     2.1 --- a/make/build.properties	Fri Nov 20 10:15:54 2009 -0800
     2.2 +++ b/make/build.properties	Fri Nov 20 11:18:43 2009 -0800
     2.3 @@ -148,6 +148,13 @@
     2.4  
     2.5  #
     2.6  
     2.7 +# The following files require the import JDK to be available
     2.8 +require.import.jdk.files =
     2.9 +
    2.10 +# The following files in the import jdk source directory are required
    2.11 +# in order to compile the files defined in ${require.import.jdk.files}
    2.12 +import.jdk.stub.files =
    2.13 +
    2.14  # The following value is used by the main jtreg target.
    2.15  # An empty value means all tests
    2.16  # Override as desired to run a specific set of tests
     3.1 --- a/make/build.xml	Fri Nov 20 10:15:54 2009 -0800
     3.2 +++ b/make/build.xml	Fri Nov 20 11:18:43 2009 -0800
     3.3 @@ -56,6 +56,7 @@
     3.4      <property name="build.coverage.dir" location="${build.dir}/coverage"/>
     3.5      <property name="build.classes.dir" location="${build.dir}/classes"/>
     3.6      <property name="build.gensrc.dir" location="${build.dir}/gensrc"/>
     3.7 +    <property name="build.genstubs.dir" location="${build.dir}/genstubs"/>
     3.8      <property name="build.javadoc.dir" location="${build.dir}/javadoc"/>
     3.9      <property name="build.jtreg.dir" location="${build.dir}/jtreg"/>
    3.10      <property name="build.toolclasses.dir" location="${build.dir}/toolclasses"/>
    3.11 @@ -93,6 +94,41 @@
    3.12          <isset property="target.java.home"/>
    3.13      </condition>
    3.14  
    3.15 +    <!-- Logic for handling access import jdk classes, if available.
    3.16 +        import.jdk should be unset, or set to jdk home (to use rt.jar)
    3.17 +        or to jdk repo (to use src/share/classes).
    3.18 +        Based on the value, if any, set up default values for javac's sourcepath,
    3.19 +        classpath and bootclasspath. Note: the default values are overridden 
    3.20 +        in the build-bootstrap-classes macro. -->
    3.21 +
    3.22 +    <available property="import.jdk.src.dir" value="${import.jdk}/src/share/classes"
    3.23 +        filepath="${import.jdk}/src/share/classes" file="java/nio/file/Path.java"/>
    3.24 +    <available property="import.jdk.jar" value="${import.jdk}/jre/lib/rt.jar"
    3.25 +        ignoresystemclasses="true"
    3.26 +        classpath="${import.jdk}/jre/lib/rt.jar" classname="java.nio.file.Path"/>
    3.27 +
    3.28 +    <condition property="javac.sourcepath" value="${build.genstubs.dir}" else="">
    3.29 +        <isset property="import.jdk.src.dir"/>
    3.30 +    </condition>
    3.31 +
    3.32 +    <property name="javac.classpath" value=""/>
    3.33 +
    3.34 +    <condition property="javac.bootclasspath.opt"
    3.35 +            value="-Xbootclasspath:${build.classes.dir}:${import.jdk.jar}"
    3.36 +            else="-Xbootclasspath/p:${build.classes.dir}">
    3.37 +        <isset property="import.jdk.jar"/>
    3.38 +    </condition>
    3.39 +
    3.40 +    <condition property="exclude.files" value="" else="${require.import.jdk.files}">
    3.41 +        <isset property="import.jdk"/>
    3.42 +    </condition>
    3.43 +
    3.44 +    <!-- for debugging -->
    3.45 +    <target name="check-import.jdk">
    3.46 +        <echo message="import.jdk: ${import.jdk}"/>
    3.47 +        <echo message="import.jdk.jar: ${import.jdk.jar}"/>
    3.48 +        <echo message="import.jdk.src.dir: ${import.jdk.src.dir}"/>
    3.49 +    </target>
    3.50  
    3.51      <!-- Standard target to build deliverables for JDK build. -->
    3.52  
    3.53 @@ -108,11 +144,15 @@
    3.54          <zip file="${dist.lib.dir}/src.zip" basedir="${src.classes.dir}"/>
    3.55      </target>
    3.56  
    3.57 -    <target name="build-bootstrap-tools" depends="build-bootstrap-javac,build-bootstrap-javadoc,build-bootstrap-doclets,build-bootstrap-javah"/>
    3.58 +    <target name="build-bootstrap-tools"
    3.59 +        depends="build-bootstrap-javac,build-bootstrap-javadoc,build-bootstrap-doclets,build-bootstrap-javah"
    3.60 +    />
    3.61  
    3.62 -    <target name="build-all-tools" depends="build-javac,build-javadoc,build-doclets,build-javah,build-javap,build-apt"/>
    3.63 +    <target name="build-all-tools"
    3.64 +        depends="build-javac,build-javadoc,build-doclets,build-javah,build-javap,build-apt"
    3.65 +    />
    3.66  
    3.67 -    <target name="build-all-classes" depends="build-bootstrap-javac">
    3.68 +    <target name="build-all-classes" depends="build-bootstrap-javac,-create-import-jdk-stubs">
    3.69          <build-classes includes="${javac.includes} ${javadoc.includes} ${doclets.includes} ${javah.includes} ${javap.includes} ${apt.includes}"/>
    3.70      </target>
    3.71  
    3.72 @@ -197,7 +237,7 @@
    3.73          <build-bootstrap-tool    name="javac"/>
    3.74      </target>
    3.75  
    3.76 -    <target name="build-classes-javac" depends="build-bootstrap-javac">
    3.77 +    <target name="build-classes-javac" depends="build-bootstrap-javac,-create-import-jdk-stubs">
    3.78          <build-classes includes="${javac.includes}"/>
    3.79      </target>
    3.80  
    3.81 @@ -379,6 +419,17 @@
    3.82  
    3.83      <target name="apt" depends="build-apt,jtreg-apt,findbugs-apt"/>
    3.84  
    3.85 +    <!-- Create import JDK stubs -->
    3.86 +
    3.87 +    <target name="-create-import-jdk-stubs" depends="-def-genstubs" if="import.jdk.src.dir">
    3.88 +        <mkdir dir="${build.genstubs.dir}"/>
    3.89 +        <genstubs
    3.90 +            srcdir="${import.jdk.src.dir}" destdir="${build.genstubs.dir}"
    3.91 +            includes="${import.jdk.stub.files}"
    3.92 +            fork="true" classpath="${build.toolclasses.dir}:${build.bootstrap.dir}/classes:${ant.home}/lib/ant.jar"
    3.93 +        />
    3.94 +    </target>
    3.95 +
    3.96      <!-- Check targets -->
    3.97  
    3.98      <target name="-check-boot.java.home" depends="-def-check">
    3.99 @@ -446,16 +497,23 @@
   3.100      <target name="-def-build-classes" depends="-def-pcompile">
   3.101          <macrodef name="build-classes">
   3.102              <attribute name="includes"/>
   3.103 -            <attribute name="excludes" default="**/package-info.java"/>
   3.104 +            <attribute name="excludes" default="${exclude.files} **/package-info.java"/>
   3.105              <attribute name="classes.dir" default="${build.classes.dir}"/>
   3.106              <attribute name="gensrc.dir" default="${build.gensrc.dir}"/>
   3.107 -            <attribute name="bootclasspath" default="${build.bootstrap.dir}/classes"/>
   3.108 +            <attribute name="javac.bootclasspath" default="${build.bootstrap.dir}/classes"/>
   3.109 +            <attribute name="bootclasspath.opt" default="${javac.bootclasspath.opt}"/>
   3.110 +            <attribute name="classpath" default="${javac.classpath}"/>
   3.111 +            <attribute name="sourcepath" default="${javac.sourcepath}"/>
   3.112              <attribute name="java.home" default="${boot.java.home}"/>
   3.113              <attribute name="source" default="${javac.source}"/>
   3.114              <attribute name="target" default="${javac.target}"/>
   3.115              <attribute name="release" default="${release}"/>
   3.116              <attribute name="full.version" default="${full.version}"/>
   3.117              <sequential>
   3.118 +                <echo level="verbose" message="build-classes: excludes=@{excludes}"/>
   3.119 +                <echo level="verbose" message="build-classes: bootclasspath.opt=@{bootclasspath.opt}"/>
   3.120 +                <echo level="verbose" message="build-classes: classpath=@{classpath}"/>
   3.121 +                <echo level="verbose" message="build-classes: sourcepath=@{sourcepath}"/>
   3.122                  <mkdir dir="@{gensrc.dir}"/>
   3.123                  <mkdir dir="@{classes.dir}"/>
   3.124                  <pcompile srcdir="${src.classes.dir}"
   3.125 @@ -479,14 +537,17 @@
   3.126                         destdir="@{classes.dir}"
   3.127                         includes="@{includes}"
   3.128                         excludes="@{excludes}"
   3.129 -                       sourcepath=""
   3.130 +                       sourcepath="@{sourcepath}"
   3.131 +                       classpath="@{classpath}"
   3.132                         includeAntRuntime="no"
   3.133                         source="@{source}"
   3.134                         target="@{target}"
   3.135                         debug="${javac.debug}"
   3.136                         debuglevel="${javac.debuglevel}">
   3.137 -                    <compilerarg value="-J-Xbootclasspath/p:@{bootclasspath}"/>
   3.138 -                    <compilerarg value="-Xbootclasspath/p:@{classes.dir}"/>
   3.139 +                    <compilerarg value="-implicit:none"/>
   3.140 +                    <compilerarg value="-Xprefer:source"/>
   3.141 +                    <compilerarg value="-J-Xbootclasspath/p:@{javac.bootclasspath}"/>
   3.142 +                    <compilerarg line="@{bootclasspath.opt}"/>
   3.143                      <compilerarg line="${javac.no.jdk.warnings}"/>
   3.144                      <compilerarg line="${javac.version.opt}"/>
   3.145                      <compilerarg line="${javac.lint.opts}"/>
   3.146 @@ -526,9 +587,12 @@
   3.147                  target="${boot.javac.target}"
   3.148                  gensrc.dir="${build.bootstrap.dir}/gensrc"
   3.149                  classes.dir="${build.bootstrap.dir}/classes"
   3.150 -                bootclasspath=""
   3.151 +                javac.bootclasspath=""
   3.152 +                bootclasspath.opt="-Xbootclasspath/p:${build.bootstrap.dir}/classes"
   3.153 +                sourcepath=""
   3.154                  release="${bootstrap.release}"
   3.155 -                full.version="${bootstrap.full.version}"/>
   3.156 +                full.version="${bootstrap.full.version}"
   3.157 +                excludes="${require.import.jdk.files} **/package-info.java"/>
   3.158          </presetdef>
   3.159      </target>
   3.160  
   3.161 @@ -546,6 +610,20 @@
   3.162                   classpath="${build.toolclasses.dir}/"/>
   3.163      </target>
   3.164  
   3.165 +    <target name="-def-genstubs" depends="build-bootstrap-javac">
   3.166 +        <mkdir dir="${build.toolclasses.dir}"/>
   3.167 +        <javac fork="true"
   3.168 +               source="${boot.javac.source}"
   3.169 +               target="${boot.javac.target}"
   3.170 +               executable="${boot.java.home}/bin/javac"
   3.171 +               srcdir="${make.tools.dir}/GenStubs"
   3.172 +               destdir="${build.toolclasses.dir}/"
   3.173 +               classpath="${build.bootstrap.dir}/classes:${ant.home}/lib/ant.jar"/>
   3.174 +        <taskdef name="genstubs"
   3.175 +                 classname="GenStubs$$Ant"
   3.176 +                 classpath="${build.toolclasses.dir}/"/>
   3.177 +    </target>
   3.178 +
   3.179      <target name="-def-javadoc-tool" depends="-check-target.java.home">
   3.180          <macrodef name="javadoc-tool">
   3.181              <attribute name="name"/>
   3.182 @@ -707,7 +785,7 @@
   3.183  
   3.184      <!-- standard JDK target -->
   3.185      <target name="sanity"
   3.186 -        description="display settings of congiguration values">
   3.187 +        description="display settings of configuration values">
   3.188          <echo level="info">ant.home = ${ant.home}</echo>
   3.189          <echo level="info">boot.java.home = ${boot.java.home}</echo>
   3.190          <echo level="info">target.java.home = ${target.java.home}</echo>
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/make/tools/GenStubs/GenStubs.java	Fri Nov 20 11:18:43 2009 -0800
     4.3 @@ -0,0 +1,392 @@
     4.4 +/*
     4.5 + * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
     4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.7 + *
     4.8 + * This code is free software; you can redistribute it and/or modify it
     4.9 + * under the terms of the GNU General Public License version 2 only, as
    4.10 + * published by the Free Software Foundation.  Sun designates this
    4.11 + * particular file as subject to the "Classpath" exception as provided
    4.12 + * by Sun in the LICENSE file that accompanied this code.
    4.13 + *
    4.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    4.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    4.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    4.17 + * version 2 for more details (a copy is included in the LICENSE file that
    4.18 + * accompanied this code).
    4.19 + *
    4.20 + * You should have received a copy of the GNU General Public License version
    4.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    4.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    4.23 + *
    4.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    4.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    4.26 + * have any questions.
    4.27 + */
    4.28 +import java.io.*;
    4.29 +import java.util.*;
    4.30 +import javax.tools.JavaFileObject;
    4.31 +import javax.tools.StandardJavaFileManager;
    4.32 +import javax.tools.StandardLocation;
    4.33 +
    4.34 +import org.apache.tools.ant.BuildException;
    4.35 +import org.apache.tools.ant.DirectoryScanner;
    4.36 +import org.apache.tools.ant.taskdefs.MatchingTask;
    4.37 +import org.apache.tools.ant.types.Path;
    4.38 +import org.apache.tools.ant.types.Reference;
    4.39 +
    4.40 +
    4.41 +import com.sun.source.tree.CompilationUnitTree;
    4.42 +import com.sun.source.util.JavacTask;
    4.43 +import com.sun.tools.javac.api.JavacTool;
    4.44 +import com.sun.tools.javac.code.Flags;
    4.45 +import com.sun.tools.javac.code.TypeTags;
    4.46 +import com.sun.tools.javac.tree.JCTree;
    4.47 +import com.sun.tools.javac.tree.JCTree.JCBlock;
    4.48 +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
    4.49 +import com.sun.tools.javac.tree.JCTree.JCLiteral;
    4.50 +import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
    4.51 +import com.sun.tools.javac.tree.JCTree.JCModifiers;
    4.52 +import com.sun.tools.javac.tree.JCTree.JCStatement;
    4.53 +import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
    4.54 +import com.sun.tools.javac.tree.Pretty;
    4.55 +import com.sun.tools.javac.tree.TreeTranslator;
    4.56 +
    4.57 +/**
    4.58 + * Generate stub source files by removing implementation details from input files.
    4.59 + *
    4.60 + * This is a special purpose stub generator, specific to the needs of generating
    4.61 + * stub files for JDK 7 API that are needed to compile langtools files that depend
    4.62 + * on that API. The stub generator works by removing as much of the API source code
    4.63 + * as possible without affecting the public signature, in order to reduce the
    4.64 + * transitive closure of the API being referenced. The resulting stubs can be
    4.65 + * put on the langtools sourcepath with -implicit:none to compile the langtools
    4.66 + * files that depend on the JDK 7 API.
    4.67 + *
    4.68 + * Usage:
    4.69 + *  genstubs -s <outdir> -sourcepath <path> <classnames>
    4.70 + *
    4.71 + * The specified class names are looked up on the sourcepath, and corresponding
    4.72 + * stubs are written to the source output directory.
    4.73 + *
    4.74 + * Classes are parsed into javac ASTs, then processed with a javac TreeTranslator
    4.75 + * to remove implementation details, and written out in the source output directory.
    4.76 + * Documentation comments and annotations are removed. Method bodies are removed
    4.77 + * and methods are marked native. Private and package-private field definitions
    4.78 + * have their initializers replace with 0, 0.0, false, null as appropriate.
    4.79 + *
    4.80 + * An Ant task, Main$Ant is also provided. Files are specified with an implicit
    4.81 + * fileset, using srcdir as a base directory. The set of files to be included
    4.82 + * is specified with an includes attribute or nested <includes> set. However,
    4.83 + * unlike a normal fileset, an empty includes attribute means "no files" instead
    4.84 + * of "all files".  The Ant task also accepts "fork=true" and classpath attribute
    4.85 + * or nested <classpath> element to run GenStubs in a separate VM with the specified
    4.86 + * path. This is likely necessary if a JDK 7 parser is required to read the
    4.87 + * JDK 7 input files.
    4.88 + */
    4.89 +
    4.90 +public class GenStubs {
    4.91 +    static class Fault extends Exception {
    4.92 +        private static final long serialVersionUID = 0;
    4.93 +        Fault(String message) {
    4.94 +            super(message);
    4.95 +        }
    4.96 +        Fault(String message, Throwable cause) {
    4.97 +            super(message);
    4.98 +            initCause(cause);
    4.99 +        }
   4.100 +    }
   4.101 +
   4.102 +    public static void main(String[] args) {
   4.103 +        boolean ok = new GenStubs().run(args);
   4.104 +        if (!ok)
   4.105 +            System.exit(1);
   4.106 +    }
   4.107 +
   4.108 +    boolean run(String... args) {
   4.109 +        File outdir = null;
   4.110 +        String sourcepath = null;
   4.111 +        List<String> classes = new ArrayList<String>();
   4.112 +        for (ListIterator<String> iter = Arrays.asList(args).listIterator(); iter.hasNext(); ) {
   4.113 +            String arg = iter.next();
   4.114 +            if (arg.equals("-s") && iter.hasNext())
   4.115 +                outdir = new File(iter.next());
   4.116 +            else if (arg.equals("-sourcepath") && iter.hasNext())
   4.117 +                sourcepath = iter.next();
   4.118 +            else if (arg.startsWith("-"))
   4.119 +                throw new IllegalArgumentException(arg);
   4.120 +            else {
   4.121 +                classes.add(arg);
   4.122 +                while (iter.hasNext())
   4.123 +                    classes.add(iter.next());
   4.124 +            }
   4.125 +        }
   4.126 +
   4.127 +        return run(sourcepath, outdir, classes);
   4.128 +    }
   4.129 +
   4.130 +    boolean run(String sourcepath, File outdir, List<String> classes) {
   4.131 +        //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
   4.132 +        if (sourcepath == null)
   4.133 +            throw new IllegalArgumentException("sourcepath not set");
   4.134 +        if (outdir == null)
   4.135 +            throw new IllegalArgumentException("source output dir not set");
   4.136 +
   4.137 +        JavacTool tool = JavacTool.create();
   4.138 +        StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
   4.139 +
   4.140 +        try {
   4.141 +            fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
   4.142 +            fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
   4.143 +            List<JavaFileObject> files = new ArrayList<JavaFileObject>();
   4.144 +            for (String c: classes) {
   4.145 +                JavaFileObject fo = fm.getJavaFileForInput(
   4.146 +                        StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
   4.147 +                if (fo == null)
   4.148 +                    error("class not found: " + c);
   4.149 +                else
   4.150 +                    files.add(fo);
   4.151 +            }
   4.152 +
   4.153 +            JavacTask t = tool.getTask(null, fm, null, null, null, files);
   4.154 +            Iterable<? extends CompilationUnitTree> trees = t.parse();
   4.155 +            for (CompilationUnitTree tree: trees) {
   4.156 +                makeStub(fm, tree);
   4.157 +            }
   4.158 +        } catch (IOException e) {
   4.159 +            error("IO error " + e, e);
   4.160 +        }
   4.161 +
   4.162 +        return (errors == 0);
   4.163 +    }
   4.164 +
   4.165 +    void makeStub(StandardJavaFileManager fm, CompilationUnitTree tree) throws IOException {
   4.166 +        CompilationUnitTree tree2 = new StubMaker().translate(tree);
   4.167 +
   4.168 +        String className = fm.inferBinaryName(StandardLocation.SOURCE_PATH, tree.getSourceFile());
   4.169 +        JavaFileObject fo = fm.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT,
   4.170 +                className, JavaFileObject.Kind.SOURCE, null);
   4.171 +        // System.err.println("Writing " + className + " to " + fo.getName());
   4.172 +        Writer out = fo.openWriter();
   4.173 +        try {
   4.174 +            new Pretty(out, true).printExpr((JCTree) tree2);
   4.175 +        } finally {
   4.176 +            out.close();
   4.177 +        }
   4.178 +    }
   4.179 +
   4.180 +    List<File> splitPath(String path) {
   4.181 +        List<File> list = new ArrayList<File>();
   4.182 +        for (String p: path.split(File.pathSeparator)) {
   4.183 +            if (p.length() > 0)
   4.184 +                list.add(new File(p));
   4.185 +        }
   4.186 +        return list;
   4.187 +    }
   4.188 +
   4.189 +    void error(String message) {
   4.190 +        System.err.println(message);
   4.191 +        errors++;
   4.192 +    }
   4.193 +
   4.194 +    void error(String message, Throwable cause) {
   4.195 +        error(message);
   4.196 +    }
   4.197 +
   4.198 +    int errors;
   4.199 +
   4.200 +    class StubMaker extends TreeTranslator {
   4.201 +        CompilationUnitTree translate(CompilationUnitTree tree) {
   4.202 +            return super.translate((JCCompilationUnit) tree);
   4.203 +        }
   4.204 +
   4.205 +        /**
   4.206 +         * compilation units: remove javadoc comments
   4.207 +         * -- required, in order to remove @deprecated tags, since we
   4.208 +         * (separately) remove all annotations, including @Deprecated
   4.209 +         */
   4.210 +        public void visitTopLevel(JCCompilationUnit tree) {
   4.211 +            super.visitTopLevel(tree);
   4.212 +            tree.docComments = Collections.emptyMap();
   4.213 +        }
   4.214 +
   4.215 +        /**
   4.216 +         * methods: remove method bodies, make methods native
   4.217 +         */
   4.218 +        @Override
   4.219 +        public void visitMethodDef(JCMethodDecl tree) {
   4.220 +            tree.mods = translate(tree.mods);
   4.221 +            tree.restype = translate(tree.restype);
   4.222 +            tree.typarams = translateTypeParams(tree.typarams);
   4.223 +            tree.params = translateVarDefs(tree.params);
   4.224 +            tree.thrown = translate(tree.thrown);
   4.225 +            if (tree.restype != null && tree.body != null) {
   4.226 +                tree.mods.flags |= Flags.NATIVE;
   4.227 +                tree.body = null;
   4.228 +            }
   4.229 +            result = tree;
   4.230 +        }
   4.231 +
   4.232 +        /**
   4.233 +         * modifiers: remove annotations
   4.234 +         */
   4.235 +        @Override
   4.236 +        public void visitModifiers(JCModifiers tree) {
   4.237 +            tree.annotations = com.sun.tools.javac.util.List.nil();
   4.238 +            result = tree;
   4.239 +        }
   4.240 +
   4.241 +        /**
   4.242 +         * field definitions: replace initializers with 0, 0.0, false etc
   4.243 +         * when possible -- i.e. leave public, protected initializers alone
   4.244 +         */
   4.245 +        @Override
   4.246 +        public void visitVarDef(JCVariableDecl tree) {
   4.247 +            tree.mods = translate(tree.mods);
   4.248 +            tree.vartype = translate(tree.vartype);
   4.249 +            if (tree.init != null) {
   4.250 +                if ((tree.mods.flags & (Flags.PUBLIC | Flags.PROTECTED)) != 0)
   4.251 +                    tree.init = translate(tree.init);
   4.252 +                else {
   4.253 +                    String t = tree.vartype.toString();
   4.254 +                    if (t.equals("boolean"))
   4.255 +                        tree.init = new JCLiteral(TypeTags.BOOLEAN, 0) { };
   4.256 +                    else if (t.equals("byte"))
   4.257 +                        tree.init = new JCLiteral(TypeTags.BYTE, 0) { };
   4.258 +                    else if (t.equals("char"))
   4.259 +                        tree.init = new JCLiteral(TypeTags.CHAR, 0) { };
   4.260 +                    else if (t.equals("double"))
   4.261 +                        tree.init = new JCLiteral(TypeTags.DOUBLE, 0.d) { };
   4.262 +                    else if (t.equals("float"))
   4.263 +                        tree.init = new JCLiteral(TypeTags.FLOAT, 0.f) { };
   4.264 +                    else if (t.equals("int"))
   4.265 +                        tree.init = new JCLiteral(TypeTags.INT, 0) { };
   4.266 +                    else if (t.equals("long"))
   4.267 +                        tree.init = new JCLiteral(TypeTags.LONG, 0) { };
   4.268 +                    else if (t.equals("short"))
   4.269 +                        tree.init = new JCLiteral(TypeTags.SHORT, 0) { };
   4.270 +                    else
   4.271 +                        tree.init = new JCLiteral(TypeTags.BOT, null) { };
   4.272 +                }
   4.273 +            }
   4.274 +            result = tree;
   4.275 +        }
   4.276 +    }
   4.277 +
   4.278 +    //---------- Ant Invocation ------------------------------------------------
   4.279 +
   4.280 +    public static class Ant extends MatchingTask {
   4.281 +        private File srcDir;
   4.282 +        private File destDir;
   4.283 +        private boolean fork;
   4.284 +        private Path classpath;
   4.285 +        private String includes;
   4.286 +
   4.287 +        public void setSrcDir(File dir) {
   4.288 +            this.srcDir = dir;
   4.289 +        }
   4.290 +
   4.291 +        public void setDestDir(File dir) {
   4.292 +            this.destDir = dir;
   4.293 +        }
   4.294 +
   4.295 +        public void setFork(boolean v) {
   4.296 +            this.fork = v;
   4.297 +        }
   4.298 +
   4.299 +        public void setClasspath(Path cp) {
   4.300 +            if (classpath == null)
   4.301 +                classpath = cp;
   4.302 +            else
   4.303 +                classpath.append(cp);
   4.304 +        }
   4.305 +
   4.306 +        public Path createClasspath() {
   4.307 +            if (classpath == null) {
   4.308 +                classpath = new Path(getProject());
   4.309 +            }
   4.310 +            return classpath.createPath();
   4.311 +        }
   4.312 +
   4.313 +        public void setClasspathRef(Reference r) {
   4.314 +            createClasspath().setRefid(r);
   4.315 +        }
   4.316 +
   4.317 +        public void setIncludes(String includes) {
   4.318 +            super.setIncludes(includes);
   4.319 +            this.includes = includes;
   4.320 +        }
   4.321 +
   4.322 +        @Override
   4.323 +        public void execute() {
   4.324 +            if (includes != null && includes.trim().isEmpty())
   4.325 +                return;
   4.326 +
   4.327 +            DirectoryScanner s = getDirectoryScanner(srcDir);
   4.328 +            String[] files = s.getIncludedFiles();
   4.329 +//            System.err.println("Ant.execute: srcDir " + srcDir);
   4.330 +//            System.err.println("Ant.execute: destDir " + destDir);
   4.331 +//            System.err.println("Ant.execute: files " + Arrays.asList(files));
   4.332 +
   4.333 +            files = filter(srcDir, destDir, files);
   4.334 +            if (files.length == 0)
   4.335 +                return;
   4.336 +            System.out.println("Generating " + files.length + " stub files to " + destDir);
   4.337 +
   4.338 +            List<String> classNames = new ArrayList<String>();
   4.339 +            for (String file: files) {
   4.340 +                classNames.add(file.replaceAll(".java$", "").replace('/', '.'));
   4.341 +            }
   4.342 +
   4.343 +            if (!fork) {
   4.344 +                GenStubs m = new GenStubs();
   4.345 +                boolean ok = m.run(srcDir.getPath(), destDir, classNames);
   4.346 +                if (!ok)
   4.347 +                    throw new BuildException("genstubs failed");
   4.348 +            } else {
   4.349 +                List<String> cmd = new ArrayList<String>();
   4.350 +                String java_home = System.getProperty("java.home");
   4.351 +                cmd.add(new File(new File(java_home, "bin"), "java").getPath());
   4.352 +                if (classpath != null)
   4.353 +                    cmd.add("-Xbootclasspath/p:" + classpath);
   4.354 +                cmd.add(GenStubs.class.getName());
   4.355 +                cmd.add("-sourcepath");
   4.356 +                cmd.add(srcDir.getPath());
   4.357 +                cmd.add("-s");
   4.358 +                cmd.add(destDir.getPath());
   4.359 +                cmd.addAll(classNames);
   4.360 +                //System.err.println("GenStubs exec " + cmd);
   4.361 +                ProcessBuilder pb = new ProcessBuilder(cmd);
   4.362 +                pb.redirectErrorStream(true);
   4.363 +                try {
   4.364 +                    Process p = pb.start();
   4.365 +                    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
   4.366 +                    try {
   4.367 +                        String line;
   4.368 +                        while ((line = in.readLine()) != null)
   4.369 +                            System.out.println(line);
   4.370 +                    } finally {
   4.371 +                        in.close();
   4.372 +                    }
   4.373 +                    int rc = p.waitFor();
   4.374 +                    if (rc != 0)
   4.375 +                        throw new BuildException("genstubs failed");
   4.376 +                } catch (IOException e) {
   4.377 +                    throw new BuildException("genstubs failed", e);
   4.378 +                } catch (InterruptedException e) {
   4.379 +                    throw new BuildException("genstubs failed", e);
   4.380 +                }
   4.381 +            }
   4.382 +        }
   4.383 +
   4.384 +        String[] filter(File srcDir, File destDir, String[] files) {
   4.385 +            List<String> results = new ArrayList<String>();
   4.386 +            for (String f: files) {
   4.387 +                long srcTime = new File(srcDir, f).lastModified();
   4.388 +                long destTime = new File(destDir, f).lastModified();
   4.389 +                if (srcTime > destTime)
   4.390 +                    results.add(f);
   4.391 +            }
   4.392 +            return results.toArray(new String[results.size()]);
   4.393 +        }
   4.394 +    }
   4.395 +}

mercurial