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.io.OutputStream;
22: import java.lang.reflect.Constructor;
23: import java.lang.reflect.Method;
24: import org.apache.tools.ant.BuildException;
25: import org.apache.tools.ant.Project;
26: import org.apache.tools.ant.util.JavaEnvUtils;
27: import org.apache.tools.ant.util.FileUtils;
28: import org.apache.tools.ant.taskdefs.LogOutputStream;
29: import org.apache.tools.ant.types.Commandline;
30:
31: /**
32: * The implementation of the javac compiler for JDK 1.2
33: * This is primarily a cut-and-paste from the original javac task before it
34: * was refactored.
35: *
36: * @since Ant 1.3
37: */
38: public class Javac12 extends DefaultCompilerAdapter {
39: protected static final String CLASSIC_COMPILER_CLASSNAME = "sun.tools.javac.Main";
40:
41: /**
42: * Run the compilation.
43: * @return true if the compiler ran with a zero exit result (ok)
44: * @exception BuildException if the compilation has problems.
45: */
46: public boolean execute() throws BuildException {
47: attributes.log("Using classic compiler", Project.MSG_VERBOSE);
48: Commandline cmd = setupJavacCommand(true);
49:
50: OutputStream logstr = new LogOutputStream(attributes,
51: Project.MSG_WARN);
52: try {
53: // Create an instance of the compiler, redirecting output to
54: // the project log
55: Class c = Class.forName(CLASSIC_COMPILER_CLASSNAME);
56: Constructor cons = c.getConstructor(new Class[] {
57: OutputStream.class, String.class });
58: Object compiler = cons.newInstance(new Object[] { logstr,
59: "javac" });
60:
61: // Call the compile() method
62: Method compile = c.getMethod("compile",
63: new Class[] { String[].class });
64: Boolean ok = (Boolean) compile.invoke(compiler,
65: new Object[] { cmd.getArguments() });
66: return ok.booleanValue();
67: } catch (ClassNotFoundException ex) {
68: throw new BuildException(
69: "Cannot use classic compiler , as it is "
70: + "not available. \n"
71: + " A common solution is "
72: + "to set the environment variable"
73: + " JAVA_HOME to your jdk directory.\n"
74: + "It is currently set to \""
75: + JavaEnvUtils.getJavaHome() + "\"",
76: location);
77: } catch (Exception ex) {
78: if (ex instanceof BuildException) {
79: throw (BuildException) ex;
80: } else {
81: throw new BuildException(
82: "Error starting classic compiler: ", ex,
83: location);
84: }
85: } finally {
86: FileUtils.close(logstr);
87: }
88: }
89: }
|