langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacFiler.java
author chegar
Sun, 17 Aug 2014 15:52:32 +0100
changeset 25874 83c19f00452c
parent 23810 langtools/src/share/classes/com/sun/tools/javac/processing/JavacFiler.java@b92eb80925f0
child 26266 2d24bda701dc
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22165
diff changeset
     2
 * Copyright (c) 2005, 2014, 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 java.io.Closeable;
6573
6bad8be140fc 6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents: 6159
diff changeset
    29
import java.io.FileNotFoundException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.io.FilterOutputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.io.Reader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.io.Writer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.io.FilterWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.io.PrintWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    38
import java.util.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import static java.util.Collections.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    42
import javax.annotation.processing.*;
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    43
import javax.lang.model.SourceVersion;
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    44
import javax.lang.model.element.NestingKind;
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    45
import javax.lang.model.element.Modifier;
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    46
import javax.lang.model.element.Element;
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    47
import javax.tools.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import javax.tools.JavaFileManager.Location;
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    49
10
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
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    53
import com.sun.tools.javac.code.Lint;
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    54
import com.sun.tools.javac.util.*;
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    55
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    56
import static com.sun.tools.javac.code.Lint.LintCategory.PROCESSING;
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    57
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 * The FilerImplementation class must maintain a number of
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 * constraints.  First, multiple attempts to open the same path within
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 * the same invocation of the tool results in an IOException being
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 * thrown.  For example, trying to open the same source file twice:
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 * <pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 * createSourceFile("foo.Bar")
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 * ...
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
 * createSourceFile("foo.Bar")
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
 * </pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
 * is disallowed as is opening a text file that happens to have
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
 * the same name as a source file:
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
 * <pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
 * createSourceFile("foo.Bar")
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
 * ...
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
 * createTextFile(SOURCE_TREE, "foo", new File("Bar"), null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
 * </pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
 * <p>Additionally, creating a source file that corresponds to an
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
 * already created class file (or vice versa) also results in an
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
 * IOException since each type can only be created once.  However, if
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
 * the Filer is used to create a text file named *.java that happens
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
 * to correspond to an existing class file, a warning is *not*
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
 * generated.  Similarly, a warning is not generated for a binary file
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
 * named *.class and an existing source file.
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
 * <p>The reason for this difference is that source files and class
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
 * files are registered with the tool and can get passed on as
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
 * declarations to the next round of processing.  Files that are just
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
 * named *.java and *.class are not processed in that manner; although
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
 * having extra source files and class files on the source path and
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
 * class path can alter the behavior of the tool and any final
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
 * compile.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    95
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
 * If you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
public class JavacFiler implements Filer, Closeable {
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    // TODO: Implement different transaction model for updating the
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    // Filer's record keeping on file close.
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    private static final String ALREADY_OPENED =
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        "Output stream or writer has already been opened.";
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    private static final String NOT_FOR_READING =
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        "FileObject was not opened for reading.";
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    private static final String NOT_FOR_WRITING =
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        "FileObject was not opened for writing.";
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     * Wrap a JavaFileObject to manage writing by the Filer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    private class FilerOutputFileObject extends ForwardingFileObject<FileObject> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        private boolean opened = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        private String name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        FilerOutputFileObject(String name, FileObject fileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            super(fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            this.name = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        public synchronized OutputStream openOutputStream() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            if (opened)
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                throw new IOException(ALREADY_OPENED);
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            opened = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
            return new FilerOutputStream(name, fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        public synchronized Writer openWriter() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
            if (opened)
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
                throw new IOException(ALREADY_OPENED);
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            opened = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            return new FilerWriter(name, fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        // Three anti-literacy methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        public InputStream openInputStream() 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 Reader openReader(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 CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            throw new IllegalStateException(NOT_FOR_READING);
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        public boolean delete() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    private class FilerOutputJavaFileObject extends FilerOutputFileObject implements JavaFileObject {
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        private final JavaFileObject javaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        FilerOutputJavaFileObject(String name, JavaFileObject javaFileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            super(name, javaFileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            this.javaFileObject = javaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        public JavaFileObject.Kind getKind() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            return javaFileObject.getKind();
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 boolean isNameCompatible(String simpleName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                                        JavaFileObject.Kind kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            return javaFileObject.isNameCompatible(simpleName, kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        public NestingKind getNestingKind() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
            return javaFileObject.getNestingKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        public Modifier getAccessLevel() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            return javaFileObject.getAccessLevel();
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
     * Wrap a JavaFileObject to manage reading by the Filer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    private class FilerInputFileObject extends ForwardingFileObject<FileObject> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        FilerInputFileObject(FileObject fileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            super(fileObject);
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 OutputStream openOutputStream() 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 Writer openWriter() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
            throw new IllegalStateException(NOT_FOR_WRITING);
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        public boolean delete() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    private class FilerInputJavaFileObject extends FilerInputFileObject implements JavaFileObject {
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
        private final JavaFileObject javaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        FilerInputJavaFileObject(JavaFileObject javaFileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
            super(javaFileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
            this.javaFileObject = javaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
        public JavaFileObject.Kind getKind() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
            return javaFileObject.getKind();
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 boolean isNameCompatible(String simpleName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
                                        JavaFileObject.Kind kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
            return javaFileObject.isNameCompatible(simpleName, kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        public NestingKind getNestingKind() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            return javaFileObject.getNestingKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        public Modifier getAccessLevel() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
            return javaFileObject.getAccessLevel();
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
     * Wrap a {@code OutputStream} returned from the {@code
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
     * JavaFileManager} to properly register source or class files
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
     * when they are closed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
    private class FilerOutputStream extends FilterOutputStream {
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
        String typeName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
        FileObject fileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        boolean closed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
         * @param typeName name of class or {@code null} if just a
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
         * binary file
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
        FilerOutputStream(String typeName, FileObject fileObject) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
            super(fileObject.openOutputStream());
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            this.typeName = typeName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            this.fileObject = fileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        public synchronized void close() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            if (!closed) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
                closed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
                /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                 * If an IOException occurs when closing the underlying
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                 * stream, still try to process the file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
                closeFileObject(typeName, fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
                out.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
     * Wrap a {@code Writer} returned from the {@code JavaFileManager}
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
     * to properly register source or class files when they are
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
     * closed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
    private class FilerWriter extends FilterWriter {
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        String typeName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
        FileObject fileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
        boolean closed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
         * @param fileObject the fileObject to be written to
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
         * @param typeName name of source file or {@code null} if just a
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
         * text file
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        FilerWriter(String typeName, FileObject fileObject) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            super(fileObject.openWriter());
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
            this.typeName = typeName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
            this.fileObject = fileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        public synchronized void close() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            if (!closed) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                closed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                 * If an IOException occurs when closing the underlying
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                 * Writer, still try to process the file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
                 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                closeFileObject(typeName, fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                out.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
    JavaFileManager fileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
    Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
    Context context;
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
    boolean lastRound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
    private final boolean lint;
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
     * Logical names of all created files.  This set must be
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
     * synchronized.
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
    private final Set<FileObject> fileObjectHistory;
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 types that have had files created but not closed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
    private final Set<String> openTypeNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
     * Names of source files closed in this round.  This set must be
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
     * synchronized.  Its iterators should preserve insertion order.
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
    private Set<String> generatedSourceNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
     * Names and class files of the class files closed in this round.
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
     * This set must be synchronized.  Its iterators should preserve
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
     * insertion order.
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
    private final Map<String, JavaFileObject> generatedClasses;
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
     * JavaFileObjects for source files closed in this round.  This
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
     * set must be synchronized.  Its iterators should preserve
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
     * insertion order.
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
    private Set<JavaFileObject> generatedSourceFileObjects;
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
     * Names of all created source files.  Its iterators should
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
     * preserve insertion order.
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
    private final Set<String> aggregateGeneratedSourceNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
     * Names of all created class files.  Its iterators should
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
     * preserve insertion order.
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
    private final Set<String> aggregateGeneratedClassNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
    JavacFiler(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
        this.context = context;
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
        fileManager = context.get(JavaFileManager.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
22165
ec53c8946fc2 8030807: langtools should still build using jdk 7
vromero
parents: 22163
diff changeset
   362
        fileObjectHistory = synchronizedSet(new LinkedHashSet<FileObject>());
ec53c8946fc2 8030807: langtools should still build using jdk 7
vromero
parents: 22163
diff changeset
   363
        generatedSourceNames = synchronizedSet(new LinkedHashSet<String>());
ec53c8946fc2 8030807: langtools should still build using jdk 7
vromero
parents: 22163
diff changeset
   364
        generatedSourceFileObjects = synchronizedSet(new LinkedHashSet<JavaFileObject>());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
22165
ec53c8946fc2 8030807: langtools should still build using jdk 7
vromero
parents: 22163
diff changeset
   366
        generatedClasses = synchronizedMap(new LinkedHashMap<String, JavaFileObject>());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
22165
ec53c8946fc2 8030807: langtools should still build using jdk 7
vromero
parents: 22163
diff changeset
   368
        openTypeNames  = synchronizedSet(new LinkedHashSet<String>());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 10633
diff changeset
   370
        aggregateGeneratedSourceNames = new LinkedHashSet<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 10633
diff changeset
   371
        aggregateGeneratedClassNames  = new LinkedHashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
   373
        lint = (Lint.instance(context)).isEnabled(PROCESSING);
10
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 createSourceFile(CharSequence name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                                           Element... originatingElements) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
        return createSourceOrClassFile(true, 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
    public JavaFileObject createClassFile(CharSequence name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                                           Element... originatingElements) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
        return createSourceOrClassFile(false, name.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
    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
   387
        if (lint) {
218670cd9b1c 6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents: 5847
diff changeset
   388
            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
   389
            if (periodIndex != -1) {
218670cd9b1c 6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents: 5847
diff changeset
   390
                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
   391
                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
   392
                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
   393
                    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
   394
            }
218670cd9b1c 6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents: 5847
diff changeset
   395
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
        checkNameAndExistence(name, isSourceFile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        Location loc = (isSourceFile ? SOURCE_OUTPUT : CLASS_OUTPUT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
        JavaFileObject.Kind kind = (isSourceFile ?
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
                                    JavaFileObject.Kind.SOURCE :
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
                                    JavaFileObject.Kind.CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
        JavaFileObject fileObject =
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
            fileManager.getJavaFileForOutput(loc, name, kind, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
        checkFileReopening(fileObject, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
        if (lastRound)
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
            log.warning("proc.file.create.last.round", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
        if (isSourceFile)
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
            aggregateGeneratedSourceNames.add(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
            aggregateGeneratedClassNames.add(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
        openTypeNames.add(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
        return new FilerOutputJavaFileObject(name, fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
    public FileObject createResource(JavaFileManager.Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
                                     CharSequence pkg,
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
                                     CharSequence relativeName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
                                     Element... originatingElements) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
        locationCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
        String strPkg = pkg.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
        if (strPkg.length() > 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
            checkName(strPkg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
        FileObject fileObject =
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
            fileManager.getFileForOutput(location, strPkg,
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
                                         relativeName.toString(), null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
        checkFileReopening(fileObject, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
        if (fileObject instanceof JavaFileObject)
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
            return new FilerOutputJavaFileObject(null, (JavaFileObject)fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
            return new FilerOutputFileObject(null, fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
    private void locationCheck(JavaFileManager.Location location) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
        if (location instanceof StandardLocation) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
            StandardLocation stdLoc = (StandardLocation) location;
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
            if (!stdLoc.isOutputLocation())
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                throw new IllegalArgumentException("Resource creation not supported in location " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
                                                   stdLoc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
    public FileObject getResource(JavaFileManager.Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
                                  CharSequence pkg,
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
                                  CharSequence relativeName) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
        String strPkg = pkg.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
        if (strPkg.length() > 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
            checkName(strPkg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
        // TODO: Only support reading resources in selected output
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
        // locations?  Only allow reading of non-source, non-class
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
        // files from the supported input locations?
10633
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   458
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   459
        // In the following, getFileForInput is the "obvious" method
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   460
        // to use, but it does not have the "obvious" semantics for
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   461
        // SOURCE_OUTPUT and CLASS_OUTPUT. Conversely, getFileForOutput
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   462
        // does not have the correct semantics for any "path" location
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   463
        // with more than one component. So, for now, we use a hybrid
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   464
        // invocation.
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   465
        FileObject fileObject;
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   466
        if (location.isOutputLocation()) {
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   467
            fileObject = fileManager.getFileForOutput(location,
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   468
                    pkg.toString(),
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   469
                    relativeName.toString(),
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   470
                    null);
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   471
        } else {
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   472
            fileObject = fileManager.getFileForInput(location,
6573
6bad8be140fc 6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents: 6159
diff changeset
   473
                    pkg.toString(),
6bad8be140fc 6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents: 6159
diff changeset
   474
                    relativeName.toString());
10633
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   475
        }
6573
6bad8be140fc 6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents: 6159
diff changeset
   476
        if (fileObject == null) {
6bad8be140fc 6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents: 6159
diff changeset
   477
            String name = (pkg.length() == 0)
6bad8be140fc 6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents: 6159
diff changeset
   478
                    ? relativeName.toString() : (pkg + "/" + relativeName);
6bad8be140fc 6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents: 6159
diff changeset
   479
            throw new FileNotFoundException(name);
6bad8be140fc 6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents: 6159
diff changeset
   480
        }
6bad8be140fc 6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents: 6159
diff changeset
   481
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
        // If the path was already opened for writing, throw an exception.
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
        checkFileReopening(fileObject, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
        return new FilerInputFileObject(fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
    private void checkName(String name) throws FilerException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
        checkName(name, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
    private void checkName(String name, boolean allowUnnamedPackageInfo) throws FilerException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
        if (!SourceVersion.isName(name) && !isPackageInfo(name, allowUnnamedPackageInfo)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
            if (lint)
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
                log.warning("proc.illegal.file.name", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
            throw new FilerException("Illegal name " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
    private boolean isPackageInfo(String name, boolean allowUnnamedPackageInfo) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
        // Is the name of the form "package-info" or
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
        // "foo.bar.package-info"?
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
        final String PKG_INFO = "package-info";
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
        int periodIndex = name.lastIndexOf(".");
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
        if (periodIndex == -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
            return allowUnnamedPackageInfo ? name.equals(PKG_INFO) : false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
            // "foo.bar.package-info." illegal
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
            String prefix = name.substring(0, periodIndex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
            String simple = name.substring(periodIndex+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
            return SourceVersion.isName(prefix) && simple.equals(PKG_INFO);
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
    private void checkNameAndExistence(String typename, boolean allowUnnamedPackageInfo) throws FilerException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
        // TODO: Check if type already exists on source or class path?
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
        // If so, use warning message key proc.type.already.exists
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
        checkName(typename, allowUnnamedPackageInfo);
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
        if (aggregateGeneratedSourceNames.contains(typename) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
            aggregateGeneratedClassNames.contains(typename)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
            if (lint)
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
                log.warning("proc.type.recreate", typename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
            throw new FilerException("Attempt to recreate a file for type " + typename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
     * Check to see if the file has already been opened; if so, throw
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
     * an exception, otherwise add it to the set of files.
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
    private void checkFileReopening(FileObject fileObject, boolean addToHistory) throws FilerException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
        for(FileObject veteran : fileObjectHistory) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
            if (fileManager.isSameFile(veteran, fileObject)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
                if (lint)
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
                    log.warning("proc.file.reopening", fileObject.getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
                throw new FilerException("Attempt to reopen a file for path " + fileObject.getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
        if (addToHistory)
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
            fileObjectHistory.add(fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
    public boolean newFiles() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
        return (!generatedSourceNames.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
            || (!generatedClasses.isEmpty());
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
    public Set<String> getGeneratedSourceNames() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
        return generatedSourceNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
    public Set<JavaFileObject> getGeneratedSourceFileObjects() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
        return generatedSourceFileObjects;
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
    public Map<String, JavaFileObject> getGeneratedClasses() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
        return generatedClasses;
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
    public void warnIfUnclosedFiles() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
        if (!openTypeNames.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
            log.warning("proc.unclosed.type.files", openTypeNames.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
     * Update internal state for a new round.
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
     */
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22165
diff changeset
   567
    public void newRound() {
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6157
diff changeset
   568
        clearRoundState();
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6157
diff changeset
   569
    }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6157
diff changeset
   570
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6157
diff changeset
   571
    void setLastRound(boolean lastRound) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
        this.lastRound = lastRound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
    public void close() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
        clearRoundState();
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
        // Cross-round state
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
        fileObjectHistory.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
        openTypeNames.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
        aggregateGeneratedSourceNames.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
        aggregateGeneratedClassNames.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
    private void clearRoundState() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
        generatedSourceNames.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
        generatedSourceFileObjects.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
        generatedClasses.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
     * Debugging function to display internal state.
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
    public void displayState() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
        PrintWriter xout = context.get(Log.outKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
        xout.println("File Object History : " +  fileObjectHistory);
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
        xout.println("Open Type Names     : " +  openTypeNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
        xout.println("Gen. Src Names      : " +  generatedSourceNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
        xout.println("Gen. Cls Names      : " +  generatedClasses.keySet());
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
        xout.println("Agg. Gen. Src Names : " +  aggregateGeneratedSourceNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
        xout.println("Agg. Gen. Cls Names : " +  aggregateGeneratedClassNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
        return "javac Filer";
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
     * Upon close, register files opened by create{Source, Class}File
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
     * for annotation processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
    private void closeFileObject(String typeName, FileObject fileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
        /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
         * If typeName is non-null, the file object was opened as a
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
         * source or class file by the user.  If a file was opened as
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
         * a resource, typeName will be null and the file is *not*
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
         * subject to annotation processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
        if ((typeName != null)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
            if (!(fileObject instanceof JavaFileObject))
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
                throw new AssertionError("JavaFileOject not found for " + fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
            JavaFileObject javaFileObject = (JavaFileObject)fileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
            switch(javaFileObject.getKind()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
            case SOURCE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
                generatedSourceNames.add(typeName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
                generatedSourceFileObjects.add(javaFileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
                openTypeNames.remove(typeName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
            case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
                generatedClasses.put(typeName, javaFileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
                openTypeNames.remove(typeName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
}