Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Portions Copyrighted 2008 Sun Microsystems, Inc. |
38 |
*/ |
39 |
|
40 |
package org.netbeans.nbbuild; |
41 |
|
42 |
import java.io.File; |
43 |
import java.lang.reflect.Method; |
44 |
import org.apache.tools.ant.AntClassLoader; |
45 |
import org.apache.tools.ant.BuildException; |
46 |
import org.apache.tools.ant.Project; |
47 |
import org.apache.tools.ant.taskdefs.Javac; |
48 |
import org.apache.tools.ant.taskdefs.compilers.CompilerAdapter; |
49 |
import org.apache.tools.ant.taskdefs.compilers.DefaultCompilerAdapter; |
50 |
import org.apache.tools.ant.types.Commandline; |
51 |
import org.apache.tools.ant.types.Path; |
52 |
|
53 |
/** |
54 |
* Runs javac in-process from a local implementation, e.g. so as to ensure JSR 269 is supported. |
55 |
* Works just like using the default <code>modern</code> compiler, except loaded from the specified place. |
56 |
*/ |
57 |
public class CustomJavac extends Javac { |
58 |
|
59 |
public CustomJavac() {} |
60 |
|
61 |
private Path javacClasspath; |
62 |
public void addJavacClasspath(Path cp) { |
63 |
javacClasspath = cp; |
64 |
} |
65 |
|
66 |
@Override |
67 |
protected void compile() { |
68 |
if (compileList.length > 0) { |
69 |
log("Compiling " + compileList.length + " source file" + |
70 |
(compileList.length == 1 ? "" : "s") + |
71 |
(getDestdir() != null ? " to " + getDestdir() : "")); |
72 |
if (listFiles) { |
73 |
for (File f : compileList) { |
74 |
log(f.getAbsolutePath()); |
75 |
} |
76 |
} |
77 |
CompilerAdapter adapter = new CustomAdapter(); |
78 |
adapter.setJavac(this); |
79 |
if (adapter.execute()) { |
80 |
// XXX updateDirList |
81 |
} else { |
82 |
// other modes not supported, see below |
83 |
throw new BuildException("Compile failed; see the compiler error output for details.", getLocation()); |
84 |
} |
85 |
} |
86 |
} |
87 |
|
88 |
@Override |
89 |
public void setErrorProperty(String errorProperty) { |
90 |
throw new UnsupportedOperationException(); |
91 |
} |
92 |
|
93 |
@Override |
94 |
public void setUpdatedProperty(String updatedProperty) { |
95 |
throw new UnsupportedOperationException(); |
96 |
} |
97 |
|
98 |
@Override |
99 |
public void setFailonerror(boolean fail) { |
100 |
throw new UnsupportedOperationException(); |
101 |
} |
102 |
|
103 |
|
104 |
private static class CustomAdapter extends DefaultCompilerAdapter { |
105 |
|
106 |
public boolean execute() throws BuildException { |
107 |
// adapted from Javac13 |
108 |
Commandline cmd = setupModernJavacCommand(); |
109 |
try { |
110 |
Path cp = ((CustomJavac) getJavac()).javacClasspath; |
111 |
ClassLoader cl = new AntClassLoader(getJavac().getProject(), cp); |
112 |
Class c = Class.forName("com.sun.tools.javac.Main", true, cl); |
113 |
getJavac().log("Running javac from " + c.getProtectionDomain().getCodeSource().getLocation(), Project.MSG_VERBOSE); |
114 |
Method compile = c.getMethod("compile", String[].class); |
115 |
int result = (Integer) compile.invoke(null, (Object) cmd.getArguments()); |
116 |
return result == 0; |
117 |
} catch (Exception ex) { |
118 |
throw new BuildException("Error starting compiler: " + ex, ex, location); |
119 |
} |
120 |
} |
121 |
|
122 |
} |
123 |
|
124 |
} |