src/java.compiler/share/classes/javax/tools/SimpleJavaFileObject.java
author naoto
Fri, 16 Feb 2018 14:03:34 -0800
changeset 48910 67cdc215ed70
parent 47216 71c04702a3d5
permissions -rw-r--r--
8198228: Spec clarification: j.u.Locale.getDisplayName() Reviewed-by: rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3995
diff changeset
     2
 * Copyright (c) 2005, 2006, 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: 3995
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: 3995
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: 3995
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3995
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3995
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 javax.tools;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.net.URI;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.nio.CharBuffer;
29291
076c277565f7 8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents: 25874
diff changeset
    31
import java.util.Objects;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import javax.lang.model.element.Modifier;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import javax.lang.model.element.NestingKind;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.tools.JavaFileObject.Kind;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * Provides simple implementations for most methods in JavaFileObject.
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * This class is designed to be subclassed and used as a basis for
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * JavaFileObject implementations.  Subclasses can override the
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * implementation and specification of any method of this class as
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * long as the general contract of JavaFileObject is obeyed.
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * @author Peter von der Ahé
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * @since 1.6
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
public class SimpleJavaFileObject implements JavaFileObject {
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
     * A URI for this file object.
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    protected final URI uri;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
     * The kind of this file object.
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    protected final Kind kind;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
     * Construct a SimpleJavaFileObject of the given kind and with the
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     * given URI.
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     * @param uri  the URI for this file object
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     * @param kind the kind of this file object
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    protected SimpleJavaFileObject(URI uri, Kind kind) {
29291
076c277565f7 8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents: 25874
diff changeset
    65
        Objects.requireNonNull(uri);
076c277565f7 8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents: 25874
diff changeset
    66
        Objects.requireNonNull(kind);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        if (uri.getPath() == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
            throw new IllegalArgumentException("URI must have a path: " + uri);
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        this.uri = uri;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        this.kind = kind;
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    public URI toUri() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        return uri;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    public String getName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        return toUri().getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     * This implementation always throws {@linkplain
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     * UnsupportedOperationException}.  Subclasses can change this
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     * behavior as long as the contract of {@link FileObject} is
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
     * obeyed.
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    public InputStream openInputStream() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     * This implementation always throws {@linkplain
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     * UnsupportedOperationException}.  Subclasses can change this
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     * behavior as long as the contract of {@link FileObject} is
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     * obeyed.
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    public OutputStream openOutputStream() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     * Wraps the result of {@linkplain #getCharContent} in a Reader.
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     * Subclasses can change this behavior as long as the contract of
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     * {@link FileObject} is obeyed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
     * @param  ignoreEncodingErrors {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     * @return a Reader wrapping the result of getCharContent
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * @throws IllegalStateException {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * @throws UnsupportedOperationException {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     * @throws IOException {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        CharSequence charContent = getCharContent(ignoreEncodingErrors);
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        if (charContent == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        if (charContent instanceof CharBuffer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
            CharBuffer buffer = (CharBuffer)charContent;
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            if (buffer.hasArray())
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
                return new CharArrayReader(buffer.array());
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        return new StringReader(charContent.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     * This implementation always throws {@linkplain
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
     * UnsupportedOperationException}.  Subclasses can change this
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     * behavior as long as the contract of {@link FileObject} is
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     * obeyed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     * Wraps the result of openOutputStream in a Writer.  Subclasses
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     * can change this behavior as long as the contract of {@link
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     * FileObject} is obeyed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     * @return a Writer wrapping the result of openOutputStream
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     * @throws IllegalStateException {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     * @throws UnsupportedOperationException {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     * @throws IOException {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    public Writer openWriter() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        return new OutputStreamWriter(openOutputStream());
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     * This implementation returns {@code 0L}.  Subclasses can change
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
     * this behavior as long as the contract of {@link FileObject} is
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
     * obeyed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
     * @return {@code 0L}
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    public long getLastModified() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        return 0L;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
     * This implementation does nothing.  Subclasses can change this
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     * behavior as long as the contract of {@link FileObject} is
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     * obeyed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     * @return {@code false}
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    public boolean delete() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
     * @return {@code this.kind}
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    public Kind getKind() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        return kind;
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
     * This implementation compares the path of its URI to the given
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     * simple name.  This method returns true if the given kind is
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
     * equal to the kind of this object, and if the path is equal to
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
     * {@code simpleName + kind.extension} or if it ends with {@code
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     * "/" + simpleName + kind.extension}.
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
     * <p>This method calls {@link #getKind} and {@link #toUri} and
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
     * does not access the fields {@link #uri} and {@link #kind}
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
     * directly.
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
     * <p>Subclasses can change this behavior as long as the contract
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
     * of {@link JavaFileObject} is obeyed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    public boolean isNameCompatible(String simpleName, Kind kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        String baseName = simpleName + kind.extension;
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        return kind.equals(getKind())
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
            && (baseName.equals(toUri().getPath())
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                || toUri().getPath().endsWith("/" + baseName));
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
     * This implementation returns {@code null}.  Subclasses can
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
     * change this behavior as long as the contract of
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
     * {@link JavaFileObject} is obeyed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
    public NestingKind getNestingKind() { return null; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
     * This implementation returns {@code null}.  Subclasses can
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
     * change this behavior as long as the contract of
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
     * {@link JavaFileObject} is obeyed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    public Modifier getAccessLevel()  { return null; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        return getClass().getName() + "[" + toUri() + "]";
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
}