1 | /*
|
---|
2 | * Copyright 2001-2002,2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs.compilers;
|
---|
19 |
|
---|
20 | import java.io.IOException;
|
---|
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.taskdefs.LogOutputStream;
|
---|
27 | import org.apache.tools.ant.types.Commandline;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * The implementation of the javac compiler for JDK 1.2
|
---|
31 | * This is primarily a cut-and-paste from the original javac task before it
|
---|
32 | * was refactored.
|
---|
33 | *
|
---|
34 | * @since Ant 1.3
|
---|
35 | */
|
---|
36 | public class Javac12 extends DefaultCompilerAdapter {
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Run the compilation.
|
---|
40 | *
|
---|
41 | * @exception BuildException if the compilation has problems.
|
---|
42 | */
|
---|
43 | public boolean execute() throws BuildException {
|
---|
44 | attributes.log("Using classic compiler", Project.MSG_VERBOSE);
|
---|
45 | Commandline cmd = setupJavacCommand(true);
|
---|
46 |
|
---|
47 | OutputStream logstr = new LogOutputStream(attributes, Project.MSG_WARN);
|
---|
48 | try {
|
---|
49 | // Create an instance of the compiler, redirecting output to
|
---|
50 | // the project log
|
---|
51 | Class c = Class.forName("sun.tools.javac.Main");
|
---|
52 | Constructor cons =
|
---|
53 | c.getConstructor(new Class[] {OutputStream.class,
|
---|
54 | String.class});
|
---|
55 | Object compiler
|
---|
56 | = cons.newInstance(new Object[] {logstr, "javac"});
|
---|
57 |
|
---|
58 | // Call the compile() method
|
---|
59 | Method compile = c.getMethod("compile",
|
---|
60 | new Class [] {String[].class});
|
---|
61 | Boolean ok =
|
---|
62 | (Boolean) compile.invoke(compiler,
|
---|
63 | new Object[] {cmd.getArguments()});
|
---|
64 | return ok.booleanValue();
|
---|
65 | } catch (ClassNotFoundException ex) {
|
---|
66 | throw new BuildException("Cannot use classic compiler, as it is "
|
---|
67 | + "not available. A common solution is "
|
---|
68 | + "to set the environment variable"
|
---|
69 | + " JAVA_HOME to your jdk directory.",
|
---|
70 | location);
|
---|
71 | } catch (Exception ex) {
|
---|
72 | if (ex instanceof BuildException) {
|
---|
73 | throw (BuildException) ex;
|
---|
74 | } else {
|
---|
75 | throw new BuildException("Error starting classic compiler: ",
|
---|
76 | ex, location);
|
---|
77 | }
|
---|
78 | } finally {
|
---|
79 | try {
|
---|
80 | logstr.close();
|
---|
81 | } catch (IOException e) {
|
---|
82 | // plain impossible
|
---|
83 | throw new BuildException(e);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|