langtools/make/tools/GenStubs/GenStubs.java
author ohair
Tue, 25 May 2010 15:54:51 -0700
changeset 5520 86e4b9a9da40
parent 4703 800e1750ff18
child 9087 e9e44877cd18
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4415
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4703
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
4415
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
     4
 *
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4703
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4415
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4703
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4415
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    10
 *
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    15
 * accompanied this code).
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    16
 *
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4703
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4703
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4703
diff changeset
    23
 * questions.
4415
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    24
 */
4703
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
    25
4415
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    26
import java.io.*;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    27
import java.util.*;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    28
import javax.tools.JavaFileObject;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    29
import javax.tools.StandardJavaFileManager;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    30
import javax.tools.StandardLocation;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    31
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    32
import org.apache.tools.ant.BuildException;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    33
import org.apache.tools.ant.DirectoryScanner;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    34
import org.apache.tools.ant.taskdefs.MatchingTask;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    35
import org.apache.tools.ant.types.Path;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    36
import org.apache.tools.ant.types.Reference;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    37
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    38
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    39
import com.sun.source.tree.CompilationUnitTree;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    40
import com.sun.source.util.JavacTask;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    41
import com.sun.tools.javac.api.JavacTool;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    42
import com.sun.tools.javac.code.Flags;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    43
import com.sun.tools.javac.code.TypeTags;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    44
import com.sun.tools.javac.tree.JCTree;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    45
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
4703
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
    46
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
    47
import com.sun.tools.javac.tree.JCTree.JCIdent;
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
    48
import com.sun.tools.javac.tree.JCTree.JCImport;
4415
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    49
import com.sun.tools.javac.tree.JCTree.JCLiteral;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    50
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    51
import com.sun.tools.javac.tree.JCTree.JCModifiers;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    52
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    53
import com.sun.tools.javac.tree.Pretty;
4703
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
    54
import com.sun.tools.javac.tree.TreeMaker;
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
    55
import com.sun.tools.javac.tree.TreeScanner;
4415
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    56
import com.sun.tools.javac.tree.TreeTranslator;
4703
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
    57
import com.sun.tools.javac.util.Context;
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
    58
import com.sun.tools.javac.util.ListBuffer;
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
    59
import com.sun.tools.javac.util.Name;
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
    60
