1 /* 2 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.annotation.processing; 27 28 import javax.tools.JavaFileManager; 29 import javax.tools.*; 30 import javax.lang.model.element.Element; 31 import java.io.IOException; 32 33 /** 34 * This interface supports the creation of new files by an annotation 35 * processor. Files created in this way will be known to the 36 * annotation processing tool implementing this interface, better 37 * enabling the tool to manage them. Source and class files so 38 * created will be {@linkplain RoundEnvironment#getRootElements 39 * considered for processing} by the tool in a subsequent {@linkplain 40 * RoundEnvironment round of processing} after the {@code close} 41 * method has been called on the {@code Writer} or {@code 42 * OutputStream} used to write the contents of the file. 43 * 44 * Three kinds of files are distinguished: source files, class files, 45 * and auxiliary resource files. 46 * 47 * <p> There are two distinguished supported locations (subtrees 48 * within the logical file system) where newly created files are 49 * placed: one for {@linkplain 50 * javax.tools.StandardLocation#SOURCE_OUTPUT new source files}, and 51 * one for {@linkplain javax.tools.StandardLocation#CLASS_OUTPUT new 52 * class files}. (These might be specified on a tool's command line, 53 * for example, using flags such as {@code -s} and {@code -d}.) The 54 * actual locations for new source files and new class files may or 55 * may not be distinct on a particular run of the tool. Resource 56 * files may be created in either location. The methods for reading 57 * and writing resources take a relative name argument. A relative 58 * name is a non-null, non-empty sequence of path segments separated 59 * by {@code '/'}; {@code '.'} and {@code '..'} are invalid path 60 * segments. A valid relative name must match the 61 * "path-rootless" rule of <a 62 * href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>, section 63 * 3.3. 64 * 65 * <p>The file creation methods take a variable number of arguments to 66 * allow the <em>originating elements</em> to be provided as hints to 67 * the tool infrastructure to better manage dependencies. The 68 * originating elements are the types or packages (representing {@code 69 * package-info} files) or modules (representing {@code 70 * module-info} files) which caused an annotation processor to 71 * attempt to create a new file. For example, if an annotation 72 * processor tries to create a source file, {@code 73 * GeneratedFromUserSource}, in response to processing 74 * 75 * <blockquote><pre> 76 * @Generate 77 * public class UserSource {} 78 * </pre></blockquote> 79 * 80 * the type element for {@code UserSource} should be passed as part of 81 * the creation method call as in: 82 * 83 * <blockquote><pre> 84 * filer.createSourceFile("GeneratedFromUserSource", 85 * eltUtils.getTypeElement("UserSource")); 86 * </pre></blockquote> 87 * 88 * If there are no originating elements, none need to be passed. This 89 * information may be used in an incremental environment to determine 90 * the need to rerun processors or remove generated files. 91 * Non-incremental environments may ignore the originating element 92 * information. 93 * 94 * <p> During each run of an annotation processing tool, a file with a 95 * given pathname may be created only once. If that file already 96 * exists before the first attempt to create it, the old contents will 97 * be deleted. Any subsequent attempt to create the same file during 98 * a run will throw a {@link FilerException}, as will attempting to 99 * create both a class file and source file for the same type name or 100 * same package name. The {@linkplain Processor initial inputs} to 101 * the tool are considered to be created by the zeroth round; 102 * therefore, attempting to create a source or class file 103 * corresponding to one of those inputs will result in a {@link 104 * FilerException}. 105 * 106 * <p> In general, processors must not knowingly attempt to overwrite 107 * existing files that were not generated by some processor. A {@code 108 * Filer} may reject attempts to open a file corresponding to an 109 * existing type, like {@code java.lang.Object}. Likewise, the 110 * invoker of the annotation processing tool must not knowingly 111 * configure the tool such that the discovered processors will attempt 112 * to overwrite existing files that were not generated. 113 * 114 * <p> Processors can indicate a source or class file is generated by 115 * including a {@code javax.annotation.Generated} annotation if the 116 * environment is configured so that that type is accessible. 117 * 118 * @apiNote Some of the effect of overwriting a file can be 119 * achieved by using a <i>decorator</i>-style pattern. Instead of 120 * modifying a class directly, the class is designed so that either 121 * its superclass is generated by annotation processing or subclasses 122 * of the class are generated by annotation processing. If the 123 * subclasses are generated, the parent class may be designed to use 124 * factories instead of public constructors so that only subclass 125 * instances would be presented to clients of the parent class. 126 * 127 * @author Joseph D. Darcy 128 * @author Scott Seligman 129 * @author Peter von der Ahé 130 * @since 1.6 131 */ 132 public interface Filer { 133 /** 134 * Creates a new source file and returns an object to allow 135 * writing to it. A source file for a type, or a package can 136 * be created. 137 * 138 * The file's name and path (relative to the {@linkplain 139 * StandardLocation#SOURCE_OUTPUT root output location for source 140 * files}) are based on the name of the item to be declared in 141 * that file as well as the specified module for the item (if 142 * any). 143 * 144 * If more than one type is being declared in a single file (that 145 * is, a single compilation unit), the name of the file should 146 * correspond to the name of the principal top-level type (the 147 * public one, for example). 148 * 149 * <p>A source file can also be created to hold information about 150 * a package, including package annotations. To create a source 151 * file for a named package, have the {@code name} argument be the 152 * package's name followed by {@code ".package-info"}; to create a 153 * source file for an unnamed package, use {@code "package-info"}. 154 * 155 * <p>The optional module name is prefixed to the type name or 156 * package name and separated using a "{@code /}" character. For 157 * example, to create a source file for type {@code a.B} in module 158 * {@code foo}, use a {@code name} argument of {@code "foo/a.B"}. 159 * 160 * <p>Creating a source file in or for an unnamed package in a named 161 * module is <em>not</em> supported. 162 * 163 * @apiNote To use a particular {@linkplain 164 * java.nio.charset.Charset charset} to encode the contents of the 165 * file, an {@code OutputStreamWriter} with the chosen charset can 166 * be created from the {@code OutputStream} from the returned 167 * object. If the {@code Writer} from the returned object is 168 * directly used for writing, its charset is determined by the 169 * implementation. An annotation processing tool may have an 170 * {@code -encoding} flag or analogous option for specifying this; 171 * otherwise, it will typically be the platform's default 172 * encoding. 173 * 174 * <p>To avoid subsequent errors, the contents of the source file 175 * should be compatible with the {@linkplain 176 * ProcessingEnvironment#getSourceVersion source version} being used 177 * for this run. 178 * 179 * @param name canonical (fully qualified) name of the principal type 180 * being declared in this file or a package name followed by 181 * {@code ".package-info"} for a package information file 182 * @param originatingElements type or package or module elements causally 183 * associated with the creation of this file, may be elided or 184 * {@code null} 185 * @return a {@code JavaFileObject} to write the new source file 186 * @throws FilerException if the same pathname has already been 187 * created, the same type has already been created, or the name is 188 * otherwise not valid for the entity requested to being created 189 * @throws IOException if the file cannot be created 190 * @jls 7.3 Compilation Units 191 */ 192 JavaFileObject createSourceFile(CharSequence name, 193 Element... originatingElements) throws IOException; 194 195 /** 196 * Creates a new class file, and returns an object to allow 197 * writing to it. A class file for a type, or a package can 198 * be created. 199 * 200 * The file's name and path (relative to the {@linkplain 201 * StandardLocation#CLASS_OUTPUT root output location for class 202 * files}) are based on the name of the item to be declared as 203 * well as the specified module for the item (if any). 204 * 205 * <p>A class file can also be created to hold information about a 206 * package, including package annotations. To create a class file 207 * for a named package, have the {@code name} argument be the 208 * package's name followed by {@code ".package-info"}; creating a 209 * class file for an unnamed package is not supported. 210 * 211 * <p>The optional module name is prefixed to the type name or 212 * package name and separated using a "{@code /}" character. For 213 * example, to create a class file for type {@code a.B} in module 214 * {@code foo}, use a {@code name} argument of {@code "foo/a.B"}. 215 * 216 * <p>Creating a class file in or for an unnamed package in a named 217 * module is <em>not</em> supported. 218 * 219 * @apiNote To avoid subsequent errors, the contents of the class 220 * file should be compatible with the {@linkplain 221 * ProcessingEnvironment#getSourceVersion source version} being 222 * used for this run. 223 * 224 * @param name binary name of the type being written or a package name followed by 225 * {@code ".package-info"} for a package information file 226 * @param originatingElements type or package or module elements causally 227 * associated with the creation of this file, may be elided or 228 * {@code null} 229 * @return a {@code JavaFileObject} to write the new class file 230 * @throws FilerException if the same pathname has already been 231 * created, the same type has already been created, or the name is 232 * not valid for a type 233 * @throws IOException if the file cannot be created 234 */ 235 JavaFileObject createClassFile(CharSequence name, 236 Element... originatingElements) throws IOException; 237 238 /** 239 * Creates a new auxiliary resource file for writing and returns a 240 * file object for it. The file may be located along with the 241 * newly created source files, newly created binary files, or 242 * other supported location. The locations {@link 243 * StandardLocation#CLASS_OUTPUT CLASS_OUTPUT} and {@link 244 * StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must be 245 * supported. The resource may be named relative to some module 246 * and/or package (as are source and class files), and from there 247 * by a relative pathname. In a loose sense, the full pathname of 248 * the new file will be the concatenation of {@code location}, 249 * {@code moduleAndPkg}, and {@code relativeName}. 250 * 251 * If {@code moduleAndPkg} contains a "{@code /}" character, the 252 * prefix before the "{@code /}" character is the module name and 253 * the suffix after the "{@code /}" character is the package 254 * name. The package suffix may be empty. If {@code moduleAndPkg} 255 * does not contain a "{@code /}" character, the entire argument 256 * is interpreted as a package name. 257 * 258 * <p>Files created via this method are <em>not</em> registered for 259 * annotation processing, even if the full pathname of the file 260 * would correspond to the full pathname of a new source file 261 * or new class file. 262 * 263 * @param location location of the new file 264 * @param moduleAndPkg module and/or package relative to which the file 265 * should be named, or the empty string if none 266 * @param relativeName final pathname components of the file 267 * @param originatingElements type or package elements causally 268 * associated with the creation of this file, may be elided or 269 * {@code null} 270 * @return a {@code FileObject} to write the new resource 271 * @throws IOException if the file cannot be created 272 * @throws FilerException if the same pathname has already been 273 * created 274 * @throws IllegalArgumentException for an unsupported location 275 * @throws IllegalArgumentException if {@code moduleAndPkg} is ill-formed 276 * @throws IllegalArgumentException if {@code relativeName} is not relative 277 */ 278 FileObject createResource(JavaFileManager.Location location, 279 CharSequence moduleAndPkg, 280 CharSequence relativeName, 281 Element... originatingElements) throws IOException; 282 283 /** 284 * Returns an object for reading an existing resource. The 285 * locations {@link StandardLocation#CLASS_OUTPUT CLASS_OUTPUT} 286 * and {@link StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must 287 * be supported. 288 * 289 * <p>If {@code moduleAndPkg} contains a "{@code /}" character, the 290 * prefix before the "{@code /}" character is the module name and 291 * the suffix after the "{@code /}" character is the package 292 * name. The package suffix may be empty; however, if a module 293 * name is present, it must be nonempty. If {@code moduleAndPkg} 294 * does not contain a "{@code /}" character, the entire argument 295 * is interpreted as a package name. 296 * 297 * @param location location of the file 298 * @param moduleAndPkg module and/or package relative to which the file 299 * should be searched for, or the empty string if none 300 * @param relativeName final pathname components of the file 301 * @return an object to read the file 302 * @throws FilerException if the same pathname has already been 303 * opened for writing 304 * @throws IOException if the file cannot be opened 305 * @throws IllegalArgumentException for an unsupported location 306 * @throws IllegalArgumentException if {@code moduleAndPkg} is ill-formed 307 * @throws IllegalArgumentException if {@code relativeName} is not relative 308 */ 309 FileObject getResource(JavaFileManager.Location location, 310 CharSequence moduleAndPkg, 311 CharSequence relativeName) throws IOException; 312 }