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

changeset 1
9a66ca7c79fa
child 231
435d5d9bb87d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/javax/annotation/processing/Filer.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,261 @@
     1.4 +/*
     1.5 + * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package javax.annotation.processing;
    1.30 +
    1.31 +import javax.tools.JavaFileManager;
    1.32 +import javax.tools.*;
    1.33 +import javax.lang.model.element.Element;
    1.34 +import java.io.IOException;
    1.35 +
    1.36 +/**
    1.37 + * This interface supports the creation of new files by an annotation
    1.38 + * processor.  Files created in this way will be known to the
    1.39 + * annotation processing tool implementing this interface, better
    1.40 + * enabling the tool to manage them.  Source and class files so
    1.41 + * created will be considered for processing by the tool after the
    1.42 + * {@code close} method has been called on the {@code Writer} or
    1.43 + * {@code OutputStream} used to write the contents of the file.
    1.44 + *
    1.45 + * Three kinds of files are distinguished: source files, class files,
    1.46 + * and auxiliary resource files.
    1.47 + *
    1.48 + * <p> There are two distinguished supported locations (subtrees
    1.49 + * within the logical file system) where newly created files are
    1.50 + * placed: one for {@linkplain
    1.51 + * javax.tools.StandardLocation#SOURCE_OUTPUT new source files}, and
    1.52 + * one for {@linkplain javax.tools.StandardLocation#CLASS_OUTPUT new
    1.53 + * class files}.  (These might be specified on a tool's command line,
    1.54 + * for example, using flags such as {@code -s} and {@code -d}.)  The
    1.55 + * actual locations for new source files and new class files may or
    1.56 + * may not be distinct on a particular run of the tool.  Resource
    1.57 + * files may be created in either location.  The methods for reading
    1.58 + * and writing resources take a relative name argument.  A relative
    1.59 + * name is a non-null, non-empty sequence of path segments separated
    1.60 + * by {@code '/'}; {@code '.'} and {@code '..'} are invalid path
    1.61 + * segments.  A valid relative name must match the
    1.62 + * &quot;path-rootless&quot; rule of <a
    1.63 + * href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>, section
    1.64 + * 3.3.
    1.65 + *
    1.66 + * <p>The file creation methods take a variable number of arguments to
    1.67 + * allow the <em>originating elements</em> to be provided as hints to
    1.68 + * the tool infrastructure to better manage dependencies.  The
    1.69 + * originating elements are the types or packages (representing {@code
    1.70 + * package-info} files) which caused an annotation processor to
    1.71 + * attempt to create a new file.  For example, if an annotation
    1.72 + * processor tries to create a source file, {@code
    1.73 + * GeneratedFromUserSource}, in response to processing
    1.74 + *
    1.75 + * <blockquote><pre>
    1.76 + *  &#64;Generate
    1.77 + *  public class UserSource {}
    1.78 + * </pre></blockquote>
    1.79 + *
    1.80 + * the type element for {@code UserSource} should be passed as part of
    1.81 + * the creation method call as in:
    1.82 + *
    1.83 + * <blockquote><pre>
    1.84 + *      filer.createSourceFile("GeneratedFromUserSource",
    1.85 + *                             eltUtils.getTypeElement("UserSource"));
    1.86 + * </pre></blockquote>
    1.87 + *
    1.88 + * If there are no originating elements, none need to be passed.  This
    1.89 + * information may be used in an incremental environment to determine
    1.90 + * the need to rerun processors or remove generated files.
    1.91 + * Non-incremental environments may ignore the originating element
    1.92 + * information.
    1.93 + *
    1.94 + * <p> During each run of an annotation processing tool, a file with a
    1.95 + * given pathname may be created only once.  If that file already
    1.96 + * exists before the first attempt to create it, the old contents will
    1.97 + * be deleted.  Any subsequent attempt to create the same file during
    1.98 + * a run will throw a {@link FilerException}, as will attempting to
    1.99 + * create both a class file and source file for the same type name or
   1.100 + * same package name.  The {@linkplain Processor initial inputs} to
   1.101 + * the tool are considered to be created by the zeroth round;
   1.102 + * therefore, attempting to create a source or class file
   1.103 + * corresponding to one of those inputs will result in a {@link
   1.104 + * FilerException}.
   1.105 + *
   1.106 + * <p> In general, processors must not knowingly attempt to overwrite
   1.107 + * existing files that were not generated by some processor.  A {@code
   1.108 + * Filer} may reject attempts to open a file corresponding to an
   1.109 + * existing type, like {@code java.lang.Object}.  Likewise, the
   1.110 + * invoker of the annotation processing tool must not knowingly
   1.111 + * configure the tool such that the discovered processors will attempt
   1.112 + * to overwrite existing files that were not generated.
   1.113 + *
   1.114 + * <p> Processors can indicate a source or class file is generated by
   1.115 + * including an {@link javax.annotation.Generated @Generated}
   1.116 + * annotation.
   1.117 + *
   1.118 + * <p> Note that some of the effect of overwriting a file can be
   1.119 + * achieved by using a <i>decorator</i>-style pattern.  Instead of
   1.120 + * modifying a class directly, the class is designed so that either
   1.121 + * its superclass is generated by annotation processing or subclasses
   1.122 + * of the class are generated by annotation processing.  If the
   1.123 + * subclasses are generated, the parent class may be designed to use
   1.124 + * factories instead of public constructors so that only subclass
   1.125 + * instances would be presented to clients of the parent class.
   1.126 + *
   1.127 + * @author Joseph D. Darcy
   1.128 + * @author Scott Seligman
   1.129 + * @author Peter von der Ah&eacute;
   1.130 + * @since 1.6
   1.131 + */
   1.132 +public interface Filer {
   1.133 +    /**
   1.134 +     * Creates a new source file and returns an object to allow
   1.135 +     * writing to it.  The file's name and path (relative to the
   1.136 +     * {@linkplain StandardLocation#SOURCE_OUTPUT root output location
   1.137 +     * for source files}) are based on the type to be declared in that
   1.138 +     * file.  If more than one type is being declared, the name of the
   1.139 +     * principal top-level type (the public one, for example) should
   1.140 +     * be used.  A source file can also be created to hold information
   1.141 +     * about a package, including package annotations.  To create a
   1.142 +     * source file for a named package, have {@code name} be the
   1.143 +     * package's name followed by {@code ".package-info"}; to create a
   1.144 +     * source file for an unnamed package, use {@code "package-info"}.
   1.145 +     *
   1.146 +     * <p> Note that to use a particular {@linkplain
   1.147 +     * java.nio.charset.Charset charset} to encode the contents of the
   1.148 +     * file, an {@code OutputStreamWriter} with the chosen charset can
   1.149 +     * be created from the {@code OutputStream} from the returned
   1.150 +     * object. If the {@code Writer} from the returned object is
   1.151 +     * directly used for writing, its charset is determined by the
   1.152 +     * implementation.  An annotation processing tool may have an
   1.153 +     * {@code -encoding} flag or analogous option for specifying this;
   1.154 +     * otherwise, it will typically be the platform's default
   1.155 +     * encoding.
   1.156 +     *
   1.157 +     * <p>To avoid subsequent errors, the contents of the source file
   1.158 +     * should be compatible with the {@linkplain
   1.159 +     * ProcessingEnvironment#getSourceVersion source version} being used
   1.160 +     * for this run.
   1.161 +     *
   1.162 +     * @param name  canonical (fully qualified) name of the principal type
   1.163 +     *          being declared in this file or a package name followed by
   1.164 +     *          {@code ".package-info"} for a package information file
   1.165 +     * @param originatingElements type or package elements causally
   1.166 +     * associated with the creation of this file, may be elided or
   1.167 +     * {@code null}
   1.168 +     * @return a {@code JavaFileObject} to write the new source file
   1.169 +     * @throws FilerException if the same pathname has already been
   1.170 +     * created, the same type has already been created, or the name is
   1.171 +     * not valid for a type
   1.172 +     * @throws IOException if the file cannot be created
   1.173 +     */
   1.174 +    JavaFileObject createSourceFile(CharSequence name,
   1.175 +                                    Element... originatingElements) throws IOException;
   1.176 +
   1.177 +    /**
   1.178 +     * Creates a new class file, and returns an object to allow
   1.179 +     * writing to it.  The file's name and path (relative to the
   1.180 +     * {@linkplain StandardLocation#CLASS_OUTPUT root output location
   1.181 +     * for class files}) are based on the name of the type being
   1.182 +     * written.  A class file can also be created to hold information
   1.183 +     * about a package, including package annotations.  To create a
   1.184 +     * class file for a named package, have {@code name} be the
   1.185 +     * package's name followed by {@code ".package-info"}; creating a
   1.186 +     * class file for an unnamed package is not supported.
   1.187 +     *
   1.188 +     * <p>To avoid subsequent errors, the contents of the class file
   1.189 +     * should be compatible with the {@linkplain
   1.190 +     * ProcessingEnvironment#getSourceVersion source version} being used
   1.191 +     * for this run.
   1.192 +     *
   1.193 +     * @param name binary name of the type being written or a package name followed by
   1.194 +     *          {@code ".package-info"} for a package information file
   1.195 +     * @param originatingElements type or package elements causally
   1.196 +     * associated with the creation of this file, may be elided or
   1.197 +     * {@code null}
   1.198 +     * @return a {@code JavaFileObject} to write the new class file
   1.199 +     * @throws FilerException if the same pathname has already been
   1.200 +     * created, the same type has already been created, or the name is
   1.201 +     * not valid for a type
   1.202 +     * @throws IOException if the file cannot be created
   1.203 +     */
   1.204 +    JavaFileObject createClassFile(CharSequence name,
   1.205 +                                   Element... originatingElements) throws IOException;
   1.206 +
   1.207 +    /**
   1.208 +     * Creates a new auxiliary resource file for writing and returns a
   1.209 +     * file object for it.  The file may be located along with the
   1.210 +     * newly created source files, newly created binary files, or
   1.211 +     * other supported location.  The locations {@link
   1.212 +     * StandardLocation#CLASS_OUTPUT CLASS_OUTPUT} and {@link
   1.213 +     * StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must be
   1.214 +     * supported.  The resource may be named relative to some package
   1.215 +     * (as are source and class files), and from there by a relative
   1.216 +     * pathname.  In a loose sense, the full pathname of the new file
   1.217 +     * will be the concatenation of {@code location}, {@code pkg}, and
   1.218 +     * {@code relativeName}.
   1.219 +     *
   1.220 +     * <p>Files created via this method are not registered for
   1.221 +     * annotation processing, even if the full pathname of the file
   1.222 +     * would correspond to the full pathname of a new source file
   1.223 +     * or new class file.
   1.224 +     *
   1.225 +     * @param location location of the new file
   1.226 +     * @param pkg package relative to which the file should be named,
   1.227 +     *          or the empty string if none
   1.228 +     * @param relativeName final pathname components of the file
   1.229 +     * @param originatingElements type or package elements causally
   1.230 +     * associated with the creation of this file, may be elided or
   1.231 +     * {@code null}
   1.232 +     * @return a {@code FileObject} to write the new resource
   1.233 +     * @throws IOException if the file cannot be created
   1.234 +     * @throws FilerException if the same pathname has already been
   1.235 +     * created
   1.236 +     * @throws IllegalArgumentException for an unsupported location
   1.237 +     * @throws IllegalArgumentException if {@code relativeName} is not relative
   1.238 +     */
   1.239 +   FileObject createResource(JavaFileManager.Location location,
   1.240 +                             CharSequence pkg,
   1.241 +                             CharSequence relativeName,
   1.242 +                             Element... originatingElements) throws IOException;
   1.243 +
   1.244 +    /**
   1.245 +     * Returns an object for reading an existing resource.  The
   1.246 +     * locations {@link StandardLocation#CLASS_OUTPUT CLASS_OUTPUT}
   1.247 +     * and {@link StandardLocation#SOURCE_OUTPUT SOURCE_OUTPUT} must
   1.248 +     * be supported.
   1.249 +     *
   1.250 +     * @param location location of the file
   1.251 +     * @param pkg package relative to which the file should be searched,
   1.252 +     *          or the empty string if none
   1.253 +     * @param relativeName final pathname components of the file
   1.254 +     * @return an object to read the file
   1.255 +     * @throws FilerException if the same pathname has already been
   1.256 +     * opened for writing
   1.257 +     * @throws IOException if the file cannot be opened
   1.258 +     * @throws IllegalArgumentException for an unsupported location
   1.259 +     * @throws IllegalArgumentException if {@code relativeName} is not relative
   1.260 +     */
   1.261 +    FileObject getResource(JavaFileManager.Location location,
   1.262 +                           CharSequence pkg,
   1.263 +                           CharSequence relativeName) throws IOException;
   1.264 +}

mercurial