01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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 java.lang.reflect.Method;
22: import org.apache.tools.ant.BuildException;
23: import org.apache.tools.ant.Project;
24: import org.apache.tools.ant.types.Commandline;
25:
26: /**
27: * The implementation of the javac compiler for JDK 1.3
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 Javac13 extends DefaultCompilerAdapter {
34:
35: /**
36: * Integer returned by the "Modern" jdk1.3 compiler to indicate success.
37: */
38: private static final int MODERN_COMPILER_SUCCESS = 0;
39:
40: /**
41: * Run the compilation.
42: * @return true if the compiler ran with a zero exit result (ok)
43: * @exception BuildException if the compilation has problems.
44: */
45: public boolean execute() throws BuildException {
46: attributes.log("Using modern compiler", Project.MSG_VERBOSE);
47: Commandline cmd = setupModernJavacCommand();
48:
49: // Use reflection to be able to build on all JDKs >= 1.1:
50: try {
51: Class c = Class.forName("com.sun.tools.javac.Main");
52: Object compiler = c.newInstance();
53: Method compile = c.getMethod("compile",
54: new Class[] { (new String[] {}).getClass() });
55: int result = ((Integer) compile.invoke(compiler,
56: new Object[] { cmd.getArguments() })).intValue();
57: return (result == MODERN_COMPILER_SUCCESS);
58: } catch (Exception ex) {
59: if (ex instanceof BuildException) {
60: throw (BuildException) ex;
61: } else {
62: throw new BuildException(
63: "Error starting modern compiler", ex, location);
64: }
65: }
66: }
67: }
|