KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > compilers > Jikes


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.compilers;
20
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Project;
23 import org.apache.tools.ant.types.Commandline;
24 import org.apache.tools.ant.types.Path;
25
26 /**
27  * The implementation of the jikes compiler.
28  * This is primarily a cut-and-paste from the original javac task before it
29  * was refactored.
30  *
31  * @since Ant 1.3
32  */

33 public class Jikes extends DefaultCompilerAdapter {
34
35     /**
36      * Performs a compile using the Jikes compiler from IBM.
37      * Mostly of this code is identical to doClassicCompile()
38      * However, it does not support all options like
39      * extdirs, deprecation and so on, because
40      * there is no option in jikes and I don't understand
41      * what they should do.
42      *
43      * It has been successfully tested with jikes >1.10.
44      * @return true if the compilation succeeded
45      * @throws BuildException on error
46      */

47     public boolean execute() throws BuildException {
48         attributes.log("Using jikes compiler", Project.MSG_VERBOSE);
49
50         Commandline cmd = new Commandline();
51
52         // For -sourcepath, use the "sourcepath" value if present.
53
// Otherwise default to the "srcdir" value.
54
Path sourcepath = null;
55         if (compileSourcepath != null) {
56             sourcepath = compileSourcepath;
57         } else {
58             sourcepath = src;
59         }
60         // If the buildfile specifies sourcepath="", then don't
61
// output any sourcepath.
62
if (sourcepath.size() > 0) {
63             cmd.createArgument().setValue("-sourcepath");
64             cmd.createArgument().setPath(sourcepath);
65         }
66
67         Path classpath = new Path(project);
68
69         if (bootclasspath == null || bootclasspath.size() == 0) {
70             // no bootclasspath, therefore, get one from the java runtime
71
includeJavaRuntime = true;
72         } else {
73             // there is a bootclasspath stated. By default, the
74
// includeJavaRuntime is false. If the user has stated a
75
// bootclasspath and said to include the java runtime, it's on
76
// their head!
77
}
78         classpath.append(getCompileClasspath());
79
80         // if the user has set JIKESPATH we should add the contents as well
81
String JavaDoc jikesPath = System.getProperty("jikes.class.path");
82         if (jikesPath != null) {
83             classpath.append(new Path(project, jikesPath));
84         }
85
86         if (extdirs != null && extdirs.size() > 0) {
87             cmd.createArgument().setValue("-extdirs");
88             cmd.createArgument().setPath(extdirs);
89         }
90
91         String JavaDoc exec = getJavac().getExecutable();
92         cmd.setExecutable(exec == null ? "jikes" : exec);
93
94         if (deprecation) {
95             cmd.createArgument().setValue("-deprecation");
96         }
97
98         if (destDir != null) {
99             cmd.createArgument().setValue("-d");
100             cmd.createArgument().setFile(destDir);
101         }
102
103         cmd.createArgument().setValue("-classpath");
104         cmd.createArgument().setPath(classpath);
105
106         if (encoding != null) {
107             cmd.createArgument().setValue("-encoding");
108             cmd.createArgument().setValue(encoding);
109         }
110         if (debug) {
111             String JavaDoc debugLevel = attributes.getDebugLevel();
112             if (debugLevel != null) {
113                 cmd.createArgument().setValue("-g:" + debugLevel);
114             } else {
115                 cmd.createArgument().setValue("-g");
116             }
117         } else {
118             cmd.createArgument().setValue("-g:none");
119         }
120         if (optimize) {
121             cmd.createArgument().setValue("-O");
122         }
123         if (verbose) {
124             cmd.createArgument().setValue("-verbose");
125         }
126         if (depend) {
127             cmd.createArgument().setValue("-depend");
128         }
129
130         if (target != null) {
131             cmd.createArgument().setValue("-target");
132             cmd.createArgument().setValue(target);
133         }
134
135         /**
136          * XXX
137          * Perhaps we shouldn't use properties for these
138          * three options (emacs mode, warnings and pedantic),
139          * but include it in the javac directive?
140          */

141
142         /**
143          * Jikes has the nice feature to print error
144          * messages in a form readable by emacs, so
145          * that emacs can directly set the cursor
146          * to the place, where the error occurred.
147          */

148         String JavaDoc emacsProperty = project.getProperty("build.compiler.emacs");
149         if (emacsProperty != null && Project.toBoolean(emacsProperty)) {
150             cmd.createArgument().setValue("+E");
151         }
152
153         /**
154          * Jikes issues more warnings that javac, for
155          * example, when you have files in your classpath
156          * that don't exist. As this is often the case, these
157          * warning can be pretty annoying.
158          */

159         String JavaDoc warningsProperty =
160             project.getProperty("build.compiler.warnings");
161         if (warningsProperty != null) {
162             attributes.log("!! the build.compiler.warnings property is "
163                            + "deprecated. !!", Project.MSG_WARN);
164             attributes.log("!! Use the nowarn attribute instead. !!",
165                            Project.MSG_WARN);
166             if (!Project.toBoolean(warningsProperty)) {
167                 cmd.createArgument().setValue("-nowarn");
168             }
169         }
170         if (attributes.getNowarn()) {
171             cmd.createArgument().setValue("-nowarn");
172         }
173
174         /**
175          * Jikes can issue pedantic warnings.
176          */

177         String JavaDoc pedanticProperty =
178             project.getProperty("build.compiler.pedantic");
179         if (pedanticProperty != null && Project.toBoolean(pedanticProperty)) {
180             cmd.createArgument().setValue("+P");
181         }
182
183         /**
184          * Jikes supports something it calls "full dependency
185          * checking", see the jikes documentation for differences
186          * between -depend and +F.
187          */

188         String JavaDoc fullDependProperty =
189             project.getProperty("build.compiler.fulldepend");
190         if (fullDependProperty != null
191             && Project.toBoolean(fullDependProperty)) {
192             cmd.createArgument().setValue("+F");
193         }
194
195         if (attributes.getSource() != null) {
196             cmd.createArgument().setValue("-source");
197             String JavaDoc source = attributes.getSource();
198             if (source.equals("1.1") || source.equals("1.2")) {
199                 // support for -source 1.1 and -source 1.2 has been
200
// added with JDK 1.4.2, Jikes doesn't like it
201
attributes.log("Jikes doesn't support '-source "
202                                + source + "', will use '-source 1.3' instead");
203                 cmd.createArgument().setValue("1.3");
204             } else {
205                 cmd.createArgument().setValue(source);
206             }
207         }
208
209         addCurrentCompilerArgs(cmd);
210
211         int firstFileName = cmd.size();
212
213         Path boot = getBootClassPath();
214         if (boot.size() > 0) {
215             cmd.createArgument().setValue("-bootclasspath");
216             cmd.createArgument().setPath(boot);
217         }
218
219         logAndAddFilesToCompile(cmd);
220
221         return
222             executeExternalCompile(cmd.getCommandline(), firstFileName) == 0;
223     }
224
225
226 }
227
Popular Tags