1 /*
   2  * Copyright 2005-2006 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any 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 considered for processing by the tool after the
  39  * {@code close} method has been called on the {@code Writer} or
  40  * {@code OutputStream} used to write the contents of the file.
  41  *
  42  * Three kinds of files are distinguished: source files, class files,
  43  * and auxiliary resource files.
  44  *
  45  * <p> There are two distinguished supported locations (subtrees
  46  * within the logical file system) where newly created files are
  47  * placed: one for {@linkplain
  48  * javax.tools.StandardLocation#SOURCE_OUTPUT new source files}, and
  49  * one for {@linkplain javax.tools.StandardLocation#CLASS_OUTPUT new
  50  * class files}.  (These might be specified on a tool's command line,
  51  * for example, using flags such as {@code -s} and {@code -d}.)  The
  52  * actual locations for new source files and new class files may or
  53  * may not be distinct on a particular run of the tool.  Resource
  54  * files may be created in either location.  The methods for reading
  55  * and writing resources take a relative name argument.  A relative
  56  * name is a non-null, non-empty sequence of path segments separated
  57  * by {@code '/'}; {@code '.'} and {@code '..'} are invalid path
  58  * segments.  A valid relative name must match the
  59  * &quot;path-rootless&quot; rule of <a
  60  * href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>, section
  61  * 3.3.
  62  *
  63  * <p>The file creation methods take a variable number of arguments to
  64  * allow the <em>originating elements</em> to be provided as hints to
  65  * the tool infrastructure to better manage dependencies.  The
  66  * originating elements are the types or packages (representing {@code
  67  * package-info} files) which caused an annotation processor to
  68  * attempt to create a new file.  For example, if an annotation
  69  * processor tries to create a source file, {@code
  70  * GeneratedFromUserSource}, in response to processing
  71  *
  72  * <blockquote><pre>
  73  *  &#64;Generate
  74  *  public class UserSource {}
  75  * </pre></blockquote>
  76  *
  77  * the type element for {@code UserSource} should be passed as part of
  78  * the creation method call as in:
  79  *
  80  * <blockquote><pre>
  81  *      filer.createSourceFile("GeneratedFromUserSource",
  82  *                             eltUtils.getTypeElement("UserSource"));
  83  * </pre></blockquote>
  84  *
  85  * If there are no originating elements, none need to be passed.  This
  86  * information may be used in an incremental environment to determine
  87  * the need to rerun processors or remove generated files.
  88  * Non-incremental environments may ignore the originating element
  89  * information.
  90  *
  91  * <p> During each run of an annotation processing tool, a file with a
  92  * given pathname may be created only once.  If that file already
  93  * exists before the first attempt to create it, the old contents will
  94  * be deleted.  Any subsequent attempt to create the same file during
  95  * a run will throw a {@link FilerException}, as will attempting to
  96  * create both a class file and source file for the same type name or
  97  * same package name.  The {@linkplain Processor initial inputs} to
  98  * the tool are considered to be created by the zeroth round;
  99  * therefore, attempting to create a source or class file
 100  * corresponding to one of those inputs will result in a {@link
 101  * FilerException}.
 102  *
 103  * <p> In general, processors must not knowingly attempt to overwrite
 104  * existing files that were not generated by some processor.  A {@code
 105  * Filer} may reject attempts to open a file corresponding to an
 106  * existing type, like {@code java.lang.Object}.  Likewise, the
 107  * invoker of the annotation processing tool must not knowingly
 108  * configure the tool such that the discovered processors will attempt
 109  * to overwrite existing files that were not generated.
 110  *
 111  * <p> Processors can indicate a source or class file is generated by
 112  * including an {@link javax.annotation.Generated @Generated}
 113  * annotation.
 114  *
 115  * <p> Note that some of the effect of overwriting a file can be
 116  * achieved by using a <i>decorator</i>-style pattern.  Instead of
 117  * modifying a class directly, the class is designed so that either
 118  * its superclass is generated by annotation processing or subclasses
 119  * of the class are generated by annotation processing.  If the
 120  * subclasses are generated, the parent class may be designed to use
 121  * factories instead of public constructors so that only subclass
 122  * instances would be presented to clients of the parent class.
 123  *
 124  * @author Joseph D. Darcy
 125  * @author Scott Seligman
 126  * @author Peter von der Ah&eacute;
 127  * @since 1.6
 128  */
 129 public interface Filer {
 130     /**
 131      * Creates a new source file and returns an object to allow
 132      * writing to it.  The file's name and path (relative to the
 133      * {@linkplain StandardLocation#SOURCE_OUTPUT root output location
 134      * for source files}) are based on the type to be declared in that
 135      * file.  If more than one type is being declared, the name of the
 136      * principal top-level type (the public one, for example) should
 137      * be used.  A source file can also be created to hold information
 138      * about a package, including package annotations.  To create a
 139      * source file for a named package, have {@code name} be the
 140      * package's name followed by {@code ".package-info"}; to create a
 141      * source file for an unnamed package, use {@code "package-info"}.
 142      *
 143      * <p> Note that to use a particular {@linkplain
 144      * java.nio.charset.Charset charset} to encode the contents of the
 145      * file, an {@code OutputStreamWriter} with the chosen charset can
 146      * be created from the {@code OutputStream} from the returned
 147      * object. If the {@code Writer} from the returned object is
 148      * directly used for writing, its charset is determined by the
 149      * implementation.  An annotation processing tool may have an
 150      * {@code -encoding} flag or analogous option for specifying this;
 151      * otherwise, it will typically be the platform's default
 152      * encoding.
 153      *
 154      * <p>To avoid subsequent errors, the contents of the source file
 155      * should be compatible with the {@linkplain
 156      * ProcessingEnvironment#getSourceVersion source version} being used
 157      * for this run.
 158      *
 159      * @param name  canonical (fully qualified) name of the principal type
 160      *          being declared in this file or a package name followed by
 161      *          {@code ".package-info"} for a package information file
 162      * @param originatingElements type or package elements causally
 163      * associated with the creation of this file, may be elided or
 164      * {@code null}
 165      * @return a {@code JavaFileObject} to write the new source file
 166      * @throws FilerException if the same pathname has already been
 167      * created, the same type has already been created, or the name is
 168      * not valid for a type
 169      * @throws IOException if the file cannot be created
 170      */
 171     JavaFileObject createSourceFile(CharSequence name,
 172                                     Element... originatingElements) throws IOException;
 173 
 174     /**
 175      * Creates a new class file, and returns an object to allow
 176      * writing to it.  The file's name and path (relative to the
 177      * {@linkplain StandardLocation#CLASS_OUTPUT root output location
 178      * for class files}) are based on the name of the type being
 179      * written.  A class file can also be created to hold information
 180      * about a package, including package annotations.  To create a
 181      * class file for a named package, have {@code name} be the
 182      * package's name followed by {@code ".package-info"}; creating a
 183      * class file for an unnamed package is not supported.
 184      *
 185      * <p>To avoid subsequent errors, the contents of the class file
 186      * should be compatible with the {@linkplain
 187      * ProcessingEnvironment#getSourceVersion source version} being used
 188      * for this run.
 189      *
 190      * @param name binary name of the type being written or a package name followed by
 191      *          {@code ".package-info"} for a package information file
 192      * @param originatingElements type or package elements causally
 193      * associated with the creation of this file, may be elided or
 194      * {@code null}
 195      * @return a {@code JavaFileObject} to write the new class file
 196      * @throws FilerException if the same pathname has already been
 197      * created, the same type has already been created, or the name is
 198      * not valid for a type
 199      * @throws IOException if the file cannot be created
 200      */
 201     JavaFileObject createClassFile(CharSequence name,
 202                                    Element... originatingElements) throws IOException;
 203 
 204     /**
 205      * Creates a new auxiliary resource file for writing and returns a
 206      * file object for it.  The file may be located along with the
 207      * newly created source files, newly created binary files, or
 208      * other supported location.  The locations {@link
 209      * StandardLocation#CLASS_OUTPUT CLASS_OUTPUT} and {@link
 210      * StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must be
 211      * supported.  The resource may be named relative to some package
 212      * (as are source and class files), and from there by a relative
 213      * pathname.  In a loose sense, the full pathname of the new file
 214      * will be the concatenation of {@code location}, {@code pkg}, and
 215      * {@code relativeName}.
 216      *
 217      * <p>Files created via this method are not registered for
 218      * annotation processing, even if the full pathname of the file
 219      * would correspond to the full pathname of a new source file
 220      * or new class file.
 221      *
 222      * @param location location of the new file
 223      * @param pkg package relative to which the file should be named,
 224      *          or the empty string if none
 225      * @param relativeName final pathname components of the file
 226      * @param originatingElements type or package elements causally
 227      * associated with the creation of this file, may be elided or
 228      * {@code null}
 229      * @return a {@code FileObject} to write the new resource
 230      * @throws IOException if the file cannot be created
 231      * @throws FilerException if the same pathname has already been
 232      * created
 233      * @throws IllegalArgumentException for an unsupported location
 234      * @throws IllegalArgumentException if {@code relativeName} is not relative
 235      */
 236    FileObject createResource(JavaFileManager.Location location,
 237                              CharSequence pkg,
 238                              CharSequence relativeName,
 239                              Element... originatingElements) throws IOException;
 240 
 241     /**
 242      * Returns an object for reading an existing resource.  The
 243      * locations {@link StandardLocation#CLASS_OUTPUT CLASS_OUTPUT}
 244      * and {@link StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must
 245      * be supported.
 246      *
 247      * @param location location of the file
 248      * @param pkg package relative to which the file should be searched,
 249      *          or the empty string if none
 250      * @param relativeName final pathname components of the file
 251      * @return an object to read the file
 252      * @throws FilerException if the same pathname has already been
 253      * opened for writing
 254      * @throws IOException if the file cannot be opened
 255      * @throws IllegalArgumentException for an unsupported location
 256      * @throws IllegalArgumentException if {@code relativeName} is not relative
 257      */
 258     FileObject getResource(JavaFileManager.Location location,
 259                            CharSequence pkg,
 260                            CharSequence relativeName) throws IOException;
 261 }