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

Wed, 13 Apr 2011 11:35:43 -0700

author
jjh
date
Wed, 13 Apr 2011 11:35:43 -0700
changeset 972
694ff82ca68e
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

7032975: API files in javax.annotation.processing need to be updated for references to JLS
7032972: API files in javax.tools need to updated for references to JVM Spec with editions/hyperlinks
7032978: API files in javax.tools need to be updated for references to JLS with editions/hyperlinks
Summary: Removed URLs and 'edition' references
Reviewed-by: jjg, darcy

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

mercurial