src/share/classes/com/sun/tools/javac/processing/JavacFiler.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacFiler.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,642 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2011, Oracle and/or its affiliates. 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.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.processing;
    1.30 +
    1.31 +import java.io.Closeable;
    1.32 +import java.io.FileNotFoundException;
    1.33 +import java.io.InputStream;
    1.34 +import java.io.OutputStream;
    1.35 +import java.io.FilterOutputStream;
    1.36 +import java.io.Reader;
    1.37 +import java.io.Writer;
    1.38 +import java.io.FilterWriter;
    1.39 +import java.io.PrintWriter;
    1.40 +import java.io.IOException;
    1.41 +import java.util.*;
    1.42 +
    1.43 +import static java.util.Collections.*;
    1.44 +
    1.45 +import javax.annotation.processing.*;
    1.46 +import javax.lang.model.SourceVersion;
    1.47 +import javax.lang.model.element.NestingKind;
    1.48 +import javax.lang.model.element.Modifier;
    1.49 +import javax.lang.model.element.Element;
    1.50 +import javax.tools.*;
    1.51 +import javax.tools.JavaFileManager.Location;
    1.52 +
    1.53 +import static javax.tools.StandardLocation.SOURCE_OUTPUT;
    1.54 +import static javax.tools.StandardLocation.CLASS_OUTPUT;
    1.55 +
    1.56 +import com.sun.tools.javac.code.Lint;
    1.57 +import com.sun.tools.javac.util.*;
    1.58 +
    1.59 +import static com.sun.tools.javac.code.Lint.LintCategory.PROCESSING;
    1.60 +
    1.61 +/**
    1.62 + * The FilerImplementation class must maintain a number of
    1.63 + * constraints.  First, multiple attempts to open the same path within
    1.64 + * the same invocation of the tool results in an IOException being
    1.65 + * thrown.  For example, trying to open the same source file twice:
    1.66 + *
    1.67 + * <pre>
    1.68 + * createSourceFile("foo.Bar")
    1.69 + * ...
    1.70 + * createSourceFile("foo.Bar")
    1.71 + * </pre>
    1.72 + *
    1.73 + * is disallowed as is opening a text file that happens to have
    1.74 + * the same name as a source file:
    1.75 + *
    1.76 + * <pre>
    1.77 + * createSourceFile("foo.Bar")
    1.78 + * ...
    1.79 + * createTextFile(SOURCE_TREE, "foo", new File("Bar"), null)
    1.80 + * </pre>
    1.81 + *
    1.82 + * <p>Additionally, creating a source file that corresponds to an
    1.83 + * already created class file (or vice versa) also results in an
    1.84 + * IOException since each type can only be created once.  However, if
    1.85 + * the Filer is used to create a text file named *.java that happens
    1.86 + * to correspond to an existing class file, a warning is *not*
    1.87 + * generated.  Similarly, a warning is not generated for a binary file
    1.88 + * named *.class and an existing source file.
    1.89 + *
    1.90 + * <p>The reason for this difference is that source files and class
    1.91 + * files are registered with the tool and can get passed on as
    1.92 + * declarations to the next round of processing.  Files that are just
    1.93 + * named *.java and *.class are not processed in that manner; although
    1.94 + * having extra source files and class files on the source path and
    1.95 + * class path can alter the behavior of the tool and any final
    1.96 + * compile.
    1.97 + *
    1.98 + * <p><b>This is NOT part of any supported API.
    1.99 + * If you write code that depends on this, you do so at your own risk.
   1.100 + * This code and its internal interfaces are subject to change or
   1.101 + * deletion without notice.</b>
   1.102 + */
   1.103 +public class JavacFiler implements Filer, Closeable {
   1.104 +    // TODO: Implement different transaction model for updating the
   1.105 +    // Filer's record keeping on file close.
   1.106 +
   1.107 +    private static final String ALREADY_OPENED =
   1.108 +        "Output stream or writer has already been opened.";
   1.109 +    private static final String NOT_FOR_READING =
   1.110 +        "FileObject was not opened for reading.";
   1.111 +    private static final String NOT_FOR_WRITING =
   1.112 +        "FileObject was not opened for writing.";
   1.113 +
   1.114 +    /**
   1.115 +     * Wrap a JavaFileObject to manage writing by the Filer.
   1.116 +     */
   1.117 +    private class FilerOutputFileObject extends ForwardingFileObject<FileObject> {
   1.118 +        private boolean opened = false;
   1.119 +        private String name;
   1.120 +
   1.121 +        FilerOutputFileObject(String name, FileObject fileObject) {
   1.122 +            super(fileObject);
   1.123 +            this.name = name;
   1.124 +        }
   1.125 +
   1.126 +        @Override
   1.127 +        public synchronized OutputStream openOutputStream() throws IOException {
   1.128 +            if (opened)
   1.129 +                throw new IOException(ALREADY_OPENED);
   1.130 +            opened = true;
   1.131 +            return new FilerOutputStream(name, fileObject);
   1.132 +        }
   1.133 +
   1.134 +        @Override
   1.135 +        public synchronized Writer openWriter() throws IOException {
   1.136 +            if (opened)
   1.137 +                throw new IOException(ALREADY_OPENED);
   1.138 +            opened = true;
   1.139 +            return new FilerWriter(name, fileObject);
   1.140 +        }
   1.141 +
   1.142 +        // Three anti-literacy methods
   1.143 +        @Override
   1.144 +        public InputStream openInputStream() throws IOException {
   1.145 +            throw new IllegalStateException(NOT_FOR_READING);
   1.146 +        }
   1.147 +
   1.148 +        @Override
   1.149 +        public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
   1.150 +            throw new IllegalStateException(NOT_FOR_READING);
   1.151 +        }
   1.152 +
   1.153 +        @Override
   1.154 +        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.155 +            throw new IllegalStateException(NOT_FOR_READING);
   1.156 +        }
   1.157 +
   1.158 +        @Override
   1.159 +        public boolean delete() {
   1.160 +            return false;
   1.161 +        }
   1.162 +    }
   1.163 +
   1.164 +    private class FilerOutputJavaFileObject extends FilerOutputFileObject implements JavaFileObject {
   1.165 +        private final JavaFileObject javaFileObject;
   1.166 +        FilerOutputJavaFileObject(String name, JavaFileObject javaFileObject) {
   1.167 +            super(name, javaFileObject);
   1.168 +            this.javaFileObject = javaFileObject;
   1.169 +        }
   1.170 +
   1.171 +        public JavaFileObject.Kind getKind() {
   1.172 +            return javaFileObject.getKind();
   1.173 +        }
   1.174 +
   1.175 +        public boolean isNameCompatible(String simpleName,
   1.176 +                                        JavaFileObject.Kind kind) {
   1.177 +            return javaFileObject.isNameCompatible(simpleName, kind);
   1.178 +        }
   1.179 +
   1.180 +        public NestingKind getNestingKind() {
   1.181 +            return javaFileObject.getNestingKind();
   1.182 +        }
   1.183 +
   1.184 +        public Modifier getAccessLevel() {
   1.185 +            return javaFileObject.getAccessLevel();
   1.186 +        }
   1.187 +    }
   1.188 +
   1.189 +    /**
   1.190 +     * Wrap a JavaFileObject to manage reading by the Filer.
   1.191 +     */
   1.192 +    private class FilerInputFileObject extends ForwardingFileObject<FileObject> {
   1.193 +        FilerInputFileObject(FileObject fileObject) {
   1.194 +            super(fileObject);
   1.195 +        }
   1.196 +
   1.197 +        @Override
   1.198 +        public OutputStream openOutputStream() throws IOException {
   1.199 +            throw new IllegalStateException(NOT_FOR_WRITING);
   1.200 +        }
   1.201 +
   1.202 +        @Override
   1.203 +        public Writer openWriter() throws IOException {
   1.204 +            throw new IllegalStateException(NOT_FOR_WRITING);
   1.205 +        }
   1.206 +
   1.207 +        @Override
   1.208 +        public boolean delete() {
   1.209 +            return false;
   1.210 +        }
   1.211 +    }
   1.212 +
   1.213 +    private class FilerInputJavaFileObject extends FilerInputFileObject implements JavaFileObject {
   1.214 +        private final JavaFileObject javaFileObject;
   1.215 +        FilerInputJavaFileObject(JavaFileObject javaFileObject) {
   1.216 +            super(javaFileObject);
   1.217 +            this.javaFileObject = javaFileObject;
   1.218 +        }
   1.219 +
   1.220 +        public JavaFileObject.Kind getKind() {
   1.221 +            return javaFileObject.getKind();
   1.222 +        }
   1.223 +
   1.224 +        public boolean isNameCompatible(String simpleName,
   1.225 +                                        JavaFileObject.Kind kind) {
   1.226 +            return javaFileObject.isNameCompatible(simpleName, kind);
   1.227 +        }
   1.228 +
   1.229 +        public NestingKind getNestingKind() {
   1.230 +            return javaFileObject.getNestingKind();
   1.231 +        }
   1.232 +
   1.233 +        public Modifier getAccessLevel() {
   1.234 +            return javaFileObject.getAccessLevel();
   1.235 +        }
   1.236 +    }
   1.237 +
   1.238 +
   1.239 +    /**
   1.240 +     * Wrap a {@code OutputStream} returned from the {@code
   1.241 +     * JavaFileManager} to properly register source or class files
   1.242 +     * when they are closed.
   1.243 +     */
   1.244 +    private class FilerOutputStream extends FilterOutputStream {
   1.245 +        String typeName;
   1.246 +        FileObject fileObject;
   1.247 +        boolean closed = false;
   1.248 +
   1.249 +        /**
   1.250 +         * @param typeName name of class or {@code null} if just a
   1.251 +         * binary file
   1.252 +         */
   1.253 +        FilerOutputStream(String typeName, FileObject fileObject) throws IOException {
   1.254 +            super(fileObject.openOutputStream());
   1.255 +            this.typeName = typeName;
   1.256 +            this.fileObject = fileObject;
   1.257 +        }
   1.258 +
   1.259 +        public synchronized void close() throws IOException {
   1.260 +            if (!closed) {
   1.261 +                closed = true;
   1.262 +                /*
   1.263 +                 * If an IOException occurs when closing the underlying
   1.264 +                 * stream, still try to process the file.
   1.265 +                 */
   1.266 +
   1.267 +                closeFileObject(typeName, fileObject);
   1.268 +                out.close();
   1.269 +            }
   1.270 +        }
   1.271 +    }
   1.272 +
   1.273 +    /**
   1.274 +     * Wrap a {@code Writer} returned from the {@code JavaFileManager}
   1.275 +     * to properly register source or class files when they are
   1.276 +     * closed.
   1.277 +     */
   1.278 +    private class FilerWriter extends FilterWriter {
   1.279 +        String typeName;
   1.280 +        FileObject fileObject;
   1.281 +        boolean closed = false;
   1.282 +
   1.283 +        /**
   1.284 +         * @param fileObject the fileObject to be written to
   1.285 +         * @param typeName name of source file or {@code null} if just a
   1.286 +         * text file
   1.287 +         */
   1.288 +        FilerWriter(String typeName, FileObject fileObject) throws IOException {
   1.289 +            super(fileObject.openWriter());
   1.290 +            this.typeName = typeName;
   1.291 +            this.fileObject = fileObject;
   1.292 +        }
   1.293 +
   1.294 +        public synchronized void close() throws IOException {
   1.295 +            if (!closed) {
   1.296 +                closed = true;
   1.297 +                /*
   1.298 +                 * If an IOException occurs when closing the underlying
   1.299 +                 * Writer, still try to process the file.
   1.300 +                 */
   1.301 +
   1.302 +                closeFileObject(typeName, fileObject);
   1.303 +                out.close();
   1.304 +            }
   1.305 +        }
   1.306 +    }
   1.307 +
   1.308 +    JavaFileManager fileManager;
   1.309 +    Log log;
   1.310 +    Context context;
   1.311 +    boolean lastRound;
   1.312 +
   1.313 +    private final boolean lint;
   1.314 +
   1.315 +    /**
   1.316 +     * Logical names of all created files.  This set must be
   1.317 +     * synchronized.
   1.318 +     */
   1.319 +    private final Set<FileObject> fileObjectHistory;
   1.320 +
   1.321 +    /**
   1.322 +     * Names of types that have had files created but not closed.
   1.323 +     */
   1.324 +    private final Set<String> openTypeNames;
   1.325 +
   1.326 +    /**
   1.327 +     * Names of source files closed in this round.  This set must be
   1.328 +     * synchronized.  Its iterators should preserve insertion order.
   1.329 +     */
   1.330 +    private Set<String> generatedSourceNames;
   1.331 +
   1.332 +    /**
   1.333 +     * Names and class files of the class files closed in this round.
   1.334 +     * This set must be synchronized.  Its iterators should preserve
   1.335 +     * insertion order.
   1.336 +     */
   1.337 +    private final Map<String, JavaFileObject> generatedClasses;
   1.338 +
   1.339 +    /**
   1.340 +     * JavaFileObjects for source files closed in this round.  This
   1.341 +     * set must be synchronized.  Its iterators should preserve
   1.342 +     * insertion order.
   1.343 +     */
   1.344 +    private Set<JavaFileObject> generatedSourceFileObjects;
   1.345 +
   1.346 +    /**
   1.347 +     * Names of all created source files.  Its iterators should
   1.348 +     * preserve insertion order.
   1.349 +     */
   1.350 +    private final Set<String> aggregateGeneratedSourceNames;
   1.351 +
   1.352 +    /**
   1.353 +     * Names of all created class files.  Its iterators should
   1.354 +     * preserve insertion order.
   1.355 +     */
   1.356 +    private final Set<String> aggregateGeneratedClassNames;
   1.357 +
   1.358 +
   1.359 +    JavacFiler(Context context) {
   1.360 +        this.context = context;
   1.361 +        fileManager = context.get(JavaFileManager.class);
   1.362 +
   1.363 +        log = Log.instance(context);
   1.364 +
   1.365 +        fileObjectHistory = synchronizedSet(new LinkedHashSet<FileObject>());
   1.366 +        generatedSourceNames = synchronizedSet(new LinkedHashSet<String>());
   1.367 +        generatedSourceFileObjects = synchronizedSet(new LinkedHashSet<JavaFileObject>());
   1.368 +
   1.369 +        generatedClasses = synchronizedMap(new LinkedHashMap<String, JavaFileObject>());
   1.370 +
   1.371 +        openTypeNames  = synchronizedSet(new LinkedHashSet<String>());
   1.372 +
   1.373 +        aggregateGeneratedSourceNames = new LinkedHashSet<String>();
   1.374 +        aggregateGeneratedClassNames  = new LinkedHashSet<String>();
   1.375 +
   1.376 +        lint = (Lint.instance(context)).isEnabled(PROCESSING);
   1.377 +    }
   1.378 +
   1.379 +    public JavaFileObject createSourceFile(CharSequence name,
   1.380 +                                           Element... originatingElements) throws IOException {
   1.381 +        return createSourceOrClassFile(true, name.toString());
   1.382 +    }
   1.383 +
   1.384 +    public JavaFileObject createClassFile(CharSequence name,
   1.385 +                                           Element... originatingElements) throws IOException {
   1.386 +        return createSourceOrClassFile(false, name.toString());
   1.387 +    }
   1.388 +
   1.389 +    private JavaFileObject createSourceOrClassFile(boolean isSourceFile, String name) throws IOException {
   1.390 +        if (lint) {
   1.391 +            int periodIndex = name.lastIndexOf(".");
   1.392 +            if (periodIndex != -1) {
   1.393 +                String base = name.substring(periodIndex);
   1.394 +                String extn = (isSourceFile ? ".java" : ".class");
   1.395 +                if (base.equals(extn))
   1.396 +                    log.warning("proc.suspicious.class.name", name, extn);
   1.397 +            }
   1.398 +        }
   1.399 +        checkNameAndExistence(name, isSourceFile);
   1.400 +        Location loc = (isSourceFile ? SOURCE_OUTPUT : CLASS_OUTPUT);
   1.401 +        JavaFileObject.Kind kind = (isSourceFile ?
   1.402 +                                    JavaFileObject.Kind.SOURCE :
   1.403 +                                    JavaFileObject.Kind.CLASS);
   1.404 +
   1.405 +        JavaFileObject fileObject =
   1.406 +            fileManager.getJavaFileForOutput(loc, name, kind, null);
   1.407 +        checkFileReopening(fileObject, true);
   1.408 +
   1.409 +        if (lastRound)
   1.410 +            log.warning("proc.file.create.last.round", name);
   1.411 +
   1.412 +        if (isSourceFile)
   1.413 +            aggregateGeneratedSourceNames.add(name);
   1.414 +        else
   1.415 +            aggregateGeneratedClassNames.add(name);
   1.416 +        openTypeNames.add(name);
   1.417 +
   1.418 +        return new FilerOutputJavaFileObject(name, fileObject);
   1.419 +    }
   1.420 +
   1.421 +    public FileObject createResource(JavaFileManager.Location location,
   1.422 +                                     CharSequence pkg,
   1.423 +                                     CharSequence relativeName,
   1.424 +                                     Element... originatingElements) throws IOException {
   1.425 +        locationCheck(location);
   1.426 +
   1.427 +        String strPkg = pkg.toString();
   1.428 +        if (strPkg.length() > 0)
   1.429 +            checkName(strPkg);
   1.430 +
   1.431 +        FileObject fileObject =
   1.432 +            fileManager.getFileForOutput(location, strPkg,
   1.433 +                                         relativeName.toString(), null);
   1.434 +        checkFileReopening(fileObject, true);
   1.435 +
   1.436 +        if (fileObject instanceof JavaFileObject)
   1.437 +            return new FilerOutputJavaFileObject(null, (JavaFileObject)fileObject);
   1.438 +        else
   1.439 +            return new FilerOutputFileObject(null, fileObject);
   1.440 +    }
   1.441 +
   1.442 +    private void locationCheck(JavaFileManager.Location location) {
   1.443 +        if (location instanceof StandardLocation) {
   1.444 +            StandardLocation stdLoc = (StandardLocation) location;
   1.445 +            if (!stdLoc.isOutputLocation())
   1.446 +                throw new IllegalArgumentException("Resource creation not supported in location " +
   1.447 +                                                   stdLoc);
   1.448 +        }
   1.449 +    }
   1.450 +
   1.451 +    public FileObject getResource(JavaFileManager.Location location,
   1.452 +                                  CharSequence pkg,
   1.453 +                                  CharSequence relativeName) throws IOException {
   1.454 +        String strPkg = pkg.toString();
   1.455 +        if (strPkg.length() > 0)
   1.456 +            checkName(strPkg);
   1.457 +
   1.458 +        // TODO: Only support reading resources in selected output
   1.459 +        // locations?  Only allow reading of non-source, non-class
   1.460 +        // files from the supported input locations?
   1.461 +
   1.462 +        // In the following, getFileForInput is the "obvious" method
   1.463 +        // to use, but it does not have the "obvious" semantics for
   1.464 +        // SOURCE_OUTPUT and CLASS_OUTPUT. Conversely, getFileForOutput
   1.465 +        // does not have the correct semantics for any "path" location
   1.466 +        // with more than one component. So, for now, we use a hybrid
   1.467 +        // invocation.
   1.468 +        FileObject fileObject;
   1.469 +        if (location.isOutputLocation()) {
   1.470 +            fileObject = fileManager.getFileForOutput(location,
   1.471 +                    pkg.toString(),
   1.472 +                    relativeName.toString(),
   1.473 +                    null);
   1.474 +        } else {
   1.475 +            fileObject = fileManager.getFileForInput(location,
   1.476 +                    pkg.toString(),
   1.477 +                    relativeName.toString());
   1.478 +        }
   1.479 +        if (fileObject == null) {
   1.480 +            String name = (pkg.length() == 0)
   1.481 +                    ? relativeName.toString() : (pkg + "/" + relativeName);
   1.482 +            throw new FileNotFoundException(name);
   1.483 +        }
   1.484 +
   1.485 +        // If the path was already opened for writing, throw an exception.
   1.486 +        checkFileReopening(fileObject, false);
   1.487 +        return new FilerInputFileObject(fileObject);
   1.488 +    }
   1.489 +
   1.490 +    private void checkName(String name) throws FilerException {
   1.491 +        checkName(name, false);
   1.492 +    }
   1.493 +
   1.494 +    private void checkName(String name, boolean allowUnnamedPackageInfo) throws FilerException {
   1.495 +        if (!SourceVersion.isName(name) && !isPackageInfo(name, allowUnnamedPackageInfo)) {
   1.496 +            if (lint)
   1.497 +                log.warning("proc.illegal.file.name", name);
   1.498 +            throw new FilerException("Illegal name " + name);
   1.499 +        }
   1.500 +    }
   1.501 +
   1.502 +    private boolean isPackageInfo(String name, boolean allowUnnamedPackageInfo) {
   1.503 +        // Is the name of the form "package-info" or
   1.504 +        // "foo.bar.package-info"?
   1.505 +        final String PKG_INFO = "package-info";
   1.506 +        int periodIndex = name.lastIndexOf(".");
   1.507 +        if (periodIndex == -1) {
   1.508 +            return allowUnnamedPackageInfo ? name.equals(PKG_INFO) : false;
   1.509 +        } else {
   1.510 +            // "foo.bar.package-info." illegal
   1.511 +            String prefix = name.substring(0, periodIndex);
   1.512 +            String simple = name.substring(periodIndex+1);
   1.513 +            return SourceVersion.isName(prefix) && simple.equals(PKG_INFO);
   1.514 +        }
   1.515 +    }
   1.516 +
   1.517 +    private void checkNameAndExistence(String typename, boolean allowUnnamedPackageInfo) throws FilerException {
   1.518 +        // TODO: Check if type already exists on source or class path?
   1.519 +        // If so, use warning message key proc.type.already.exists
   1.520 +        checkName(typename, allowUnnamedPackageInfo);
   1.521 +        if (aggregateGeneratedSourceNames.contains(typename) ||
   1.522 +            aggregateGeneratedClassNames.contains(typename)) {
   1.523 +            if (lint)
   1.524 +                log.warning("proc.type.recreate", typename);
   1.525 +            throw new FilerException("Attempt to recreate a file for type " + typename);
   1.526 +        }
   1.527 +    }
   1.528 +
   1.529 +    /**
   1.530 +     * Check to see if the file has already been opened; if so, throw
   1.531 +     * an exception, otherwise add it to the set of files.
   1.532 +     */
   1.533 +    private void checkFileReopening(FileObject fileObject, boolean addToHistory) throws FilerException {
   1.534 +        for(FileObject veteran : fileObjectHistory) {
   1.535 +            if (fileManager.isSameFile(veteran, fileObject)) {
   1.536 +                if (lint)
   1.537 +                    log.warning("proc.file.reopening", fileObject.getName());
   1.538 +                throw new FilerException("Attempt to reopen a file for path " + fileObject.getName());
   1.539 +            }
   1.540 +        }
   1.541 +        if (addToHistory)
   1.542 +            fileObjectHistory.add(fileObject);
   1.543 +    }
   1.544 +
   1.545 +    public boolean newFiles() {
   1.546 +        return (!generatedSourceNames.isEmpty())
   1.547 +            || (!generatedClasses.isEmpty());
   1.548 +    }
   1.549 +
   1.550 +    public Set<String> getGeneratedSourceNames() {
   1.551 +        return generatedSourceNames;
   1.552 +    }
   1.553 +
   1.554 +    public Set<JavaFileObject> getGeneratedSourceFileObjects() {
   1.555 +        return generatedSourceFileObjects;
   1.556 +    }
   1.557 +
   1.558 +    public Map<String, JavaFileObject> getGeneratedClasses() {
   1.559 +        return generatedClasses;
   1.560 +    }
   1.561 +
   1.562 +    public void warnIfUnclosedFiles() {
   1.563 +        if (!openTypeNames.isEmpty())
   1.564 +            log.warning("proc.unclosed.type.files", openTypeNames.toString());
   1.565 +    }
   1.566 +
   1.567 +    /**
   1.568 +     * Update internal state for a new round.
   1.569 +     */
   1.570 +    public void newRound(Context context) {
   1.571 +        this.context = context;
   1.572 +        this.log = Log.instance(context);
   1.573 +        clearRoundState();
   1.574 +    }
   1.575 +
   1.576 +    void setLastRound(boolean lastRound) {
   1.577 +        this.lastRound = lastRound;
   1.578 +    }
   1.579 +
   1.580 +    public void close() {
   1.581 +        clearRoundState();
   1.582 +        // Cross-round state
   1.583 +        fileObjectHistory.clear();
   1.584 +        openTypeNames.clear();
   1.585 +        aggregateGeneratedSourceNames.clear();
   1.586 +        aggregateGeneratedClassNames.clear();
   1.587 +    }
   1.588 +
   1.589 +    private void clearRoundState() {
   1.590 +        generatedSourceNames.clear();
   1.591 +        generatedSourceFileObjects.clear();
   1.592 +        generatedClasses.clear();
   1.593 +    }
   1.594 +
   1.595 +    /**
   1.596 +     * Debugging function to display internal state.
   1.597 +     */
   1.598 +    public void displayState() {
   1.599 +        PrintWriter xout = context.get(Log.outKey);
   1.600 +        xout.println("File Object History : " +  fileObjectHistory);
   1.601 +        xout.println("Open Type Names     : " +  openTypeNames);
   1.602 +        xout.println("Gen. Src Names      : " +  generatedSourceNames);
   1.603 +        xout.println("Gen. Cls Names      : " +  generatedClasses.keySet());
   1.604 +        xout.println("Agg. Gen. Src Names : " +  aggregateGeneratedSourceNames);
   1.605 +        xout.println("Agg. Gen. Cls Names : " +  aggregateGeneratedClassNames);
   1.606 +    }
   1.607 +
   1.608 +    public String toString() {
   1.609 +        return "javac Filer";
   1.610 +    }
   1.611 +
   1.612 +    /**
   1.613 +     * Upon close, register files opened by create{Source, Class}File
   1.614 +     * for annotation processing.
   1.615 +     */
   1.616 +    private void closeFileObject(String typeName, FileObject fileObject) {
   1.617 +        /*
   1.618 +         * If typeName is non-null, the file object was opened as a
   1.619 +         * source or class file by the user.  If a file was opened as
   1.620 +         * a resource, typeName will be null and the file is *not*
   1.621 +         * subject to annotation processing.
   1.622 +         */
   1.623 +        if ((typeName != null)) {
   1.624 +            if (!(fileObject instanceof JavaFileObject))
   1.625 +                throw new AssertionError("JavaFileOject not found for " + fileObject);
   1.626 +            JavaFileObject javaFileObject = (JavaFileObject)fileObject;
   1.627 +            switch(javaFileObject.getKind()) {
   1.628 +            case SOURCE:
   1.629 +                generatedSourceNames.add(typeName);
   1.630 +                generatedSourceFileObjects.add(javaFileObject);
   1.631 +                openTypeNames.remove(typeName);
   1.632 +                break;
   1.633 +
   1.634 +            case CLASS:
   1.635 +                generatedClasses.put(typeName, javaFileObject);
   1.636 +                openTypeNames.remove(typeName);
   1.637 +                break;
   1.638 +
   1.639 +            default:
   1.640 +                break;
   1.641 +            }
   1.642 +        }
   1.643 +    }
   1.644 +
   1.645 +}

mercurial