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

Tue, 09 Oct 2012 19:10:00 -0700

author
jjg
date
Tue, 09 Oct 2012 19:10:00 -0700
changeset 1357
c75be5bc5283
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8000663: clean up langtools imports
Reviewed-by: darcy

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

mercurial