KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > compilers > AptCompilerAdapter


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
19 package org.apache.tools.ant.taskdefs.compilers;
20
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Project;
23 import org.apache.tools.ant.taskdefs.Apt;
24 import org.apache.tools.ant.types.Commandline;
25 import org.apache.tools.ant.types.Path;
26
27 import java.io.File JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Vector JavaDoc;
31
32
33 /**
34  * The implementation of the apt compiler for JDK 1.5
35  * <p/>
36  * As usual, the low level entry points for Java tools are neither documented or
37  * stable; this entry point may change from that of 1.5.0_01-b08 without any
38  * warning at all. The IDE decompile of the tool entry points is as follows:
39  * <pre>
40  * public class Main {
41  * public Main() ;
42  * <p/>
43  * public static transient void main(String... strings) ;
44  * <p/>
45  * public static transient int process(String... strings);
46  * <p/>
47  * public static transient int process(PrintWriter printWriter,
48  * String... strings) ;
49  * public static transient int process(
50  * AnnotationProcessorFactory annotationProcessorFactory,
51  * String... strings) ;
52  * <p/>
53  * public static transient int process(
54  * AnnotationProcessorFactory annotationProcessorFactory,
55  * PrintWriter printWriter,
56  * String... strings);
57  * private static transient int processing(
58  * AnnotationProcessorFactory annotationProcessorFactory,
59  * PrintWriter printWriter,
60  * String... strings) ;
61  * }
62  * </pre>
63  *
64  * This Adapter is designed to run Apt in-JVM, an option that is not actually
65  * exposed to end-users, because it was too brittle during beta testing; classpath
66  * problems being the core issue.
67  *
68  *
69  *
70  * @since Ant 1.7
71  */

72 public class AptCompilerAdapter extends DefaultCompilerAdapter {
73
74     /**
75      * Integer returned by the Apt compiler to indicate success.
76      */

77     private static final int APT_COMPILER_SUCCESS = 0;
78     /**
79      * class in tools.jar that implements APT
80      */

81     public static final String JavaDoc APT_ENTRY_POINT = "com.sun.tools.apt.Main";
82
83     /**
84      * method used to compile.
85      */

86     public static final String JavaDoc APT_METHOD_NAME = "process";
87
88     /**
89      * Get the facade task that fronts this adapter
90      *
91      * @return task instance
92      * @see DefaultCompilerAdapter#getJavac()
93      */

94     protected Apt getApt() {
95         return (Apt) getJavac();
96     }
97
98     /**
99      * Using the front end arguments, set up the command line to run Apt
100      *
101      * @param apt task
102      * @param cmd command that is set up with the various switches from the task
103      * options
104      */

105     static void setAptCommandlineSwitches(Apt apt, Commandline cmd) {
106
107         if (!apt.isCompile()) {
108             cmd.createArgument().setValue("-nocompile");
109         }
110
111         // Process the factory class
112
String JavaDoc factory = apt.getFactory();
113         if (factory != null) {
114             cmd.createArgument().setValue("-factory");
115             cmd.createArgument().setValue(factory);
116         }
117
118         // Process the factory path
119
Path factoryPath = apt.getFactoryPath();
120         if (factoryPath != null) {
121             cmd.createArgument().setValue("-factorypath");
122             cmd.createArgument().setPath(factoryPath);
123         }
124
125         File JavaDoc preprocessDir = apt.getPreprocessDir();
126         if (preprocessDir != null) {
127             cmd.createArgument().setValue("-s");
128             cmd.createArgument().setFile(preprocessDir);
129         }
130
131         // Process the processor options
132
Vector JavaDoc options = apt.getOptions();
133         Enumeration JavaDoc elements = options.elements();
134         Apt.Option opt;
135         StringBuffer JavaDoc arg = null;
136         while (elements.hasMoreElements()) {
137             opt = (Apt.Option) elements.nextElement();
138             arg = new StringBuffer JavaDoc();
139             arg.append("-A").append(opt.getName());
140             if (opt.getValue() != null) {
141                 arg.append("=").append(opt.getValue());
142             }
143             cmd.createArgument().setValue(arg.toString());
144         }
145     }
146
147     /**
148      * using our front end task, set up the command line switches
149      *
150      * @param cmd command line to set up
151      */

152     protected void setAptCommandlineSwitches(Commandline cmd) {
153         Apt apt = getApt();
154         setAptCommandlineSwitches(apt, cmd);
155     }
156
157     /**
158      * Run the compilation.
159      * @return true on success.
160      * @throws BuildException if the compilation has problems.
161      */

162     public boolean execute() throws BuildException {
163         attributes.log("Using apt compiler", Project.MSG_VERBOSE);
164         //set up the javac options
165
Commandline cmd = setupModernJavacCommand();
166         //then add the Apt options
167
setAptCommandlineSwitches(cmd);
168
169         //finally invoke APT
170
// Use reflection to be able to build on all JDKs:
171
try {
172             Class JavaDoc c = Class.forName(APT_ENTRY_POINT);
173             Object JavaDoc compiler = c.newInstance();
174             Method JavaDoc compile = c.getMethod(APT_METHOD_NAME,
175                     new Class JavaDoc[]{(new String JavaDoc[]{}).getClass()});
176             int result = ((Integer JavaDoc) compile.invoke
177                     (compiler, new Object JavaDoc[]{cmd.getArguments()}))
178                     .intValue();
179             return (result == APT_COMPILER_SUCCESS);
180         } catch (BuildException be) {
181             //rethrow build exceptions
182
throw be;
183         } catch (Exception JavaDoc ex) {
184             //cast everything else to a build exception
185
throw new BuildException("Error starting apt compiler",
186                     ex, location);
187         }
188     }
189 }
190
Popular Tags