langtools/src/java.compiler/share/classes/javax/annotation/processing/Filer.java
changeset 44291 e1b620ac6c98
parent 43567 d0d89c3da7be
equal deleted inserted replaced
44290:202973b2d1ae 44291:e1b620ac6c98
    26 package javax.annotation.processing;
    26 package javax.annotation.processing;
    27 
    27 
    28 import javax.tools.JavaFileManager;
    28 import javax.tools.JavaFileManager;
    29 import javax.tools.*;
    29 import javax.tools.*;
    30 import javax.lang.model.element.Element;
    30 import javax.lang.model.element.Element;
       
    31 import javax.lang.model.util.Elements;
    31 import java.io.IOException;
    32 import java.io.IOException;
    32 
    33 
    33 /**
    34 /**
    34  * This interface supports the creation of new files by an annotation
    35  * This interface supports the creation of new files by an annotation
    35  * processor.  Files created in this way will be known to the
    36  * processor.  Files created in this way will be known to the
   155      * <p>The optional module name is prefixed to the type name or
   156      * <p>The optional module name is prefixed to the type name or
   156      * package name and separated using a "{@code /}" character. For
   157      * package name and separated using a "{@code /}" character. For
   157      * example, to create a source file for type {@code a.B} in module
   158      * 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      * {@code foo}, use a {@code name} argument of {@code "foo/a.B"}.
   159      *
   160      *
       
   161      * <p>If no explicit module prefix is given and modules are supported
       
   162      * in the environment, a suitable module is inferred. If a suitable
       
   163      * module cannot be inferred {@link FilerException} is thrown.
       
   164      * An implementation may use information about the configuration of
       
   165      * the annotation processing tool as part of the inference.
       
   166      *
   160      * <p>Creating a source file in or for an unnamed package in a named
   167      * <p>Creating a source file in or for an unnamed package in a named
   161      * module is <em>not</em> supported.
   168      * module is <em>not</em> supported.
   162      *
   169      *
   163      * @apiNote To use a particular {@linkplain
   170      * @apiNote To use a particular {@linkplain
   164      * java.nio.charset.Charset charset} to encode the contents of the
   171      * java.nio.charset.Charset charset} to encode the contents of the
   174      * <p>To avoid subsequent errors, the contents of the source file
   181      * <p>To avoid subsequent errors, the contents of the source file
   175      * should be compatible with the {@linkplain
   182      * should be compatible with the {@linkplain
   176      * ProcessingEnvironment#getSourceVersion source version} being used
   183      * ProcessingEnvironment#getSourceVersion source version} being used
   177      * for this run.
   184      * for this run.
   178      *
   185      *
       
   186      * @implNote In the reference implementation, if the annotation
       
   187      * processing tool is processing a single module <i>M</i>,
       
   188      * then <i>M</i> is used as the module for files created without
       
   189      * an explicit module prefix. If the tool is processing multiple
       
   190      * modules, and {@link
       
   191      * Elements#getPackageElement(java.lang.CharSequence)
       
   192      * Elements.getPackageElement(package-of(name))}
       
   193      * returns a package, the module that owns the returned package is used
       
   194      * as the target module. A separate option may be used to provide the target
       
   195      * module if it cannot be determined using the above rules.
       
   196      *
   179      * @param name  canonical (fully qualified) name of the principal type
   197      * @param name  canonical (fully qualified) name of the principal type
   180      *          being declared in this file or a package name followed by
   198      *          being declared in this file or a package name followed by
   181      *          {@code ".package-info"} for a package information file
   199      *          {@code ".package-info"} for a package information file
   182      * @param originatingElements type or package or module elements causally
   200      * @param originatingElements type or package or module elements causally
   183      * associated with the creation of this file, may be elided or
   201      * associated with the creation of this file, may be elided or
   184      * {@code null}
   202      * {@code null}
   185      * @return a {@code JavaFileObject} to write the new source file
   203      * @return a {@code JavaFileObject} to write the new source file
   186      * @throws FilerException if the same pathname has already been
   204      * @throws FilerException if the same pathname has already been
   187      * created, the same type has already been created, or the name is
   205      * created, the same type has already been created, the name is
   188      * otherwise not valid for the entity requested to being created
   206      * otherwise not valid for the entity requested to being created,
       
   207      * if the target module cannot be determined, if the target
       
   208      * module is not writable, or a module is specified when the environment
       
   209      * doesn't support modules.
   189      * @throws IOException if the file cannot be created
   210      * @throws IOException if the file cannot be created
   190      * @jls 7.3 Compilation Units
   211      * @jls 7.3 Compilation Units
   191      */
   212      */
   192     JavaFileObject createSourceFile(CharSequence name,
   213     JavaFileObject createSourceFile(CharSequence name,
   193                                     Element... originatingElements) throws IOException;
   214                                     Element... originatingElements) throws IOException;
   211      * <p>The optional module name is prefixed to the type name or
   232      * <p>The optional module name is prefixed to the type name or
   212      * package name and separated using a "{@code /}" character. For
   233      * package name and separated using a "{@code /}" character. For
   213      * example, to create a class file for type {@code a.B} in module
   234      * 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"}.
   235      * {@code foo}, use a {@code name} argument of {@code "foo/a.B"}.
   215      *
   236      *
       
   237      * <p>If no explicit module prefix is given and modules are supported
       
   238      * in the environment, a suitable module is inferred. If a suitable
       
   239      * module cannot be inferred {@link FilerException} is thrown.
       
   240      * An implementation may use information about the configuration of
       
   241      * the annotation processing tool as part of the inference.
       
   242      *
   216      * <p>Creating a class file in or for an unnamed package in a named
   243      * <p>Creating a class file in or for an unnamed package in a named
   217      * module is <em>not</em> supported.
   244      * module is <em>not</em> supported.
   218      *
   245      *
   219      * @apiNote To avoid subsequent errors, the contents of the class
   246      * @apiNote To avoid subsequent errors, the contents of the class
   220      * file should be compatible with the {@linkplain
   247      * file should be compatible with the {@linkplain
   221      * ProcessingEnvironment#getSourceVersion source version} being
   248      * ProcessingEnvironment#getSourceVersion source version} being
   222      * used for this run.
   249      * used for this run.
       
   250      *
       
   251      * @implNote In the reference implementation, if the annotation
       
   252      * processing tool is processing a single module <i>M</i>,
       
   253      * then <i>M</i> is used as the module for files created without
       
   254      * an explicit module prefix. If the tool is processing multiple
       
   255      * modules, and {@link
       
   256      * Elements#getPackageElement(java.lang.CharSequence)
       
   257      * Elements.getPackageElement(package-of(name))}
       
   258      * returns a package, the module that owns the returned package is used
       
   259      * as the target module. A separate option may be used to provide the target
       
   260      * module if it cannot be determined using the above rules.
   223      *
   261      *
   224      * @param name binary name of the type being written or a package name followed by
   262      * @param name binary name of the type being written or a package name followed by
   225      *          {@code ".package-info"} for a package information file
   263      *          {@code ".package-info"} for a package information file
   226      * @param originatingElements type or package or module elements causally
   264      * @param originatingElements type or package or module elements causally
   227      * associated with the creation of this file, may be elided or
   265      * associated with the creation of this file, may be elided or
   228      * {@code null}
   266      * {@code null}
   229      * @return a {@code JavaFileObject} to write the new class file
   267      * @return a {@code JavaFileObject} to write the new class file
   230      * @throws FilerException if the same pathname has already been
   268      * @throws FilerException if the same pathname has already been
   231      * created, the same type has already been created, or the name is
   269      * created, the same type has already been created, the name is
   232      * not valid for a type
   270      * not valid for a type, if the target module cannot be determined,
       
   271      * if the target module is not writable, or a module is specified when
       
   272      * the environment doesn't support modules.
   233      * @throws IOException if the file cannot be created
   273      * @throws IOException if the file cannot be created
   234      */
   274      */
   235     JavaFileObject createClassFile(CharSequence name,
   275     JavaFileObject createClassFile(CharSequence name,
   236                                    Element... originatingElements) throws IOException;
   276                                    Element... originatingElements) throws IOException;
   237 
   277 
   253      * the suffix after the "{@code /}" character is the package
   293      * the suffix after the "{@code /}" character is the package
   254      * name. The package suffix may be empty. If {@code moduleAndPkg}
   294      * name. The package suffix may be empty. If {@code moduleAndPkg}
   255      * does not contain a "{@code /}" character, the entire argument
   295      * does not contain a "{@code /}" character, the entire argument
   256      * is interpreted as a package name.
   296      * is interpreted as a package name.
   257      *
   297      *
       
   298      * <p>If the given location is neither a {@linkplain
       
   299      * JavaFileManager.Location#isModuleOrientedLocation()
       
   300      * module oriented location}, nor an {@linkplain
       
   301      * JavaFileManager.Location#isOutputLocation()
       
   302      * output location containing multiple modules}, and the explicit
       
   303      * module prefix is given, {@link FilerException} is thrown.
       
   304      *
       
   305      * <p>If the given location is either a module oriented location,
       
   306      * or an output location containing multiple modules, and no explicit
       
   307      * modules prefix is given, a suitable module is
       
   308      * inferred. If a suitable module cannot be inferred {@link
       
   309      * FilerException} is thrown. An implementation may use information
       
   310      * about the configuration of the annotation processing tool
       
   311      * as part of the inference.
       
   312      *
   258      * <p>Files created via this method are <em>not</em> registered for
   313      * <p>Files created via this method are <em>not</em> registered for
   259      * annotation processing, even if the full pathname of the file
   314      * annotation processing, even if the full pathname of the file
   260      * would correspond to the full pathname of a new source file
   315      * would correspond to the full pathname of a new source file
   261      * or new class file.
   316      * or new class file.
       
   317      *
       
   318      * @implNote In the reference implementation, if the annotation
       
   319      * processing tool is processing a single module <i>M</i>,
       
   320      * then <i>M</i> is used as the module for files created without
       
   321      * an explicit module prefix. If the tool is processing multiple
       
   322      * modules, and {@link
       
   323      * Elements#getPackageElement(java.lang.CharSequence)
       
   324      * Elements.getPackageElement(package-of(name))}
       
   325      * returns a package, the module that owns the returned package is used
       
   326      * as the target module. A separate option may be used to provide the target
       
   327      * module if it cannot be determined using the above rules.
   262      *
   328      *
   263      * @param location location of the new file
   329      * @param location location of the new file
   264      * @param moduleAndPkg module and/or package relative to which the file
   330      * @param moduleAndPkg module and/or package relative to which the file
   265      *           should be named, or the empty string if none
   331      *           should be named, or the empty string if none
   266      * @param relativeName final pathname components of the file
   332      * @param relativeName final pathname components of the file
   268      * associated with the creation of this file, may be elided or
   334      * associated with the creation of this file, may be elided or
   269      * {@code null}
   335      * {@code null}
   270      * @return a {@code FileObject} to write the new resource
   336      * @return a {@code FileObject} to write the new resource
   271      * @throws IOException if the file cannot be created
   337      * @throws IOException if the file cannot be created
   272      * @throws FilerException if the same pathname has already been
   338      * @throws FilerException if the same pathname has already been
   273      * created
   339      * created, if the target module cannot be determined,
       
   340      * or if the target module is not writable, or if an explicit
       
   341      * target module is specified and the location does not support it.
   274      * @throws IllegalArgumentException for an unsupported location
   342      * @throws IllegalArgumentException for an unsupported location
   275      * @throws IllegalArgumentException if {@code moduleAndPkg} is ill-formed
   343      * @throws IllegalArgumentException if {@code moduleAndPkg} is ill-formed
   276      * @throws IllegalArgumentException if {@code relativeName} is not relative
   344      * @throws IllegalArgumentException if {@code relativeName} is not relative
   277      */
   345      */
   278    FileObject createResource(JavaFileManager.Location location,
   346    FileObject createResource(JavaFileManager.Location location,
   292      * name. The package suffix may be empty; however, if a module
   360      * name. The package suffix may be empty; however, if a module
   293      * name is present, it must be nonempty. If {@code moduleAndPkg}
   361      * name is present, it must be nonempty. If {@code moduleAndPkg}
   294      * does not contain a "{@code /}" character, the entire argument
   362      * does not contain a "{@code /}" character, the entire argument
   295      * is interpreted as a package name.
   363      * is interpreted as a package name.
   296      *
   364      *
       
   365      * <p>If the given location is neither a {@linkplain
       
   366      * JavaFileManager.Location#isModuleOrientedLocation()
       
   367      * module oriented location}, nor an {@linkplain
       
   368      * JavaFileManager.Location#isOutputLocation()
       
   369      * output location containing multiple modules}, and the explicit
       
   370      * module prefix is given, {@link FilerException} is thrown.
       
   371      *
       
   372      * <p>If the given location is either a module oriented location,
       
   373      * or an output location containing multiple modules, and no explicit
       
   374      * modules prefix is given, a suitable module is
       
   375      * inferred. If a suitable module cannot be inferred {@link
       
   376      * FilerException} is thrown. An implementation may use information
       
   377      * about the configuration of the annotation processing tool
       
   378      * as part of the inference.
       
   379      *
       
   380      * @implNote In the reference implementation, if the annotation
       
   381      * processing tool is processing a single module <i>M</i>,
       
   382      * then <i>M</i> is used as the module for files read without
       
   383      * an explicit module prefix. If the tool is processing multiple
       
   384      * modules, and {@link
       
   385      * Elements#getPackageElement(java.lang.CharSequence)
       
   386      * Elements.getPackageElement(package-of(name))}
       
   387      * returns a package, the module that owns the returned package is used
       
   388      * as the source module. A separate option may be used to provide the target
       
   389      * module if it cannot be determined using the above rules.
       
   390      *
   297      * @param location location of the file
   391      * @param location location of the file
   298      * @param moduleAndPkg module and/or package relative to which the file
   392      * @param moduleAndPkg module and/or package relative to which the file
   299      *          should be searched for, or the empty string if none
   393      *          should be searched for, or the empty string if none
   300      * @param relativeName final pathname components of the file
   394      * @param relativeName final pathname components of the file
   301      * @return an object to read the file
   395      * @return an object to read the file
   302      * @throws FilerException if the same pathname has already been
   396      * @throws FilerException if the same pathname has already been
   303      * opened for writing
   397      * opened for writing, if the source module cannot be determined,
       
   398      * or if the target module is not writable, or if an explicit target
       
   399      * module is specified and the location does not support it.
   304      * @throws IOException if the file cannot be opened
   400      * @throws IOException if the file cannot be opened
   305      * @throws IllegalArgumentException for an unsupported location
   401      * @throws IllegalArgumentException for an unsupported location
   306      * @throws IllegalArgumentException if {@code moduleAndPkg} is ill-formed
   402      * @throws IllegalArgumentException if {@code moduleAndPkg} is ill-formed
   307      * @throws IllegalArgumentException if {@code relativeName} is not relative
   403      * @throws IllegalArgumentException if {@code relativeName} is not relative
   308      */
   404      */