📄 javac.java
字号:
/** * Indicates whether source should be compiled * with debug information; defaults to off. * @param debug if true compile with debug information */ public void setDebug(boolean debug) { this.debug = debug; } /** * Gets the debug flag. * @return the debug flag */ public boolean getDebug() { return debug; } /** * If true, compiles with optimization enabled. * @param optimize if true compile with optimization enabled */ public void setOptimize(boolean optimize) { this.optimize = optimize; } /** * Gets the optimize flag. * @return the optimize flag */ public boolean getOptimize() { return optimize; } /** * Enables dependency-tracking for compilers * that support this (jikes and classic). * @param depend if true enable dependency-tracking */ public void setDepend(boolean depend) { this.depend = depend; } /** * Gets the depend flag. * @return the depend flag */ public boolean getDepend() { return depend; } /** * If true, asks the compiler for verbose output. * @param verbose if true, asks the compiler for verbose output */ public void setVerbose(boolean verbose) { this.verbose = verbose; } /** * Gets the verbose flag. * @return the verbose flag */ public boolean getVerbose() { return verbose; } /** * Sets the target VM that the classes will be compiled for. Valid * values depend on the compiler, for jdk 1.4 the valid values are * "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "5" and "6". * @param target the target VM */ public void setTarget(String target) { this.targetAttribute = target; } /** * Gets the target VM that the classes will be compiled for. * @return the target VM */ public String getTarget() { return targetAttribute != null ? targetAttribute : getProject().getProperty(MagicNames.BUILD_JAVAC_TARGET); } /** * If true, includes Ant's own classpath in the classpath. * @param include if true, includes Ant's own classpath in the classpath */ public void setIncludeantruntime(boolean include) { includeAntRuntime = include; } /** * Gets whether or not the ant classpath is to be included in the classpath. * @return whether or not the ant classpath is to be included in the classpath */ public boolean getIncludeantruntime() { return includeAntRuntime; } /** * If true, includes the Java runtime libraries in the classpath. * @param include if true, includes the Java runtime libraries in the classpath */ public void setIncludejavaruntime(boolean include) { includeJavaRuntime = include; } /** * Gets whether or not the java runtime should be included in this * task's classpath. * @return the includejavaruntime attribute */ public boolean getIncludejavaruntime() { return includeJavaRuntime; } /** * If true, forks the javac compiler. * * @param f "true|false|on|off|yes|no" */ public void setFork(boolean f) { fork = f; } /** * Sets the name of the javac executable. * * <p>Ignored unless fork is true or extJavac has been specified * as the compiler.</p> * @param forkExec the name of the executable */ public void setExecutable(String forkExec) { forkedExecutable = forkExec; } /** * The value of the executable attribute, if any. * * @since Ant 1.6 * @return the name of the java executable */ public String getExecutable() { return forkedExecutable; } /** * Is this a forked invocation of JDK's javac? * @return true if this is a forked invocation */ public boolean isForkedJavac() { return fork || "extJavac".equals(getCompiler()); } /** * The name of the javac executable to use in fork-mode. * * <p>This is either the name specified with the executable * attribute or the full path of the javac compiler of the VM Ant * is currently running in - guessed by Ant.</p> * * <p>You should <strong>not</strong> invoke this method if you * want to get the value of the executable command - use {@link * #getExecutable getExecutable} for this.</p> * @return the name of the javac executable */ public String getJavacExecutable() { if (forkedExecutable == null && isForkedJavac()) { forkedExecutable = getSystemJavac(); } else if (forkedExecutable != null && !isForkedJavac()) { forkedExecutable = null; } return forkedExecutable; } /** * If true, enables the -nowarn option. * @param flag if true, enable the -nowarn option */ public void setNowarn(boolean flag) { this.nowarn = flag; } /** * Should the -nowarn option be used. * @return true if the -nowarn option should be used */ public boolean getNowarn() { return nowarn; } /** * Adds an implementation specific command-line argument. * @return a ImplementationSpecificArgument to be configured */ public ImplementationSpecificArgument createCompilerArg() { ImplementationSpecificArgument arg = new ImplementationSpecificArgument(); facade.addImplementationArgument(arg); return arg; } /** * Get the additional implementation specific command line arguments. * @return array of command line arguments, guaranteed to be non-null. */ public String[] getCurrentCompilerArgs() { String chosen = facade.getExplicitChoice(); try { // make sure facade knows about magic properties and fork setting String appliedCompiler = getCompiler(); facade.setImplementation(appliedCompiler); String[] result = facade.getArgs(); String altCompilerName = getAltCompilerName(facade.getImplementation()); if (result.length == 0 && altCompilerName != null) { facade.setImplementation(altCompilerName); result = facade.getArgs(); } return result; } finally { facade.setImplementation(chosen); } } private String getAltCompilerName(String anImplementation) { if (JAVAC16.equalsIgnoreCase(anImplementation) || JAVAC15.equalsIgnoreCase(anImplementation) || JAVAC14.equalsIgnoreCase(anImplementation) || JAVAC13.equalsIgnoreCase(anImplementation)) { return MODERN; } if (JAVAC12.equalsIgnoreCase(anImplementation) || JAVAC11.equalsIgnoreCase(anImplementation)) { return CLASSIC; } if (MODERN.equalsIgnoreCase(anImplementation)) { String nextSelected = assumedJavaVersion(); if (JAVAC16.equalsIgnoreCase(nextSelected) || JAVAC15.equalsIgnoreCase(nextSelected) || JAVAC14.equalsIgnoreCase(nextSelected) || JAVAC13.equalsIgnoreCase(nextSelected)) { return nextSelected; } } if (CLASSIC.equals(anImplementation)) { return assumedJavaVersion(); } if (EXTJAVAC.equalsIgnoreCase(anImplementation)) { return assumedJavaVersion(); } return null; } /** * Where Ant should place temporary files. * * @since Ant 1.6 * @param tmpDir the temporary directory */ public void setTempdir(File tmpDir) { this.tmpDir = tmpDir; } /** * Where Ant should place temporary files. * * @since Ant 1.6 * @return the temporary directory */ public File getTempdir() { return tmpDir; } /** * Executes the task. * @exception BuildException if an error occurs */ public void execute() throws BuildException { checkParameters(); resetFileLists(); // scan source directories and dest directory to build up // compile lists String[] list = src.list(); for (int i = 0; i < list.length; i++) { File srcDir = getProject().resolveFile(list[i]); if (!srcDir.exists()) { throw new BuildException("srcdir \"" + srcDir.getPath() + "\" does not exist!", getLocation()); } DirectoryScanner ds = this.getDirectoryScanner(srcDir); String[] files = ds.getIncludedFiles(); scanDir(srcDir, destDir != null ? destDir : srcDir, files); } compile(); } /** * Clear the list of files to be compiled and copied.. */ protected void resetFileLists() { compileList = new File[0]; } /** * Scans the directory looking for source files to be compiled. * The results are returned in the class variable compileList * * @param srcDir The source directory * @param destDir The destination directory * @param files An array of filenames */ protected void scanDir(File srcDir, File destDir, String[] files) { GlobPatternMapper m = new GlobPatternMapper(); m.setFrom("*.java"); m.setTo("*.class"); SourceFileScanner sfs = new SourceFileScanner(this); File[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m); if (newFiles.length > 0) { File[] newCompileList = new File[compileList.length + newFiles.length]; System.arraycopy(compileList, 0, newCompileList, 0, compileList.length); System.arraycopy(newFiles, 0, newCompileList, compileList.length, newFiles.length); compileList = newCompileList; } } /** * Gets the list of files to be compiled. * @return the list of files as an array */ public File[] getFileList() { return compileList; } /** * Is the compiler implementation a jdk compiler * * @param compilerImpl the name of the compiler implementation * @return true if compilerImpl is "modern", "classic", * "javac1.1", "javac1.2", "javac1.3", "javac1.4", "javac1.5" or * "javac1.6". */ protected boolean isJdkCompiler(String compilerImpl) { return MODERN.equals(compilerImpl) || CLASSIC.equals(compilerImpl) || JAVAC16.equals(compilerImpl) || JAVAC15.equals(compilerImpl) || JAVAC14.equals(compilerImpl) || JAVAC13.equals(compilerImpl) || JAVAC12.equals(compilerImpl) || JAVAC11.equals(compilerImpl); } /** * @return the executable name of the java compiler */ protected String getSystemJavac() { return JavaEnvUtils.getJdkExecutable("javac"); } /** * Choose the implementation for this particular task. * @param compiler the name of the compiler * @since Ant 1.5 */ public void setCompiler(String compiler) { facade.setImplementation(compiler); } /** * The implementation for this particular task. * * <p>Defaults to the build.compiler property but can be overridden * via the compiler and fork attributes.</p> * * <p>If fork has been set to true, the result will be extJavac * and not classic or java1.2 - no matter what the compiler * attribute looks like.</p> * * @see #getCompilerVersion * @return the compiler. * @since Ant 1.5 */ public String getCompiler() { String compilerImpl = getCompilerVersion(); if (fork) { if (isJdkCompiler(compilerImpl)) { compilerImpl = "extJavac"; } else { log("Since compiler setting isn't classic or modern," + "ignoring fork setting.", Project.MSG_WARN); } } return compilerImpl; } /** * The implementation for this particular task. * * <p>Defaults to the build.compiler property but can be overridden * via the compiler attribute.</p> * * <p>This method does not take the fork attribute into * account.</p> * * @see #getCompiler * @return the compiler. * * @since Ant 1.5 */ public String getCompilerVersion() { facade.setMagicValue(getProject().getProperty("build.compiler")); return facade.getImplementation(); } /** * Check that all required attributes have been set and nothing * silly has been entered. * * @since Ant 1.5 * @exception BuildException if an error occurs */ protected void checkParameters() throws BuildException { if (src == null) { throw new BuildException("srcdir attribute must be set!", getLocation()); } if (src.size() == 0) { throw new BuildException("srcdir attribute must be set!", getLocation()); } if (destDir != null && !destDir.isDirectory()) { throw new BuildException("destination directory \"" + destDir + "\" does not exist " + "or is not a directory", getLocation()); } } /** * Perform the compilation. * * @since Ant 1.5 */ protected void compile() { String compilerImpl = getCompiler(); if (compileList.length > 0) { log("Compiling " + compileList.length + " source file" + (compileList.length == 1 ? "" : "s") + (destDir != null ? " to " + destDir : "")); if (listFiles) { for (int i = 0; i < compileList.length; i++) { String filename = compileList[i].getAbsolutePath(); log(filename); } } CompilerAdapter adapter = CompilerAdapterFactory.getCompiler(compilerImpl, this); // now we need to populate the compiler adapter adapter.setJavac(this); // finally, lets execute the compiler!! if (!adapter.execute()) { if (failOnError) { throw new BuildException(FAIL_MSG, getLocation()); } else { log(FAIL_MSG, Project.MSG_ERR); } } } } /** * Adds an "compiler" attribute to Commandline$Attribute used to * filter command line attributes based on the current * implementation. */ public class ImplementationSpecificArgument extends org.apache.tools.ant.util.facade.ImplementationSpecificArgument { /** * @param impl the name of the compiler */ public void setCompiler(String impl) { super.setImplementation(impl); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -