KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Javac


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.File JavaDoc;
22
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.DirectoryScanner;
25 import org.apache.tools.ant.MagicNames;
26 import org.apache.tools.ant.Project;
27 import org.apache.tools.ant.taskdefs.compilers.CompilerAdapter;
28 import org.apache.tools.ant.taskdefs.compilers.CompilerAdapterFactory;
29 import org.apache.tools.ant.types.Path;
30 import org.apache.tools.ant.types.Reference;
31 import org.apache.tools.ant.util.GlobPatternMapper;
32 import org.apache.tools.ant.util.JavaEnvUtils;
33 import org.apache.tools.ant.util.SourceFileScanner;
34 import org.apache.tools.ant.util.facade.FacadeTaskHelper;
35
36 /**
37  * Compiles Java source files. This task can take the following
38  * arguments:
39  * <ul>
40  * <li>sourcedir
41  * <li>destdir
42  * <li>deprecation
43  * <li>classpath
44  * <li>bootclasspath
45  * <li>extdirs
46  * <li>optimize
47  * <li>debug
48  * <li>encoding
49  * <li>target
50  * <li>depend
51  * <li>verbose
52  * <li>failonerror
53  * <li>includeantruntime
54  * <li>includejavaruntime
55  * <li>source
56  * <li>compiler
57  * </ul>
58  * Of these arguments, the <b>sourcedir</b> and <b>destdir</b> are required.
59  * <p>
60  * When this task executes, it will recursively scan the sourcedir and
61  * destdir looking for Java source files to compile. This task makes its
62  * compile decision based on timestamp.
63  *
64  *
65  * @since Ant 1.1
66  *
67  * @ant.task category="java"
68  */

69
70 public class Javac extends MatchingTask {
71
72     private static final String JavaDoc FAIL_MSG
73         = "Compile failed; see the compiler error output for details.";
74
75     private static final String JavaDoc JAVAC16 = "javac1.6";
76     private static final String JavaDoc JAVAC15 = "javac1.5";
77     private static final String JavaDoc JAVAC14 = "javac1.4";
78     private static final String JavaDoc JAVAC13 = "javac1.3";
79     private static final String JavaDoc JAVAC12 = "javac1.2";
80     private static final String JavaDoc JAVAC11 = "javac1.1";
81     private static final String JavaDoc MODERN = "modern";
82     private static final String JavaDoc CLASSIC = "classic";
83     private static final String JavaDoc EXTJAVAC = "extJavac";
84
85     private Path src;
86     private File JavaDoc destDir;
87     private Path compileClasspath;
88     private Path compileSourcepath;
89     private String JavaDoc encoding;
90     private boolean debug = false;
91     private boolean optimize = false;
92     private boolean deprecation = false;
93     private boolean depend = false;
94     private boolean verbose = false;
95     private String JavaDoc targetAttribute;
96     private Path bootclasspath;
97     private Path extdirs;
98     private boolean includeAntRuntime = true;
99     private boolean includeJavaRuntime = false;
100     private boolean fork = false;
101     private String JavaDoc forkedExecutable = null;
102     private boolean nowarn = false;
103     private String JavaDoc memoryInitialSize;
104     private String JavaDoc memoryMaximumSize;
105     private FacadeTaskHelper facade = null;
106
107     // CheckStyle:VisibilityModifier OFF - bc
108
protected boolean failOnError = true;
109     protected boolean listFiles = false;
110     protected File JavaDoc[] compileList = new File JavaDoc[0];
111     // CheckStyle:VisibilityModifier ON
112

113     private String JavaDoc source;
114     private String JavaDoc debugLevel;
115     private File JavaDoc tmpDir;
116
117     /**
118      * Javac task for compilation of Java files.
119      */

120     public Javac() {
121         facade = new FacadeTaskHelper(assumedJavaVersion());
122     }
123
124     private String JavaDoc assumedJavaVersion() {
125         if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_2)) {
126             return JAVAC12;
127         } else if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_3)) {
128             return JAVAC13;
129         } else if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_4)) {
130             return JAVAC14;
131         } else if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_5)) {
132             return JAVAC15;
133         } else if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_6)) {
134             return JAVAC16;
135         } else {
136             return CLASSIC;
137         }
138     }
139
140     /**
141      * Get the value of debugLevel.
142      * @return value of debugLevel.
143      */

144     public String JavaDoc getDebugLevel() {
145         return debugLevel;
146     }
147
148     /**
149      * Keyword list to be appended to the -g command-line switch.
150      *
151      * This will be ignored by all implementations except modern
152      * and classic(ver >= 1.2). Legal values are none or a
153      * comma-separated list of the following keywords: lines, vars,
154      * and source. If debuglevel is not specified, by default, :none
155      * will be appended to -g. If debug is not turned on, this attribute
156      * will be ignored.
157      *
158      * @param v Value to assign to debugLevel.
159      */

160     public void setDebugLevel(String JavaDoc v) {
161         this.debugLevel = v;
162     }
163
164     /**
165      * Get the value of source.
166      * @return value of source.
167      */

168     public String JavaDoc getSource() {
169         return source != null
170             ? source : getProject().getProperty(MagicNames.BUILD_JAVAC_SOURCE);
171     }
172
173     /**
174      * Value of the -source command-line switch; will be ignored
175      * by all implementations except modern and jikes.
176      *
177      * If you use this attribute together with jikes, you must make
178      * sure that your version of jikes supports the -source switch.
179      * Legal values are 1.3, 1.4, 1.5, and 5 - by default, no
180      * -source argument will be used at all.
181      *
182      * @param v Value to assign to source.
183      */

184     public void setSource(String JavaDoc v) {
185         this.source = v;
186     }
187
188     /**
189      * Adds a path for source compilation.
190      *
191      * @return a nested src element.
192      */

193     public Path createSrc() {
194         if (src == null) {
195             src = new Path(getProject());
196         }
197         return src.createPath();
198     }
199
200     /**
201      * Recreate src.
202      *
203      * @return a nested src element.
204      */

205     protected Path recreateSrc() {
206         src = null;
207         return createSrc();
208     }
209
210     /**
211      * Set the source directories to find the source Java files.
212      * @param srcDir the source directories as a path
213      */

214     public void setSrcdir(Path srcDir) {
215         if (src == null) {
216             src = srcDir;
217         } else {
218             src.append(srcDir);
219         }
220     }
221
222     /**
223      * Gets the source dirs to find the source java files.
224      * @return the source directories as a path
225      */

226     public Path getSrcdir() {
227         return src;
228     }
229
230     /**
231      * Set the destination directory into which the Java source
232      * files should be compiled.
233      * @param destDir the destination director
234      */

235     public void setDestdir(File JavaDoc destDir) {
236         this.destDir = destDir;
237     }
238
239     /**
240      * Gets the destination directory into which the java source files
241      * should be compiled.
242      * @return the destination directory
243      */

244     public File JavaDoc getDestdir() {
245         return destDir;
246     }
247
248     /**
249      * Set the sourcepath to be used for this compilation.
250      * @param sourcepath the source path
251      */

252     public void setSourcepath(Path sourcepath) {
253         if (compileSourcepath == null) {
254             compileSourcepath = sourcepath;
255         } else {
256             compileSourcepath.append(sourcepath);
257         }
258     }
259
260     /**
261      * Gets the sourcepath to be used for this compilation.
262      * @return the source path
263      */

264     public Path getSourcepath() {
265         return compileSourcepath;
266     }
267
268     /**
269      * Adds a path to sourcepath.
270      * @return a sourcepath to be configured
271      */

272     public Path createSourcepath() {
273         if (compileSourcepath == null) {
274             compileSourcepath = new Path(getProject());
275         }
276         return compileSourcepath.createPath();
277     }
278
279     /**
280      * Adds a reference to a source path defined elsewhere.
281      * @param r a reference to a source path
282      */

283     public void setSourcepathRef(Reference r) {
284         createSourcepath().setRefid(r);
285     }
286
287     /**
288      * Set the classpath to be used for this compilation.
289      *
290      * @param classpath an Ant Path object containing the compilation classpath.
291      */

292     public void setClasspath(Path classpath) {
293         if (compileClasspath == null) {
294             compileClasspath = classpath;
295         } else {
296             compileClasspath.append(classpath);
297         }
298     }
299
300     /**
301      * Gets the classpath to be used for this compilation.
302      * @return the class path
303      */

304     public Path getClasspath() {
305         return compileClasspath;
306     }
307
308     /**
309      * Adds a path to the classpath.
310      * @return a class path to be configured
311      */

312     public Path createClasspath() {
313         if (compileClasspath == null) {
314             compileClasspath = new Path(getProject());
315         }
316         return compileClasspath.createPath();
317     }
318
319     /**
320      * Adds a reference to a classpath defined elsewhere.
321      * @param r a reference to a classpath
322      */

323     public void setClasspathRef(Reference r) {
324         createClasspath().setRefid(r);
325     }
326
327     /**
328      * Sets the bootclasspath that will be used to compile the classes
329      * against.
330      * @param bootclasspath a path to use as a boot class path (may be more
331      * than one)
332      */

333     public void setBootclasspath(Path bootclasspath) {
334         if (this.bootclasspath == null) {
335             this.bootclasspath = bootclasspath;
336         } else {
337             this.bootclasspath.append(bootclasspath);
338         }
339     }
340
341     /**
342      * Gets the bootclasspath that will be used to compile the classes
343      * against.
344      * @return the boot path
345      */

346     public Path getBootclasspath() {
347         return bootclasspath;
348     }
349
350     /**
351      * Adds a path to the bootclasspath.
352      * @return a path to be configured
353      */

354     public Path createBootclasspath() {
355         if (bootclasspath == null) {
356             bootclasspath = new Path(getProject());
357         }
358         return bootclasspath.createPath();
359     }
360
361     /**
362      * Adds a reference to a classpath defined elsewhere.
363      * @param r a reference to a classpath
364      */

365     public void setBootClasspathRef(Reference r) {
366         createBootclasspath().setRefid(r);
367     }
368
369     /**
370      * Sets the extension directories that will be used during the
371      * compilation.
372      * @param extdirs a path
373      */

374     public void setExtdirs(Path extdirs) {
375         if (this.extdirs == null) {
376             this.extdirs = extdirs;
377         } else {
378             this.extdirs.append(extdirs);
379         }
380     }
381
382     /**
383      * Gets the extension directories that will be used during the
384      * compilation.
385      * @return the extension directories as a path
386      */

387     public Path getExtdirs() {
388         return extdirs;
389     }
390
391     /**
392      * Adds a path to extdirs.
393      * @return a path to be configured
394      */

395     public Path createExtdirs() {
396         if (extdirs == null) {
397             extdirs = new Path(getProject());
398         }
399         return extdirs.createPath();
400     }
401
402     /**
403      * If true, list the source files being handed off to the compiler.
404      * @param list if true list the source files
405      */

406     public void setListfiles(boolean list) {
407         listFiles = list;
408     }
409
410     /**
411      * Get the listfiles flag.
412      * @return the listfiles flag
413      */

414     public boolean getListfiles() {
415         return listFiles;
416     }
417
418     /**
419      * Indicates whether the build will continue
420      * even if there are compilation errors; defaults to true.
421      * @param fail if true halt the build on failure
422      */

423     public void setFailonerror(boolean fail) {
424         failOnError = fail;
425     }
426
427     /**
428      * @ant.attribute ignore="true"
429      * @param proceed inverse of failoferror
430      */

431     public void setProceed(boolean proceed) {
432         failOnError = !proceed;
433     }
434
435     /**
436      * Gets the failonerror flag.
437      * @return the failonerror flag
438      */

439     public boolean getFailonerror() {
440         return failOnError;
441     }
442
443     /**
444      * Indicates whether source should be
445      * compiled with deprecation information; defaults to off.
446      * @param deprecation if true turn on deprecation information
447      */

448     public void setDeprecation(boolean deprecation) {
449         this.deprecation = deprecation;
450     }
451
452     /**
453      * Gets the deprecation flag.
454      * @return the deprecation flag
455      */

456     public boolean getDeprecation() {
457         return deprecation;
458     }
459
460     /**
461      * The initial size of the memory for the underlying VM
462      * if javac is run externally; ignored otherwise.
463      * Defaults to the standard VM memory setting.
464      * (Examples: 83886080, 81920k, or 80m)
465      * @param memoryInitialSize string to pass to VM
466      */

467     public void setMemoryInitialSize(String JavaDoc memoryInitialSize) {
468         this.memoryInitialSize = memoryInitialSize;
469     }
470
471     /**
472      * Gets the memoryInitialSize flag.
473      * @return the memoryInitialSize flag
474      */

475     public String JavaDoc getMemoryInitialSize() {
476         return memoryInitialSize;
477     }
478
479     /**
480      * The maximum size of the memory for the underlying VM
481      * if javac is run externally; ignored otherwise.
482      * Defaults to the standard VM memory setting.
483      * (Examples: 83886080, 81920k, or 80m)
484      * @param memoryMaximumSize string to pass to VM
485      */

486     public void setMemoryMaximumSize(String JavaDoc memoryMaximumSize) {
487         this.memoryMaximumSize = memoryMaximumSize;
488     }
489
490     /**
491      * Gets the memoryMaximumSize flag.
492      * @return the memoryMaximumSize flag
493      */

494     public String JavaDoc getMemoryMaximumSize() {
495         return memoryMaximumSize;
496     }
497
498     /**
499      * Set the Java source file encoding name.
500      * @param encoding the source file encoding
501      */

502     public void setEncoding(String JavaDoc encoding) {
503         this.encoding = encoding;
504     }
505
506     /**
507      * Gets the java source file encoding name.
508      * @return the source file encoding name
509      */

510     public String JavaDoc getEncoding() {
511         return encoding;
512     }
513
514     /**
515      * Indicates whether source should be compiled
516      * with debug information; defaults to off.
517      * @param debug if true compile with debug information
518      */

519     public void setDebug(boolean debug) {
520         this.debug = debug;
521     }
522
523     /**
524      * Gets the debug flag.
525      * @return the debug flag
526      */

527     public boolean getDebug() {
528         return debug;
529     }
530
531     /**
532      * If true, compiles with optimization enabled.
533      * @param optimize if true compile with optimization enabled
534      */

535     public void setOptimize(boolean optimize) {
536         this.optimize = optimize;
537     }
538
539     /**
540      * Gets the optimize flag.
541      * @return the optimize flag
542      */

543     public boolean getOptimize() {
544         return optimize;
545     }
546
547     /**
548      * Enables dependency-tracking for compilers
549      * that support this (jikes and classic).
550      * @param depend if true enable dependency-tracking
551      */

552     public void setDepend(boolean depend) {
553         this.depend = depend;
554     }
555
556     /**
557      * Gets the depend flag.
558      * @return the depend flag
559      */

560     public boolean getDepend() {
561         return depend;
562     }
563
564     /**
565      * If true, asks the compiler for verbose output.
566      * @param verbose if true, asks the compiler for verbose output
567      */

568     public void setVerbose(boolean verbose) {
569         this.verbose = verbose;
570     }
571
572     /**
573      * Gets the verbose flag.
574      * @return the verbose flag
575      */

576     public boolean getVerbose() {
577         return verbose;
578     }
579
580     /**
581      * Sets the target VM that the classes will be compiled for. Valid
582      * values depend on the compiler, for jdk 1.4 the valid values are
583      * "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "5" and "6".
584      * @param target the target VM
585      */

586     public void setTarget(String JavaDoc target) {
587         this.targetAttribute = target;
588     }
589
590     /**
591      * Gets the target VM that the classes will be compiled for.
592      * @return the target VM
593      */

594     public String JavaDoc getTarget() {
595         return targetAttribute != null
596             ? targetAttribute
597             : getProject().getProperty(MagicNames.BUILD_JAVAC_TARGET);
598     }
599
600     /**
601      * If true, includes Ant's own classpath in the classpath.
602      * @param include if true, includes Ant's own classpath in the classpath
603      */

604     public void setIncludeantruntime(boolean include) {
605         includeAntRuntime = include;
606     }
607
608     /**
609      * Gets whether or not the ant classpath is to be included in the classpath.
610      * @return whether or not the ant classpath is to be included in the classpath
611      */

612     public boolean getIncludeantruntime() {
613         return includeAntRuntime;
614     }
615
616     /**
617      * If true, includes the Java runtime libraries in the classpath.
618      * @param include if true, includes the Java runtime libraries in the classpath
619      */

620     public void setIncludejavaruntime(boolean include) {
621         includeJavaRuntime = include;
622     }
623
624     /**
625      * Gets whether or not the java runtime should be included in this
626      * task's classpath.
627      * @return the includejavaruntime attribute
628      */

629     public boolean getIncludejavaruntime() {
630         return includeJavaRuntime;
631     }
632
633     /**
634      * If true, forks the javac compiler.
635      *
636      * @param f "true|false|on|off|yes|no"
637      */

638     public void setFork(boolean f) {
639         fork = f;
640     }
641
642     /**
643      * Sets the name of the javac executable.
644      *
645      * <p>Ignored unless fork is true or extJavac has been specified
646      * as the compiler.</p>
647      * @param forkExec the name of the executable
648      */

649     public void setExecutable(String JavaDoc forkExec) {
650         forkedExecutable = forkExec;
651     }
652
653     /**
654      * The value of the executable attribute, if any.
655      *
656      * @since Ant 1.6
657      * @return the name of the java executable
658      */

659     public String JavaDoc getExecutable() {
660         return forkedExecutable;
661     }
662
663     /**
664      * Is this a forked invocation of JDK's javac?
665      * @return true if this is a forked invocation
666      */

667     public boolean isForkedJavac() {
668         return fork || "extJavac".equals(getCompiler());
669     }
670
671     /**
672      * The name of the javac executable to use in fork-mode.
673      *
674      * <p>This is either the name specified with the executable
675      * attribute or the full path of the javac compiler of the VM Ant
676      * is currently running in - guessed by Ant.</p>
677      *
678      * <p>You should <strong>not</strong> invoke this method if you
679      * want to get the value of the executable command - use {@link
680      * #getExecutable getExecutable} for this.</p>
681      * @return the name of the javac executable
682      */

683     public String JavaDoc getJavacExecutable() {
684         if (forkedExecutable == null && isForkedJavac()) {
685             forkedExecutable = getSystemJavac();
686         } else if (forkedExecutable != null && !isForkedJavac()) {
687             forkedExecutable = null;
688         }
689         return forkedExecutable;
690     }
691
692     /**
693      * If true, enables the -nowarn option.
694      * @param flag if true, enable the -nowarn option
695      */

696     public void setNowarn(boolean flag) {
697         this.nowarn = flag;
698     }
699
700     /**
701      * Should the -nowarn option be used.
702      * @return true if the -nowarn option should be used
703      */

704     public boolean getNowarn() {
705         return nowarn;
706     }
707
708     /**
709      * Adds an implementation specific command-line argument.
710      * @return a ImplementationSpecificArgument to be configured
711      */

712     public ImplementationSpecificArgument createCompilerArg() {
713         ImplementationSpecificArgument arg =
714             new ImplementationSpecificArgument();
715         facade.addImplementationArgument(arg);
716         return arg;
717     }
718
719     /**
720      * Get the additional implementation specific command line arguments.
721      * @return array of command line arguments, guaranteed to be non-null.
722      */

723     public String JavaDoc[] getCurrentCompilerArgs() {
724         String JavaDoc chosen = facade.getExplicitChoice();
725         try {
726             // make sure facade knows about magic properties and fork setting
727
String JavaDoc appliedCompiler = getCompiler();
728             facade.setImplementation(appliedCompiler);
729
730             String JavaDoc[] result = facade.getArgs();
731
732             String JavaDoc altCompilerName = getAltCompilerName(facade.getImplementation());
733
734             if (result.length == 0 && altCompilerName != null) {
735                 facade.setImplementation(altCompilerName);
736                 result = facade.getArgs();
737             }
738
739             return result;
740
741         } finally {
742             facade.setImplementation(chosen);
743         }
744     }
745
746     private String JavaDoc getAltCompilerName(String JavaDoc anImplementation) {
747         if (JAVAC16.equalsIgnoreCase(anImplementation)
748                 || JAVAC15.equalsIgnoreCase(anImplementation)
749                 || JAVAC14.equalsIgnoreCase(anImplementation)
750                 || JAVAC13.equalsIgnoreCase(anImplementation)) {
751             return MODERN;
752         }
753         if (JAVAC12.equalsIgnoreCase(anImplementation)
754                 || JAVAC11.equalsIgnoreCase(anImplementation)) {
755             return CLASSIC;
756         }
757         if (MODERN.equalsIgnoreCase(anImplementation)) {
758             String JavaDoc nextSelected = assumedJavaVersion();
759             if (JAVAC16.equalsIgnoreCase(nextSelected)
760                     || JAVAC15.equalsIgnoreCase(nextSelected)
761                     || JAVAC14.equalsIgnoreCase(nextSelected)
762                     || JAVAC13.equalsIgnoreCase(nextSelected)) {
763                 return nextSelected;
764             }
765         }
766         if (CLASSIC.equals(anImplementation)) {
767             return assumedJavaVersion();
768         }
769         if (EXTJAVAC.equalsIgnoreCase(anImplementation)) {
770             return assumedJavaVersion();
771         }
772         return null;
773     }
774
775     /**
776      * Where Ant should place temporary files.
777      *
778      * @since Ant 1.6
779      * @param tmpDir the temporary directory
780      */

781     public void setTempdir(File JavaDoc tmpDir) {
782         this.tmpDir = tmpDir;
783     }
784
785     /**
786      * Where Ant should place temporary files.
787      *
788      * @since Ant 1.6
789      * @return the temporary directory
790      */

791     public File JavaDoc getTempdir() {
792         return tmpDir;
793     }
794
795     /**
796      * Executes the task.
797      * @exception BuildException if an error occurs
798      */

799     public void execute() throws BuildException {
800         checkParameters();
801         resetFileLists();
802
803         // scan source directories and dest directory to build up
804
// compile lists
805
String JavaDoc[] list = src.list();
806         for (int i = 0; i < list.length; i++) {
807             File JavaDoc srcDir = getProject().resolveFile(list[i]);
808             if (!srcDir.exists()) {
809                 throw new BuildException("srcdir \""
810                                          + srcDir.getPath()
811                                          + "\" does not exist!", getLocation());
812             }
813
814             DirectoryScanner ds = this.getDirectoryScanner(srcDir);
815             String JavaDoc[] files = ds.getIncludedFiles();
816
817             scanDir(srcDir, destDir != null ? destDir : srcDir, files);
818         }
819
820         compile();
821     }
822
823     /**
824      * Clear the list of files to be compiled and copied..
825      */

826     protected void resetFileLists() {
827         compileList = new File JavaDoc[0];
828     }
829
830     /**
831      * Scans the directory looking for source files to be compiled.
832      * The results are returned in the class variable compileList
833      *
834      * @param srcDir The source directory
835      * @param destDir The destination directory
836      * @param files An array of filenames
837      */

838     protected void scanDir(File JavaDoc srcDir, File JavaDoc destDir, String JavaDoc[] files) {
839         GlobPatternMapper m = new GlobPatternMapper();
840         m.setFrom("*.java");
841         m.setTo("*.class");
842         SourceFileScanner sfs = new SourceFileScanner(this);
843         File JavaDoc[] newFiles = sfs.restrictAsFiles(files, srcDir, destDir, m);
844
845         if (newFiles.length > 0) {
846             File JavaDoc[] newCompileList
847                 = new File JavaDoc[compileList.length + newFiles.length];
848             System.arraycopy(compileList, 0, newCompileList, 0,
849                     compileList.length);
850             System.arraycopy(newFiles, 0, newCompileList,
851                     compileList.length, newFiles.length);
852             compileList = newCompileList;
853         }
854     }
855
856     /**
857      * Gets the list of files to be compiled.
858      * @return the list of files as an array
859      */

860     public File JavaDoc[] getFileList() {
861         return compileList;
862     }
863
864     /**
865      * Is the compiler implementation a jdk compiler
866      *
867      * @param compilerImpl the name of the compiler implementation
868      * @return true if compilerImpl is "modern", "classic",
869      * "javac1.1", "javac1.2", "javac1.3", "javac1.4", "javac1.5" or
870      * "javac1.6".
871      */

872     protected boolean isJdkCompiler(String JavaDoc compilerImpl) {
873         return MODERN.equals(compilerImpl)
874             || CLASSIC.equals(compilerImpl)
875             || JAVAC16.equals(compilerImpl)
876             || JAVAC15.equals(compilerImpl)
877             || JAVAC14.equals(compilerImpl)
878             || JAVAC13.equals(compilerImpl)
879             || JAVAC12.equals(compilerImpl)
880             || JAVAC11.equals(compilerImpl);
881     }
882
883     /**
884      * @return the executable name of the java compiler
885      */

