< prev index next >

src/java.compiler/share/classes/javax/annotation/processing/Filer.java

Print this page


   1 /*
   2  * Copyright (c) 2005, 2006, 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


  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  * &quot;path-rootless&quot; 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) which caused an annotation processor to

  70  * attempt to create a new file.  For example, if an annotation
  71  * processor tries to create a source file, {@code
  72  * GeneratedFromUserSource}, in response to processing
  73  *
  74  * <blockquote><pre>
  75  *  @Generate
  76  *  public class UserSource {}
  77  * </pre></blockquote>
  78  *
  79  * the type element for {@code UserSource} should be passed as part of
  80  * the creation method call as in:
  81  *
  82  * <blockquote><pre>
  83  *      filer.createSourceFile("GeneratedFromUserSource",
  84  *                             eltUtils.getTypeElement("UserSource"));
  85  * </pre></blockquote>
  86  *
  87  * If there are no originating elements, none need to be passed.  This
  88  * information may be used in an incremental environment to determine
  89  * the need to rerun processors or remove generated files.
  90  * Non-incremental environments may ignore the originating element
  91  * information.
  92  *
  93  * <p> During each run of an annotation processing tool, a file with a
  94  * given pathname may be created only once.  If that file already
  95  * exists before the first attempt to create it, the old contents will
  96  * be deleted.  Any subsequent attempt to create the same file during
  97  * a run will throw a {@link FilerException}, as will attempting to
  98  * create both a class file and source file for the same type name or
  99  * same package name.  The {@linkplain Processor initial inputs} to
 100  * the tool are considered to be created by the zeroth round;
 101  * therefore, attempting to create a source or class file
 102  * corresponding to one of those inputs will result in a {@link
 103  * FilerException}.
 104  *
 105  * <p> In general, processors must not knowingly attempt to overwrite
 106  * existing files that were not generated by some processor.  A {@code
 107  * Filer} may reject attempts to open a file corresponding to an
 108  * existing type, like {@code java.lang.Object}.  Likewise, the
 109  * invoker of the annotation processing tool must not knowingly
 110  * configure the tool such that the discovered processors will attempt
 111  * to overwrite existing files that were not generated.
 112  *
 113  * <p> Processors can indicate a source or class file is generated by
 114  * including an {@link javax.annotation.Generated @Generated}
 115  * annotation.

 116  *
 117  * <p> Note that some of the effect of overwriting a file can be
 118  * achieved by using a <i>decorator</i>-style pattern.  Instead of
 119  * modifying a class directly, the class is designed so that either
 120  * its superclass is generated by annotation processing or subclasses
 121  * of the class are generated by annotation processing.  If the
 122  * subclasses are generated, the parent class may be designed to use
 123  * factories instead of public constructors so that only subclass
 124  * instances would be presented to clients of the parent class.
 125  *
 126  * @author Joseph D. Darcy
 127  * @author Scott Seligman
 128  * @author Peter von der Ah&eacute;
 129  * @since 1.6
 130  */
 131 public interface Filer {
 132     /**
 133      * Creates a new source file and returns an object to allow
 134      * writing to it.  The file's name and path (relative to the



 135      * {@linkplain StandardLocation#SOURCE_OUTPUT root output location
 136      * for source files}) are based on the type to be declared in that
 137      * file.  If more than one type is being declared, the name of the
 138      * principal top-level type (the public one, for example) should
 139      * be used.  A source file can also be created to hold information




 140      * about a package, including package annotations.  To create a
 141      * source file for a named package, have {@code name} be the
 142      * package's name followed by {@code ".package-info"}; to create a
 143      * source file for an unnamed package, use {@code "package-info"}.
 144      *
 145      * <p> Note that to use a particular {@linkplain













 146      * java.nio.charset.Charset charset} to encode the contents of the
 147      * file, an {@code OutputStreamWriter} with the chosen charset can
 148      * be created from the {@code OutputStream} from the returned
 149      * object. If the {@code Writer} from the returned object is
 150      * directly used for writing, its charset is determined by the
 151      * implementation.  An annotation processing tool may have an
 152      * {@code -encoding} flag or analogous option for specifying this;
 153      * otherwise, it will typically be the platform's default
 154      * encoding.
 155      *
 156      * <p>To avoid subsequent errors, the contents of the source file
 157      * should be compatible with the {@linkplain
 158      * ProcessingEnvironment#getSourceVersion source version} being used
 159      * for this run.
 160      *
 161      * @param name  canonical (fully qualified) name of the principal type
 162      *          being declared in this file or a package name followed by
 163      *          {@code ".package-info"} for a package information file
 164      * @param originatingElements type or package elements causally


 165      * associated with the creation of this file, may be elided or
 166      * {@code null}
 167      * @return a {@code JavaFileObject} to write the new source file
 168      * @throws FilerException if the same pathname has already been
 169      * created, the same type has already been created, or the name is
 170      * not valid for a type
 171      * @throws IOException if the file cannot be created
 172      */
 173     JavaFileObject createSourceFile(CharSequence name,
 174                                     Element... originatingElements) throws IOException;
 175 
 176     /**
 177      * Creates a new class file, and returns an object to allow
 178      * writing to it.  The file's name and path (relative to the
 179      * {@linkplain StandardLocation#CLASS_OUTPUT root output location
 180      * for class files}) are based on the name of the type being
 181      * written.  A class file can also be created to hold information
 182      * about a package, including package annotations.  To create a
 183      * class file for a named package, have {@code name} be the
 184      * package's name followed by {@code ".package-info"}; creating a
 185      * class file for an unnamed package is not supported.
 186      *
 187      * <p>To avoid subsequent errors, the contents of the class file
 188      * should be compatible with the {@linkplain
 189      * ProcessingEnvironment#getSourceVersion source version} being used
 190      * for this run.


 191      *
 192      * @param name binary name of the type being written or a package name followed by
 193      *          {@code ".package-info"} for a package information file
 194      * @param originatingElements type or package elements causally
 195      * associated with the creation of this file, may be elided or
 196      * {@code null}
 197      * @return a {@code JavaFileObject} to write the new class file
 198      * @throws FilerException if the same pathname has already been
 199      * created, the same type has already been created, or the name is
 200      * not valid for a type
 201      * @throws IOException if the file cannot be created
 202      */
 203     JavaFileObject createClassFile(CharSequence name,
 204                                    Element... originatingElements) throws IOException;
 205 
 206     /**
 207      * Creates a new auxiliary resource file for writing and returns a
 208      * file object for it.  The file may be located along with the
 209      * newly created source files, newly created binary files, or
 210      * other supported location.  The locations {@link
 211      * StandardLocation#CLASS_OUTPUT CLASS_OUTPUT} and {@link
 212      * StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must be
 213      * supported.  The resource may be named relative to some package
 214      * (as are source and class files), and from there by a relative
 215      * pathname.  In a loose sense, the full pathname of the new file
 216      * will be the concatenation of {@code location}, {@code pkg}, and
 217      * {@code relativeName}.






 218      *
 219      * <p>Files created via this method are not registered for
 220      * annotation processing, even if the full pathname of the file
 221      * would correspond to the full pathname of a new source file
 222      * or new class file.
 223      *
 224      * @param location location of the new file
 225      * @param pkg package relative to which the file should be named,
 226      *          or the empty string if none
 227      * @param relativeName final pathname components of the file
 228      * @param originatingElements type or package elements causally
 229      * associated with the creation of this file, may be elided or
 230      * {@code null}
 231      * @return a {@code FileObject} to write the new resource
 232      * @throws IOException if the file cannot be created
 233      * @throws FilerException if the same pathname has already been
 234      * created
 235      * @throws IllegalArgumentException for an unsupported location
 236      * @throws IllegalArgumentException if {@code relativeName} is not relative
 237      */
 238    FileObject createResource(JavaFileManager.Location location,
 239                              CharSequence pkg,
 240                              CharSequence relativeName,
 241                              Element... originatingElements) throws IOException;
 242 
 243     /**
 244      * Returns an object for reading an existing resource.  The
 245      * locations {@link StandardLocation#CLASS_OUTPUT CLASS_OUTPUT}
 246      * and {@link StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must
 247      * be supported.
 248      *






 249      * @param location location of the file
 250      * @param pkg package relative to which the file should be searched,
 251      *          or the empty string if none
 252      * @param relativeName final pathname components of the file
 253      * @return an object to read the file
 254      * @throws FilerException if the same pathname has already been
 255      * opened for writing
 256      * @throws IOException if the file cannot be opened
 257      * @throws IllegalArgumentException for an unsupported location
 258      * @throws IllegalArgumentException if {@code relativeName} is not relative
 259      */
 260     FileObject getResource(JavaFileManager.Location location,
 261                            CharSequence pkg,
 262                            CharSequence relativeName) throws IOException;
 263 }
   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


  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  * &quot;path-rootless&quot; 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 or same module name.  The {@linkplain Processor
 101  * initial inputs} to the tool are considered to be created by the
 102  * zeroth round; therefore, attempting to create a source or class
 103  * file 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 {@link javax.annotation.Generated @Generated}
 116  * annotation if the environment is configured so that that type is
 117  * accessible.
 118  *
 119  * @apiNote Some of the effect of overwriting a file can be
 120  * achieved by using a <i>decorator</i>-style pattern.  Instead of
 121  * modifying a class directly, the class is designed so that either
 122  * its superclass is generated by annotation processing or subclasses
 123  * of the class are generated by annotation processing.  If the
 124  * subclasses are generated, the parent class may be designed to use
 125  * factories instead of public constructors so that only subclass
 126  * instances would be presented to clients of the parent class.
 127  *
 128  * @author Joseph D. Darcy
 129  * @author Scott Seligman
 130  * @author Peter von der Ah&eacute;
 131  * @since 1.6
 132  */
 133 public interface Filer {
 134     /**
 135      * Creates a new source file and returns an object to allow
 136      * writing to it. A source file for a type, a package, or a module
 137      * can be created.
 138      *
 139      * The file's name and path (relative to the
 140      * {@linkplain StandardLocation#SOURCE_OUTPUT root output location
 141      * for source files}) are based on the type to be declared in that
 142      * file as well as the specified module for the type (if any).
 143 
 144      * If more than one type is being declared, the name
 145      * of the principal top-level type (the public one, for example)
 146      * should be used.
 147      *
 148      * <p>A source file can also be created to hold information
 149      * about a package, including package annotations.  To create a
 150      * source file for a named package, have {@code name} be the
 151      * package's name followed by {@code ".package-info"}; to create a
 152      * source file for an unnamed package, use {@code "package-info"}.
 153      *
 154      * The optional module name is prefixed to the type name or
 155      * package name and separated using a "{@code /}" character. For
 156      * example, to create a source file for type {@code a.B} in module
 157      * {@code foo}, use a name argument of {@code "foo/a.B"}.
 158      *
 159      * To create a source file for a named module, have {@code name}
 160      * be the modules's name followed by {@code "/module-info"}
 161      *
 162      * TOOD: For module m should {@code "m/package-info"} be rejected? 
 163      * In other words, an unnamed package cannot be created at will in
 164      * a named module? Is creating a source file for an unnamed module
 165      * supported?
 166      *
 167      * @apiNote To use a particular {@linkplain
 168      * java.nio.charset.Charset charset} to encode the contents of the
 169      * file, an {@code OutputStreamWriter} with the chosen charset can
 170      * be created from the {@code OutputStream} from the returned
 171      * object. If the {@code Writer} from the returned object is
 172      * directly used for writing, its charset is determined by the
 173      * implementation.  An annotation processing tool may have an
 174      * {@code -encoding} flag or analogous option for specifying this;
 175      * otherwise, it will typically be the platform's default
 176      * encoding.
 177      *
 178      * <p>To avoid subsequent errors, the contents of the source file
 179      * should be compatible with the {@linkplain
 180      * ProcessingEnvironment#getSourceVersion source version} being used
 181      * for this run.
 182      *
 183      * @param name  canonical (fully qualified) name of the principal type
 184      *          being declared in this file or a package name followed by
 185      *          {@code ".package-info"} for a package information file
 186      *          or a module name followed by {@code "/module-info"}
 187      *          for a module information file.
 188      * @param originatingElements type or package or module elements causally
 189      * associated with the creation of this file, may be elided or
 190      * {@code null}
 191      * @return a {@code JavaFileObject} to write the new source file
 192      * @throws FilerException if the same pathname has already been
 193      * created, the same type has already been created, or the name is
 194      * otherwise not valid for the entity requested to being created
 195      * @throws IOException if the file cannot be created
 196      */
 197     JavaFileObject createSourceFile(CharSequence name,
 198                                     Element... originatingElements) throws IOException;
 199 
 200     /**
 201      * Creates a new class file, and returns an object to allow
 202      * writing to it.  The file's name and path (relative to the
 203      * {@linkplain StandardLocation#CLASS_OUTPUT root output location
 204      * for class files}) are based on the name of the type being
 205      * written.  A class file can also be created to hold information
 206      * about a package, including package annotations.  To create a
 207      * class file for a named package, have {@code name} 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>TODO:update in parallel to the above
 212      *
 213      * @apiNote To avoid subsequent errors, the contents of the class
 214      * file should be compatible with the {@linkplain
 215      * ProcessingEnvironment#getSourceVersion source version} being
 216      * used for this run.
 217      *
 218      * @param name binary name of the type being written or a package name followed by
 219      *          {@code ".package-info"} for a package information file
 220      * @param originatingElements type or package elements causally
 221      * associated with the creation of this file, may be elided or
 222      * {@code null}
 223      * @return a {@code JavaFileObject} to write the new class file
 224      * @throws FilerException if the same pathname has already been
 225      * created, the same type has already been created, or the name is
 226      * not valid for a type
 227      * @throws IOException if the file cannot be created
 228      */
 229     JavaFileObject createClassFile(CharSequence name,
 230                                    Element... originatingElements) throws IOException;
 231 
 232     /**
 233      * Creates a new auxiliary resource file for writing and returns a
 234      * file object for it.  The file may be located along with the
 235      * newly created source files, newly created binary files, or
 236      * other supported location.  The locations {@link
 237      * StandardLocation#CLASS_OUTPUT CLASS_OUTPUT} and {@link
 238      * StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must be
 239      * supported.  The resource may be named relative to some module
 240      * and/or package (as are source and class files), and from there
 241      * by a relative pathname.  In a loose sense, the full pathname of
 242      * the new file will be the concatenation of {@code location},
 243      * {@code moduleAndPkg}, and {@code relativeName}.
 244      *
 245      * If {@code moduleAndPkg} contains a "{@code /}" character, the
 246      * prefix before "{@code /}" character is the module name and the
 247      * suffix after the "{@code /}" character is package
 248      * name. Otherwise, the entire argument is interpreted as a
 249      * package name.
 250      *
 251      * <p>Files created via this method are <em>not</em> registered for
 252      * annotation processing, even if the full pathname of the file
 253      * would correspond to the full pathname of a new source file
 254      * or new class file.
 255      *
 256      * @param location location of the new file
 257      * @param moduleAndPkg module and/or package relative to which the file
 258      *           should be named, or the empty string if none
 259      * @param relativeName final pathname components of the file
 260      * @param originatingElements type or package elements causally
 261      * associated with the creation of this file, may be elided or
 262      * {@code null}
 263      * @return a {@code FileObject} to write the new resource
 264      * @throws IOException if the file cannot be created
 265      * @throws FilerException if the same pathname has already been
 266      * created
 267      * @throws IllegalArgumentException for an unsupported location
 268      * @throws IllegalArgumentException if {@code relativeName} is not relative
 269      */
 270    FileObject createResource(JavaFileManager.Location location,
 271                              CharSequence moduleAndPkg,
 272                              CharSequence relativeName,
 273                              Element... originatingElements) throws IOException;
 274 
 275     /**
 276      * Returns an object for reading an existing resource.  The
 277      * locations {@link StandardLocation#CLASS_OUTPUT CLASS_OUTPUT}
 278      * and {@link StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must
 279      * be supported.
 280      *
 281      * <p>If {@code moduleAndPkg} contains a "{@code /}" character, the
 282      * prefix before "{@code /}" character is the module name and the
 283      * suffix after the "{@code /}" character is package
 284      * name. Otherwise, the entire argument is interpreted as a package
 285      * name.
 286      *
 287      * @param location location of the file
 288      * @param moduleAndPkg module and/or package relative to which the file 
 289      *          should be searched for, or the empty string if none
 290      * @param relativeName final pathname components of the file
 291      * @return an object to read the file
 292      * @throws FilerException if the same pathname has already been
 293      * opened for writing
 294      * @throws IOException if the file cannot be opened
 295      * @throws IllegalArgumentException for an unsupported location
 296      * @throws IllegalArgumentException if {@code relativeName} is not relative
 297      */
 298     FileObject getResource(JavaFileManager.Location location,
 299                            CharSequence moduleAndPkg,
 300                            CharSequence relativeName) throws IOException;
 301 }
< prev index next >