aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/taskdefs/Apt.java
blob: 52154a8c05aff017bfb899072b7c9d8a67fc5547 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.tools.ant.taskdefs;

import java.io.File;
import java.util.Vector;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.compilers.AptExternalCompilerAdapter;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
import org.apache.tools.ant.util.JavaEnvUtils;

/**
 * Apt Task for running the Annotation processing tool for JDK 1.5.  It derives
 * from the existing Javac task, and forces the compiler based on whether we're
 * executing internally, or externally.
 *
 * @since Ant 1.7
 */


public class Apt
        extends Javac {
    private boolean compile = true;
    private String factory;
    private Path factoryPath;
    private Vector<Option> options = new Vector<Option>();
    private File preprocessDir;
    /** The name of the apt tool. */
    public static final String EXECUTABLE_NAME = "apt";
    /** An warning message when ignoring compiler attribute. */
    public static final String ERROR_IGNORING_COMPILER_OPTION
        = "Ignoring compiler attribute for the APT task, as it is fixed";
    /** A warning message if used with java &lt; 1.5. */
    public static final String ERROR_WRONG_JAVA_VERSION
        = "Apt task requires Java 1.5+";

    /**
     * exposed for debug messages
     */
    public static final String WARNING_IGNORING_FORK =
        "Apt only runs in its own JVM; fork=false option ignored";

    /**
     * The nested option element.
     */
    public static final class Option {
        private String name;
        private String value;

        /** Constructor for Option */
        public Option() {
            //default
        }

        /**
         * Get the name attribute.
         * @return the name attribute.
         */
        public String getName() {
            return name;
        }

        /**
         * Set the name attribute.
         * @param name the name of the option.
         */
        public void setName(String name) {
            this.name = name;
        }

        /**
         * Get the value attribute.
         * @return the value attribute.
         */
        public String getValue() {
            return value;
        }

        /**
         * Set the value attribute.
         * @param value the value of the option.
         */
        public void setValue(String value) {
            this.value = value;
        }
    }

    /**
     * Constructor for Apt task.
     * This sets the apt compiler adapter as the compiler in the super class.
     */
    public Apt() {
        super();
        super.setCompiler(AptExternalCompilerAdapter.class.getName());
        super.setFork(true);
    }

    /**
     * Get the name of the apt executable.
     *
     * @return the name of the executable.
     */
    public String getAptExecutable() {
        String exe = getExecutable();
        return exe != null ? exe :
            JavaEnvUtils.getJdkExecutable(EXECUTABLE_NAME);
    }

    /**
     * Set the compiler.
     * This is not allowed and a warning log message is made.
     * @param compiler not used.
     */
    public void setCompiler(String compiler) {
        log(ERROR_IGNORING_COMPILER_OPTION, Project.MSG_WARN);
    }

    /**
     * Set the fork attribute.
     * Non-forking APT is highly classpath dependent and appears to be too
     * brittle to work. The sole reason this attribute is retained
     * is the superclass does it
     * @param fork if false; warn the option is ignored.
     */
    public void setFork(boolean fork) {
        if (!fork) {
            log(WARNING_IGNORING_FORK, Project.MSG_WARN);
        }
    }

    /**
     * Get the compiler class name.
     * @return the compiler class name.
     */
    public String getCompiler() {
        return super.getCompiler();
    }

    /**
     * Get the compile option for the apt compiler.
     * If this is false the "-nocompile" argument will be used.
     * @return the value of the compile option.
     */
    public boolean isCompile() {
        return compile;
    }

    /**
     * Set the compile option for the apt compiler.
     * Default value is true.
     * @param compile if true set the compile option.
     */
    public void setCompile(boolean compile) {
        this.compile = compile;
    }

    /**
     * Get the factory option for the apt compiler.
     * If this is non-null the "-factory" argument will be used.
     * @return the value of the factory option.
     */
    public String getFactory() {
        return factory;
    }

    /**
     * Set the factory option for the apt compiler.
     * Default value is null.
     * @param factory the classname of the factory.
     */
    public void setFactory(String factory) {
        this.factory = factory;
    }

    /**
     * Add a reference to a path to the factoryPath attribute.
     * @param ref a reference to a path.
     */
    public void setFactoryPathRef(Reference ref) {
        createFactoryPath().setRefid(ref);
    }

    /**
     * Add a path to the factoryPath attribute.
     * @return a path to be configured.
     */
    public Path createFactoryPath() {
        if (factoryPath == null) {
            factoryPath = new Path(getProject());
        }
        return factoryPath.createPath();
    }

    /**
     * Get the factory path attribute.
     * If this is not null, the "-factorypath" argument will be used.
     * The default value is null.
     * @return the factory path attribute.
     */
    public Path getFactoryPath() {
        return factoryPath;
    }

    /**
     * Create a nested option.
     * @return an option to be configured.
     */
    public Option createOption() {
        Option opt = new Option();
        options.add(opt);
        return opt;
    }

    /**
     * Get the options to the compiler.
     * Each option will use '"-E" name ["=" value]' argument.
     * @return the options.
     */
    public Vector<Option> getOptions() {
        return options;
    }

    /**
     * Get the preprocessdir attribute.
     * This corresponds to the "-s" argument.
     * The default value is null.
     * @return the preprocessdir attribute.
     */
    public File getPreprocessDir() {
        return preprocessDir;
    }

    /**
     * Set the preprocessdir attribute.
     * @param preprocessDir where to place processor generated source files.
     */
    public void setPreprocessDir(File preprocessDir) {
        this.preprocessDir = preprocessDir;
    }

    /**
     * Do the compilation.
     * @throws BuildException on error.
     */
    public void execute()
            throws BuildException {
        if (JavaEnvUtils.getJavaVersionNumber() >= 18) {
           throw new BuildException("apt does not exist under Java 1.8 and higher");
        }
        super.execute();
    }
}