src/share/classes/javax/tools/SimpleJavaFileObject.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 554
9d9f26857129
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package javax.tools;
aoqi@0 27
aoqi@0 28 import java.io.*;
aoqi@0 29 import java.net.URI;
aoqi@0 30 import java.nio.CharBuffer;
aoqi@0 31 import javax.lang.model.element.Modifier;
aoqi@0 32 import javax.lang.model.element.NestingKind;
aoqi@0 33 import javax.tools.JavaFileObject.Kind;
aoqi@0 34
aoqi@0 35 /**
aoqi@0 36 * Provides simple implementations for most methods in JavaFileObject.
aoqi@0 37 * This class is designed to be subclassed and used as a basis for
aoqi@0 38 * JavaFileObject implementations. Subclasses can override the
aoqi@0 39 * implementation and specification of any method of this class as
aoqi@0 40 * long as the general contract of JavaFileObject is obeyed.
aoqi@0 41 *
aoqi@0 42 * @author Peter von der Ahé
aoqi@0 43 * @since 1.6
aoqi@0 44 */
aoqi@0 45 public class SimpleJavaFileObject implements JavaFileObject {
aoqi@0 46 /**
aoqi@0 47 * A URI for this file object.
aoqi@0 48 */
aoqi@0 49 protected final URI uri;
aoqi@0 50
aoqi@0 51 /**
aoqi@0 52 * The kind of this file object.
aoqi@0 53 */
aoqi@0 54 protected final Kind kind;
aoqi@0 55
aoqi@0 56 /**
aoqi@0 57 * Construct a SimpleJavaFileObject of the given kind and with the
aoqi@0 58 * given URI.
aoqi@0 59 *
aoqi@0 60 * @param uri the URI for this file object
aoqi@0 61 * @param kind the kind of this file object
aoqi@0 62 */
aoqi@0 63 protected SimpleJavaFileObject(URI uri, Kind kind) {
aoqi@0 64 // null checks
aoqi@0 65 uri.getClass();
aoqi@0 66 kind.getClass();
aoqi@0 67 if (uri.getPath() == null)
aoqi@0 68 throw new IllegalArgumentException("URI must have a path: " + uri);
aoqi@0 69 this.uri = uri;
aoqi@0 70 this.kind = kind;
aoqi@0 71 }
aoqi@0 72
aoqi@0 73 public URI toUri() {
aoqi@0 74 return uri;
aoqi@0 75 }
aoqi@0 76
aoqi@0 77 public String getName() {
aoqi@0 78 return toUri().getPath();
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 /**
aoqi@0 82 * This implementation always throws {@linkplain
aoqi@0 83 * UnsupportedOperationException}. Subclasses can change this
aoqi@0 84 * behavior as long as the contract of {@link FileObject} is
aoqi@0 85 * obeyed.
aoqi@0 86 */
aoqi@0 87 public InputStream openInputStream() throws IOException {
aoqi@0 88 throw new UnsupportedOperationException();
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 /**
aoqi@0 92 * This implementation always throws {@linkplain
aoqi@0 93 * UnsupportedOperationException}. Subclasses can change this
aoqi@0 94 * behavior as long as the contract of {@link FileObject} is
aoqi@0 95 * obeyed.
aoqi@0 96 */
aoqi@0 97 public OutputStream openOutputStream() throws IOException {
aoqi@0 98 throw new UnsupportedOperationException();
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 /**
aoqi@0 102 * Wraps the result of {@linkplain #getCharContent} in a Reader.
aoqi@0 103 * Subclasses can change this behavior as long as the contract of
aoqi@0 104 * {@link FileObject} is obeyed.
aoqi@0 105 *
aoqi@0 106 * @param ignoreEncodingErrors {@inheritDoc}
aoqi@0 107 * @return a Reader wrapping the result of getCharContent
aoqi@0 108 * @throws IllegalStateException {@inheritDoc}
aoqi@0 109 * @throws UnsupportedOperationException {@inheritDoc}
aoqi@0 110 * @throws IOException {@inheritDoc}
aoqi@0 111 */
aoqi@0 112 public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
aoqi@0 113 CharSequence charContent = getCharContent(ignoreEncodingErrors);
aoqi@0 114 if (charContent == null)
aoqi@0 115 throw new UnsupportedOperationException();
aoqi@0 116 if (charContent instanceof CharBuffer) {
aoqi@0 117 CharBuffer buffer = (CharBuffer)charContent;
aoqi@0 118 if (buffer.hasArray())
aoqi@0 119 return new CharArrayReader(buffer.array());
aoqi@0 120 }
aoqi@0 121 return new StringReader(charContent.toString());
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 /**
aoqi@0 125 * This implementation always throws {@linkplain
aoqi@0 126 * UnsupportedOperationException}. Subclasses can change this
aoqi@0 127 * behavior as long as the contract of {@link FileObject} is
aoqi@0 128 * obeyed.
aoqi@0 129 */
aoqi@0 130 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
aoqi@0 131 throw new UnsupportedOperationException();
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 /**
aoqi@0 135 * Wraps the result of openOutputStream in a Writer. Subclasses
aoqi@0 136 * can change this behavior as long as the contract of {@link
aoqi@0 137 * FileObject} is obeyed.
aoqi@0 138 *
aoqi@0 139 * @return a Writer wrapping the result of openOutputStream
aoqi@0 140 * @throws IllegalStateException {@inheritDoc}
aoqi@0 141 * @throws UnsupportedOperationException {@inheritDoc}
aoqi@0 142 * @throws IOException {@inheritDoc}
aoqi@0 143 */
aoqi@0 144 public Writer openWriter() throws IOException {
aoqi@0 145 return new OutputStreamWriter(openOutputStream());
aoqi@0 146 }
aoqi@0 147
aoqi@0 148 /**
aoqi@0 149 * This implementation returns {@code 0L}. Subclasses can change
aoqi@0 150 * this behavior as long as the contract of {@link FileObject} is
aoqi@0 151 * obeyed.
aoqi@0 152 *
aoqi@0 153 * @return {@code 0L}
aoqi@0 154 */
aoqi@0 155 public long getLastModified() {
aoqi@0 156 return 0L;
aoqi@0 157 }
aoqi@0 158
aoqi@0 159 /**
aoqi@0 160 * This implementation does nothing. Subclasses can change this
aoqi@0 161 * behavior as long as the contract of {@link FileObject} is
aoqi@0 162 * obeyed.
aoqi@0 163 *
aoqi@0 164 * @return {@code false}
aoqi@0 165 */
aoqi@0 166 public boolean delete() {
aoqi@0 167 return false;
aoqi@0 168 }
aoqi@0 169
aoqi@0 170 /**
aoqi@0 171 * @return {@code this.kind}
aoqi@0 172 */
aoqi@0 173 public Kind getKind() {
aoqi@0 174 return kind;
aoqi@0 175 }
aoqi@0 176
aoqi@0 177 /**
aoqi@0 178 * This implementation compares the path of its URI to the given
aoqi@0 179 * simple name. This method returns true if the given kind is
aoqi@0 180 * equal to the kind of this object, and if the path is equal to
aoqi@0 181 * {@code simpleName + kind.extension} or if it ends with {@code
aoqi@0 182 * "/" + simpleName + kind.extension}.
aoqi@0 183 *
aoqi@0 184 * <p>This method calls {@link #getKind} and {@link #toUri} and
aoqi@0 185 * does not access the fields {@link #uri} and {@link #kind}
aoqi@0 186 * directly.
aoqi@0 187 *
aoqi@0 188 * <p>Subclasses can change this behavior as long as the contract
aoqi@0 189 * of {@link JavaFileObject} is obeyed.
aoqi@0 190 */
aoqi@0 191 public boolean isNameCompatible(String simpleName, Kind kind) {
aoqi@0 192 String baseName = simpleName + kind.extension;
aoqi@0 193 return kind.equals(getKind())
aoqi@0 194 && (baseName.equals(toUri().getPath())
aoqi@0 195 || toUri().getPath().endsWith("/" + baseName));
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 /**
aoqi@0 199 * This implementation returns {@code null}. Subclasses can
aoqi@0 200 * change this behavior as long as the contract of
aoqi@0 201 * {@link JavaFileObject} is obeyed.
aoqi@0 202 */
aoqi@0 203 public NestingKind getNestingKind() { return null; }
aoqi@0 204
aoqi@0 205 /**
aoqi@0 206 * This implementation returns {@code null}. Subclasses can
aoqi@0 207 * change this behavior as long as the contract of
aoqi@0 208 * {@link JavaFileObject} is obeyed.
aoqi@0 209 */
aoqi@0 210 public Modifier getAccessLevel() { return null; }
aoqi@0 211
aoqi@0 212 @Override
aoqi@0 213 public String toString() {
aoqi@0 214 return getClass().getName() + "[" + toUri() + "]";
aoqi@0 215 }
aoqi@0 216 }

mercurial