langtools/src/share/classes/com/sun/tools/javac/processing/JavacFiler.java
author jjg
Thu, 29 Jul 2010 19:30:35 -0700
changeset 6159 88930a1c409e
parent 6157 218670cd9b1c
child 6573 6bad8be140fc
permissions -rw-r--r--
6966604: JavacFiler not correctly notified of lastRound Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     2
 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.processing;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import javax.annotation.processing.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import javax.lang.model.SourceVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import javax.lang.model.element.NestingKind;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import javax.lang.model.element.Modifier;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import javax.lang.model.element.Element;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.io.Closeable;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import java.io.InputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import java.io.OutputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import java.io.FilterOutputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import java.io.Reader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import java.io.Writer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import java.io.FilterWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import java.io.PrintWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import javax.tools.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import static java.util.Collections.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import javax.tools.JavaFileManager.Location;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import static javax.tools.StandardLocation.SOURCE_OUTPUT;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import static javax.tools.StandardLocation.CLASS_OUTPUT;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 * The FilerImplementation class must maintain a number of
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 * constraints.  First, multiple attempts to open the same path within
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 * the same invocation of the tool results in an IOException being
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 * thrown.  For example, trying to open the same source file twice:
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 * <pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 * createSourceFile("foo.Bar")
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 * ...
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 * createSourceFile("foo.Bar")
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 * </pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 * is disallowed as is opening a text file that happens to have
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 * the same name as a source file:
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
 * <pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
 * createSourceFile("foo.Bar")
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
 * ...
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
 * createTextFile(SOURCE_TREE, "foo", new File("Bar"), null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
 * </pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
 * <p>Additionally, creating a source file that corresponds to an
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
 * already created class file (or vice versa) also results in an
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
 * IOException since each type can only be created once.  However, if
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
 * the Filer is used to create a text file named *.java that happens
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
 * to correspond to an existing class file, a warning is *not*
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
 * generated.  Similarly, a warning is not generated for a binary file
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
 * named *.class and an existing source file.
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
 * <p>The reason for this difference is that source files and class
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
 * files are registered with the tool and can get passed on as
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
 * declarations to the next round of processing.  Files that are just
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
 * named *.java and *.class are not processed in that manner; although
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
 * having extra source files and class files on the source path and
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
 * class path can alter the behavior of the tool and any final
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
 * compile.
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    90
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
 * If you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
public class JavacFiler implements Filer, Closeable {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    // TODO: Implement different transaction model for updating the
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    // Filer's record keeping on file close.
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    private static final String ALREADY_OPENED =
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        "Output stream or writer has already been opened.";
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    private static final String NOT_FOR_READING =
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        "FileObject was not opened for reading.";
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    private static final String NOT_FOR_WRITING =
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        "FileObject was not opened for writing.";
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     * Wrap a JavaFileObject to manage writing by the Filer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    private class FilerOutputFileObject extends ForwardingFileObject<FileObject> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        private boolean opened = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        private String name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        FilerOutputFileObject(String name, FileObject fileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            super(fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            this.name = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        public synchronized OutputStream openOutputStream() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            if (opened)
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
                throw new IOException(ALREADY_OPENED);
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
            opened = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
            return new FilerOutputStream(name, fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        public synchronized Writer openWriter() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
            if (opened)
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                throw new IOException(ALREADY_OPENED);
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            opened = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            return new FilerWriter(name, fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        // Three anti-literacy methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        public InputStream openInputStream() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            throw new IllegalStateException(NOT_FOR_READING);
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            throw new IllegalStateException(NOT_FOR_READING);
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            throw new IllegalStateException(NOT_FOR_READING);
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        public boolean delete() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    private class FilerOutputJavaFileObject extends FilerOutputFileObject implements JavaFileObject {
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        private final JavaFileObject javaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        FilerOutputJavaFileObject(String name, JavaFileObject javaFileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            super(name, javaFileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
            this.javaFileObject = javaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        public JavaFileObject.Kind getKind() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            return javaFileObject.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        public boolean isNameCompatible(String simpleName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                                        JavaFileObject.Kind kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            return javaFileObject.isNameCompatible(simpleName, kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        public NestingKind getNestingKind() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
            return javaFileObject.getNestingKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        public Modifier getAccessLevel() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
            return javaFileObject.getAccessLevel();
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     * Wrap a JavaFileObject to manage reading by the Filer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    private class FilerInputFileObject extends ForwardingFileObject<FileObject> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        FilerInputFileObject(FileObject fileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            super(fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        public OutputStream openOutputStream() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            throw new IllegalStateException(NOT_FOR_WRITING);
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        public Writer openWriter() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
            throw new IllegalStateException(NOT_FOR_WRITING);
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        public boolean delete() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    private class FilerInputJavaFileObject extends FilerInputFileObject implements JavaFileObject {
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
        private final JavaFileObject javaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        FilerInputJavaFileObject(JavaFileObject javaFileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
            super(javaFileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
            this.javaFileObject = javaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        public JavaFileObject.Kind getKind() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
            return javaFileObject.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        public boolean isNameCompatible(String simpleName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                                        JavaFileObject.Kind kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
            return javaFileObject.isNameCompatible(simpleName, kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        public NestingKind getNestingKind() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
            return javaFileObject.getNestingKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        public Modifier getAccessLevel() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
            return javaFileObject.getAccessLevel();
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
     * Wrap a {@code OutputStream} returned from the {@code
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
     * JavaFileManager} to properly register source or class files
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
     * when they are closed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
    private class FilerOutputStream extends FilterOutputStream {
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        String typeName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        FileObject fileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        boolean closed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
         * @param typeName name of class or {@code null} if just a
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
         * binary file
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
        FilerOutputStream(String typeName, FileObject fileObject) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
            super(fileObject.openOutputStream());
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            this.typeName = typeName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
            this.fileObject = fileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
        public synchronized void close() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            if (!closed) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                closed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
                /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                 * If an IOException occurs when closing the underlying
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                 * stream, still try to process the file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
                closeFileObject(typeName, fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                out.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
     * Wrap a {@code Writer} returned from the {@code JavaFileManager}
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
     * to properly register source or class files when they are
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
     * closed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
    private class FilerWriter extends FilterWriter {
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
        String typeName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        FileObject fileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
        boolean closed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
         * @param fileObject the fileObject to be written to
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
         * @param typeName name of source file or {@code null} if just a
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
         * text file
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        FilerWriter(String typeName, FileObject fileObject) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            super(fileObject.openWriter());
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            this.typeName = typeName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            this.fileObject = fileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
        public synchronized void close() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
            if (!closed) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
                closed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                 * If an IOException occurs when closing the underlying
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                 * Writer, still try to process the file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                closeFileObject(typeName, fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                out.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
    JavaFileManager fileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
    Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
    Context context;
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
    boolean lastRound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
    private final boolean lint;
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
     * Logical names of all created files.  This set must be
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
     * synchronized.
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    private final Set<FileObject> fileObjectHistory;
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
     * Names of types that have had files created but not closed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
    private final Set<String> openTypeNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
     * Names of source files closed in this round.  This set must be
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
     * synchronized.  Its iterators should preserve insertion order.
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
    private Set<String> generatedSourceNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
     * Names and class files of the class files closed in this round.
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
     * This set must be synchronized.  Its iterators should preserve
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
     * insertion order.
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
    private final Map<String, JavaFileObject> generatedClasses;
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
     * JavaFileObjects for source files closed in this round.  This
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
     * set must be synchronized.  Its iterators should preserve
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
     * insertion order.
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
    private Set<JavaFileObject> generatedSourceFileObjects;
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
     * Names of all created source files.  Its iterators should
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
     * preserve insertion order.
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
    private final Set<String> aggregateGeneratedSourceNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
     * Names of all created class files.  Its iterators should
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
     * preserve insertion order.
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
    private final Set<String> aggregateGeneratedClassNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
    JavacFiler(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
        this.context = context;
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
        fileManager = context.get(JavaFileManager.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
        fileObjectHistory = synchronizedSet(new LinkedHashSet<FileObject>());
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
        generatedSourceNames = synchronizedSet(new LinkedHashSet<String>());
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
        generatedSourceFileObjects = synchronizedSet(new LinkedHashSet<JavaFileObject>());
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
        generatedClasses = synchronizedMap(new LinkedHashMap<String, JavaFileObject>());
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
        openTypeNames  = synchronizedSet(new LinkedHashSet<String>());
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
        aggregateGeneratedSourceNames = new LinkedHashSet<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
        aggregateGeneratedClassNames  = new LinkedHashSet<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        lint = (Options.instance(context)).lint("processing");
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
    public JavaFileObject createSourceFile(CharSequence name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
                                           Element... originatingElements) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
        return createSourceOrClassFile(true, name.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
    public JavaFileObject createClassFile(CharSequence name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                                           Element... originatingElements) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
        return createSourceOrClassFile(false, name.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
    private JavaFileObject createSourceOrClassFile(boolean isSourceFile, String name) throws IOException {
6157
218670cd9b1c 6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents: 5847
diff changeset
   382
        if (lint) {
218670cd9b1c 6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents: 5847
diff changeset
   383
            int periodIndex = name.lastIndexOf(".");
218670cd9b1c 6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents: 5847
diff changeset
   384
            if (periodIndex != -1) {
218670cd9b1c 6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents: 5847
diff changeset
   385
                String base = name.substring(periodIndex);
218670cd9b1c 6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents: 5847
diff changeset
   386
                String extn = (isSourceFile ? ".java" : ".class");
218670cd9b1c 6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents: 5847
diff changeset
   387
                if (base.equals(extn))
218670cd9b1c 6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents: 5847
diff changeset
   388
                    log.warning("proc.suspicious.class.name", name, extn);
218670cd9b1c 6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents: 5847
diff changeset
   389
            }
218670cd9b1c 6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents: 5847
diff changeset
   390
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
        checkNameAndExistence(name, isSourceFile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
        Location loc = (isSourceFile ? SOURCE_OUTPUT : CLASS_OUTPUT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
        JavaFileObject.Kind kind = (isSourceFile ?
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
                                    JavaFileObject.Kind.SOURCE :
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
                                    JavaFileObject.Kind.CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        JavaFileObject fileObject =
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
            fileManager.getJavaFileForOutput(loc, name, kind, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
        checkFileReopening(fileObject, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
        if (lastRound)
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
            log.warning("proc.file.create.last.round", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
        if (isSourceFile)
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
            aggregateGeneratedSourceNames.add(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
            aggregateGeneratedClassNames.add(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
        openTypeNames.add(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
        return new FilerOutputJavaFileObject(name, fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
    public FileObject createResource(JavaFileManager.Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
                                     CharSequence pkg,
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
                                     CharSequence relativeName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
                                     Element... originatingElements) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
        locationCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
        String strPkg = pkg.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
        if (strPkg.length() > 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
            checkName(strPkg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
        FileObject fileObject =
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
            fileManager.getFileForOutput(location, strPkg,
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
                                         relativeName.toString(), null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
        checkFileReopening(fileObject, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
        if (fileObject instanceof JavaFileObject)
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
            return new FilerOutputJavaFileObject(null, (JavaFileObject)fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
            return new FilerOutputFileObject(null, fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
    private void locationCheck(JavaFileManager.Location location) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
        if (location instanceof StandardLocation) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
            StandardLocation stdLoc = (StandardLocation) location;
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
            if (!stdLoc.isOutputLocation())
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
                throw new IllegalArgumentException("Resource creation not supported in location " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
                                                   stdLoc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
    public FileObject getResource(JavaFileManager.Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
                                  CharSequence pkg,
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
                                  CharSequence relativeName) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
        String strPkg = pkg.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
        if (strPkg.length() > 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
            checkName(strPkg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
        // TODO: Only support reading resources in selected output
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
        // locations?  Only allow reading of non-source, non-class
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
        // files from the supported input locations?
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
        FileObject fileObject = fileManager.getFileForOutput(location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
                                                             pkg.toString(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
                                                             relativeName.toString(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
                                                             null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
        // If the path was already opened for writing, throw an exception.
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
        checkFileReopening(fileObject, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
        return new FilerInputFileObject(fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
    private void checkName(String name) throws FilerException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
        checkName(name, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
    private void checkName(String name, boolean allowUnnamedPackageInfo) throws FilerException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
        if (!SourceVersion.isName(name) && !isPackageInfo(name, allowUnnamedPackageInfo)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
            if (lint)
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
                log.warning("proc.illegal.file.name", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
            throw new FilerException("Illegal name " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
    private boolean isPackageInfo(String name, boolean allowUnnamedPackageInfo) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
        // Is the name of the form "package-info" or
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
        // "foo.bar.package-info"?
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
        final String PKG_INFO = "package-info";
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
        int periodIndex = name.lastIndexOf(".");
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
        if (periodIndex == -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
            return allowUnnamedPackageInfo ? name.equals(PKG_INFO) : false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
            // "foo.bar.package-info." illegal
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
            String prefix = name.substring(0, periodIndex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
            String simple = name.substring(periodIndex+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
            return SourceVersion.isName(prefix) && simple.equals(PKG_INFO);
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
    private void checkNameAndExistence(String typename, boolean allowUnnamedPackageInfo) throws FilerException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
        // TODO: Check if type already exists on source or class path?
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
        // If so, use warning message key proc.type.already.exists
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
        checkName(typename, allowUnnamedPackageInfo);
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
        if (aggregateGeneratedSourceNames.contains(typename) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
            aggregateGeneratedClassNames.contains(typename)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
            if (lint)
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
                log.warning("proc.type.recreate", typename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
            throw new FilerException("Attempt to recreate a file for type " + typename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
     * Check to see if the file has already been opened; if so, throw
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
     * an exception, otherwise add it to the set of files.
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
    private void checkFileReopening(FileObject fileObject, boolean addToHistory) throws FilerException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
        for(FileObject veteran : fileObjectHistory) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
            if (fileManager.isSameFile(veteran, fileObject)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
                if (lint)
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
                    log.warning("proc.file.reopening", fileObject.getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
                throw new FilerException("Attempt to reopen a file for path " + fileObject.getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
        if (addToHistory)
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
            fileObjectHistory.add(fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
    public boolean newFiles() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
        return (!generatedSourceNames.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
            || (!generatedClasses.isEmpty());
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
    public Set<String> getGeneratedSourceNames() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
        return generatedSourceNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
    public Set<JavaFileObject> getGeneratedSourceFileObjects() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
        return generatedSourceFileObjects;
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
    public Map<String, JavaFileObject> getGeneratedClasses() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
        return generatedClasses;
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
    public void warnIfUnclosedFiles() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
        if (!openTypeNames.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
            log.warning("proc.unclosed.type.files", openTypeNames.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
     * Update internal state for a new round.
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
     */
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6157
diff changeset
   542
    public void newRound(Context context) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
        this.context = context;
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
        this.log = Log.instance(context);
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6157
diff changeset
   545
        clearRoundState();
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6157
diff changeset
   546
    }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6157
diff changeset
   547
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6157
diff changeset
   548
    void setLastRound(boolean lastRound) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
        this.lastRound = lastRound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
    public void close() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
        clearRoundState();
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
        // Cross-round state
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
        fileObjectHistory.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
        openTypeNames.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
        aggregateGeneratedSourceNames.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
        aggregateGeneratedClassNames.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
    private void clearRoundState() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
        generatedSourceNames.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
        generatedSourceFileObjects.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
        generatedClasses.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
     * Debugging function to display internal state.
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
    public void displayState() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
        PrintWriter xout = context.get(Log.outKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
        xout.println("File Object History : " +  fileObjectHistory);
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
        xout.println("Open Type Names     : " +  openTypeNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
        xout.println("Gen. Src Names      : " +  generatedSourceNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
        xout.println("Gen. Cls Names      : " +  generatedClasses.keySet());
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
        xout.println("Agg. Gen. Src Names : " +  aggregateGeneratedSourceNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
        xout.println("Agg. Gen. Cls Names : " +  aggregateGeneratedClassNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
        return "javac Filer";
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
     * Upon close, register files opened by create{Source, Class}File
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
     * for annotation processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
    private void closeFileObject(String typeName, FileObject fileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
        /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
         * If typeName is non-null, the file object was opened as a
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
         * source or class file by the user.  If a file was opened as
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
         * a resource, typeName will be null and the file is *not*
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
         * subject to annotation processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
        if ((typeName != null)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
            if (!(fileObject instanceof JavaFileObject))
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
                throw new AssertionError("JavaFileOject not found for " + fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
            JavaFileObject javaFileObject = (JavaFileObject)fileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
            switch(javaFileObject.getKind()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
            case SOURCE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
                generatedSourceNames.add(typeName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
                generatedSourceFileObjects.add(javaFileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
                openTypeNames.remove(typeName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
            case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
                generatedClasses.put(typeName, javaFileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
                openTypeNames.remove(typeName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
}