make/tools/genstubs/GenStubs.java

Wed, 09 Jan 2013 10:26:58 -0800

author
jjg
date
Wed, 09 Jan 2013 10:26:58 -0800
changeset 1483
d2eb08b3f64f
parent 1379
384f7a4beae7
child 1753
46b9c25f7024
permissions
-rw-r--r--

8005644: set default max errs and max warns
Reviewed-by: darcy

jjg@441 1 /*
jjh@1305 2 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
jjg@441 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@441 4 *
jjg@441 5 * This code is free software; you can redistribute it and/or modify it
jjg@441 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
jjg@441 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@441 10 *
jjg@441 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@441 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@441 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@441 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@441 15 * accompanied this code).
jjg@441 16 *
jjg@441 17 * You should have received a copy of the GNU General Public License version
jjg@441 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@441 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@441 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
jjg@441 24 */
jjg@468 25
ohrstrom@1224 26 package genstubs;
ohrstrom@1224 27
jjg@441 28 import java.io.*;
jjg@441 29 import java.util.*;
jjg@441 30 import javax.tools.JavaFileObject;
jjg@441 31 import javax.tools.StandardJavaFileManager;
jjg@441 32 import javax.tools.StandardLocation;
jjg@441 33
jjg@441 34 import com.sun.source.tree.CompilationUnitTree;
jjg@441 35 import com.sun.source.util.JavacTask;
jjg@441 36 import com.sun.tools.javac.api.JavacTool;
jjg@441 37 import com.sun.tools.javac.code.Flags;
jjg@1379 38 import com.sun.tools.javac.code.TypeTag;
jjg@441 39 import com.sun.tools.javac.tree.JCTree;
jjg@441 40 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
jjg@468 41 import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
jjg@468 42 import com.sun.tools.javac.tree.JCTree.JCIdent;
jjg@468 43 import com.sun.tools.javac.tree.JCTree.JCImport;
jjg@441 44 import com.sun.tools.javac.tree.JCTree.JCLiteral;
jjg@441 45 import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
jjg@441 46 import com.sun.tools.javac.tree.JCTree.JCModifiers;
jjg@441 47 import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
jjg@441 48 import com.sun.tools.javac.tree.Pretty;
jjg@468 49 import com.sun.tools.javac.tree.TreeMaker;
jjg@468 50 import com.sun.tools.javac.tree.TreeScanner;
jjg@441 51 import com.sun.tools.javac.tree.TreeTranslator;
jjg@468 52 import com.sun.tools.javac.util.Context;
jjg@468 53 import com.sun.tools.javac.util.ListBuffer;
jjg@468 54 import com.sun.tools.javac.util.Name;
jjg@468 55 import javax.tools.JavaFileManager;
jjg@441 56
jjg@441 57 /**
jjg@441 58 * Generate stub source files by removing implementation details from input files.
jjg@441 59 *
jjg@441 60 * This is a special purpose stub generator, specific to the needs of generating
jjg@441 61 * stub files for JDK 7 API that are needed to compile langtools files that depend
jjg@441 62 * on that API. The stub generator works by removing as much of the API source code
jjg@441 63 * as possible without affecting the public signature, in order to reduce the
jjg@441 64 * transitive closure of the API being referenced. The resulting stubs can be
jjg@441 65 * put on the langtools sourcepath with -implicit:none to compile the langtools
jjg@441 66 * files that depend on the JDK 7 API.
jjg@441 67 *
jjg@441 68 * Usage:
jjg@441 69 * genstubs -s <outdir> -sourcepath <path> <classnames>
jjg@441 70 *
jjg@441 71 * The specified class names are looked up on the sourcepath, and corresponding
jjg@441 72 * stubs are written to the source output directory.
jjg@441 73 *
jjg@441 74 * Classes are parsed into javac ASTs, then processed with a javac TreeTranslator
jjg@441 75 * to remove implementation details, and written out in the source output directory.
jjg@441 76 * Documentation comments and annotations are removed. Method bodies are removed
jjg@441 77 * and methods are marked native. Private and package-private field definitions
jjg@441 78 * have their initializers replace with 0, 0.0, false, null as appropriate.
jjg@441 79 */
jjg@441 80
jjg@441 81 public class GenStubs {
jjg@441 82 static class Fault extends Exception {
jjg@441 83 private static final long serialVersionUID = 0;
jjg@441 84 Fault(String message) {
jjg@441 85 super(message);
jjg@441 86 }
jjg@441 87 Fault(String message, Throwable cause) {
jjg@441 88 super(message);
jjg@441 89 initCause(cause);
jjg@441 90 }
jjg@441 91 }
jjg@441 92
jjg@441 93 public static void main(String[] args) {
jjg@441 94 boolean ok = new GenStubs().run(args);
jjg@441 95 if (!ok)
jjg@441 96 System.exit(1);
jjg@441 97 }
jjg@441 98
ohrstrom@1224 99 public boolean run(String... args) {
jjg@441 100 File outdir = null;
jjg@441 101 String sourcepath = null;
jjg@441 102 List<String> classes = new ArrayList<String>();
jjg@441 103 for (ListIterator<String> iter = Arrays.asList(args).listIterator(); iter.hasNext(); ) {
jjg@441 104 String arg = iter.next();
jjg@441 105 if (arg.equals("-s") && iter.hasNext())
jjg@441 106 outdir = new File(iter.next());
jjg@441 107 else if (arg.equals("-sourcepath") && iter.hasNext())
jjg@441 108 sourcepath = iter.next();
jjg@441 109 else if (arg.startsWith("-"))
jjg@441 110 throw new IllegalArgumentException(arg);
jjg@441 111 else {
jjg@441 112 classes.add(arg);
jjg@441 113 while (iter.hasNext())
jjg@441 114 classes.add(iter.next());
jjg@441 115 }
jjg@441 116 }
jjg@441 117
jjg@441 118 return run(sourcepath, outdir, classes);
jjg@441 119 }
jjg@441 120
ohrstrom@1224 121 public boolean run(String sourcepath, File outdir, List<String> classes) {
jjg@441 122 //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes);
jjg@441 123 if (sourcepath == null)
jjg@441 124 throw new IllegalArgumentException("sourcepath not set");
jjg@441 125 if (outdir == null)
jjg@441 126 throw new IllegalArgumentException("source output dir not set");
jjg@441 127
jjg@441 128 JavacTool tool = JavacTool.create();
jjg@441 129 StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
jjg@441 130
jjg@441 131 try {
jjg@441 132 fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir));
jjg@441 133 fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath));
jjg@441 134 List<JavaFileObject> files = new ArrayList<JavaFileObject>();
jjg@441 135 for (String c: classes) {
jjg@441 136 JavaFileObject fo = fm.getJavaFileForInput(
jjg@441 137 StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE);
jjg@441 138 if (fo == null)
jjg@441 139 error("class not found: " + c);
jjg@441 140 else
jjg@441 141 files.add(fo);
jjg@441 142 }
jjg@441 143
jjg@441 144 JavacTask t = tool.getTask(null, fm, null, null, null, files);
jjg@441 145 Iterable<? extends CompilationUnitTree> trees = t.parse();
jjg@441 146 for (CompilationUnitTree tree: trees) {
jjg@441 147 makeStub(fm, tree);
jjg@441 148 }
jjg@441 149 } catch (IOException e) {
jjg@441 150 error("IO error " + e, e);
jjg@441 151 }
jjg@441 152
jjg@441 153 return (errors == 0);
jjg@441 154 }
jjg@441 155
jjg@441 156 void makeStub(StandardJavaFileManager fm, CompilationUnitTree tree) throws IOException {
jjg@441 157 CompilationUnitTree tree2 = new StubMaker().translate(tree);
jjg@468 158 CompilationUnitTree tree3 = new ImportCleaner(fm).removeRedundantImports(tree2);
jjg@441 159
jjg@441 160 String className = fm.inferBinaryName(StandardLocation.SOURCE_PATH, tree.getSourceFile());
jjg@441 161 JavaFileObject fo = fm.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT,
jjg@441 162 className, JavaFileObject.Kind.SOURCE, null);
jjg@441 163 // System.err.println("Writing " + className + " to " + fo.getName());
jjg@441 164 Writer out = fo.openWriter();
jjg@441 165 try {
jjg@468 166 new Pretty(out, true).printExpr((JCTree) tree3);
jjg@441 167 } finally {
jjg@441 168 out.close();
jjg@441 169 }
jjg@441 170 }
jjg@441 171
jjg@441 172 List<File> splitPath(String path) {
jjg@441 173 List<File> list = new ArrayList<File>();
jjg@441 174 for (String p: path.split(File.pathSeparator)) {
jjg@441 175 if (p.length() > 0)
jjg@441 176 list.add(new File(p));
jjg@441 177 }
jjg@441 178 return list;
jjg@441 179 }
jjg@441 180
jjg@441 181 void error(String message) {
jjg@441 182 System.err.println(message);
jjg@441 183 errors++;
jjg@441 184 }
jjg@441 185
jjg@441 186 void error(String message, Throwable cause) {
jjg@441 187 error(message);
jjg@441 188 }
jjg@441 189
jjg@441 190 int errors;
jjg@441 191
jjg@441 192 class StubMaker extends TreeTranslator {
jjg@441 193 CompilationUnitTree translate(CompilationUnitTree tree) {
jjg@441 194 return super.translate((JCCompilationUnit) tree);
jjg@441 195 }
jjg@441 196
jjg@441 197 /**
jjg@441 198 * compilation units: remove javadoc comments
jjg@441 199 * -- required, in order to remove @deprecated tags, since we
jjg@441 200 * (separately) remove all annotations, including @Deprecated
jjg@441 201 */
jjg@441 202 public void visitTopLevel(JCCompilationUnit tree) {
jjg@441 203 super.visitTopLevel(tree);
erikj@1286 204 tree.docComments = null;
jjg@441 205 }
jjg@441 206
jjg@441 207 /**
jjg@441 208 * methods: remove method bodies, make methods native
jjg@441 209 */
jjg@441 210 @Override
jjg@441 211 public void visitMethodDef(JCMethodDecl tree) {
jjg@441 212 tree.mods = translate(tree.mods);
jjg@441 213 tree.restype = translate(tree.restype);
jjg@441 214 tree.typarams = translateTypeParams(tree.typarams);
jjg@441 215 tree.params = translateVarDefs(tree.params);
jjg@441 216 tree.thrown = translate(tree.thrown);
jjg@441 217 if (tree.restype != null && tree.body != null) {
jjg@441 218 tree.mods.flags |= Flags.NATIVE;
jjg@441 219 tree.body = null;
jjg@441 220 }
jjg@441 221 result = tree;
jjg@441 222 }
jjg@441 223
jjg@441 224 /**
jjg@441 225 * modifiers: remove annotations
jjg@441 226 */
jjg@441 227 @Override
jjg@441 228 public void visitModifiers(JCModifiers tree) {
jjg@441 229 tree.annotations = com.sun.tools.javac.util.List.nil();
jjg@441 230 result = tree;
jjg@441 231 }
jjg@441 232
jjg@441 233 /**
jjg@441 234 * field definitions: replace initializers with 0, 0.0, false etc
jjg@441 235 * when possible -- i.e. leave public, protected initializers alone
jjg@441 236 */
jjg@441 237 @Override
jjg@441 238 public void visitVarDef(JCVariableDecl tree) {
jjg@441 239 tree.mods = translate(tree.mods);
jjg@441 240 tree.vartype = translate(tree.vartype);
jjg@441 241 if (tree.init != null) {
jjg@441 242 if ((tree.mods.flags & (Flags.PUBLIC | Flags.PROTECTED)) != 0)
jjg@441 243 tree.init = translate(tree.init);
jjg@441 244 else {
jjg@441 245 String t = tree.vartype.toString();
jjg@441 246 if (t.equals("boolean"))
jjg@1379 247 tree.init = new JCLiteral(TypeTag.BOOLEAN, 0) { };
jjg@441 248 else if (t.equals("byte"))
jjg@1379 249 tree.init = new JCLiteral(TypeTag.BYTE, 0) { };
jjg@441 250 else if (t.equals("char"))
jjg@1379 251 tree.init = new JCLiteral(TypeTag.CHAR, 0) { };
jjg@441 252 else if (t.equals("double"))
jjg@1379 253 tree.init = new JCLiteral(TypeTag.DOUBLE, 0.d) { };
jjg@441 254 else if (t.equals("float"))
jjg@1379 255 tree.init = new JCLiteral(TypeTag.FLOAT, 0.f) { };
jjg@441 256 else if (t.equals("int"))
jjg@1379 257 tree.init = new JCLiteral(TypeTag.INT, 0) { };
jjg@441 258 else if (t.equals("long"))
jjg@1379 259 tree.init = new JCLiteral(TypeTag.LONG, 0) { };
jjg@441 260 else if (t.equals("short"))
jjg@1379 261 tree.init = new JCLiteral(TypeTag.SHORT, 0) { };
jjg@441 262 else
jjg@1379 263 tree.init = new JCLiteral(TypeTag.BOT, null) { };
jjg@441 264 }
jjg@441 265 }
jjg@441 266 result = tree;
jjg@441 267 }
jjg@441 268 }
jjg@441 269
jjg@468 270 class ImportCleaner extends TreeScanner {
jjg@468 271 private Set<Name> names = new HashSet<Name>();
jjg@468 272 private TreeMaker m;
jjg@468 273
jjg@468 274 ImportCleaner(JavaFileManager fm) {
jjg@468 275 // ImportCleaner itself doesn't require a filemanager, but instantiating
jjg@468 276 // a TreeMaker does, indirectly (via ClassReader, sigh)
jjg@468 277 Context c = new Context();
jjg@468 278 c.put(JavaFileManager.class, fm);
jjg@468 279 m = TreeMaker.instance(c);
jjg@468 280 }
jjg@468 281
jjg@468 282 CompilationUnitTree removeRedundantImports(CompilationUnitTree t) {
jjg@468 283 JCCompilationUnit tree = (JCCompilationUnit) t;
jjg@468 284 tree.accept(this);
jjg@468 285 ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
jjg@468 286 for (JCTree def: tree.defs) {
ohrstrom@1224 287 if (def.getTag() == JCTree.Tag.IMPORT) {
jjg@468 288 JCImport imp = (JCImport) def;
ohrstrom@1224 289 if (imp.qualid.getTag() == JCTree.Tag.SELECT) {
jjg@468 290 JCFieldAccess qualid = (JCFieldAccess) imp.qualid;
jjg@468 291 if (!qualid.name.toString().equals("*")
jjg@468 292 && !names.contains(qualid.name)) {
jjg@468 293 continue;
jjg@468 294 }
jjg@468 295 }
jjg@468 296 }
jjg@468 297 defs.add(def);
jjg@468 298 }
jjg@468 299 return m.TopLevel(tree.packageAnnotations, tree.pid, defs.toList());
jjg@468 300 }
jjg@468 301
jjg@468 302 @Override
jjg@468 303 public void visitImport(JCImport tree) { } // ignore names found in imports
jjg@468 304
jjg@468 305 @Override
jjg@468 306 public void visitIdent(JCIdent tree) {
jjg@468 307 names.add(tree.name);
jjg@468 308 }
jjg@468 309
jjg@468 310 @Override
jjg@468 311 public void visitSelect(JCFieldAccess tree) {
jjg@468 312 super.visitSelect(tree);
jjg@468 313 names.add(tree.name);
jjg@468 314 }
jjg@468 315 }
jjg@441 316 }

mercurial