KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > javah > SunJavah


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 package org.apache.tools.ant.taskdefs.optional.javah;
19
20 import java.io.File JavaDoc;
21
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.launch.Locator;
24 import org.apache.tools.ant.taskdefs.ExecuteJava;
25 import org.apache.tools.ant.taskdefs.optional.Javah;
26 import org.apache.tools.ant.types.Commandline;
27 import org.apache.tools.ant.types.Path;
28
29
30 /**
31  * Adapter to com.sun.tools.javah.oldjavah.Main or com.sun.tools.javah.Main.
32  *
33  * @since Ant 1.6.3
34  */

35 public class SunJavah implements JavahAdapter {
36
37     /** the name of the javah adapter - sun */
38     public static final String JavaDoc IMPLEMENTATION_NAME = "sun";
39
40     /**
41      * Performs the actual compilation.
42      * @param javah the calling javah task.
43      * @return true if the compilation was successful.
44      * @throws BuildException if there is an error.
45      * @since Ant 1.6.3
46      */

47     public boolean compile(Javah javah) throws BuildException {
48         Commandline cmd = setupJavahCommand(javah);
49         ExecuteJava ej = new ExecuteJava();
50         Class JavaDoc c = null;
51         try {
52             try {
53                 // first search for the "old" javah class in 1.4.2 tools.jar
54
c = Class.forName("com.sun.tools.javah.oldjavah.Main");
55             } catch (ClassNotFoundException JavaDoc cnfe) {
56                 // assume older than 1.4.2 tools.jar
57
c = Class.forName("com.sun.tools.javah.Main");
58             }
59         } catch (ClassNotFoundException JavaDoc ex) {
60             throw new BuildException(
61                 "Can't load javah", ex, javah.getLocation());
62         }
63         cmd.setExecutable(c.getName());
64         ej.setJavaCommand(cmd);
65         File JavaDoc f = Locator.getClassSource(c);
66         if (f != null) {
67             ej.setClasspath(new Path(javah.getProject(), f.getPath()));
68         }
69         return ej.fork(javah) == 0;
70     }
71
72     private Commandline setupJavahCommand(Javah javah) {
73         Commandline cmd = new Commandline();
74
75         if (javah.getDestdir() != null) {
76             cmd.createArgument().setValue("-d");
77             cmd.createArgument().setFile(javah.getDestdir());
78         }
79
80         if (javah.getOutputfile() != null) {
81             cmd.createArgument().setValue("-o");
82             cmd.createArgument().setFile(javah.getOutputfile());
83         }
84
85         if (javah.getClasspath() != null) {
86             cmd.createArgument().setValue("-classpath");
87             cmd.createArgument().setPath(javah.getClasspath());
88         }
89
90         if (javah.getVerbose()) {
91             cmd.createArgument().setValue("-verbose");
92         }
93         if (javah.getOld()) {
94             cmd.createArgument().setValue("-old");
95         }
96         if (javah.getForce()) {
97             cmd.createArgument().setValue("-force");
98         }
99         if (javah.getStubs() && !javah.getOld()) {
100             throw new BuildException(
101                 "stubs only available in old mode.", javah.getLocation());
102         }
103
104         if (javah.getStubs()) {
105             cmd.createArgument().setValue("-stubs");
106         }
107         Path bcp = new Path(javah.getProject());
108         if (javah.getBootclasspath() != null) {
109             bcp.append(javah.getBootclasspath());
110         }
111         bcp = bcp.concatSystemBootClasspath("ignore");
112         if (bcp.size() > 0) {
113             cmd.createArgument().setValue("-bootclasspath");
114             cmd.createArgument().setPath(bcp);
115         }
116
117         cmd.addArguments(javah.getCurrentArgs());
118
119         javah.logAndAddFiles(cmd);
120         return cmd;
121     }
122
123 }
124
Popular Tags