# HG changeset patch # User ohrstrom # Date 1331122287 -3600 # Node ID ce2780cb121fef292eafdc75b0906f61c230cb31 # Parent b367473ef0f1aaa1aec4f49cb4fccf3f9ff7548a 7150579: Moved ant code into a separate package, anttasks. Summary: To allow langtools to be built without ant, the ant tasks were moved to a separated package. The packages were also renamed to proper lower case package names. Reviewed-by: jjg diff -r b367473ef0f1 -r ce2780cb121f langtools/make/build.xml --- a/langtools/make/build.xml Tue Mar 06 20:27:12 2012 +0000 +++ b/langtools/make/build.xml Wed Mar 07 13:11:27 2012 +0100 @@ -862,7 +862,8 @@ source="${boot.javac.source}" target="${boot.javac.target}" executable="${boot.java.home}/bin/javac" - srcdir="${make.tools.dir}/CompileProperties" + srcdir="${make.tools.dir}" + includes="compileproperties/* anttasks/CompileProperties*" destdir="${build.toolclasses.dir}/" classpath="${ant.core.lib}" bootclasspath="${boot.java.home}/jre/lib/rt.jar" @@ -870,7 +871,7 @@ @@ -880,7 +881,8 @@ source="${boot.javac.source}" target="${boot.javac.target}" executable="${boot.java.home}/bin/javac" - srcdir="${make.tools.dir}/GenStubs" + srcdir="${make.tools.dir}" + includes="genstubs/* anttasks/GenStubs*" destdir="${build.toolclasses.dir}/" classpath="${ant.core.lib}" includeantruntime="false"> @@ -888,7 +890,7 @@ diff -r b367473ef0f1 -r ce2780cb121f langtools/make/netbeans/langtools/build.xml --- a/langtools/make/netbeans/langtools/build.xml Tue Mar 06 20:27:12 2012 +0000 +++ b/langtools/make/netbeans/langtools/build.xml Wed Mar 07 13:11:27 2012 +0100 @@ -261,7 +261,8 @@ - diff -r b367473ef0f1 -r ce2780cb121f langtools/make/tools/CompileProperties/CompileProperties.java --- a/langtools/make/tools/CompileProperties/CompileProperties.java Tue Mar 06 20:27:12 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,402 +0,0 @@ -/* - * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Properties; - -/** Translates a .properties file into a .java file containing the - * definition of a java.util.Properties subclass which can then be - * compiled with javac.