import javax.tools.JavaFileManager;
4415
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    61
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    62
/**
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    63
 * Generate stub source files by removing implementation details from input files.
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    64
 *
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    65
 * This is a special purpose stub generator, specific to the needs of generating
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    66
 * stub files for JDK 7 API that are needed to compile langtools files that depend
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    67
 * on that API. The stub generator works by removing as much of the API source code
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    68
 * as possible without affecting the public signature, in order to reduce the
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    69
 * transitive closure of the API being referenced. The resulting stubs can be
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    70
 * put on the langtools sourcepath with -implicit:none to compile the langtools
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    71
 * files that depend on the JDK 7 API.
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    72
 *
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    73
 * Usage:
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    74
 *  genstubs -s <outdir> -sourcepath <path> <classnames>
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    75
 *
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    76
 * The specified class names are looked up on the sourcepath, and corresponding
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    77
 * stubs are written to the source output directory.
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    78
 *
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    79
 * Classes are parsed into javac ASTs, then processed with a javac TreeTranslator
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    80
 * to remove implementation details, and written out in the source output directory.
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    81
 * Documentation comments and annotations are removed. Method bodies are removed
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    82
 * and methods are marked native. Private and package-private field definitions
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    83
 * have their initializers replace with 0, 0.0, false, null as appropriate.
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    84
 *
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    85
 * An Ant task, Main$Ant is also provided. Files are specified with an implicit
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    86
 * fileset, using srcdir as a base directory. The set of files to be included
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    87
 * is specified with an includes attribute or nested <includes> set. However,
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    88
 * unlike a normal fileset, an empty includes attribute means "no files" instead
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    89
 * of "all files".  The Ant task also accepts "fork=true" and classpath attribute
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    90
 * or nested <classpath> element to run GenStubs in a separate VM with the specified
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    91
 * path. This is likely necessary if a JDK 7 parser is required to read the
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    92
 * JDK 7 input files.
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    93
 */
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    94
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    95
public class GenStubs {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    96
    static class Fault extends Exception {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    97
        private static final long serialVersionUID = 0;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    98
        Fault(String message) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
    99
            super(message);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   100
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   101
        Fault(String message, Throwable cause) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   102
            super(message);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   103
            initCause(cause);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   104
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   105
    }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   106
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   107
    public static void main(String[] args) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   108
        boolean ok = new GenStubs().run(args);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   109
        if (!ok)
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   110
            System.exit(1);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   111
    }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   112
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   113
    boolean run(String... args) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   114
        File outdir = null;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   115
        String sourcepath = null;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   116
        List<String> classes = new ArrayList<String>();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   117
        for (ListIterator<String> iter = Arrays.asList(args).listIterator(); iter.hasNext(); ) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   118
            String arg = iter.next();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   119
            if (arg.equals("-s") && iter.hasNext())
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   120
                outdir = new File(iter.next());
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   121
            else if (arg.equals("-sourcepath") && iter.hasNext())
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   122
                sourcepath = iter.next();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   123
            else if (arg.startsWith("-"))
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   124
                throw new IllegalArgumentException(arg);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   125
            else {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   126
                classes.add(arg);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   127
                while (iter.hasNext())
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   128
                    classes.add(iter.next());
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   129
            }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   130
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   131
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   132
        return run(sourcepath, outdir, classes);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   133
    }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   134
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   135
    boolean run(String sourcepath, File outdir, List<String> classes) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   136
        //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   137
        if (sourcepath == null)
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   138
            throw new IllegalArgumentException("sourcepath not set");
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   139
        if (outdir == null)
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   140
            throw new IllegalArgumentException("source output dir not set");
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   141
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   142
        JavacTool tool = JavacTool.create();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   143
        StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   144
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   145
        try {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   146
            fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   147
            fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   148
            List<JavaFileObject> files = new ArrayList<JavaFileObject>();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   149
            for (String c: classes) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   150
                JavaFileObject fo = fm.getJavaFileForInput(
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   151
                        StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   152
                if (fo == null)
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   153
                    error("class not found: " + c);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   154
                else
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   155
                    files.add(fo);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   156
            }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   157
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   158
            JavacTask t = tool.getTask(null, fm, null, null, null, files);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   159
            Iterable<? extends CompilationUnitTree> trees = t.parse();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   160
            for (CompilationUnitTree tree: trees) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   161
                makeStub(fm, tree);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   162
            }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   163
        } catch (IOException e) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   164
            error("IO error " + e, e);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   165
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   166
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   167
        return (errors == 0);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   168
    }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   169
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   170
    void makeStub(StandardJavaFileManager fm, CompilationUnitTree tree) throws IOException {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   171
        CompilationUnitTree tree2 = new StubMaker().translate(tree);
4703
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   172
        CompilationUnitTree tree3 = new ImportCleaner(fm).removeRedundantImports(tree2);
4415
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   173
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   174
        String className = fm.inferBinaryName(StandardLocation.SOURCE_PATH, tree.getSourceFile());
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   175
        JavaFileObject fo = fm.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT,
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   176
                className, JavaFileObject.Kind.SOURCE, null);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   177
        // System.err.println("Writing " + className + " to " + fo.getName());
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   178
        Writer out = fo.openWriter();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   179
        try {
4703
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   180
            new Pretty(out, true).printExpr((JCTree) tree3);
4415
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   181
        } finally {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   182
            out.close();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   183
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   184
    }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   185
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   186
    List<File> splitPath(String path) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   187
        List<File> list = new ArrayList<File>();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   188
        for (String p: path.split(File.pathSeparator)) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   189
            if (p.length() > 0)
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   190
                list.add(new File(p));
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   191
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   192
        return list;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   193
    }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   194
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   195
    void error(String message) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   196
        System.err.println(message);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   197
        errors++;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   198
    }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   199
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   200
    void error(String message, Throwable cause) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   201
        error(message);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   202
    }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   203
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   204
    int errors;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   205
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   206
    class StubMaker extends TreeTranslator {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   207
        CompilationUnitTree translate(CompilationUnitTree tree) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   208
            return super.translate((JCCompilationUnit) tree);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   209
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   210
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   211
        /**
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   212
         * compilation units: remove javadoc comments
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   213
         * -- required, in order to remove @deprecated tags, since we
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   214
         * (separately) remove all annotations, including @Deprecated
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   215
         */
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   216
        public void visitTopLevel(JCCompilationUnit tree) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   217
            super.visitTopLevel(tree);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   218
            tree.docComments = Collections.emptyMap();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   219
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   220
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   221
        /**
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   222
         * methods: remove method bodies, make methods native
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   223
         */
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   224
        @Override
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   225
        public void visitMethodDef(JCMethodDecl tree) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   226
            tree.mods = translate(tree.mods);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   227
            tree.restype = translate(tree.restype);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   228
            tree.typarams = translateTypeParams(tree.typarams);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   229
            tree.params = translateVarDefs(tree.params);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   230
            tree.thrown = translate(tree.thrown);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   231
            if (tree.restype != null && tree.body != null) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   232
                tree.mods.flags |= Flags.NATIVE;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   233
                tree.body = null;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   234
            }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   235
            result = tree;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   236
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   237
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   238
        /**
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   239
         * modifiers: remove annotations
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   240
         */
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   241
        @Override
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   242
        public void visitModifiers(JCModifiers tree) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   243
            tree.annotations = com.sun.tools.javac.util.List.nil();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   244
            result = tree;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   245
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   246
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   247
        /**
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   248
         * field definitions: replace initializers with 0, 0.0, false etc
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   249
         * when possible -- i.e. leave public, protected initializers alone
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   250
         */
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   251
        @Override
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   252
        public void visitVarDef(JCVariableDecl tree) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   253
            tree.mods = translate(tree.mods);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   254
            tree.vartype = translate(tree.vartype);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   255
            if (tree.init != null) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   256
                if ((tree.mods.flags & (Flags.PUBLIC | Flags.PROTECTED)) != 0)
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   257
                    tree.init = translate(tree.init);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   258
                else {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   259
                    String t = tree.vartype.toString();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   260
                    if (t.equals("boolean"))
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   261
                        tree.init = new JCLiteral(TypeTags.BOOLEAN, 0) { };
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   262
                    else if (t.equals("byte"))
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   263
                        tree.init = new JCLiteral(TypeTags.BYTE, 0) { };
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   264
                    else if (t.equals("char"))
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   265
                        tree.init = new JCLiteral(TypeTags.CHAR, 0) { };
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   266
                    else if (t.equals("double"))
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   267
                        tree.init = new JCLiteral(TypeTags.DOUBLE, 0.d) { };
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   268
                    else if (t.equals("float"))
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   269
                        tree.init = new JCLiteral(TypeTags.FLOAT, 0.f) { };
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   270
                    else if (t.equals("int"))
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   271
                        tree.init = new JCLiteral(TypeTags.INT, 0) { };
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   272
                    else if (t.equals("long"))
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   273
                        tree.init = new JCLiteral(TypeTags.LONG, 0) { };
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   274
                    else if (t.equals("short"))
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   275
                        tree.init = new JCLiteral(TypeTags.SHORT, 0) { };
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   276
                    else
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   277
                        tree.init = new JCLiteral(TypeTags.BOT, null) { };
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   278
                }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   279
            }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   280
            result = tree;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   281
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   282
    }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   283
