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