- * - * Usage: java CompileProperties [path to .properties file] [path to .java file to be output] [super class] - * - * Infers the package by looking at the common suffix of the two - * inputs, eliminating "classes" from it. - * - * @author Scott Violet - * @author Kenneth Russell - */ - -public class CompileProperties { - - public static void main(String[] args) { - CompileProperties cp = new CompileProperties(); - boolean ok = cp.run(args); - if ( !ok ) { - System.exit(1); - } - } - - static interface Log { - void info(String msg); - void verbose(String msg); - void error(String msg, Exception e); - } - - private String propfiles[]; - private String outfiles[] ; - private String supers[] ; - private int compileCount = 0; - private boolean quiet = false; - private Log log; - - public void setLog(Log log) { - this.log = log; - } - - public boolean run(String[] args) { - if (log == null) { - log = new Log() { - public void error(String msg, Exception e) { - System.err.println("ERROR: CompileProperties: " + msg); - if ( e != null ) { - System.err.println("EXCEPTION: " + e.toString()); - e.printStackTrace(); - } - } - public void info(String msg) { - System.out.println(msg); - } - public void verbose(String msg) { - if (!quiet) - System.out.println(msg); - } - }; - } - - boolean ok = true; - /* Original usage */ - if (args.length == 2 && args[0].charAt(0) != '-' ) { - ok = createFile(args[0], args[1], "java.util.ListResourceBundle"); - } else if (args.length == 3) { - ok = createFile(args[0], args[1], args[2]); - } else if (args.length == 0) { - usage(log); - ok = false; - } else { - /* New batch usage */ - ok = parseOptions(args); - if ( ok && compileCount == 0 ) { - log.error("options parsed but no files to compile", null); - ok = false; - } - /* Need at least one file. */ - if ( !ok ) { - usage(log); - } else { - /* Process files */ - for ( int i = 0; i < compileCount && ok ; i++ ) { - ok = createFile(propfiles[i], outfiles[i], supers[i]); - } - } - } - return ok; - } - - private boolean parseOptions(String args[]) { - boolean ok = true; - if ( compileCount > 0 ) { - String new_propfiles[] = new String[compileCount + args.length]; - String new_outfiles[] = new String[compileCount + args.length]; - String new_supers[] = new String[compileCount + args.length]; - System.arraycopy(propfiles, 0, new_propfiles, 0, compileCount); - System.arraycopy(outfiles, 0, new_outfiles, 0, compileCount); - System.arraycopy(supers, 0, new_supers, 0, compileCount); - propfiles = new_propfiles; - outfiles = new_outfiles; - supers = new_supers; - } else { - propfiles = new String[args.length]; - outfiles = new String[args.length]; - supers = new String[args.length]; - } - - for ( int i = 0; i < args.length ; i++ ) { - if ( "-compile".equals(args[i]) && i+3 < args.length ) { - propfiles[compileCount] = args[++i]; - outfiles[compileCount] = args[++i]; - supers[compileCount] = args[++i]; - compileCount++; - } else if ( "-optionsfile".equals(args[i]) && i+1 < args.length ) { - String filename = args[++i]; - FileInputStream finput = null; - byte contents[] = null; - try { - finput = new FileInputStream(filename); - int byteCount = finput.available(); - if ( byteCount <= 0 ) { - log.error("The -optionsfile file is empty", null); - ok = false; - } else { - contents = new byte[byteCount]; - int bytesRead = finput.read(contents); - if ( byteCount != bytesRead ) { - log.error("Cannot read all of -optionsfile file", null); - ok = false; - } - } - } catch ( IOException e ) { - log.error("cannot open " + filename, e); - ok = false; - } - if ( finput != null ) { - try { - finput.close(); - } catch ( IOException e ) { - ok = false; - log.error("cannot close " + filename, e); - } - } - if ( ok = true && contents != null ) { - String tokens[] = (new String(contents)).split("\\s+"); - if ( tokens.length > 0 ) { - ok = parseOptions(tokens); - } - } - if ( !ok ) { - break; - } - } else if ( "-quiet".equals(args[i]) ) { - quiet = true; - } else { - log.error("argument error", null); - ok = false; - } - } - return ok; - } - - private boolean createFile(String propertiesPath, String outputPath, - String superClass) { - boolean ok = true; - log.verbose("parsing: " + propertiesPath); - Properties p = new Properties(); - try { - p.load(new FileInputStream(propertiesPath)); - } catch ( FileNotFoundException e ) { - ok = false; - log.error("Cannot find file " + propertiesPath, e); - } catch ( IOException e ) { - ok = false; - log.error("IO error on file " + propertiesPath, e); - } - if ( ok ) { - String packageName = inferPackageName(propertiesPath, outputPath); - log.verbose("inferred package name: " + packageName); - List sortedKeys = new ArrayList(); - for ( Object key : p.keySet() ) { - sortedKeys.add((String)key); - } - Collections.sort(sortedKeys); - Iterator keys = sortedKeys.iterator(); - - StringBuffer data = new StringBuffer(); - - while (keys.hasNext()) { - String key = keys.next(); - data.append(" { \"" + escape(key) + "\", \"" + - escape((String)p.get(key)) + "\" },\n"); - } - - // Get class name from java filename, not the properties filename. - // (zh_TW properties might be used to create zh_HK files) - File file = new File(outputPath); - String name = file.getName(); - int dotIndex = name.lastIndexOf('.'); - String className; - if (dotIndex == -1) { - className = name; - } else { - className = name.substring(0, dotIndex); - } - - String packageString = ""; - if (packageName != null && !packageName.equals("")) { - packageString = "package " + packageName + ";\n\n"; - } - - Writer writer = null; - try { - writer = new BufferedWriter( - new OutputStreamWriter(new FileOutputStream(outputPath), "8859_1")); - MessageFormat format = new MessageFormat(FORMAT); - writer.write(format.format(new Object[] { packageString, className, superClass, data })); - } catch ( IOException e ) { - ok = false; - log.error("IO error writing to file " + outputPath, e); - } - if ( writer != null ) { - try { - writer.flush(); - } catch ( IOException e ) { - ok = false; - log.error("IO error flush " + outputPath, e); - } - try { - writer.close(); - } catch ( IOException e ) { - ok = false; - log.error("IO error close " + outputPath, e); - } - } - log.verbose("wrote: " + outputPath); - } - return ok; - } - - private static void usage(Log log) { - log.info("usage:"); - log.info(" java CompileProperties path_to_properties_file path_to_java_output_file [super_class]"); - log.info(" -OR-"); - log.info(" java CompileProperties {-compile path_to_properties_file path_to_java_output_file super_class} -or- -optionsfile filename"); - log.info(""); - log.info("Example:"); - log.info(" java CompileProperties -compile test.properties test.java java.util.ListResourceBundle"); - log.info(" java CompileProperties -optionsfile option_file"); - log.info("option_file contains: -compile test.properties test.java java.util.ListResourceBundle"); - } - - private static String escape(String theString) { - // This is taken from Properties.saveConvert with changes for Java strings - int len = theString.length(); - StringBuffer outBuffer = new StringBuffer(len*2); - - for(int x=0; x 0x007e)) { - outBuffer.append('\\'); - outBuffer.append('u'); - outBuffer.append(toHex((aChar >> 12) & 0xF)); - outBuffer.append(toHex((aChar >> 8) & 0xF)); - outBuffer.append(toHex((aChar >> 4) & 0xF)); - outBuffer.append(toHex( aChar & 0xF)); - } else { - if (specialSaveChars.indexOf(aChar) != -1) { - outBuffer.append('\\'); - } - outBuffer.append(aChar); - } - } - } - return outBuffer.toString(); - } - - private static String inferPackageName(String inputPath, String outputPath) { - // Normalize file names - inputPath = new File(inputPath).getPath(); - outputPath = new File(outputPath).getPath(); - // Split into components - String sep; - if (File.separatorChar == '\\') { - sep = "\\\\"; - } else { - sep = File.separator; - } - String[] inputs = inputPath.split(sep); - String[] outputs = outputPath.split(sep); - // Match common names, eliminating first "classes" entry from - // each if present - int inStart = 0; - int inEnd = inputs.length - 2; - int outEnd = outputs.length - 2; - int i = inEnd; - int j = outEnd; - while (i >= 0 && j >= 0) { - if (!inputs[i].equals(outputs[j]) || - (inputs[i].equals("gensrc") && inputs[j].equals("gensrc"))) { - ++i; - ++j; - break; - } - --i; - --j; - } - String result; - if (i < 0 || j < 0 || i >= inEnd || j >= outEnd) { - result = ""; - } else { - if (inputs[i].equals("classes") && outputs[j].equals("classes")) { - ++i; - } - inStart = i; - StringBuffer buf = new StringBuffer(); - for (i = inStart; i <= inEnd; i++) { - buf.append(inputs[i]); - if (i < inEnd) { - buf.append('.'); - } - } - result = buf.toString(); - } - return result; - } - - private static final String FORMAT = - "{0}" + - "public final class {1} extends {2} '{'\n" + - " protected final Object[][] getContents() '{'\n" + - " return new Object[][] '{'\n" + - "{3}" + - " };\n" + - " }\n" + - "}\n"; - - // This comes from Properties - private static char toHex(int nibble) { - return hexDigit[(nibble & 0xF)]; - } - - // This comes from Properties - private static final char[] hexDigit = { - '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' - }; - - // Note: different from that in Properties - private static final String specialSaveChars = "\""; -} diff -r b367473ef0f1 -r ce2780cb121f langtools/make/tools/CompileProperties/CompilePropertiesTask.java --- a/langtools/make/tools/CompileProperties/CompilePropertiesTask.java Tue Mar 06 20:27:12 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,97 +0,0 @@ -/* - * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -import java.io.File; -import java.util.ArrayList; -import java.util.List; - -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.DirectoryScanner; -import org.apache.tools.ant.Project; -import org.apache.tools.ant.taskdefs.MatchingTask; - -public class CompilePropertiesTask extends MatchingTask { - public void setSrcDir(File srcDir) { - this.srcDir = srcDir; - } - - public void setDestDir(File destDir) { - this.destDir = destDir; - } - - public void setSuperclass(String superclass) { - this.superclass = superclass; - } - - @Override - public void execute() { - CompileProperties.Log log = new CompileProperties.Log() { - public void error(String msg, Exception e) { - log(msg, Project.MSG_ERR); - } - public void info(String msg) { - log(msg, Project.MSG_INFO); - } - public void verbose(String msg) { - log(msg, Project.MSG_VERBOSE); - } - }; - List mainOpts = new ArrayList(); - int count = 0; - DirectoryScanner s = getDirectoryScanner(srcDir); - for (String path: s.getIncludedFiles()) { - if (path.endsWith(".properties")) { - String destPath = - path.substring(0, path.length() - ".properties".length()) + - ".java"; - File srcFile = new File(srcDir, path); - File destFile = new File(destDir, destPath); - // Arguably, the comparison in the next line should be ">", not ">=" - // but that assumes the resolution of the last modified time is fine - // grained enough; in practice, it is better to use ">=". - if (destFile.exists() && destFile.lastModified() >= srcFile.lastModified()) - continue; - destFile.getParentFile().mkdirs(); - mainOpts.add("-compile"); - mainOpts.add(srcFile.getPath()); - mainOpts.add(destFile.getPath()); - mainOpts.add(superclass); - count++; - } - } - if (mainOpts.size() > 0) { - log("Generating " + count + " resource files to " + destDir, Project.MSG_INFO); - CompileProperties cp = new CompileProperties(); - cp.setLog(log); - boolean ok = cp.run(mainOpts.toArray(new String[mainOpts.size()])); - if (!ok) - throw new BuildException("CompileProperties failed."); - } - } - - private File srcDir; - private File destDir; - private String superclass = "java.util.ListResourceBundle"; -} diff -r b367473ef0f1 -r ce2780cb121f langtools/make/tools/GenStubs/GenStubs.java --- a/langtools/make/tools/GenStubs/GenStubs.java Tue Mar 06 20:27:12 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,448 +0,0 @@ -/* - * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -import java.io.*; -import java.util.*; -import javax.tools.JavaFileObject; -import javax.tools.StandardJavaFileManager; -import javax.tools.StandardLocation; - -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.DirectoryScanner; -import org.apache.tools.ant.taskdefs.MatchingTask; -import org.apache.tools.ant.types.Path; -import org.apache.tools.ant.types.Reference; - - -import com.sun.source.tree.CompilationUnitTree; -import com.sun.source.util.JavacTask; -import com.sun.tools.javac.api.JavacTool; -import com.sun.tools.javac.code.Flags; -import com.sun.tools.javac.code.TypeTags; -import com.sun.tools.javac.tree.JCTree; -import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; -import com.sun.tools.javac.tree.JCTree.JCFieldAccess; -import com.sun.tools.javac.tree.JCTree.JCIdent; -import com.sun.tools.javac.tree.JCTree.JCImport; -import com.sun.tools.javac.tree.JCTree.JCLiteral; -import com.sun.tools.javac.tree.JCTree.JCMethodDecl; -import com.sun.tools.javac.tree.JCTree.JCModifiers; -import com.sun.tools.javac.tree.JCTree.JCVariableDecl; -import com.sun.tools.javac.tree.Pretty; -import com.sun.tools.javac.tree.TreeMaker; -import com.sun.tools.javac.tree.TreeScanner; -import com.sun.tools.javac.tree.TreeTranslator; -import com.sun.tools.javac.util.Context; -import com.sun.tools.javac.util.ListBuffer; -import com.sun.tools.javac.util.Name; -import javax.tools.JavaFileManager; - -/** - * Generate stub source files by removing implementation details from input files. - * - * This is a special purpose stub generator, specific to the needs of generating - * stub files for JDK 7 API that are needed to compile langtools files that depend - * on that API. The stub generator works by removing as much of the API source code - * as possible without affecting the public signature, in order to reduce the - * transitive closure of the API being referenced. The resulting stubs can be - * put on the langtools sourcepath with -implicit:none to compile the langtools - * files that depend on the JDK 7 API. - * - * Usage: - * genstubs -s -sourcepath - * - * The specified class names are looked up on the sourcepath, and corresponding - * stubs are written to the source output directory. - * - * Classes are parsed into javac ASTs, then processed with a javac TreeTranslator - * to remove implementation details, and written out in the source output directory. - * Documentation comments and annotations are removed. Method bodies are removed - * and methods are marked native. Private and package-private field definitions - * have their initializers replace with 0, 0.0, false, null as appropriate. - * - * An Ant task, Main$Ant is also provided. Files are specified with an implicit - * fileset, using srcdir as a base directory. The set of files to be included - * is specified with an includes attribute or nested set. However, - * unlike a normal fileset, an empty includes attribute means "no files" instead - * of "all files". The Ant task also accepts "fork=true" and classpath attribute - * or nested element to run GenStubs in a separate VM with the specified - * path. This is likely necessary if a JDK 7 parser is required to read the - * JDK 7 input files. - */ - -public class GenStubs { - static class Fault extends Exception { - private static final long serialVersionUID = 0; - Fault(String message) { - super(message); - } - Fault(String message, Throwable cause) { - super(message); - initCause(cause); - } - } - - public static void main(String[] args) { - boolean ok = new GenStubs().run(args); - if (!ok) - System.exit(1); - } - - boolean run(String... args) { - File outdir = null; - String sourcepath = null; - List classes = new ArrayList(); - for (ListIterator iter = Arrays.asList(args).listIterator(); iter.hasNext(); ) { - String arg = iter.next(); - if (arg.equals("-s") && iter.hasNext()) - outdir = new File(iter.next()); - else if (arg.equals("-sourcepath") && iter.hasNext()) - sourcepath = iter.next(); - else if (arg.startsWith("-")) - throw new IllegalArgumentException(arg); - else { - classes.add(arg); - while (iter.hasNext()) - classes.add(iter.next()); - } - } - - return run(sourcepath, outdir, classes); - } - - boolean run(String sourcepath, File outdir, List classes) { - //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes); - if (sourcepath == null) - throw new IllegalArgumentException("sourcepath not set"); - if (outdir == null) - throw new IllegalArgumentException("source output dir not set"); - - JavacTool tool = JavacTool.create(); - StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); - - try { - fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir)); - fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath)); - List files = new ArrayList(); - for (String c: classes) { - JavaFileObject fo = fm.getJavaFileForInput( - StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE); - if (fo == null) - error("class not found: " + c); - else - files.add(fo); - } - - JavacTask t = tool.getTask(null, fm, null, null, null, files); - Iterable trees = t.parse(); - for (CompilationUnitTree tree: trees) { - makeStub(fm, tree); - } - } catch (IOException e) { - error("IO error " + e, e); - } - - return (errors == 0); - } - - void makeStub(StandardJavaFileManager fm, CompilationUnitTree tree) throws IOException { - CompilationUnitTree tree2 = new StubMaker().translate(tree); - CompilationUnitTree tree3 = new ImportCleaner(fm).removeRedundantImports(tree2); - - String className = fm.inferBinaryName(StandardLocation.SOURCE_PATH, tree.getSourceFile()); - JavaFileObject fo = fm.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT, - className, JavaFileObject.Kind.SOURCE, null); - // System.err.println("Writing " + className + " to " + fo.getName()); - Writer out = fo.openWriter(); - try { - new Pretty(out, true).printExpr((JCTree) tree3); - } finally { - out.close(); - } - } - - List splitPath(String path) { - List list = new ArrayList(); - for (String p: path.split(File.pathSeparator)) { - if (p.length() > 0) - list.add(new File(p)); - } - return list; - } - - void error(String message) { - System.err.println(message); - errors++; - } - - void error(String message, Throwable cause) { - error(message); - } - - int errors; - - class StubMaker extends TreeTranslator { - CompilationUnitTree translate(CompilationUnitTree tree) { - return super.translate((JCCompilationUnit) tree); - } - - /** - * compilation units: remove javadoc comments - * -- required, in order to remove @deprecated tags, since we - * (separately) remove all annotations, including @Deprecated - */ - public void visitTopLevel(JCCompilationUnit tree) { - super.visitTopLevel(tree); - tree.docComments = Collections.emptyMap(); - } - - /** - * methods: remove method bodies, make methods native - */ - @Override - public void visitMethodDef(JCMethodDecl tree) { - tree.mods = translate(tree.mods); - tree.restype = translate(tree.restype); - tree.typarams = translateTypeParams(tree.typarams); - tree.params = translateVarDefs(tree.params); - tree.thrown = translate(tree.thrown); - if (tree.restype != null && tree.body != null) { - tree.mods.flags |= Flags.NATIVE; - tree.body = null; - } - result = tree; - } - - /** - * modifiers: remove annotations - */ - @Override - public void visitModifiers(JCModifiers tree) { - tree.annotations = com.sun.tools.javac.util.List.nil(); - result = tree; - } - - /** - * field definitions: replace initializers with 0, 0.0, false etc - * when possible -- i.e. leave public, protected initializers alone - */ - @Override - public void visitVarDef(JCVariableDecl tree) { - tree.mods = translate(tree.mods); - tree.vartype = translate(tree.vartype); - if (tree.init != null) { - if ((tree.mods.flags & (Flags.PUBLIC | Flags.PROTECTED)) != 0) - tree.init = translate(tree.init); - else { - String t = tree.vartype.toString(); - if (t.equals("boolean")) - tree.init = new JCLiteral(TypeTags.BOOLEAN, 0) { }; - else if (t.equals("byte")) - tree.init = new JCLiteral(TypeTags.BYTE, 0) { }; - else if (t.equals("char")) - tree.init = new JCLiteral(TypeTags.CHAR, 0) { }; - else if (t.equals("double")) - tree.init = new JCLiteral(TypeTags.DOUBLE, 0.d) { }; - else if (t.equals("float")) - tree.init = new JCLiteral(TypeTags.FLOAT, 0.f) { }; - else if (t.equals("int")) - tree.init = new JCLiteral(TypeTags.INT, 0) { }; - else if (t.equals("long")) - tree.init = new JCLiteral(TypeTags.LONG, 0) { }; - else if (t.equals("short")) - tree.init = new JCLiteral(TypeTags.SHORT, 0) { }; - else - tree.init = new JCLiteral(TypeTags.BOT, null) { }; - } - } - result = tree; - } - } - - class ImportCleaner extends TreeScanner { - private Set names = new HashSet(); - private TreeMaker m; - - ImportCleaner(JavaFileManager fm) { - // ImportCleaner itself doesn't require a filemanager, but instantiating - // a TreeMaker does, indirectly (via ClassReader, sigh) - Context c = new Context(); - c.put(JavaFileManager.class, fm); - m = TreeMaker.instance(c); - } - - CompilationUnitTree removeRedundantImports(CompilationUnitTree t) { - JCCompilationUnit tree = (JCCompilationUnit) t; - tree.accept(this); - ListBuffer defs = new ListBuffer(); - for (JCTree def: tree.defs) { - if (def.getTag() == JCTree.IMPORT) { - JCImport imp = (JCImport) def; - if (imp.qualid.getTag() == JCTree.SELECT) { - JCFieldAccess qualid = (JCFieldAccess) imp.qualid; - if (!qualid.name.toString().equals("*") - && !names.contains(qualid.name)) { - continue; - } - } - } - defs.add(def); - } - return m.TopLevel(tree.packageAnnotations, tree.pid, defs.toList()); - } - - @Override - public void visitImport(JCImport tree) { } // ignore names found in imports - - @Override - public void visitIdent(JCIdent tree) { - names.add(tree.name); - } - - @Override - public void visitSelect(JCFieldAccess tree) { - super.visitSelect(tree); - names.add(tree.name); - } - } - - //---------- Ant Invocation ------------------------------------------------ - - public static class Ant extends MatchingTask { - private File srcDir; - private File destDir; - private boolean fork; - private Path classpath; - private String includes; - - public void setSrcDir(File dir) { - this.srcDir = dir; - } - - public void setDestDir(File dir) { - this.destDir = dir; - } - - public void setFork(boolean v) { - this.fork = v; - } - - public void setClasspath(Path cp) { - if (classpath == null) - classpath = cp; - else - classpath.append(cp); - } - - public Path createClasspath() { - if (classpath == null) { - classpath = new Path(getProject()); - } - return classpath.createPath(); - } - - public void setClasspathRef(Reference r) { - createClasspath().setRefid(r); - } - - public void setIncludes(String includes) { - super.setIncludes(includes); - this.includes = includes; - } - - @Override - public void execute() { - if (includes != null && includes.trim().isEmpty()) - return; - - DirectoryScanner s = getDirectoryScanner(srcDir); - String[] files = s.getIncludedFiles(); -// System.err.println("Ant.execute: srcDir " + srcDir); -// System.err.println("Ant.execute: destDir " + destDir); -// System.err.println("Ant.execute: files " + Arrays.asList(files)); - - files = filter(srcDir, destDir, files); - if (files.length == 0) - return; - System.out.println("Generating " + files.length + " stub files to " + destDir); - - List classNames = new ArrayList(); - for (String file: files) { - classNames.add(file.replaceAll(".java$", "").replace('/', '.')); - } - - if (!fork) { - GenStubs m = new GenStubs(); - boolean ok = m.run(srcDir.getPath(), destDir, classNames); - if (!ok) - throw new BuildException("genstubs failed"); - } else { - List cmd = new ArrayList(); - String java_home = System.getProperty("java.home"); - cmd.add(new File(new File(java_home, "bin"), "java").getPath()); - if (classpath != null) - cmd.add("-Xbootclasspath/p:" + classpath); - cmd.add(GenStubs.class.getName()); - cmd.add("-sourcepath"); - cmd.add(srcDir.getPath()); - cmd.add("-s"); - cmd.add(destDir.getPath()); - cmd.addAll(classNames); - //System.err.println("GenStubs exec " + cmd); - ProcessBuilder pb = new ProcessBuilder(cmd); - pb.redirectErrorStream(true); - try { - Process p = pb.start(); - BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); - try { - String line; - while ((line = in.readLine()) != null) - System.out.println(line); - } finally { - in.close(); - } - int rc = p.waitFor(); - if (rc != 0) - throw new BuildException("genstubs failed"); - } catch (IOException e) { - throw new BuildException("genstubs failed", e); - } catch (InterruptedException e) { - throw new BuildException("genstubs failed", e); - } - } - } - - String[] filter(File srcDir, File destDir, String[] files) { - List results = new ArrayList(); - for (String f: files) { - long srcTime = new File(srcDir, f).lastModified(); - long destTime = new File(destDir, f).lastModified(); - if (srcTime > destTime) - results.add(f); - } - return results.toArray(new String[results.size()]); - } - } -} diff -r b367473ef0f1 -r ce2780cb121f langtools/make/tools/SelectTool/SelectToolTask.java --- a/langtools/make/tools/SelectTool/SelectToolTask.java Tue Mar 06 20:27:12 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,284 +0,0 @@ -/* - * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.FocusEvent; -import java.awt.event.FocusListener; -import java.awt.event.ItemEvent; -import java.awt.event.ItemListener; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.Reader; -import java.io.Writer; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Properties; -import javax.swing.JButton; -import javax.swing.JCheckBox; -import javax.swing.JComboBox; -import javax.swing.JDialog; -import javax.swing.JLabel; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JTextField; - -import javax.swing.SwingUtilities; -import org.apache.tools.ant.BuildException; -import org.apache.tools.ant.Project; -import org.apache.tools.ant.Task; - -/** - * Task to allow the user to control langtools tools built when using NetBeans. - * - * There are two primary modes. - * 1) Property mode. In this mode, property names are provided to get values - * that may be specified by the user, either directly in a GUI dialog, or - * read from a properties file. If the GUI dialog is invoked, values may - * optionally be set for future use. - * 2) Setup mode. In this mode, no property names are provided, and the GUI - * is invoked to allow the user to set or reset values for use in property mode. - */ -public class SelectToolTask extends Task { - /** - * Set the location of the private properties file used to keep the retain - * user preferences for this repository. - */ - public void setPropertyFile(File propertyFile) { - this.propertyFile = propertyFile; - } - - /** - * Set the name of the property which will be set to the name of the - * selected tool, if any. If no tool is selected, the property will - * remain unset. - */ - public void setToolProperty(String toolProperty) { - this.toolProperty = toolProperty; - } - - /** - * Set the name of the property which will be set to the execution args of the - * selected tool, if any. The args default to an empty string. - */ - public void setArgsProperty(String argsProperty) { - this.argsProperty = argsProperty; - } - - /** - * Specify whether or not to pop up a dialog if the user has not specified - * a default value for a property. - */ - public void setAskIfUnset(boolean askIfUnset) { - this.askIfUnset = askIfUnset; - } - - @Override - public void execute() { - Project p = getProject(); - - Properties props = readProperties(propertyFile); - toolName = props.getProperty("tool.name"); - if (toolName != null) { - toolArgs = props.getProperty(toolName + ".args", ""); - } - - if (toolProperty == null || - askIfUnset && (toolName == null - || (argsProperty != null && toolArgs == null))) { - showGUI(props); - } - - // finally, return required values, if any - if (toolProperty != null && !(toolName == null || toolName.equals(""))) { - p.setProperty(toolProperty, toolName); - - if (argsProperty != null && toolArgs != null) - p.setProperty(argsProperty, toolArgs); - } - } - - void showGUI(Properties fileProps) { - Properties guiProps = new Properties(fileProps); - JOptionPane p = createPane(guiProps); - p.createDialog("Select Tool").setVisible(true); - - toolName = (String) toolChoice.getSelectedItem(); - toolArgs = argsField.getText(); - - if (defaultCheck.isSelected()) { - if (toolName.equals("")) { - fileProps.remove("tool.name"); - } else { - fileProps.put("tool.name", toolName); - fileProps.put(toolName + ".args", toolArgs); - } - writeProperties(propertyFile, fileProps); - } - } - - JOptionPane createPane(final Properties props) { - JPanel body = new JPanel(new GridBagLayout()); - GridBagConstraints lc = new GridBagConstraints(); - lc.insets.right = 10; - lc.insets.bottom = 3; - GridBagConstraints fc = new GridBagConstraints(); - fc.anchor = GridBagConstraints.WEST; - fc.gridx = 1; - fc.gridwidth = GridBagConstraints.REMAINDER; - fc.insets.bottom = 3; - - JLabel toolLabel = new JLabel("Tool:"); - body.add(toolLabel, lc); - String[] toolChoices = { "apt", "javac", "javadoc", "javah", "javap" }; - if (true || toolProperty == null) { - // include empty value in setup mode - List l = new ArrayList(Arrays.asList(toolChoices)); - l.add(0, ""); - toolChoices = l.toArray(new String[l.size()]); - } - toolChoice = new JComboBox(toolChoices); - if (toolName != null) - toolChoice.setSelectedItem(toolName); - toolChoice.addItemListener(new ItemListener() { - public void itemStateChanged(ItemEvent e) { - String tn = (String) e.getItem(); - argsField.setText(getDefaultArgsForTool(props, tn)); - if (toolProperty != null) - okButton.setEnabled(!tn.equals("")); - } - }); - body.add(toolChoice, fc); - - argsField = new JTextField(getDefaultArgsForTool(props, toolName), 40); - if (toolProperty == null || argsProperty != null) { - JLabel argsLabel = new JLabel("Args:"); - body.add(argsLabel, lc); - body.add(argsField, fc); - argsField.addFocusListener(new FocusListener() { - public void focusGained(FocusEvent e) { - } - public void focusLost(FocusEvent e) { - String toolName = (String) toolChoice.getSelectedItem(); - if (toolName.length() > 0) - props.put(toolName + ".args", argsField.getText()); - } - }); - } - - defaultCheck = new JCheckBox("Set as default"); - if (toolProperty == null) - defaultCheck.setSelected(true); - else - body.add(defaultCheck, fc); - - final JOptionPane p = new JOptionPane(body); - okButton = new JButton("OK"); - okButton.setEnabled(toolProperty == null || (toolName != null && !toolName.equals(""))); - okButton.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { - JDialog d = (JDialog) SwingUtilities.getAncestorOfClass(JDialog.class, p); - d.setVisible(false); - } - }); - p.setOptions(new Object[] { okButton }); - - return p; - } - - Properties readProperties(File file) { - Properties p = new Properties(); - if (file != null && file.exists()) { - Reader in = null; - try { - in = new BufferedReader(new FileReader(file)); - p.load(in); - in.close(); - } catch (IOException e) { - throw new BuildException("error reading property file", e); - } finally { - if (in != null) { - try { - in.close(); - } catch (IOException e) { - throw new BuildException("cannot close property file", e); - } - } - } - } - return p; - } - - void writeProperties(File file, Properties p) { - if (file != null) { - Writer out = null; - try { - File dir = file.getParentFile(); - if (dir != null && !dir.exists()) - dir.mkdirs(); - out = new BufferedWriter(new FileWriter(file)); - p.store(out, "langtools properties"); - out.close(); - } catch (IOException e) { - throw new BuildException("error writing property file", e); - } finally { - if (out != null) { - try { - out.close(); - } catch (IOException e) { - throw new BuildException("cannot close property file", e); - } - } - } - } - } - - String getDefaultArgsForTool(Properties props, String tn) { - return (tn == null || tn.equals("")) ? "" : props.getProperty(tn + ".args", ""); - } - - // Ant task parameters - private boolean askIfUnset; - private String toolProperty; - private String argsProperty; - private File propertyFile; - - // GUI components - private JComboBox toolChoice; - private JTextField argsField; - private JCheckBox defaultCheck; - private JButton okButton; - - // Result values for the client - private String toolName; - private String toolArgs; -} diff -r b367473ef0f1 -r ce2780cb121f langtools/make/tools/anttasks/CompilePropertiesTask.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/make/tools/anttasks/CompilePropertiesTask.java Wed Mar 07 13:11:27 2012 +0100 @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package anttasks; + +import compileproperties.CompileProperties; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.DirectoryScanner; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.taskdefs.MatchingTask; + +public class CompilePropertiesTask extends MatchingTask { + public void setSrcDir(File srcDir) { + this.srcDir = srcDir; + } + + public void setDestDir(File destDir) { + this.destDir = destDir; + } + + public void setSuperclass(String superclass) { + this.superclass = superclass; + } + + @Override + public void execute() { + CompileProperties.Log log = new CompileProperties.Log() { + public void error(String msg, Exception e) { + log(msg, Project.MSG_ERR); + } + public void info(String msg) { + log(msg, Project.MSG_INFO); + } + public void verbose(String msg) { + log(msg, Project.MSG_VERBOSE); + } + }; + List mainOpts = new ArrayList(); + int count = 0; + DirectoryScanner s = getDirectoryScanner(srcDir); + for (String path: s.getIncludedFiles()) { + if (path.endsWith(".properties")) { + String destPath = + path.substring(0, path.length() - ".properties".length()) + + ".java"; + File srcFile = new File(srcDir, path); + File destFile = new File(destDir, destPath); + // Arguably, the comparison in the next line should be ">", not ">=" + // but that assumes the resolution of the last modified time is fine + // grained enough; in practice, it is better to use ">=". + if (destFile.exists() && destFile.lastModified() >= srcFile.lastModified()) + continue; + destFile.getParentFile().mkdirs(); + mainOpts.add("-compile"); + mainOpts.add(srcFile.getPath()); + mainOpts.add(destFile.getPath()); + mainOpts.add(superclass); + count++; + } + } + if (mainOpts.size() > 0) { + log("Generating " + count + " resource files to " + destDir, Project.MSG_INFO); + CompileProperties cp = new CompileProperties(); + cp.setLog(log); + boolean ok = cp.run(mainOpts.toArray(new String[mainOpts.size()])); + if (!ok) + throw new BuildException("CompileProperties failed."); + } + } + + private File srcDir; + private File destDir; + private String superclass = "java.util.ListResourceBundle"; +} diff -r b367473ef0f1 -r ce2780cb121f langtools/make/tools/anttasks/GenStubsTask.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/make/tools/anttasks/GenStubsTask.java Wed Mar 07 13:11:27 2012 +0100 @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package anttasks; + +import genstubs.GenStubs; + +import java.io.*; +import java.util.*; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.DirectoryScanner; +import org.apache.tools.ant.taskdefs.MatchingTask; +import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.types.Reference; + +/** + * Files are specified with an implicit fileset, using srcdir as a base directory. + * The set of files to be included is specified with an includes attribute or + * nested set. However, unlike a normal fileset, an empty includes attribute + * means "no files" instead of "all files". The Ant task also accepts "fork=true" and + * classpath attribute or nested element to run GenStubs in a separate VM + * with the specified path. This is likely necessary if a JDK 7 parser is required to read the + * JDK 7 input files. + */ +public class GenStubsTask extends MatchingTask { + private File srcDir; + private File destDir; + private boolean fork; + private Path classpath; + private String includes; + + public void setSrcDir(File dir) { + this.srcDir = dir; + } + + public void setDestDir(File dir) { + this.destDir = dir; + } + + public void setFork(boolean v) { + this.fork = v; + } + + public void setClasspath(Path cp) { + if (classpath == null) + classpath = cp; + else + classpath.append(cp); + } + + public Path createClasspath() { + if (classpath == null) { + classpath = new Path(getProject()); + } + return classpath.createPath(); + } + + public void setClasspathRef(Reference r) { + createClasspath().setRefid(r); + } + + public void setIncludes(String includes) { + super.setIncludes(includes); + this.includes = includes; + } + + @Override + public void execute() { + if (includes != null && includes.trim().isEmpty()) + return; + + DirectoryScanner s = getDirectoryScanner(srcDir); + String[] files = s.getIncludedFiles(); +// System.err.println("Ant.execute: srcDir " + srcDir); +// System.err.println("Ant.execute: destDir " + destDir); +// System.err.println("Ant.execute: files " + Arrays.asList(files)); + + files = filter(srcDir, destDir, files); + if (files.length == 0) + return; + System.out.println("Generating " + files.length + " stub files to " + destDir); + + List classNames = new ArrayList(); + for (String file: files) { + classNames.add(file.replaceAll(".java$", "").replace('/', '.')); + } + + if (!fork) { + GenStubs m = new GenStubs(); + boolean ok = m.run(srcDir.getPath(), destDir, classNames); + if (!ok) + throw new BuildException("genstubs failed"); + } else { + List cmd = new ArrayList(); + String java_home = System.getProperty("java.home"); + cmd.add(new File(new File(java_home, "bin"), "java").getPath()); + if (classpath != null) + cmd.add("-Xbootclasspath/p:" + classpath); + cmd.add(GenStubs.class.getName()); + cmd.add("-sourcepath"); + cmd.add(srcDir.getPath()); + cmd.add("-s"); + cmd.add(destDir.getPath()); + cmd.addAll(classNames); + //System.err.println("GenStubs exec " + cmd); + ProcessBuilder pb = new ProcessBuilder(cmd); + pb.redirectErrorStream(true); + try { + Process p = pb.start(); + BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); + try { + String line; + while ((line = in.readLine()) != null) + System.out.println(line); + } finally { + in.close(); + } + int rc = p.waitFor(); + if (rc != 0) + throw new BuildException("genstubs failed"); + } catch (IOException e) { + throw new BuildException("genstubs failed", e); + } catch (InterruptedException e) { + throw new BuildException("genstubs failed", e); + } + } + } + + String[] filter(File srcDir, File destDir, String[] files) { + List results = new ArrayList(); + for (String f: files) { + long srcTime = new File(srcDir, f).lastModified(); + long destTime = new File(destDir, f).lastModified(); + if (srcTime > destTime) + results.add(f); + } + return results.toArray(new String[results.size()]); + } +} diff -r b367473ef0f1 -r ce2780cb121f langtools/make/tools/anttasks/SelectToolTask.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/make/tools/anttasks/SelectToolTask.java Wed Mar 07 13:11:27 2012 +0100 @@ -0,0 +1,286 @@ +/* + * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package anttasks; + +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextField; + +import javax.swing.SwingUtilities; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.Task; + +/** + * Task to allow the user to control langtools tools built when using NetBeans. + * + * There are two primary modes. + * 1) Property mode. In this mode, property names are provided to get values + * that may be specified by the user, either directly in a GUI dialog, or + * read from a properties file. If the GUI dialog is invoked, values may + * optionally be set for future use. + * 2) Setup mode. In this mode, no property names are provided, and the GUI + * is invoked to allow the user to set or reset values for use in property mode. + */ +public class SelectToolTask extends Task { + /** + * Set the location of the private properties file used to keep the retain + * user preferences for this repository. + */ + public void setPropertyFile(File propertyFile) { + this.propertyFile = propertyFile; + } + + /** + * Set the name of the property which will be set to the name of the + * selected tool, if any. If no tool is selected, the property will + * remain unset. + */ + public void setToolProperty(String toolProperty) { + this.toolProperty = toolProperty; + } + + /** + * Set the name of the property which will be set to the execution args of the + * selected tool, if any. The args default to an empty string. + */ + public void setArgsProperty(String argsProperty) { + this.argsProperty = argsProperty; + } + + /** + * Specify whether or not to pop up a dialog if the user has not specified + * a default value for a property. + */ + public void setAskIfUnset(boolean askIfUnset) { + this.askIfUnset = askIfUnset; + } + + @Override + public void execute() { + Project p = getProject(); + + Properties props = readProperties(propertyFile); + toolName = props.getProperty("tool.name"); + if (toolName != null) { + toolArgs = props.getProperty(toolName + ".args", ""); + } + + if (toolProperty == null || + askIfUnset && (toolName == null + || (argsProperty != null && toolArgs == null))) { + showGUI(props); + } + + // finally, return required values, if any + if (toolProperty != null && !(toolName == null || toolName.equals(""))) { + p.setProperty(toolProperty, toolName); + + if (argsProperty != null && toolArgs != null) + p.setProperty(argsProperty, toolArgs); + } + } + + void showGUI(Properties fileProps) { + Properties guiProps = new Properties(fileProps); + JOptionPane p = createPane(guiProps); + p.createDialog("Select Tool").setVisible(true); + + toolName = (String) toolChoice.getSelectedItem(); + toolArgs = argsField.getText(); + + if (defaultCheck.isSelected()) { + if (toolName.equals("")) { + fileProps.remove("tool.name"); + } else { + fileProps.put("tool.name", toolName); + fileProps.put(toolName + ".args", toolArgs); + } + writeProperties(propertyFile, fileProps); + } + } + + JOptionPane createPane(final Properties props) { + JPanel body = new JPanel(new GridBagLayout()); + GridBagConstraints lc = new GridBagConstraints(); + lc.insets.right = 10; + lc.insets.bottom = 3; + GridBagConstraints fc = new GridBagConstraints(); + fc.anchor = GridBagConstraints.WEST; + fc.gridx = 1; + fc.gridwidth = GridBagConstraints.REMAINDER; + fc.insets.bottom = 3; + + JLabel toolLabel = new JLabel("Tool:"); + body.add(toolLabel, lc); + String[] toolChoices = { "apt", "javac", "javadoc", "javah", "javap" }; + if (true || toolProperty == null) { + // include empty value in setup mode + List l = new ArrayList(Arrays.asList(toolChoices)); + l.add(0, ""); + toolChoices = l.toArray(new String[l.size()]); + } + toolChoice = new JComboBox(toolChoices); + if (toolName != null) + toolChoice.setSelectedItem(toolName); + toolChoice.addItemListener(new ItemListener() { + public void itemStateChanged(ItemEvent e) { + String tn = (String) e.getItem(); + argsField.setText(getDefaultArgsForTool(props, tn)); + if (toolProperty != null) + okButton.setEnabled(!tn.equals("")); + } + }); + body.add(toolChoice, fc); + + argsField = new JTextField(getDefaultArgsForTool(props, toolName), 40); + if (toolProperty == null || argsProperty != null) { + JLabel argsLabel = new JLabel("Args:"); + body.add(argsLabel, lc); + body.add(argsField, fc); + argsField.addFocusListener(new FocusListener() { + public void focusGained(FocusEvent e) { + } + public void focusLost(FocusEvent e) { + String toolName = (String) toolChoice.getSelectedItem(); + if (toolName.length() > 0) + props.put(toolName + ".args", argsField.getText()); + } + }); + } + + defaultCheck = new JCheckBox("Set as default"); + if (toolProperty == null) + defaultCheck.setSelected(true); + else + body.add(defaultCheck, fc); + + final JOptionPane p = new JOptionPane(body); + okButton = new JButton("OK"); + okButton.setEnabled(toolProperty == null || (toolName != null && !toolName.equals(""))); + okButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JDialog d = (JDialog) SwingUtilities.getAncestorOfClass(JDialog.class, p); + d.setVisible(false); + } + }); + p.setOptions(new Object[] { okButton }); + + return p; + } + + Properties readProperties(File file) { + Properties p = new Properties(); + if (file != null && file.exists()) { + Reader in = null; + try { + in = new BufferedReader(new FileReader(file)); + p.load(in); + in.close(); + } catch (IOException e) { + throw new BuildException("error reading property file", e); + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + throw new BuildException("cannot close property file", e); + } + } + } + } + return p; + } + + void writeProperties(File file, Properties p) { + if (file != null) { + Writer out = null; + try { + File dir = file.getParentFile(); + if (dir != null && !dir.exists()) + dir.mkdirs(); + out = new BufferedWriter(new FileWriter(file)); + p.store(out, "langtools properties"); + out.close(); + } catch (IOException e) { + throw new BuildException("error writing property file", e); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + throw new BuildException("cannot close property file", e); + } + } + } + } + } + + String getDefaultArgsForTool(Properties props, String tn) { + return (tn == null || tn.equals("")) ? "" : props.getProperty(tn + ".args", ""); + } + + // Ant task parameters + private boolean askIfUnset; + private String toolProperty; + private String argsProperty; + private File propertyFile; + + // GUI components + private JComboBox toolChoice; + private JTextField argsField; + private JCheckBox defaultCheck; + private JButton okButton; + + // Result values for the client + private String toolName; + private String toolArgs; +} diff -r b367473ef0f1 -r ce2780cb121f langtools/make/tools/compileproperties/CompileProperties.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/make/tools/compileproperties/CompileProperties.java Wed Mar 07 13:11:27 2012 +0100 @@ -0,0 +1,404 @@ +/* + * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package compileproperties; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Properties; + +/** Translates a .properties file into a .java file containing the + * definition of a java.util.Properties subclass which can then be + * compiled with javac.

+ * + * Usage: java CompileProperties [path to .properties file] [path to .java file to be output] [super class] + * + * Infers the package by looking at the common suffix of the two + * inputs, eliminating "classes" from it. + * + * @author Scott Violet + * @author Kenneth Russell + */ + +public class CompileProperties { + + public static void main(String[] args) { + CompileProperties cp = new CompileProperties(); + boolean ok = cp.run(args); + if ( !ok ) { + System.exit(1); + } + } + + public static interface Log { + void info(String msg); + void verbose(String msg); + void error(String msg, Exception e); + } + + private String propfiles[]; + private String outfiles[] ; + private String supers[] ; + private int compileCount = 0; + private boolean quiet = false; + public Log log; + + public void setLog(Log log) { + this.log = log; + } + + public boolean run(String[] args) { + if (log == null) { + log = new Log() { + public void error(String msg, Exception e) { + System.err.println("ERROR: CompileProperties: " + msg); + if ( e != null ) { + System.err.println("EXCEPTION: " + e.toString()); + e.printStackTrace(); + } + } + public void info(String msg) { + System.out.println(msg); + } + public void verbose(String msg) { + if (!quiet) + System.out.println(msg); + } + }; + } + + boolean ok = true; + /* Original usage */ + if (args.length == 2 && args[0].charAt(0) != '-' ) { + ok = createFile(args[0], args[1], "java.util.ListResourceBundle"); + } else if (args.length == 3) { + ok = createFile(args[0], args[1], args[2]); + } else if (args.length == 0) { + usage(log); + ok = false; + } else { + /* New batch usage */ + ok = parseOptions(args); + if ( ok && compileCount == 0 ) { + log.error("options parsed but no files to compile", null); + ok = false; + } + /* Need at least one file. */ + if ( !ok ) { + usage(log); + } else { + /* Process files */ + for ( int i = 0; i < compileCount && ok ; i++ ) { + ok = createFile(propfiles[i], outfiles[i], supers[i]); + } + } + } + return ok; + } + + private boolean parseOptions(String args[]) { + boolean ok = true; + if ( compileCount > 0 ) { + String new_propfiles[] = new String[compileCount + args.length]; + String new_outfiles[] = new String[compileCount + args.length]; + String new_supers[] = new String[compileCount + args.length]; + System.arraycopy(propfiles, 0, new_propfiles, 0, compileCount); + System.arraycopy(outfiles, 0, new_outfiles, 0, compileCount); + System.arraycopy(supers, 0, new_supers, 0, compileCount); + propfiles = new_propfiles; + outfiles = new_outfiles; + supers = new_supers; + } else { + propfiles = new String[args.length]; + outfiles = new String[args.length]; + supers = new String[args.length]; + } + + for ( int i = 0; i < args.length ; i++ ) { + if ( "-compile".equals(args[i]) && i+3 < args.length ) { + propfiles[compileCount] = args[++i]; + outfiles[compileCount] = args[++i]; + supers[compileCount] = args[++i]; + compileCount++; + } else if ( "-optionsfile".equals(args[i]) && i+1 < args.length ) { + String filename = args[++i]; + FileInputStream finput = null; + byte contents[] = null; + try { + finput = new FileInputStream(filename); + int byteCount = finput.available(); + if ( byteCount <= 0 ) { + log.error("The -optionsfile file is empty", null); + ok = false; + } else { + contents = new byte[byteCount]; + int bytesRead = finput.read(contents); + if ( byteCount != bytesRead ) { + log.error("Cannot read all of -optionsfile file", null); + ok = false; + } + } + } catch ( IOException e ) { + log.error("cannot open " + filename, e); + ok = false; + } + if ( finput != null ) { + try { + finput.close(); + } catch ( IOException e ) { + ok = false; + log.error("cannot close " + filename, e); + } + } + if ( ok = true && contents != null ) { + String tokens[] = (new String(contents)).split("\\s+"); + if ( tokens.length > 0 ) { + ok = parseOptions(tokens); + } + } + if ( !ok ) { + break; + } + } else if ( "-quiet".equals(args[i]) ) { + quiet = true; + } else { + log.error("argument error", null); + ok = false; + } + } + return ok; + } + + private boolean createFile(String propertiesPath, String outputPath, + String superClass) { + boolean ok = true; + log.verbose("parsing: " + propertiesPath); + Properties p = new Properties(); + try { + p.load(new FileInputStream(propertiesPath)); + } catch ( FileNotFoundException e ) { + ok = false; + log.error("Cannot find file " + propertiesPath, e); + } catch ( IOException e ) { + ok = false; + log.error("IO error on file " + propertiesPath, e); + } + if ( ok ) { + String packageName = inferPackageName(propertiesPath, outputPath); + log.verbose("inferred package name: " + packageName); + List sortedKeys = new ArrayList(); + for ( Object key : p.keySet() ) { + sortedKeys.add((String)key); + } + Collections.sort(sortedKeys); + Iterator keys = sortedKeys.iterator(); + + StringBuffer data = new StringBuffer(); + + while (keys.hasNext()) { + String key = keys.next(); + data.append(" { \"" + escape(key) + "\", \"" + + escape((String)p.get(key)) + "\" },\n"); + } + + // Get class name from java filename, not the properties filename. + // (zh_TW properties might be used to create zh_HK files) + File file = new File(outputPath); + String name = file.getName(); + int dotIndex = name.lastIndexOf('.'); + String className; + if (dotIndex == -1) { + className = name; + } else { + className = name.substring(0, dotIndex); + } + + String packageString = ""; + if (packageName != null && !packageName.equals("")) { + packageString = "package " + packageName + ";\n\n"; + } + + Writer writer = null; + try { + writer = new BufferedWriter( + new OutputStreamWriter(new FileOutputStream(outputPath), "8859_1")); + MessageFormat format = new MessageFormat(FORMAT); + writer.write(format.format(new Object[] { packageString, className, superClass, data })); + } catch ( IOException e ) { + ok = false; + log.error("IO error writing to file " + outputPath, e); + } + if ( writer != null ) { + try { + writer.flush(); + } catch ( IOException e ) { + ok = false; + log.error("IO error flush " + outputPath, e); + } + try { + writer.close(); + } catch ( IOException e ) { + ok = false; + log.error("IO error close " + outputPath, e); + } + } + log.verbose("wrote: " + outputPath); + } + return ok; + } + + private static void usage(Log log) { + log.info("usage:"); + log.info(" java CompileProperties path_to_properties_file path_to_java_output_file [super_class]"); + log.info(" -OR-"); + log.info(" java CompileProperties {-compile path_to_properties_file path_to_java_output_file super_class} -or- -optionsfile filename"); + log.info(""); + log.info("Example:"); + log.info(" java CompileProperties -compile test.properties test.java java.util.ListResourceBundle"); + log.info(" java CompileProperties -optionsfile option_file"); + log.info("option_file contains: -compile test.properties test.java java.util.ListResourceBundle"); + } + + private static String escape(String theString) { + // This is taken from Properties.saveConvert with changes for Java strings + int len = theString.length(); + StringBuffer outBuffer = new StringBuffer(len*2); + + for(int x=0; x 0x007e)) { + outBuffer.append('\\'); + outBuffer.append('u'); + outBuffer.append(toHex((aChar >> 12) & 0xF)); + outBuffer.append(toHex((aChar >> 8) & 0xF)); + outBuffer.append(toHex((aChar >> 4) & 0xF)); + outBuffer.append(toHex( aChar & 0xF)); + } else { + if (specialSaveChars.indexOf(aChar) != -1) { + outBuffer.append('\\'); + } + outBuffer.append(aChar); + } + } + } + return outBuffer.toString(); + } + + private static String inferPackageName(String inputPath, String outputPath) { + // Normalize file names + inputPath = new File(inputPath).getPath(); + outputPath = new File(outputPath).getPath(); + // Split into components + String sep; + if (File.separatorChar == '\\') { + sep = "\\\\"; + } else { + sep = File.separator; + } + String[] inputs = inputPath.split(sep); + String[] outputs = outputPath.split(sep); + // Match common names, eliminating first "classes" entry from + // each if present + int inStart = 0; + int inEnd = inputs.length - 2; + int outEnd = outputs.length - 2; + int i = inEnd; + int j = outEnd; + while (i >= 0 && j >= 0) { + if (!inputs[i].equals(outputs[j]) || + (inputs[i].equals("gensrc") && inputs[j].equals("gensrc"))) { + ++i; + ++j; + break; + } + --i; + --j; + } + String result; + if (i < 0 || j < 0 || i >= inEnd || j >= outEnd) { + result = ""; + } else { + if (inputs[i].equals("classes") && outputs[j].equals("classes")) { + ++i; + } + inStart = i; + StringBuffer buf = new StringBuffer(); + for (i = inStart; i <= inEnd; i++) { + buf.append(inputs[i]); + if (i < inEnd) { + buf.append('.'); + } + } + result = buf.toString(); + } + return result; + } + + private static final String FORMAT = + "{0}" + + "public final class {1} extends {2} '{'\n" + + " protected final Object[][] getContents() '{'\n" + + " return new Object[][] '{'\n" + + "{3}" + + " };\n" + + " }\n" + + "}\n"; + + // This comes from Properties + private static char toHex(int nibble) { + return hexDigit[(nibble & 0xF)]; + } + + // This comes from Properties + private static final char[] hexDigit = { + '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' + }; + + // Note: different from that in Properties + private static final String specialSaveChars = "\""; +} diff -r b367473ef0f1 -r ce2780cb121f langtools/make/tools/genstubs/GenStubs.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/langtools/make/tools/genstubs/GenStubs.java Wed Mar 07 13:11:27 2012 +0100 @@ -0,0 +1,316 @@ +/* + * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package genstubs; + +import java.io.*; +import java.util.*; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.StandardLocation; + +import com.sun.source.tree.CompilationUnitTree; +import com.sun.source.util.JavacTask; +import com.sun.tools.javac.api.JavacTool; +import com.sun.tools.javac.code.Flags; +import com.sun.tools.javac.code.TypeTags; +import com.sun.tools.javac.tree.JCTree; +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; +import com.sun.tools.javac.tree.JCTree.JCFieldAccess; +import com.sun.tools.javac.tree.JCTree.JCIdent; +import com.sun.tools.javac.tree.JCTree.JCImport; +import com.sun.tools.javac.tree.JCTree.JCLiteral; +import com.sun.tools.javac.tree.JCTree.JCMethodDecl; +import com.sun.tools.javac.tree.JCTree.JCModifiers; +import com.sun.tools.javac.tree.JCTree.JCVariableDecl; +import com.sun.tools.javac.tree.Pretty; +import com.sun.tools.javac.tree.TreeMaker; +import com.sun.tools.javac.tree.TreeScanner; +import com.sun.tools.javac.tree.TreeTranslator; +import com.sun.tools.javac.util.Context; +import com.sun.tools.javac.util.ListBuffer; +import com.sun.tools.javac.util.Name; +import javax.tools.JavaFileManager; + +/** + * Generate stub source files by removing implementation details from input files. + * + * This is a special purpose stub generator, specific to the needs of generating + * stub files for JDK 7 API that are needed to compile langtools files that depend + * on that API. The stub generator works by removing as much of the API source code + * as possible without affecting the public signature, in order to reduce the + * transitive closure of the API being referenced. The resulting stubs can be + * put on the langtools sourcepath with -implicit:none to compile the langtools + * files that depend on the JDK 7 API. + * + * Usage: + * genstubs -s -sourcepath + * + * The specified class names are looked up on the sourcepath, and corresponding + * stubs are written to the source output directory. + * + * Classes are parsed into javac ASTs, then processed with a javac TreeTranslator + * to remove implementation details, and written out in the source output directory. + * Documentation comments and annotations are removed. Method bodies are removed + * and methods are marked native. Private and package-private field definitions + * have their initializers replace with 0, 0.0, false, null as appropriate. + */ + +public class GenStubs { + static class Fault extends Exception { + private static final long serialVersionUID = 0; + Fault(String message) { + super(message); + } + Fault(String message, Throwable cause) { + super(message); + initCause(cause); + } + } + + public static void main(String[] args) { + boolean ok = new GenStubs().run(args); + if (!ok) + System.exit(1); + } + + public boolean run(String... args) { + File outdir = null; + String sourcepath = null; + List classes = new ArrayList(); + for (ListIterator iter = Arrays.asList(args).listIterator(); iter.hasNext(); ) { + String arg = iter.next(); + if (arg.equals("-s") && iter.hasNext()) + outdir = new File(iter.next()); + else if (arg.equals("-sourcepath") && iter.hasNext()) + sourcepath = iter.next(); + else if (arg.startsWith("-")) + throw new IllegalArgumentException(arg); + else { + classes.add(arg); + while (iter.hasNext()) + classes.add(iter.next()); + } + } + + return run(sourcepath, outdir, classes); + } + + public boolean run(String sourcepath, File outdir, List classes) { + //System.err.println("run: sourcepath:" + sourcepath + " outdir:" + outdir + " classes:" + classes); + if (sourcepath == null) + throw new IllegalArgumentException("sourcepath not set"); + if (outdir == null) + throw new IllegalArgumentException("source output dir not set"); + + JavacTool tool = JavacTool.create(); + StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); + + try { + fm.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outdir)); + fm.setLocation(StandardLocation.SOURCE_PATH, splitPath(sourcepath)); + List files = new ArrayList(); + for (String c: classes) { + JavaFileObject fo = fm.getJavaFileForInput( + StandardLocation.SOURCE_PATH, c, JavaFileObject.Kind.SOURCE); + if (fo == null) + error("class not found: " + c); + else + files.add(fo); + } + + JavacTask t = tool.getTask(null, fm, null, null, null, files); + Iterable trees = t.parse(); + for (CompilationUnitTree tree: trees) { + makeStub(fm, tree); + } + } catch (IOException e) { + error("IO error " + e, e); + } + + return (errors == 0); + } + + void makeStub(StandardJavaFileManager fm, CompilationUnitTree tree) throws IOException { + CompilationUnitTree tree2 = new StubMaker().translate(tree); + CompilationUnitTree tree3 = new ImportCleaner(fm).removeRedundantImports(tree2); + + String className = fm.inferBinaryName(StandardLocation.SOURCE_PATH, tree.getSourceFile()); + JavaFileObject fo = fm.getJavaFileForOutput(StandardLocation.SOURCE_OUTPUT, + className, JavaFileObject.Kind.SOURCE, null); + // System.err.println("Writing " + className + " to " + fo.getName()); + Writer out = fo.openWriter(); + try { + new Pretty(out, true).printExpr((JCTree) tree3); + } finally { + out.close(); + } + } + + List splitPath(String path) { + List list = new ArrayList(); + for (String p: path.split(File.pathSeparator)) { + if (p.length() > 0) + list.add(new File(p)); + } + return list; + } + + void error(String message) { + System.err.println(message); + errors++; + } + + void error(String message, Throwable cause) { + error(message); + } + + int errors; + + class StubMaker extends TreeTranslator { + CompilationUnitTree translate(CompilationUnitTree tree) { + return super.translate((JCCompilationUnit) tree); + } + + /** + * compilation units: remove javadoc comments + * -- required, in order to remove @deprecated tags, since we + * (separately) remove all annotations, including @Deprecated + */ + public void visitTopLevel(JCCompilationUnit tree) { + super.visitTopLevel(tree); + tree.docComments = Collections.emptyMap(); + } + + /** + * methods: remove method bodies, make methods native + */ + @Override + public void visitMethodDef(JCMethodDecl tree) { + tree.mods = translate(tree.mods); + tree.restype = translate(tree.restype); + tree.typarams = translateTypeParams(tree.typarams); + tree.params = translateVarDefs(tree.params); + tree.thrown = translate(tree.thrown); + if (tree.restype != null && tree.body != null) { + tree.mods.flags |= Flags.NATIVE; + tree.body = null; + } + result = tree; + } + + /** + * modifiers: remove annotations + */ + @Override + public void visitModifiers(JCModifiers tree) { + tree.annotations = com.sun.tools.javac.util.List.nil(); + result = tree; + } + + /** + * field definitions: replace initializers with 0, 0.0, false etc + * when possible -- i.e. leave public, protected initializers alone + */ + @Override + public void visitVarDef(JCVariableDecl tree) { + tree.mods = translate(tree.mods); + tree.vartype = translate(tree.vartype); + if (tree.init != null) { + if ((tree.mods.flags & (Flags.PUBLIC | Flags.PROTECTED)) != 0) + tree.init = translate(tree.init); + else { + String t = tree.vartype.toString(); + if (t.equals("boolean")) + tree.init = new JCLiteral(TypeTags.BOOLEAN, 0) { }; + else if (t.equals("byte")) + tree.init = new JCLiteral(TypeTags.BYTE, 0) { }; + else if (t.equals("char")) + tree.init = new JCLiteral(TypeTags.CHAR, 0) { }; + else if (t.equals("double")) + tree.init = new JCLiteral(TypeTags.DOUBLE, 0.d) { }; + else if (t.equals("float")) + tree.init = new JCLiteral(TypeTags.FLOAT, 0.f) { }; + else if (t.equals("int")) + tree.init = new JCLiteral(TypeTags.INT, 0) { }; + else if (t.equals("long")) + tree.init = new JCLiteral(TypeTags.LONG, 0) { }; + else if (t.equals("short")) + tree.init = new JCLiteral(TypeTags.SHORT, 0) { }; + else + tree.init = new JCLiteral(TypeTags.BOT, null) { }; + } + } + result = tree; + } + } + + class ImportCleaner extends TreeScanner { + private Set names = new HashSet(); + private TreeMaker m; + + ImportCleaner(JavaFileManager fm) { + // ImportCleaner itself doesn't require a filemanager, but instantiating + // a TreeMaker does, indirectly (via ClassReader, sigh) + Context c = new Context(); + c.put(JavaFileManager.class, fm); + m = TreeMaker.instance(c); + } + + CompilationUnitTree removeRedundantImports(CompilationUnitTree t) { + JCCompilationUnit tree = (JCCompilationUnit) t; + tree.accept(this); + ListBuffer defs = new ListBuffer(); + for (JCTree def: tree.defs) { + if (def.getTag() == JCTree.Tag.IMPORT) { + JCImport imp = (JCImport) def; + if (imp.qualid.getTag() == JCTree.Tag.SELECT) { + JCFieldAccess qualid = (JCFieldAccess) imp.qualid; + if (!qualid.name.toString().equals("*") + && !names.contains(qualid.name)) { + continue; + } + } + } + defs.add(def); + } + return m.TopLevel(tree.packageAnnotations, tree.pid, defs.toList()); + } + + @Override + public void visitImport(JCImport tree) { } // ignore names found in imports + + @Override + public void visitIdent(JCIdent tree) { + names.add(tree.name); + } + + @Override + public void visitSelect(JCFieldAccess tree) { + super.visitSelect(tree); + names.add(tree.name); + } + } +}