4703
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   284
    class ImportCleaner extends TreeScanner {
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   285
        private Set<Name> names = new HashSet<Name>();
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   286
        private TreeMaker m;
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   287
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   288
        ImportCleaner(JavaFileManager fm) {
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   289
            // ImportCleaner itself doesn't require a filemanager, but instantiating
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   290
            // a TreeMaker does, indirectly (via ClassReader, sigh)
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   291
            Context c = new Context();
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   292
            c.put(JavaFileManager.class, fm);
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   293
            m = TreeMaker.instance(c);
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   294
        }
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   295
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   296
        CompilationUnitTree removeRedundantImports(CompilationUnitTree t) {
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   297
            JCCompilationUnit tree = (JCCompilationUnit) t;
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   298
            tree.accept(this);
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   299
            ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   300
            for (JCTree def: tree.defs) {
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   301
                if (def.getTag() == JCTree.IMPORT) {
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   302
                    JCImport imp = (JCImport) def;
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   303
                    if (imp.qualid.getTag() == JCTree.SELECT) {
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   304
                        JCFieldAccess qualid = (JCFieldAccess) imp.qualid;
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   305
                        if (!qualid.name.toString().equals("*")
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   306
                                && !names.contains(qualid.name)) {
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   307
                            continue;
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   308
                        }
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   309
                    }
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   310
                }
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   311
                defs.add(def);
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   312
            }
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   313
            return m.TopLevel(tree.packageAnnotations, tree.pid, defs.toList());
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   314
        }
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   315
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   316
        @Override
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   317
        public void visitImport(JCImport tree) { } // ignore names found in imports
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   318
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   319
        @Override
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   320
        public void visitIdent(JCIdent tree) {
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   321
            names.add(tree.name);
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   322
        }
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   323
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   324
        @Override
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   325
        public void visitSelect(JCFieldAccess tree) {
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   326
            super.visitSelect(tree);
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   327
            names.add(tree.name);
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   328
        }
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   329
    }
