make/tools/GenStubs/GenStubs.java

Mon, 07 Feb 2011 18:10:13 +0000

author
mcimadamore
date
Mon, 07 Feb 2011 18:10:13 +0000
changeset 858
96d4226bdd60
parent 554
9d9f26857129
child 962
0ff2bbd38f10
permissions
-rw-r--r--

7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
Summary: override clash algorithm is not implemented correctly
Reviewed-by: jjg

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

mercurial