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

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 415
49359d0e6a9c
permissions
-rw-r--r--

Initial load

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

mercurial