duke@1: /* ohair@554: * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package javax.annotation.processing; duke@1: duke@1: import javax.tools.JavaFileManager; duke@1: import javax.tools.*; duke@1: import javax.lang.model.element.Element; duke@1: import java.io.IOException; duke@1: duke@1: /** duke@1: * This interface supports the creation of new files by an annotation duke@1: * processor. Files created in this way will be known to the duke@1: * annotation processing tool implementing this interface, better duke@1: * enabling the tool to manage them. Source and class files so darcy@231: * created will be {@linkplain RoundEnvironment#getRootElements darcy@231: * considered for processing} by the tool in a subsequent {@linkplain darcy@231: * RoundEnvironment round of processing} after the {@code close} darcy@231: * method has been called on the {@code Writer} or {@code darcy@231: * OutputStream} used to write the contents of the file. duke@1: * duke@1: * Three kinds of files are distinguished: source files, class files, duke@1: * and auxiliary resource files. duke@1: * duke@1: *

There are two distinguished supported locations (subtrees duke@1: * within the logical file system) where newly created files are duke@1: * placed: one for {@linkplain duke@1: * javax.tools.StandardLocation#SOURCE_OUTPUT new source files}, and duke@1: * one for {@linkplain javax.tools.StandardLocation#CLASS_OUTPUT new duke@1: * class files}. (These might be specified on a tool's command line, duke@1: * for example, using flags such as {@code -s} and {@code -d}.) The duke@1: * actual locations for new source files and new class files may or duke@1: * may not be distinct on a particular run of the tool. Resource duke@1: * files may be created in either location. The methods for reading duke@1: * and writing resources take a relative name argument. A relative duke@1: * name is a non-null, non-empty sequence of path segments separated duke@1: * by {@code '/'}; {@code '.'} and {@code '..'} are invalid path duke@1: * segments. A valid relative name must match the duke@1: * "path-rootless" rule of RFC 3986, section duke@1: * 3.3. duke@1: * duke@1: *

The file creation methods take a variable number of arguments to duke@1: * allow the originating elements to be provided as hints to duke@1: * the tool infrastructure to better manage dependencies. The duke@1: * originating elements are the types or packages (representing {@code duke@1: * package-info} files) which caused an annotation processor to duke@1: * attempt to create a new file. For example, if an annotation duke@1: * processor tries to create a source file, {@code duke@1: * GeneratedFromUserSource}, in response to processing duke@1: * duke@1: *

duke@1:  *  @Generate
duke@1:  *  public class UserSource {}
duke@1:  * 
duke@1: * duke@1: * the type element for {@code UserSource} should be passed as part of duke@1: * the creation method call as in: duke@1: * duke@1: *
duke@1:  *      filer.createSourceFile("GeneratedFromUserSource",
duke@1:  *                             eltUtils.getTypeElement("UserSource"));
duke@1:  * 
duke@1: * duke@1: * If there are no originating elements, none need to be passed. This duke@1: * information may be used in an incremental environment to determine duke@1: * the need to rerun processors or remove generated files. duke@1: * Non-incremental environments may ignore the originating element duke@1: * information. duke@1: * duke@1: *

During each run of an annotation processing tool, a file with a duke@1: * given pathname may be created only once. If that file already duke@1: * exists before the first attempt to create it, the old contents will duke@1: * be deleted. Any subsequent attempt to create the same file during duke@1: * a run will throw a {@link FilerException}, as will attempting to duke@1: * create both a class file and source file for the same type name or duke@1: * same package name. The {@linkplain Processor initial inputs} to duke@1: * the tool are considered to be created by the zeroth round; duke@1: * therefore, attempting to create a source or class file duke@1: * corresponding to one of those inputs will result in a {@link duke@1: * FilerException}. duke@1: * duke@1: *

In general, processors must not knowingly attempt to overwrite duke@1: * existing files that were not generated by some processor. A {@code duke@1: * Filer} may reject attempts to open a file corresponding to an duke@1: * existing type, like {@code java.lang.Object}. Likewise, the duke@1: * invoker of the annotation processing tool must not knowingly duke@1: * configure the tool such that the discovered processors will attempt duke@1: * to overwrite existing files that were not generated. duke@1: * duke@1: *

Processors can indicate a source or class file is generated by duke@1: * including an {@link javax.annotation.Generated @Generated} duke@1: * annotation. duke@1: * duke@1: *

Note that some of the effect of overwriting a file can be duke@1: * achieved by using a decorator-style pattern. Instead of duke@1: * modifying a class directly, the class is designed so that either duke@1: * its superclass is generated by annotation processing or subclasses duke@1: * of the class are generated by annotation processing. If the duke@1: * subclasses are generated, the parent class may be designed to use duke@1: * factories instead of public constructors so that only subclass duke@1: * instances would be presented to clients of the parent class. duke@1: * duke@1: * @author Joseph D. Darcy duke@1: * @author Scott Seligman duke@1: * @author Peter von der Ahé duke@1: * @since 1.6 duke@1: */ duke@1: public interface Filer { duke@1: /** duke@1: * Creates a new source file and returns an object to allow duke@1: * writing to it. The file's name and path (relative to the duke@1: * {@linkplain StandardLocation#SOURCE_OUTPUT root output location duke@1: * for source files}) are based on the type to be declared in that duke@1: * file. If more than one type is being declared, the name of the duke@1: * principal top-level type (the public one, for example) should duke@1: * be used. A source file can also be created to hold information duke@1: * about a package, including package annotations. To create a duke@1: * source file for a named package, have {@code name} be the duke@1: * package's name followed by {@code ".package-info"}; to create a duke@1: * source file for an unnamed package, use {@code "package-info"}. duke@1: * duke@1: *

Note that to use a particular {@linkplain duke@1: * java.nio.charset.Charset charset} to encode the contents of the duke@1: * file, an {@code OutputStreamWriter} with the chosen charset can duke@1: * be created from the {@code OutputStream} from the returned duke@1: * object. If the {@code Writer} from the returned object is duke@1: * directly used for writing, its charset is determined by the duke@1: * implementation. An annotation processing tool may have an duke@1: * {@code -encoding} flag or analogous option for specifying this; duke@1: * otherwise, it will typically be the platform's default duke@1: * encoding. duke@1: * duke@1: *

To avoid subsequent errors, the contents of the source file duke@1: * should be compatible with the {@linkplain duke@1: * ProcessingEnvironment#getSourceVersion source version} being used duke@1: * for this run. duke@1: * duke@1: * @param name canonical (fully qualified) name of the principal type duke@1: * being declared in this file or a package name followed by duke@1: * {@code ".package-info"} for a package information file duke@1: * @param originatingElements type or package elements causally duke@1: * associated with the creation of this file, may be elided or duke@1: * {@code null} duke@1: * @return a {@code JavaFileObject} to write the new source file duke@1: * @throws FilerException if the same pathname has already been duke@1: * created, the same type has already been created, or the name is duke@1: * not valid for a type duke@1: * @throws IOException if the file cannot be created duke@1: */ duke@1: JavaFileObject createSourceFile(CharSequence name, duke@1: Element... originatingElements) throws IOException; duke@1: duke@1: /** duke@1: * Creates a new class file, and returns an object to allow duke@1: * writing to it. The file's name and path (relative to the duke@1: * {@linkplain StandardLocation#CLASS_OUTPUT root output location duke@1: * for class files}) are based on the name of the type being duke@1: * written. A class file can also be created to hold information duke@1: * about a package, including package annotations. To create a duke@1: * class file for a named package, have {@code name} be the duke@1: * package's name followed by {@code ".package-info"}; creating a duke@1: * class file for an unnamed package is not supported. duke@1: * duke@1: *

To avoid subsequent errors, the contents of the class file duke@1: * should be compatible with the {@linkplain duke@1: * ProcessingEnvironment#getSourceVersion source version} being used duke@1: * for this run. duke@1: * duke@1: * @param name binary name of the type being written or a package name followed by duke@1: * {@code ".package-info"} for a package information file duke@1: * @param originatingElements type or package elements causally duke@1: * associated with the creation of this file, may be elided or duke@1: * {@code null} duke@1: * @return a {@code JavaFileObject} to write the new class file duke@1: * @throws FilerException if the same pathname has already been duke@1: * created, the same type has already been created, or the name is duke@1: * not valid for a type duke@1: * @throws IOException if the file cannot be created duke@1: */ duke@1: JavaFileObject createClassFile(CharSequence name, duke@1: Element... originatingElements) throws IOException; duke@1: duke@1: /** duke@1: * Creates a new auxiliary resource file for writing and returns a duke@1: * file object for it. The file may be located along with the duke@1: * newly created source files, newly created binary files, or duke@1: * other supported location. The locations {@link duke@1: * StandardLocation#CLASS_OUTPUT CLASS_OUTPUT} and {@link duke@1: * StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must be duke@1: * supported. The resource may be named relative to some package duke@1: * (as are source and class files), and from there by a relative duke@1: * pathname. In a loose sense, the full pathname of the new file duke@1: * will be the concatenation of {@code location}, {@code pkg}, and duke@1: * {@code relativeName}. duke@1: * duke@1: *

Files created via this method are not registered for duke@1: * annotation processing, even if the full pathname of the file duke@1: * would correspond to the full pathname of a new source file duke@1: * or new class file. duke@1: * duke@1: * @param location location of the new file duke@1: * @param pkg package relative to which the file should be named, duke@1: * or the empty string if none duke@1: * @param relativeName final pathname components of the file duke@1: * @param originatingElements type or package elements causally duke@1: * associated with the creation of this file, may be elided or duke@1: * {@code null} duke@1: * @return a {@code FileObject} to write the new resource duke@1: * @throws IOException if the file cannot be created duke@1: * @throws FilerException if the same pathname has already been duke@1: * created duke@1: * @throws IllegalArgumentException for an unsupported location duke@1: * @throws IllegalArgumentException if {@code relativeName} is not relative duke@1: */ duke@1: FileObject createResource(JavaFileManager.Location location, duke@1: CharSequence pkg, duke@1: CharSequence relativeName, duke@1: Element... originatingElements) throws IOException; duke@1: duke@1: /** duke@1: * Returns an object for reading an existing resource. The duke@1: * locations {@link StandardLocation#CLASS_OUTPUT CLASS_OUTPUT} duke@1: * and {@link StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must duke@1: * be supported. duke@1: * duke@1: * @param location location of the file duke@1: * @param pkg package relative to which the file should be searched, duke@1: * or the empty string if none duke@1: * @param relativeName final pathname components of the file duke@1: * @return an object to read the file duke@1: * @throws FilerException if the same pathname has already been duke@1: * opened for writing duke@1: * @throws IOException if the file cannot be opened duke@1: * @throws IllegalArgumentException for an unsupported location duke@1: * @throws IllegalArgumentException if {@code relativeName} is not relative duke@1: */ duke@1: FileObject getResource(JavaFileManager.Location location, duke@1: CharSequence pkg, duke@1: CharSequence relativeName) throws IOException; duke@1: }