langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacFiler.java
author jlahoda
Tue, 13 Dec 2016 10:49:28 +0100
changeset 42815 050370edaade
parent 40232 4995ab1a4558
child 43368 cabe410a7a5c
permissions -rw-r--r--
8072988: Update javax.annotation.processing for modules Summary: Support for generating files in multi-module mode. Reviewed-by: darcy, jjg Contributed-by: joe.darcy@oracle.com, jan.lahoda@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 26266
diff changeset
     2
 * Copyright (c) 2005, 2016, 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;
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
    54
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
    55
import com.sun.tools.javac.code.Symtab;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
    56
import com.sun.tools.javac.comp.Modules;
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    57
import com.sun.tools.javac.util.*;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    58
import com.sun.tools.javac.util.DefinedBy.Api;
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    59
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    60
import static com.sun.tools.javac.code.Lint.LintCategory.PROCESSING;
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
    61
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 * The FilerImplementation class must maintain a number of
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 * constraints.  First, multiple attempts to open the same path within
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 * the same invocation of the tool results in an IOException being
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 * thrown.  For example, trying to open the same source file twice:
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
 * createSourceFile("foo.Bar")
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
 * is disallowed as is opening a text file that happens to have
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
 * the same name as a source file:
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
 * <pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
 * createSourceFile("foo.Bar")
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
 * ...
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
 * createTextFile(SOURCE_TREE, "foo", new File("Bar"), null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
 * </pre>
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
 * <p>Additionally, creating a source file that corresponds to an
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
 * already created class file (or vice versa) also results in an
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
 * IOException since each type can only be created once.  However, if
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
 * the Filer is used to create a text file named *.java that happens
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
 * to correspond to an existing class file, a warning is *not*
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
 * generated.  Similarly, a warning is not generated for a binary file
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
 * named *.class and an existing source file.
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
 * <p>The reason for this difference is that source files and class
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
 * files are registered with the tool and can get passed on as
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
 * declarations to the next round of processing.  Files that are just
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
 * named *.java and *.class are not processed in that manner; although
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
 * having extra source files and class files on the source path and
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
 * class path can alter the behavior of the tool and any final
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
 * compile.
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    99
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
 * If you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
 * This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
 * deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
public class JavacFiler implements Filer, Closeable {
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    // TODO: Implement different transaction model for updating the
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    // Filer's record keeping on file close.
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    private static final String ALREADY_OPENED =
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        "Output stream or writer has already been opened.";
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    private static final String NOT_FOR_READING =
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        "FileObject was not opened for reading.";
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    private static final String NOT_FOR_WRITING =
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        "FileObject was not opened for writing.";
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     * Wrap a JavaFileObject to manage writing by the Filer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    private class FilerOutputFileObject extends ForwardingFileObject<FileObject> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        private boolean opened = false;
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   120
        private ModuleSymbol mod;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        private String name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   123
        FilerOutputFileObject(ModuleSymbol mod, String name, FileObject fileObject) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
            super(fileObject);
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   125
            this.mod = mod;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
            this.name = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   129
        @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        public synchronized OutputStream openOutputStream() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            if (opened)
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
                throw new IOException(ALREADY_OPENED);
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
            opened = true;
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   134
            return new FilerOutputStream(mod, name, fileObject);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   137
        @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        public synchronized Writer openWriter() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            if (opened)
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                throw new IOException(ALREADY_OPENED);
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
            opened = true;
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   142
            return new FilerWriter(mod, name, fileObject);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        // Three anti-literacy methods
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   146
        @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        public InputStream openInputStream() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
            throw new IllegalStateException(NOT_FOR_READING);
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   151
        @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            throw new IllegalStateException(NOT_FOR_READING);
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   156
        @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
            throw new IllegalStateException(NOT_FOR_READING);
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   161
        @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        public boolean delete() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
    private class FilerOutputJavaFileObject extends FilerOutputFileObject implements JavaFileObject {
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        private final JavaFileObject javaFileObject;
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   169
        FilerOutputJavaFileObject(ModuleSymbol mod, String name, JavaFileObject javaFileObject) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   170
            super(mod, name, javaFileObject);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
            this.javaFileObject = javaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   174
        @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        public JavaFileObject.Kind getKind() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
            return javaFileObject.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   179
        @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        public boolean isNameCompatible(String simpleName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
                                        JavaFileObject.Kind kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            return javaFileObject.isNameCompatible(simpleName, kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   185
        @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        public NestingKind getNestingKind() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
            return javaFileObject.getNestingKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   190
        @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        public Modifier getAccessLevel() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
            return javaFileObject.getAccessLevel();
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
     * Wrap a JavaFileObject to manage reading by the Filer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    private class FilerInputFileObject extends ForwardingFileObject<FileObject> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        FilerInputFileObject(FileObject fileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
            super(fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   204
        @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        public OutputStream openOutputStream() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            throw new IllegalStateException(NOT_FOR_WRITING);
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   209
        @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        public Writer openWriter() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
            throw new IllegalStateException(NOT_FOR_WRITING);
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   214
        @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        public boolean delete() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
    private class FilerInputJavaFileObject extends FilerInputFileObject implements JavaFileObject {
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        private final JavaFileObject javaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        FilerInputJavaFileObject(JavaFileObject javaFileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
            super(javaFileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
            this.javaFileObject = javaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   227
        @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        public JavaFileObject.Kind getKind() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            return javaFileObject.getKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   232
        @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        public boolean isNameCompatible(String simpleName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                                        JavaFileObject.Kind kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
            return javaFileObject.isNameCompatible(simpleName, kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   238
        @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        public NestingKind getNestingKind() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
            return javaFileObject.getNestingKind();
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   243
        @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        public Modifier getAccessLevel() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
            return javaFileObject.getAccessLevel();
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
     * Wrap a {@code OutputStream} returned from the {@code
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
     * JavaFileManager} to properly register source or class files
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
     * when they are closed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
    private class FilerOutputStream extends FilterOutputStream {
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   256
        ModuleSymbol mod;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
        String typeName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
        FileObject fileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        boolean closed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
         * @param typeName name of class or {@code null} if just a
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
         * binary file
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
         */
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   265
        FilerOutputStream(ModuleSymbol mod, String typeName, FileObject fileObject) throws IOException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
            super(fileObject.openOutputStream());
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   267
            this.mod = mod;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
            this.typeName = typeName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
            this.fileObject = fileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        public synchronized void close() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
            if (!closed) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                closed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                 * If an IOException occurs when closing the underlying
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                 * stream, still try to process the file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   280
                closeFileObject(mod, typeName, fileObject);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                out.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
     * Wrap a {@code Writer} returned from the {@code JavaFileManager}
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
     * to properly register source or class files when they are
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     * closed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
    private class FilerWriter extends FilterWriter {
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   292
        ModuleSymbol mod;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
        String typeName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        FileObject fileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
        boolean closed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
         * @param fileObject the fileObject to be written to
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
         * @param typeName name of source file or {@code null} if just a
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
         * text file
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
         */
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   302
        FilerWriter(ModuleSymbol mod, String typeName, FileObject fileObject) throws IOException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            super(fileObject.openWriter());
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   304
            this.mod = mod;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
            this.typeName = typeName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
            this.fileObject = fileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        public synchronized void close() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
            if (!closed) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                closed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                 * If an IOException occurs when closing the underlying
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                 * Writer, still try to process the file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   317
                closeFileObject(mod, typeName, fileObject);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                out.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
    JavaFileManager fileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
    Log log;
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   325
    Modules modules;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   326
    Names names;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   327
    Symtab syms;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
    Context context;
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
    boolean lastRound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
    private final boolean lint;
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
     * Logical names of all created files.  This set must be
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
     * synchronized.
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
    private final Set<FileObject> fileObjectHistory;
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
     * Names of types that have had files created but not closed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
    private final Set<String> openTypeNames;
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 source files closed in this round.  This set must be
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
     * synchronized.  Its iterators should preserve insertion order.
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
    private Set<String> generatedSourceNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
     * Names and class files of the class files closed in this round.
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
     * This set must be synchronized.  Its iterators should preserve
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
     * insertion order.
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
     */
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   355
    private final Map<ModuleSymbol, Map<String, JavaFileObject>> generatedClasses;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
     * JavaFileObjects for source files closed in this round.  This
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
     * set must be synchronized.  Its iterators should preserve
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
     * insertion order.
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
    private Set<JavaFileObject> generatedSourceFileObjects;
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
     * Names of all created source files.  Its iterators should
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
     * preserve insertion order.
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
     */
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   368
    private final Set<Pair<ModuleSymbol, String>> aggregateGeneratedSourceNames;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
     * Names of all created class files.  Its iterators should
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
     * preserve insertion order.
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
     */
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   374
    private final Set<Pair<ModuleSymbol, String>> aggregateGeneratedClassNames;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
    JavacFiler(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
        this.context = context;
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
        fileManager = context.get(JavaFileManager.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
        log = Log.instance(context);
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   382
        modules = Modules.instance(context);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   383
        names = Names.instance(context);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   384
        syms = Symtab.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
22165
ec53c8946fc2 8030807: langtools should still build using jdk 7
vromero
parents: 22163
diff changeset
   386
        fileObjectHistory = synchronizedSet(new LinkedHashSet<FileObject>());
ec53c8946fc2 8030807: langtools should still build using jdk 7
vromero
parents: 22163
diff changeset
   387
        generatedSourceNames = synchronizedSet(new LinkedHashSet<String>());
ec53c8946fc2 8030807: langtools should still build using jdk 7
vromero
parents: 22163
diff changeset
   388
        generatedSourceFileObjects = synchronizedSet(new LinkedHashSet<JavaFileObject>());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   390
        generatedClasses = synchronizedMap(new LinkedHashMap<>());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
22165
ec53c8946fc2 8030807: langtools should still build using jdk 7
vromero
parents: 22163
diff changeset
   392
        openTypeNames  = synchronizedSet(new LinkedHashSet<String>());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 10633
diff changeset
   394
        aggregateGeneratedSourceNames = new LinkedHashSet<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 10633
diff changeset
   395
        aggregateGeneratedClassNames  = new LinkedHashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
6721
d92073844278 6988436: Cleanup javac option handling
jjg
parents: 6573
diff changeset
   397
        lint = (Lint.instance(context)).isEnabled(PROCESSING);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   400
    @Override @DefinedBy(Api.ANNOTATION_PROCESSING)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   401
    public JavaFileObject createSourceFile(CharSequence nameAndModule,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
                                           Element... originatingElements) throws IOException {
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   403
        Pair<ModuleSymbol, String> moduleAndClass = checkOrInferModule(nameAndModule);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   404
        return createSourceOrClassFile(moduleAndClass.fst, true, moduleAndClass.snd);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   405
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   406
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   407
    @Override @DefinedBy(Api.ANNOTATION_PROCESSING)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   408
    public JavaFileObject createClassFile(CharSequence nameAndModule,
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   409
                                          Element... originatingElements) throws IOException {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   410
        Pair<ModuleSymbol, String> moduleAndClass = checkOrInferModule(nameAndModule);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   411
        return createSourceOrClassFile(moduleAndClass.fst, false, moduleAndClass.snd);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   414
    private Pair<ModuleSymbol, String> checkOrInferModule(CharSequence moduleAndPkg) throws FilerException {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   415
        String moduleAndPkgString = moduleAndPkg.toString();
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   416
        int slash = moduleAndPkgString.indexOf('/');
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   417
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   418
        if (slash != (-1)) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   419
            //module name specified:
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   420
            String module = moduleAndPkgString.substring(0, slash);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   421
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   422
            ModuleSymbol explicitModule = syms.getModule(names.fromString(module));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   423
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   424
            if (explicitModule == null) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   425
                throw new FilerException("Module: " + module + " does not exist.");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   426
            }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   427
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   428
            if (!modules.isRootModule(explicitModule)) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   429
                throw new FilerException("Cannot write to the given module!");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   430
            }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   431
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   432
            return Pair.of(explicitModule, moduleAndPkgString.substring(slash + 1));
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   433
        } else {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   434
            if (modules.multiModuleMode) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   435
                throw new FilerException("No module to write to specified!");
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   436
            }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   437
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   438
            return Pair.of(modules.getDefaultModule(), moduleAndPkgString);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   439
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   442
    private JavaFileObject createSourceOrClassFile(ModuleSymbol mod, boolean isSourceFile, String name) throws IOException {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   443
        Assert.checkNonNull(mod);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   444
6157
218670cd9b1c 6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents: 5847
diff changeset
   445
        if (lint) {
218670cd9b1c 6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents: 5847
diff changeset
   446
            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
   447
            if (periodIndex != -1) {
218670cd9b1c 6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents: 5847
diff changeset
   448
                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
   449
                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
   450
                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
   451
                    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
   452
            }
218670cd9b1c 6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents: 5847
diff changeset
   453
        }
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   454
        checkNameAndExistence(mod, name, isSourceFile);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
        Location loc = (isSourceFile ? SOURCE_OUTPUT : CLASS_OUTPUT);
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   456
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   457
        if (modules.multiModuleMode) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   458
            loc = this.fileManager.getLocationForModule(loc, mod.name.toString());
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   459
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
        JavaFileObject.Kind kind = (isSourceFile ?
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
                                    JavaFileObject.Kind.SOURCE :
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
                                    JavaFileObject.Kind.CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
        JavaFileObject fileObject =
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
            fileManager.getJavaFileForOutput(loc, name, kind, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
        checkFileReopening(fileObject, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
        if (lastRound)
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
            log.warning("proc.file.create.last.round", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
        if (isSourceFile)
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   472
            aggregateGeneratedSourceNames.add(Pair.of(mod, name));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
        else
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   474
            aggregateGeneratedClassNames.add(Pair.of(mod, name));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
        openTypeNames.add(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   477
        return new FilerOutputJavaFileObject(mod, name, fileObject);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   480
    @Override @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
    public FileObject createResource(JavaFileManager.Location location,
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   482
                                     CharSequence moduleAndPkg,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
                                     CharSequence relativeName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
                                     Element... originatingElements) throws IOException {
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   485
        Pair<ModuleSymbol, String> moduleAndPackage = checkOrInferModule(moduleAndPkg);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   486
        ModuleSymbol msym = moduleAndPackage.fst;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   487
        String pkg = moduleAndPackage.snd;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   488
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
        locationCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   491
        if (modules.multiModuleMode) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   492
            Assert.checkNonNull(msym);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   493
            location = this.fileManager.getLocationForModule(location, msym.name.toString());
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   494
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   495
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
        String strPkg = pkg.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
        if (strPkg.length() > 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
            checkName(strPkg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
        FileObject fileObject =
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
            fileManager.getFileForOutput(location, strPkg,
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
                                         relativeName.toString(), null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
        checkFileReopening(fileObject, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
        if (fileObject instanceof JavaFileObject)
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   506
            return new FilerOutputJavaFileObject(msym, null, (JavaFileObject)fileObject);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
        else
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   508
            return new FilerOutputFileObject(msym, null, fileObject);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
    private void locationCheck(JavaFileManager.Location location) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
        if (location instanceof StandardLocation) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
            StandardLocation stdLoc = (StandardLocation) location;
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
            if (!stdLoc.isOutputLocation())
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
                throw new IllegalArgumentException("Resource creation not supported in location " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
                                                   stdLoc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   520
    @Override @DefinedBy(Api.ANNOTATION_PROCESSING)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
    public FileObject getResource(JavaFileManager.Location location,
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   522
                                  CharSequence moduleAndPkg,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
                                  CharSequence relativeName) throws IOException {
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   524
        Pair<ModuleSymbol, String> moduleAndPackage = checkOrInferModule(moduleAndPkg);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   525
        ModuleSymbol msym = moduleAndPackage.fst;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   526
        String pkg = moduleAndPackage.snd;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   527
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   528
        if (modules.multiModuleMode) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   529
            Assert.checkNonNull(msym);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   530
            location = this.fileManager.getLocationForModule(location, msym.name.toString());
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   531
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   532
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   533
        if (pkg.length() > 0)
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   534
            checkName(pkg);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
        // TODO: Only support reading resources in selected output
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
        // locations?  Only allow reading of non-source, non-class
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
        // 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
   539
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   540
        // 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
   541
        // 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
   542
        // 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
   543
        // 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
   544
        // 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
   545
        // invocation.
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   546
        FileObject fileObject;
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   547
        if (location.isOutputLocation()) {
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   548
            fileObject = fileManager.getFileForOutput(location,
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   549
                    pkg,
10633
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   550
                    relativeName.toString(),
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   551
                    null);
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   552
        } else {
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   553
            fileObject = fileManager.getFileForInput(location,
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   554
                    pkg,
6573
6bad8be140fc 6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents: 6159
diff changeset
   555
                    relativeName.toString());
10633
5dd595ab058e 7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents: 7681
diff changeset
   556
        }
6573
6bad8be140fc 6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents: 6159
diff changeset
   557
        if (fileObject == null) {
6bad8be140fc 6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents: 6159
diff changeset
   558
            String name = (pkg.length() == 0)
6bad8be140fc 6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents: 6159
diff changeset
   559
                    ? relativeName.toString() : (pkg + "/" + relativeName);
6bad8be140fc 6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents: 6159
diff changeset
   560
            throw new FileNotFoundException(name);
6bad8be140fc 6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents: 6159
diff changeset
   561
        }
6bad8be140fc 6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents: 6159
diff changeset
   562
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
        // If the path was already opened for writing, throw an exception.
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
        checkFileReopening(fileObject, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
        return new FilerInputFileObject(fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
    private void checkName(String name) throws FilerException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
        checkName(name, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
    private void checkName(String name, boolean allowUnnamedPackageInfo) throws FilerException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
        if (!SourceVersion.isName(name) && !isPackageInfo(name, allowUnnamedPackageInfo)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
            if (lint)
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
                log.warning("proc.illegal.file.name", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
            throw new FilerException("Illegal name " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
    private boolean isPackageInfo(String name, boolean allowUnnamedPackageInfo) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
        // Is the name of the form "package-info" or
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
        // "foo.bar.package-info"?
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
        final String PKG_INFO = "package-info";
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
        int periodIndex = name.lastIndexOf(".");
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
        if (periodIndex == -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
            return allowUnnamedPackageInfo ? name.equals(PKG_INFO) : false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
            // "foo.bar.package-info." illegal
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
            String prefix = name.substring(0, periodIndex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
            String simple = name.substring(periodIndex+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
            return SourceVersion.isName(prefix) && simple.equals(PKG_INFO);
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   595
    private void checkNameAndExistence(ModuleSymbol mod, String typename, boolean allowUnnamedPackageInfo) throws FilerException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
        // TODO: Check if type already exists on source or class path?
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
        // If so, use warning message key proc.type.already.exists
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
        checkName(typename, allowUnnamedPackageInfo);
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   599
        if (aggregateGeneratedSourceNames.contains(Pair.of(mod, typename)) ||
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   600
            aggregateGeneratedClassNames.contains(Pair.of(mod, typename))) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
            if (lint)
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
                log.warning("proc.type.recreate", typename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
            throw new FilerException("Attempt to recreate a file for type " + typename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
        }
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   605
        if (!mod.isUnnamed() && !typename.contains(".")) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   606
            throw new FilerException("Attempt to create a type in unnamed package of a named module: " + typename);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   607
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
     * Check to see if the file has already been opened; if so, throw
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
     * an exception, otherwise add it to the set of files.
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
    private void checkFileReopening(FileObject fileObject, boolean addToHistory) throws FilerException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
        for(FileObject veteran : fileObjectHistory) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
            if (fileManager.isSameFile(veteran, fileObject)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
                if (lint)
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
                    log.warning("proc.file.reopening", fileObject.getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
                throw new FilerException("Attempt to reopen a file for path " + fileObject.getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
        if (addToHistory)
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
            fileObjectHistory.add(fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
    public boolean newFiles() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
        return (!generatedSourceNames.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
            || (!generatedClasses.isEmpty());
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
    public Set<String> getGeneratedSourceNames() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
        return generatedSourceNames;
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
    public Set<JavaFileObject> getGeneratedSourceFileObjects() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
        return generatedSourceFileObjects;
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   639
    public Map<ModuleSymbol, Map<String, JavaFileObject>> getGeneratedClasses() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
        return generatedClasses;
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
    public void warnIfUnclosedFiles() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
        if (!openTypeNames.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
            log.warning("proc.unclosed.type.files", openTypeNames.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
     * Update internal state for a new round.
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
     */
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22165
diff changeset
   651
    public void newRound() {
6159
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6157
diff changeset
   652
        clearRoundState();
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6157
diff changeset
   653
    }
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6157
diff changeset
   654
88930a1c409e 6966604: JavacFiler not correctly notified of lastRound
jjg
parents: 6157
diff changeset
   655
    void setLastRound(boolean lastRound) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
        this.lastRound = lastRound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
    public void close() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
        clearRoundState();
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
        // Cross-round state
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
        fileObjectHistory.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
        openTypeNames.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
        aggregateGeneratedSourceNames.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
        aggregateGeneratedClassNames.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
    private void clearRoundState() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
        generatedSourceNames.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
        generatedSourceFileObjects.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
        generatedClasses.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
     * Debugging function to display internal state.
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
    public void displayState() {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 26266
diff changeset
   678
        PrintWriter xout = context.get(Log.logKey).getWriter(Log.WriterKind.STDERR);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
        xout.println("File Object History : " +  fileObjectHistory);
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
        xout.println("Open Type Names     : " +  openTypeNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
        xout.println("Gen. Src Names      : " +  generatedSourceNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
        xout.println("Gen. Cls Names      : " +  generatedClasses.keySet());
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
        xout.println("Agg. Gen. Src Names : " +  aggregateGeneratedSourceNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
        xout.println("Agg. Gen. Cls Names : " +  aggregateGeneratedClassNames);
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
        return "javac Filer";
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
     * Upon close, register files opened by create{Source, Class}File
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
     * for annotation processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
     */
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   695
    private void closeFileObject(ModuleSymbol mod, String typeName, FileObject fileObject) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
        /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
         * If typeName is non-null, the file object was opened as a
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
         * source or class file by the user.  If a file was opened as
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
         * a resource, typeName will be null and the file is *not*
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
         * subject to annotation processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
        if ((typeName != null)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
            if (!(fileObject instanceof JavaFileObject))
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
                throw new AssertionError("JavaFileOject not found for " + fileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
            JavaFileObject javaFileObject = (JavaFileObject)fileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
            switch(javaFileObject.getKind()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
            case SOURCE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
                generatedSourceNames.add(typeName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
                generatedSourceFileObjects.add(javaFileObject);
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
                openTypeNames.remove(typeName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
            case CLASS:
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 40232
diff changeset
   714
                generatedClasses.computeIfAbsent(mod, m -> Collections.synchronizedMap(new LinkedHashMap<>())).put(typeName, javaFileObject);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
                openTypeNames.remove(typeName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
}