Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 514   Methods: 16
NCLOC: 314   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultCompilerAdapter.java 44.4% 52.4% 56.2% 50.4%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.taskdefs.compilers;
 56   
 
 57   
 import java.io.File;
 58   
 import java.io.FileWriter;
 59   
 import java.io.IOException;
 60   
 import java.io.PrintWriter;
 61   
 import org.apache.tools.ant.BuildException;
 62   
 import org.apache.tools.ant.Location;
 63   
 import org.apache.tools.ant.Project;
 64   
 import org.apache.tools.ant.taskdefs.Execute;
 65   
 import org.apache.tools.ant.taskdefs.Javac;
 66   
 import org.apache.tools.ant.taskdefs.LogStreamHandler;
 67   
 import org.apache.tools.ant.types.Commandline;
 68   
 import org.apache.tools.ant.types.Path;
 69   
 import org.apache.tools.ant.util.FileUtils;
 70   
 import org.apache.tools.ant.util.JavaEnvUtils;
 71   
 
 72   
 /**
 73   
  * This is the default implementation for the CompilerAdapter interface.
 74   
  * Currently, this is a cut-and-paste of the original javac task.
 75   
  *
 76   
  * @author James Davidson <a href="mailto:duncan@x180.com">duncan@x180.com</a>
 77   
  * @author Robin Green 
 78   
  *         <a href="mailto:greenrd@hotmail.com">greenrd@hotmail.com</a>
 79   
  * @author Stefan Bodewig
 80   
  * @author <a href="mailto:jayglanville@home.com">J D Glanville</a>
 81   
  *
 82   
  * @since Ant 1.3
 83   
  */
 84   
 public abstract class DefaultCompilerAdapter implements CompilerAdapter {
 85   
 
 86   
     /* jdg - TODO - all these attributes are currently protected, but they
 87   
      * should probably be private in the near future.
 88   
      */
 89   
 
 90   
     protected Path src;
 91   
     protected File destDir;
 92   
     protected String encoding;
 93   
     protected boolean debug = false;
 94   
     protected boolean optimize = false;
 95   
     protected boolean deprecation = false;
 96   
     protected boolean depend = false;
 97   
     protected boolean verbose = false;
 98   
     protected String target;
 99   
     protected Path bootclasspath;
 100   
     protected Path extdirs;
 101   
     protected Path compileClasspath;
 102   
     protected Path compileSourcepath;
 103   
     protected Project project;
 104   
     protected Location location;
 105   
     protected boolean includeAntRuntime;
 106   
     protected boolean includeJavaRuntime;
 107   
     protected String memoryInitialSize;
 108   
     protected String memoryMaximumSize;
 109   
 
 110   
     protected File[] compileList;
 111   
     protected static String lSep = System.getProperty("line.separator");
 112   
     protected Javac attributes;
 113   
 
 114   
     private FileUtils fileUtils = FileUtils.newFileUtils();
 115   
 
 116   
     /**
 117   
      * Set the Javac instance which contains the configured compilation
 118   
      * attributes.
 119   
      *
 120   
      * @param attributes a configured Javac task.
 121   
      */
 122  17
     public void setJavac(Javac attributes) {
 123  17
         this.attributes = attributes;
 124  17
         src = attributes.getSrcdir();
 125  17
         destDir = attributes.getDestdir();
 126  17
         encoding = attributes.getEncoding();
 127  17
         debug = attributes.getDebug();
 128  17
         optimize = attributes.getOptimize();
 129  17
         deprecation = attributes.getDeprecation();
 130  17
         depend = attributes.getDepend();
 131  17
         verbose = attributes.getVerbose();
 132  17
         target = attributes.getTarget();
 133  17
         bootclasspath = attributes.getBootclasspath();
 134  17
         extdirs = attributes.getExtdirs();
 135  17
         compileList = attributes.getFileList();
 136  17
         compileClasspath = attributes.getClasspath();
 137  17
         compileSourcepath = attributes.getSourcepath();
 138  17
         project = attributes.getProject();
 139  17
         location = attributes.getLocation();
 140  17
         includeAntRuntime = attributes.getIncludeantruntime();
 141  17
         includeJavaRuntime = attributes.getIncludejavaruntime();
 142  17
         memoryInitialSize = attributes.getMemoryInitialSize();
 143  17
         memoryMaximumSize = attributes.getMemoryMaximumSize();
 144   
     }
 145   
 
 146   
     /**
 147   
      * Get the Javac task instance associated with this compiler adapter
 148   
      *
 149   
      * @return the configured Javac task instance used by this adapter.
 150   
      */
 151  17
     public Javac getJavac() {
 152  17
         return attributes;
 153   
     }
 154   
 
 155   
     /**
 156   
      * Builds the compilation classpath.
 157   
      *
 158   
      */
 159  17
     protected Path getCompileClasspath() {
 160  17
         Path classpath = new Path(project);
 161   
 
 162   
         // add dest dir to classpath so that previously compiled and
 163   
         // untouched classes are on classpath
 164   
 
 165  17
         if (destDir != null) {
 166  16
             classpath.setLocation(destDir);
 167   
         }
 168   
 
 169   
         // Combine the build classpath with the system classpath, in an
 170   
         // order determined by the value of build.sysclasspath
 171   
 
 172  17
         Path cp = compileClasspath;
 173  17
         if (cp == null) {
 174  16
             cp = new Path(project);
 175   
         }
 176  17
         if (includeAntRuntime) {
 177  17
             classpath.addExisting(cp.concatSystemClasspath("last"));
 178   
         } else {
 179  0
             classpath.addExisting(cp.concatSystemClasspath("ignore"));
 180   
         }
 181   
 
 182  17
         if (includeJavaRuntime) {
 183  0
             classpath.addJavaRuntime();
 184   
         }
 185   
 
 186  17
         return classpath;
 187   
     }
 188   
 
 189  0
     protected Commandline setupJavacCommandlineSwitches(Commandline cmd) {
 190  0
         return setupJavacCommandlineSwitches(cmd, false);
 191   
     }
 192   
 
 193   
     /**
 194   
      * Does the command line argument processing common to classic and
 195   
      * modern.  Doesn't add the files to compile.
 196   
      */
 197  17
     protected Commandline setupJavacCommandlineSwitches(Commandline cmd,
 198   
                                                         boolean useDebugLevel) {
 199  17
         Path classpath = getCompileClasspath();
 200   
         // For -sourcepath, use the "sourcepath" value if present.
 201   
         // Otherwise default to the "srcdir" value.
 202  17
         Path sourcepath = null;
 203  17
         if (compileSourcepath != null) {
 204  0
             sourcepath = compileSourcepath;
 205   
         } else {
 206  17
             sourcepath = src;
 207   
         }
 208   
 
 209  17
         String memoryParameterPrefix = assumeJava11() ? "-J-" : "-J-X";
 210  17
         if (memoryInitialSize != null) {
 211  0
             if (!attributes.isForkedJavac()) {
 212  0
                 attributes.log("Since fork is false, ignoring "
 213   
                                + "memoryInitialSize setting.", 
 214   
                                Project.MSG_WARN);
 215   
             } else {
 216  0
                 cmd.createArgument().setValue(memoryParameterPrefix
 217   
                                               + "ms" + memoryInitialSize);
 218   
             }
 219   
         }
 220   
 
 221  17
         if (memoryMaximumSize != null) {
 222  0
             if (!attributes.isForkedJavac()) {
 223  0
                 attributes.log("Since fork is false, ignoring "
 224   
                                + "memoryMaximumSize setting.",
 225   
                                Project.MSG_WARN);
 226   
             } else {
 227  0
                 cmd.createArgument().setValue(memoryParameterPrefix
 228   
                                               + "mx" + memoryMaximumSize);
 229   
             }
 230   
         }
 231   
 
 232  17
         if (attributes.getNowarn()) {
 233  0
             cmd.createArgument().setValue("-nowarn");
 234   
         }
 235   
 
 236  17
         if (deprecation == true) {
 237  0
             cmd.createArgument().setValue("-deprecation");
 238   
         }
 239   
 
 240  17
         if (destDir != null) {
 241  16
             cmd.createArgument().setValue("-d");
 242  16
             cmd.createArgument().setFile(destDir);
 243   
         }
 244   
 
 245  17
         cmd.createArgument().setValue("-classpath");
 246   
 
 247   
         // Just add "sourcepath" to classpath ( for JDK1.1 )
 248   
         // as well as "bootclasspath" and "extdirs"
 249  17
         if (assumeJava11()) {
 250  0
             Path cp = new Path(project);
 251   
             /*
 252   
              * XXX - This doesn't mix very well with build.systemclasspath,
 253   
              */
 254  0
             if (bootclasspath != null) {
 255  0
                 cp.append(bootclasspath);
 256   
             }
 257  0
             if (extdirs != null) {
 258  0
                 cp.addExtdirs(extdirs);
 259   
             }
 260  0
             cp.append(classpath);
 261  0
             cp.append(sourcepath);
 262  0
             cmd.createArgument().setPath(cp);
 263   
         } else {
 264  17
             cmd.createArgument().setPath(classpath);
 265   
             // If the buildfile specifies sourcepath="", then don't
 266   
             // output any sourcepath.
 267  17
             if (sourcepath.size() > 0) {
 268  17
                 cmd.createArgument().setValue("-sourcepath");
 269  17
                 cmd.createArgument().setPath(sourcepath);
 270   
             }
 271  17
             if (target != null) {
 272  0
                 cmd.createArgument().setValue("-target");
 273  0
                 cmd.createArgument().setValue(target);
 274   
             }
 275  17
             if (bootclasspath != null && bootclasspath.size() > 0) {
 276  0
                 cmd.createArgument().setValue("-bootclasspath");
 277  0
                 cmd.createArgument().setPath(bootclasspath);
 278   
             }
 279  17
             if (extdirs != null && extdirs.size() > 0) {
 280  0
                 cmd.createArgument().setValue("-extdirs");
 281  0
                 cmd.createArgument().setPath(extdirs);
 282   
             }
 283   
         }
 284   
 
 285  17
         if (encoding != null) {
 286  0
             cmd.createArgument().setValue("-encoding");
 287  0
             cmd.createArgument().setValue(encoding);
 288   
         }
 289  17
         if (debug) {
 290  1
             if (useDebugLevel && !assumeJava11()) {
 291  1
                 String debugLevel = attributes.getDebugLevel();
 292  1
                 if (debugLevel != null) {
 293  0
                     cmd.createArgument().setValue("-g:" + debugLevel);
 294   
                 } else {
 295  1
                     cmd.createArgument().setValue("-g");
 296   
                 }
 297   
             } else {
 298  0
                 cmd.createArgument().setValue("-g");
 299   
             }
 300  16
         } else if (!assumeJava11()) {
 301  16
             cmd.createArgument().setValue("-g:none");
 302   
         }
 303  17
         if (optimize) {
 304  0
             cmd.createArgument().setValue("-O");
 305   
         }
 306   
 
 307  17
         if (depend) {
 308  0
             if (assumeJava11()) {
 309  0
                 cmd.createArgument().setValue("-depend");
 310  0
             } else if (assumeJava12()) {
 311  0
                 cmd.createArgument().setValue("-Xdepend");
 312   
             } else {
 313  0
                 attributes.log("depend attribute is not supported by the "
 314   
                                + "modern compiler", Project.MSG_WARN);
 315   
             }
 316   
         }
 317   
 
 318  17
         if (verbose) {
 319  0
             cmd.createArgument().setValue("-verbose");
 320   
         }
 321   
 
 322  17
         addCurrentCompilerArgs(cmd);
 323   
 
 324  17
         return cmd;
 325   
     }
 326   
 
 327   
     /**
 328   
      * Does the command line argument processing for modern.  Doesn't
 329   
      * add the files to compile.
 330   
      */
 331  17
     protected Commandline setupModernJavacCommandlineSwitches(Commandline cmd) {
 332  17
         setupJavacCommandlineSwitches(cmd, true);
 333  17
         if (attributes.getSource() != null && !assumeJava13()) {
 334  0
             cmd.createArgument().setValue("-source");
 335  0
             cmd.createArgument().setValue(attributes.getSource());
 336   
         }
 337  17
         return cmd;
 338   
     }
 339   
 
 340   
     /**
 341   
      * Does the command line argument processing for modern and adds
 342   
      * the files to compile as well.
 343   
      */
 344  17
     protected Commandline setupModernJavacCommand() {
 345  17
         Commandline cmd = new Commandline();
 346  17
         setupModernJavacCommandlineSwitches(cmd);
 347   
 
 348  17
         logAndAddFilesToCompile(cmd);
 349  17
         return cmd;
 350   
     }
 351   
 
 352  0
     protected Commandline setupJavacCommand() {
 353  0
         return setupJavacCommand(false);
 354   
     }
 355   
 
 356   
     /**
 357   
      * Does the command line argument processing for classic and adds
 358   
      * the files to compile as well.
 359   
      */
 360  0
     protected Commandline setupJavacCommand(boolean debugLevelCheck) {
 361  0
         Commandline cmd = new Commandline();
 362  0
         setupJavacCommandlineSwitches(cmd, debugLevelCheck);
 363  0
         logAndAddFilesToCompile(cmd);
 364  0
         return cmd;
 365   
     }
 366   
 
 367   
     /**
 368   
      * Logs the compilation parameters, adds the files to compile and logs the
 369   
      * &qout;niceSourceList&quot;
 370   
      */
 371  17
     protected void logAndAddFilesToCompile(Commandline cmd) {
 372  17
         attributes.log("Compilation " + cmd.describeArguments(),
 373   
                        Project.MSG_VERBOSE);
 374   
 
 375  17
         StringBuffer niceSourceList = new StringBuffer("File");
 376  17
         if (compileList.length != 1) {
 377  13
             niceSourceList.append("s");
 378   
         }
 379  17
         niceSourceList.append(" to be compiled:");
 380   
 
 381  17
         niceSourceList.append(lSep);
 382   
 
 383  17
         for (int i = 0; i < compileList.length; i++) {
 384  60
             String arg = compileList[i].getAbsolutePath();
 385  60
             cmd.createArgument().setValue(arg);
 386  60
             niceSourceList.append("    " + arg + lSep);
 387   
         }
 388   
 
 389  17
         attributes.log(niceSourceList.toString(), Project.MSG_VERBOSE);
 390   
     }
 391   
 
 392   
     /**
 393   
      * Do the compile with the specified arguments.
 394   
      * @param args - arguments to pass to process on command line
 395   
      * @param firstFileName - index of the first source file in args,
 396   
      * if the index is negative, no temporary file will ever be
 397   
      * created, but this may hit the command line length limit on your
 398   
      * system.
 399   
      */
 400  0
     protected int executeExternalCompile(String[] args, int firstFileName) {
 401  0
         String[] commandArray = null;
 402  0
         File tmpFile = null;
 403   
 
 404  0
         try {
 405   
             /*
 406   
              * Many system have been reported to get into trouble with
 407   
              * long command lines - no, not only Windows ;-).
 408   
              *
 409   
              * POSIX seems to define a lower limit of 4k, so use a temporary
 410   
              * file if the total length of the command line exceeds this limit.
 411   
              */
 412  0
             if (Commandline.toString(args).length() > 4096 
 413   
                 && firstFileName >= 0) {
 414  0
                 PrintWriter out = null;
 415  0
                 try {
 416  0
                     String userDirName = System.getProperty("user.dir");
 417  0
                     File userDir = new File(userDirName);
 418  0
                     tmpFile = fileUtils.createTempFile("files", "", userDir);
 419  0
                     out = new PrintWriter(new FileWriter(tmpFile));
 420  0
                     for (int i = firstFileName; i < args.length; i++) {
 421  0
                         out.println(args[i]);
 422   
                     }
 423  0
                     out.flush();
 424  0
                     commandArray = new String[firstFileName + 1];
 425  0
                     System.arraycopy(args, 0, commandArray, 0, firstFileName);
 426  0
                     commandArray[firstFileName] = "@" + tmpFile;
 427   
                 } catch (IOException e) {
 428  0
                     throw new BuildException("Error creating temporary file", 
 429   
                                              e, location);
 430   
                 } finally {
 431  0
                     if (out != null) {
 432  0
                         try {out.close();} catch (Throwable t) {}
 433   
                     }
 434   
                 }
 435   
             } else {
 436  0
                 commandArray = args;
 437   
             }
 438   
 
 439  0
             try {
 440  0
                 Execute exe = new Execute(
 441   
                                   new LogStreamHandler(attributes,
 442   
                                                        Project.MSG_INFO,
 443   
                                                        Project.MSG_WARN));
 444  0
                 exe.setAntRun(project);
 445  0
                 exe.setWorkingDirectory(project.getBaseDir());
 446  0
                 exe.setCommandline(commandArray);
 447  0
                 exe.execute();
 448  0
                 return exe.getExitValue();
 449   
             } catch (IOException e) {
 450  0
                 throw new BuildException("Error running " + args[0]
 451   
                         + " compiler", e, location);
 452   
             }
 453   
         } finally {
 454  0
             if (tmpFile != null) {
 455  0
                 tmpFile.delete();
 456   
             }
 457   
         }
 458   
     }
 459   
 
 460   
     /**
 461   
      * @deprecated use org.apache.tools.ant.types.Path#addExtdirs instead
 462   
      */
 463  0
     protected void addExtdirsToClasspath(Path classpath) {
 464  0
         classpath.addExtdirs(extdirs);
 465   
     }
 466   
 
 467   
     /**
 468   
      * Adds the command line arguments specifc to the current implementation.
 469   
      */
 470  17
     protected void addCurrentCompilerArgs(Commandline cmd) {
 471  17
         cmd.addArguments(getJavac().getCurrentCompilerArgs());
 472   
     }
 473   
 
 474   
     /**
 475   
      * Shall we assume JDK 1.1 command line switches?
 476   
      * @since Ant 1.5
 477   
      */
 478  51
     protected boolean assumeJava11() {
 479  51
         return "javac1.1".equals(attributes.getCompilerVersion()) ||
 480   
             ("classic".equals(attributes.getCompilerVersion()) 
 481   
              && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) ||
 482   
             ("extJavac".equals(attributes.getCompilerVersion()) 
 483   
              && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1));
 484   
     }
 485   
 
 486   
     /**
 487   
      * Shall we assume JDK 1.2 command line switches?
 488   
      * @since Ant 1.5
 489   
      */
 490  0
     protected boolean assumeJava12() {
 491  0
         return "javac1.2".equals(attributes.getCompilerVersion()) ||
 492   
             ("classic".equals(attributes.getCompilerVersion()) 
 493   
              && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2)) ||
 494   
             ("extJavac".equals(attributes.getCompilerVersion()) 
 495   
              && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2));
 496   
     }
 497   
 
 498   
     /**
 499   
      * Shall we assume JDK 1.3 command line switches?
 500   
      * @since Ant 1.5
 501   
      */
 502  0
     protected boolean assumeJava13() {
 503  0
         return "javac1.3".equals(attributes.getCompilerVersion()) ||
 504   
             ("classic".equals(attributes.getCompilerVersion()) 
 505   
              && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) ||
 506   
             ("modern".equals(attributes.getCompilerVersion()) 
 507   
              && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) ||
 508   
             ("extJavac".equals(attributes.getCompilerVersion()) 
 509   
              && JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3));
 510   
     }
 511   
 
 512   
 }
 513   
 
 514