Tue, 08 Jan 2019 16:17:56 -0800
Added tag jdk8u202-b26 for changeset c0f526208190
jjg@441 | 1 | /* |
jjg@1753 | 2 | * Copyright (c) 2009, 2013, 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@1753 | 40 | import com.sun.tools.javac.tree.JCTree.JCClassDecl; |
jjg@441 | 41 | import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
jjg@468 | 42 | import com.sun.tools.javac.tree.JCTree.JCFieldAccess; |
jjg@468 | 43 | import com.sun.tools.javac.tree.JCTree.JCIdent; |
jjg@468 | 44 | import com.sun.tools.javac.tree.JCTree.JCImport; |
jjg@441 | 45 | import com.sun.tools.javac.tree.JCTree.JCLiteral; |
jjg@441 | 46 | import com.sun.tools.javac.tree.JCTree.JCMethodDecl; |
jjg@441 | 47 | import com.sun.tools.javac.tree.JCTree.JCModifiers; |
jjg@441 | 48 | import com.sun.tools.javac.tree.JCTree.JCVariableDecl; |
jjg@441 | 49 | import com.sun.tools.javac.tree.Pretty; |
jjg@468 | 50 | import com.sun.tools.javac.tree.TreeMaker; |
jjg@468 | 51 | import com.sun.tools.javac.tree.TreeScanner; |
jjg@441 | 52 | import com.sun.tools.javac.tree.TreeTranslator; |
jjg@468 | 53 | import com.sun.tools.javac.util.Context; |
jjg@468 | 54 | import com.sun.tools.javac.util.ListBuffer; |
jjg@468 | 55 | import com.sun.tools.javac.util.Name; |
jjg@468 | 56 | import javax.tools.JavaFileManager; |
jjg@441 | 57 | |
jjg@441 | 58 | /** |
jjg@441 | 59 | * Generate stub source files by removing implementation details from input files. |
jjg@441 | 60 | * |
jjg@441 | 61 | * This is a special purpose stub generator, specific to the needs of generating |
jjg@441 | 62 | * stub files for JDK 7 API that are needed to compile langtools files that depend |
jjg@441 | 63 | * on that API. The stub generator works by removing as much of the API source code |
jjg@441 | 64 | * as possible without affecting the public signature, in order to reduce the |
jjg@441 | 65 | * transitive closure of the API being referenced. The resulting stubs can be |
jjg@441 | 66 | * put on the langtools sourcepath with -implicit:none to compile the langtools |
jjg@441 | 67 | * files that depend on the JDK 7 API. |
jjg@441 | 68 | * |
jjg@441 | 69 | * Usage: |
jjg@441 | 70 | * genstubs -s <outdir> -sourcepath <path> <classnames> |
jjg@441 | 71 | * |
jjg@441 | 72 | * The specified class names are looked up on the sourcepath, and corresponding |
jjg@441 | 73 | * stubs are written to the source output directory. |
jjg@441 | 74 | * |
jjg@441 | 75 | * Classes are parsed into javac ASTs, then processed with a javac TreeTranslator |
jjg@441 | 76 | * to remove implementation details, and written out in the source output directory. |
jjg@441 | 77 | * Documentation comments and annotations are removed. Method bodies are removed |
jjg@441 | 78 | * and methods are marked native. Private and package-private field definitions |
jjg@441 | 79 | * have their initializers replace with 0, 0.0, false, null as appropriate. |
jjg@441 | 80 | */ |
jjg@441 | 81 | |
jjg@441 | 82 | public class GenStubs { |
jjg@441 | 83 | static class Fault extends Exception { |
jjg@441 | 84 | private static final long serialVersionUID = 0; |
jjg@441 | 85 | Fault(String message) { |
jjg@441 | 86 | super(message); |
jjg@441 | 87 | } |
jjg@441 | 88 | Fault(String message, Throwable cause) { |
jjg@441 | 89 | super(message); |
jjg@441 | 90 | initCause(cause); |
jjg@441 | 91 | } |
jjg@441 | 92 | } |
jjg@441 | 93 | |
jjg@441 | 94 | public static void main(String[] args) { |
jjg@441 | 95 | boolean ok = new GenStubs().run(args); |
jjg@441 | 96 | if (!ok) |
jjg@441 | 97 | System.exit(1); |
jjg@441 | 98 | } |
jjg@441 | 99 | |
ohrstrom@1224 | 100 | public boolean run(String... args) { |
jjg@441 | 101 | File outdir = null; |
jjg@441 | 102 | String sourcepath = null; |
jjg@441 | 103 | List<String> classes = new ArrayList<String>(); |
jjg@441 | 104 | for (ListIterator<String> iter = Arrays.asList(args).listIterator(); iter.hasNext(); ) { |
jjg@441 | 105 | String arg = iter.next(); |
jjg@441 | 106 | if (arg.equals("-s") && iter.hasNext()) |
jjg@441 | 107 | outdir = new File(iter.next()); |
jjg@441 | 108 | else if (arg.equals("-sourcepath") && iter.hasNext()) |
jjg@441 | 109 | sourcepath = iter.next(); |
jjg@441 | 110 | else if (arg.startsWith("-")) |
jjg@441 | 111 | throw new IllegalArgumentException(arg); |
jjg@441 | 112 | else { |
jjg@441 | 113 | classes.add(arg); |
jjg@441 | 114 | while (iter.hasNext()) |
jjg@441 | 115 | classes.add(iter.next()); |
jjg@441 | 116 | } |
jjg@441 | 117 | } |
jjg@441 | 118 | |
jjg@441 | 119 | return run(sourcepath, outdir, classes); |
jjg@441 | 120 | } |
jjg@441 | 121 | |
ohrstrom@1224 | 122 | public boolean run(String sourcepath, File outdir, List<String> classes) { |
jjg@441 | 123 | //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes); |
jjg@441 | 124 | if (sourcepath == null) |
jjg@441 | 125 | throw new IllegalArgumentException("sourcepath not set"); |
jjg@441 | 126 | if (outdir == null) |
jjg@441 | 127 | throw new IllegalArgumentException("source output dir not set"); |
jjg@441 | 128 | |
jjg@441 | 129 | JavacTool tool = JavacTool.create(); |
jjg@441 | 130 | StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); |
jjg@441 | 131 | |
jjg@441 | 132 | try { |
jjg@441 | 133 | fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir)); |
jjg@441 | 134 | fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath)); |
jjg@441 | 135 | List<JavaFileObject> files = new ArrayList<JavaFileObject>(); |
jjg@441 | 136 | for (String c: classes) { |
jjg@441 | 137 | JavaFileObject fo = fm.getJavaFileForInput( |
jjg@441 | 138 | StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE); |
jjg@441 | 139 | if (fo == null) |
jjg@441 | 140 | error("class not found: " + c); |
jjg@441 | 141 | else |
jjg@441 | 142 | files.add(fo); |
jjg@441 | 143 | } |
jjg@441 | 144 | |
jjg@441 | 145 | JavacTask t = tool.getTask(null, fm, null, null, null, files); |
jjg@441 | 146 | Iterable<? extends CompilationUnitTree> trees = t.parse(); |
jjg@441 | 147 | for (CompilationUnitTree tree: trees) { |
jjg@441 | 148 | makeStub(fm, tree); |
jjg@441 | 149 | } |
jjg@441 | 150 | } catch (IOException e) { |
jjg@441 | 151 | error("IO error " + e, e); |
jjg@441 | 152 | } |
jjg@441 | 153 | |
jjg@441 | 154 | return (errors == 0); |
jjg@441 | 155 | } |
jjg@441 | 156 | |
jjg@441 | 157 | void makeStub(StandardJavaFileManager fm, CompilationUnitTree tree) throws IOException { |
jjg@441 | 158 | CompilationUnitTree tree2 = new StubMaker().translate(tree); |
jjg@468 | 159 | CompilationUnitTree tree3 = new ImportCleaner(fm).removeRedundantImports(tree2); |
jjg@441 | 160 | |
jjg@441 | 161 | String className = fm.inferBinaryName(StandardLocation.SOURCE_PATH, tree.getSourceFile()); |
jjg@441 | 162 | JavaFileObject fo = fm.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT, |
jjg@441 | 163 | className, JavaFileObject.Kind.SOURCE, null); |
jjg@441 | 164 | // System.err.println("Writing " + className + " to " + fo.getName()); |
jjg@441 | 165 | Writer out = fo.openWriter(); |
jjg@441 | 166 | try { |
jjg@468 | 167 | new Pretty(out, true).printExpr((JCTree) tree3); |
jjg@441 | 168 | } finally { |
jjg@441 | 169 | out.close(); |
jjg@441 | 170 | } |
jjg@441 | 171 | } |
jjg@441 | 172 | |
jjg@441 | 173 | List<File> splitPath(String path) { |
jjg@441 | 174 | List<File> list = new ArrayList<File>(); |
jjg@441 | 175 | for (String p: path.split(File.pathSeparator)) { |
jjg@441 | 176 | if (p.length() > 0) |
jjg@441 | 177 | list.add(new File(p)); |
jjg@441 | 178 | } |
jjg@441 | 179 | return list; |
jjg@441 | 180 | } |
jjg@441 | 181 | |
jjg@441 | 182 | void error(String message) { |
jjg@441 | 183 | System.err.println(message); |
jjg@441 | 184 | errors++; |
jjg@441 | 185 | } |
jjg@441 | 186 | |
jjg@441 | 187 | void error(String message, Throwable cause) { |
jjg@441 | 188 | error(message); |
jjg@441 | 189 | } |
jjg@441 | 190 | |
jjg@441 | 191 | int errors; |
jjg@441 | 192 | |
jjg@441 | 193 | class StubMaker extends TreeTranslator { |
jjg@441 | 194 | CompilationUnitTree translate(CompilationUnitTree tree) { |
jjg@441 | 195 | return super.translate((JCCompilationUnit) tree); |
jjg@441 | 196 | } |
jjg@441 | 197 | |
jjg@441 | 198 | /** |
jjg@441 | 199 | * compilation units: remove javadoc comments |
jjg@441 | 200 | * -- required, in order to remove @deprecated tags, since we |
jjg@441 | 201 | * (separately) remove all annotations, including @Deprecated |
jjg@441 | 202 | */ |
jjg@441 | 203 | public void visitTopLevel(JCCompilationUnit tree) { |
jjg@441 | 204 | super.visitTopLevel(tree); |
erikj@1286 | 205 | tree.docComments = null; |
jjg@441 | 206 | } |
jjg@441 | 207 | |
jjg@441 | 208 | /** |
jjg@441 | 209 | * methods: remove method bodies, make methods native |
jjg@441 | 210 | */ |
jjg@441 | 211 | @Override |
jjg@1753 | 212 | public void visitClassDef(JCClassDecl tree) { |
jjg@1753 | 213 | long prevClassMods = currClassMods; |
jjg@1753 | 214 | currClassMods = tree.mods.flags; |
jjg@1753 | 215 | try { |
jjg@1753 | 216 | super.visitClassDef(tree);; |
jjg@1753 | 217 | } finally { |
jjg@1753 | 218 | currClassMods = prevClassMods; |
jjg@1753 | 219 | } |
jjg@1753 | 220 | } |
jjg@1753 | 221 | private long currClassMods = 0; |
jjg@1753 | 222 | |
jjg@1753 | 223 | /** |
jjg@1753 | 224 | * methods: remove method bodies, make methods native |
jjg@1753 | 225 | */ |
jjg@1753 | 226 | @Override |
jjg@441 | 227 | public void visitMethodDef(JCMethodDecl tree) { |
jjg@441 | 228 | tree.mods = translate(tree.mods); |
jjg@441 | 229 | tree.restype = translate(tree.restype); |
jjg@441 | 230 | tree.typarams = translateTypeParams(tree.typarams); |
jjg@441 | 231 | tree.params = translateVarDefs(tree.params); |
jjg@441 | 232 | tree.thrown = translate(tree.thrown); |
jjg@1786 | 233 | if (tree.body != null) { |
jjg@1753 | 234 | if ((currClassMods & Flags.INTERFACE) != 0) { |
jjg@1786 | 235 | tree.mods.flags &= ~(Flags.DEFAULT | Flags.STATIC); |
jjg@1753 | 236 | } else { |
jjg@1753 | 237 | tree.mods.flags |= Flags.NATIVE; |
jjg@1753 | 238 | } |
jjg@441 | 239 | tree.body = null; |
jjg@441 | 240 | } |
jjg@441 | 241 | result = tree; |
jjg@441 | 242 | } |
jjg@441 | 243 | |
jjg@441 | 244 | /** |
jjg@441 | 245 | * modifiers: remove annotations |
jjg@441 | 246 | */ |
jjg@441 | 247 | @Override |
jjg@441 | 248 | public void visitModifiers(JCModifiers tree) { |
jjg@441 | 249 | tree.annotations = com.sun.tools.javac.util.List.nil(); |
jjg@441 | 250 | result = tree; |
jjg@441 | 251 | } |
jjg@441 | 252 | |
jjg@441 | 253 | /** |
jjg@441 | 254 | * field definitions: replace initializers with 0, 0.0, false etc |
jjg@441 | 255 | * when possible -- i.e. leave public, protected initializers alone |
jjg@441 | 256 | */ |
jjg@441 | 257 | @Override |
jjg@441 | 258 | public void visitVarDef(JCVariableDecl tree) { |
jjg@441 | 259 | tree.mods = translate(tree.mods); |
jjg@441 | 260 | tree.vartype = translate(tree.vartype); |
jjg@441 | 261 | if (tree.init != null) { |
jjg@441 | 262 | if ((tree.mods.flags & (Flags.PUBLIC | Flags.PROTECTED)) != 0) |
jjg@441 | 263 | tree.init = translate(tree.init); |
jjg@441 | 264 | else { |
jjg@441 | 265 | String t = tree.vartype.toString(); |
jjg@441 | 266 | if (t.equals("boolean")) |
jjg@1379 | 267 | tree.init = new JCLiteral(TypeTag.BOOLEAN, 0) { }; |
jjg@441 | 268 | else if (t.equals("byte")) |
jjg@1379 | 269 | tree.init = new JCLiteral(TypeTag.BYTE, 0) { }; |
jjg@441 | 270 | else if (t.equals("char")) |
jjg@1379 | 271 | tree.init = new JCLiteral(TypeTag.CHAR, 0) { }; |
jjg@441 | 272 | else if (t.equals("double")) |
jjg@1379 | 273 | tree.init = new JCLiteral(TypeTag.DOUBLE, 0.d) { }; |
jjg@441 | 274 | else if (t.equals("float")) |
jjg@1379 | 275 | tree.init = new JCLiteral(TypeTag.FLOAT, 0.f) { }; |
jjg@441 | 276 | else if (t.equals("int")) |
jjg@1379 | 277 | tree.init = new JCLiteral(TypeTag.INT, 0) { }; |
jjg@441 | 278 | else if (t.equals("long")) |
jjg@1379 | 279 | tree.init = new JCLiteral(TypeTag.LONG, 0) { }; |
jjg@441 | 280 | else if (t.equals("short")) |
jjg@1379 | 281 | tree.init = new JCLiteral(TypeTag.SHORT, 0) { }; |
jjg@441 | 282 | else |
jjg@1379 | 283 | tree.init = new JCLiteral(TypeTag.BOT, null) { }; |
jjg@441 | 284 | } |
jjg@441 | 285 | } |
jjg@441 | 286 | result = tree; |
jjg@441 | 287 | } |
jjg@441 | 288 | } |
jjg@441 | 289 | |
jjg@468 | 290 | class ImportCleaner extends TreeScanner { |
jjg@468 | 291 | private Set<Name> names = new HashSet<Name>(); |
jjg@468 | 292 | private TreeMaker m; |
jjg@468 | 293 | |
jjg@468 | 294 | ImportCleaner(JavaFileManager fm) { |
jjg@468 | 295 | // ImportCleaner itself doesn't require a filemanager, but instantiating |
jjg@468 | 296 | // a TreeMaker does, indirectly (via ClassReader, sigh) |
jjg@468 | 297 | Context c = new Context(); |
jjg@468 | 298 | c.put(JavaFileManager.class, fm); |
jjg@468 | 299 | m = TreeMaker.instance(c); |
jjg@468 | 300 | } |
jjg@468 | 301 | |
jjg@468 | 302 | CompilationUnitTree removeRedundantImports(CompilationUnitTree t) { |
jjg@468 | 303 | JCCompilationUnit tree = (JCCompilationUnit) t; |
jjg@468 | 304 | tree.accept(this); |
jjg@468 | 305 | ListBuffer<JCTree> defs = new ListBuffer<JCTree>(); |
jjg@468 | 306 | for (JCTree def: tree.defs) { |
ohrstrom@1224 | 307 | if (def.getTag() == JCTree.Tag.IMPORT) { |
jjg@468 | 308 | JCImport imp = (JCImport) def; |
ohrstrom@1224 | 309 | if (imp.qualid.getTag() == JCTree.Tag.SELECT) { |
jjg@468 | 310 | JCFieldAccess qualid = (JCFieldAccess) imp.qualid; |
jjg@468 | 311 | if (!qualid.name.toString().equals("*") |
jjg@468 | 312 | && !names.contains(qualid.name)) { |
jjg@468 | 313 | continue; |
jjg@468 | 314 | } |
jjg@468 | 315 | } |
jjg@468 | 316 | } |
jjg@468 | 317 | defs.add(def); |
jjg@468 | 318 | } |
jjg@468 | 319 | return m.TopLevel(tree.packageAnnotations, tree.pid, defs.toList()); |
jjg@468 | 320 | } |
jjg@468 | 321 | |
jjg@468 | 322 | @Override |
jjg@468 | 323 | public void visitImport(JCImport tree) { } // ignore names found in imports |
jjg@468 | 324 | |
jjg@468 | 325 | @Override |
jjg@468 | 326 | public void visitIdent(JCIdent tree) { |
jjg@468 | 327 | names.add(tree.name); |
jjg@468 | 328 | } |
jjg@468 | 329 | |
jjg@468 | 330 | @Override |
jjg@468 | 331 | public void visitSelect(JCFieldAccess tree) { |
jjg@468 | 332 | super.visitSelect(tree); |
jjg@468 | 333 | names.add(tree.name); |
jjg@468 | 334 | } |
jjg@468 | 335 | } |
jjg@441 | 336 | } |