886     protected String JavaDoc getSystemJavac() {
887         return JavaEnvUtils.getJdkExecutable("javac");
888     }
889
890     /**
891      * Choose the implementation for this particular task.
892      * @param compiler the name of the compiler
893      * @since Ant 1.5
894      */

895     public void setCompiler(String JavaDoc compiler) {
896         facade.setImplementation(compiler);
897     }
898
899     /**
900      * The implementation for this particular task.
901      *
902      * <p>Defaults to the build.compiler property but can be overridden
903      * via the compiler and fork attributes.</p>
904      *
905      * <p>If fork has been set to true, the result will be extJavac
906      * and not classic or java1.2 - no matter what the compiler
907      * attribute looks like.</p>
908      *
909      * @see #getCompilerVersion
910      * @return the compiler.
911      * @since Ant 1.5
912      */

913     public String JavaDoc getCompiler() {
914         String JavaDoc compilerImpl = getCompilerVersion();
915         if (fork) {
916             if (isJdkCompiler(compilerImpl)) {
917                 compilerImpl = "extJavac";
918             } else {
919                 log("Since compiler setting isn't classic or modern,"
920                     + "ignoring fork setting.", Project.MSG_WARN);
921             }
922         }
923         return compilerImpl;
924     }
925
926     /**
927      * The implementation for this particular task.
928      *
929      * <p>Defaults to the build.compiler property but can be overridden
930      * via the compiler attribute.</p>
931      *
932      * <p>This method does not take the fork attribute into
933      * account.</p>
934      *
935      * @see #getCompiler
936      * @return the compiler.
937      *
938      * @since Ant 1.5
939      */

940     public String JavaDoc getCompilerVersion() {
941         facade.setMagicValue(getProject().getProperty("build.compiler"));
942         return facade.getImplementation();
943     }
944
945     /**
946      * Check that all required attributes have been set and nothing
947      * silly has been entered.
948      *
949      * @since Ant 1.5
950      * @exception BuildException if an error occurs
951      */

952     protected void checkParameters() throws BuildException {
953         if (src == null) {
954             throw new BuildException("srcdir attribute must be set!",
955                                      getLocation());
956         }
957         if (src.size() == 0) {
958             throw new BuildException("srcdir attribute must be set!",
959                                      getLocation());
960         }
961
962         if (destDir != null && !destDir.isDirectory()) {
963             throw new BuildException("destination directory \""
964                                      + destDir
965                                      + "\" does not exist "
966                                      + "or is not a directory", getLocation());
967         }
968     }
969
970     /**
971      * Perform the compilation.
972      *
973      * @since Ant 1.5
974      */

975     protected void compile() {
976         String JavaDoc compilerImpl = getCompiler();
977
978         if (compileList.length > 0) {
979             log("Compiling " + compileList.length + " source file"
980                 + (compileList.length == 1 ? "" : "s")
981                 + (destDir != null ? " to " + destDir : ""));
982
983             if (listFiles) {
984                 for (int i = 0; i < compileList.length; i++) {
985                   String JavaDoc filename = compileList[i].getAbsolutePath();
986                   log(filename);
987                 }
988             }
989
990             CompilerAdapter adapter =
991                 CompilerAdapterFactory.getCompiler(compilerImpl, this);
992
993             // now we need to populate the compiler adapter
994
adapter.setJavac(this);
995
996             // finally, lets execute the compiler!!
997
if (!adapter.execute()) {
998                 if (failOnError) {
999                     throw new BuildException(FAIL_MSG, getLocation());
1000                } else {
1001                    log(FAIL_MSG, Project.MSG_ERR);
1002                }
1003            }
1004        }
1005    }
1006
1007    /**
1008     * Adds an "compiler" attribute to Commandline$Attribute used to
1009     * filter command line attributes based on the current
1010     * implementation.
1011     */

1012    public class ImplementationSpecificArgument extends
1013        org.apache.tools.ant.util.facade.ImplementationSpecificArgument {
1014
1015        /**
1016         * @param impl the name of the compiler
1017         */

1018        public void setCompiler(String JavaDoc impl) {
1019            super.setImplementation(impl);
1020        }
1021    }
1022
1023}
1024
Popular Tags