800e1750ff18 6909470: langtools stub generator should prune unnecessary imports
jjg
parents: 4415
diff changeset
   330
4415
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   331
    //---------- Ant Invocation ------------------------------------------------
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   332
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   333
    public static class Ant extends MatchingTask {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   334
        private File srcDir;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   335
        private File destDir;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   336
        private boolean fork;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   337
        private Path classpath;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   338
        private String includes;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   339
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   340
        public void setSrcDir(File dir) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   341
            this.srcDir = dir;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   342
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   343
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   344
        public void setDestDir(File dir) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   345
            this.destDir = dir;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   346
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   347
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   348
        public void setFork(boolean v) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   349
            this.fork = v;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   350
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   351
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   352
        public void setClasspath(Path cp) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   353
            if (classpath == null)
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   354
                classpath = cp;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   355
            else
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   356
                classpath.append(cp);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   357
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   358
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   359
        public Path createClasspath() {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   360
            if (classpath == null) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   361
                classpath = new Path(getProject());
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   362
            }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   363
            return classpath.createPath();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   364
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   365
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   366
        public void setClasspathRef(Reference r) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   367
            createClasspath().setRefid(r);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   368
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   369
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   370
        public void setIncludes(String includes) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   371
            super.setIncludes(includes);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   372
            this.includes = includes;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   373
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   374
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   375
        @Override
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   376
        public void execute() {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   377
            if (includes != null && includes.trim().isEmpty())
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   378
                return;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   379
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   380
            DirectoryScanner s = getDirectoryScanner(srcDir);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   381
            String[] files = s.getIncludedFiles();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   382
//            System.err.println("Ant.execute: srcDir " + srcDir);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   383
//            System.err.println("Ant.execute: destDir " + destDir);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   384
//            System.err.println("Ant.execute: files " + Arrays.asList(files));
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   385
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   386
            files = filter(srcDir, destDir, files);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   387
            if (files.length == 0)
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   388
                return;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   389
            System.out.println("Generating " + files.length + " stub files to " + destDir);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   390
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   391
            List<String> classNames = new ArrayList<String>();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   392
            for (String file: files) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   393
                classNames.add(file.replaceAll(".java$", "").replace('/', '.'));
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   394
            }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   395
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   396
            if (!fork) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   397
                GenStubs m = new GenStubs();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   398
                boolean ok = m.run(srcDir.getPath(), destDir, classNames);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   399
                if (!ok)
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   400
                    throw new BuildException("genstubs failed");
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   401
            } else {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   402
                List<String> cmd = new ArrayList<String>();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   403
                String java_home = System.getProperty("java.home");
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   404
                cmd.add(new File(new File(java_home, "bin"), "java").getPath());
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   405
                if (classpath != null)
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   406
                    cmd.add("-Xbootclasspath/p:" + classpath);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   407
                cmd.add(GenStubs.class.getName());
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   408
                cmd.add("-sourcepath");
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   409
                cmd.add(srcDir.getPath());
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   410
                cmd.add("-s");
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   411
                cmd.add(destDir.getPath());
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   412
                cmd.addAll(classNames);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   413
                //System.err.println("GenStubs exec " + cmd);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   414
                ProcessBuilder pb = new ProcessBuilder(cmd);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   415
                pb.redirectErrorStream(true);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   416
                try {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   417
                    Process p = pb.start();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   418
                    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   419
                    try {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   420
                        String line;
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   421
                        while ((line = in.readLine()) != null)
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   422
                            System.out.println(line);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   423
                    } finally {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   424
                        in.close();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   425
                    }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   426
                    int rc = p.waitFor();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   427
                    if (rc != 0)
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   428
                        throw new BuildException("genstubs failed");
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   429
                } catch (IOException e) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   430
                    throw new BuildException("genstubs failed", e);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   431
                } catch (InterruptedException e) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   432
                    throw new BuildException("genstubs failed", e);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   433
                }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   434
            }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   435
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   436
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   437
        String[] filter(File srcDir, File destDir, String[] files) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   438
            List<String> results = new ArrayList<String>();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   439
            for (String f: files) {
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   440
                long srcTime = new File(srcDir, f).lastModified();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   441
                long destTime = new File(destDir, f).lastModified();
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   442
                if (srcTime > destTime)
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   443
                    results.add(f);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   444
            }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   445
            return results.toArray(new String[results.size()]);
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   446
        }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   447
    }
7d4f73717fba 6902337: fix langtools build to allow forward refs into jdk/ repo
jjg
parents:
diff changeset
   448
}