1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
/* * @(#)SimpleJavaFileObject.java 1.13 06/06/25 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.tools; import java.io.*; import java.net.URI; import java.net.URISyntaxException; import java.nio.CharBuffer; import javax.lang.model.element.Modifier; import javax.lang.model.element.NestingKind; import javax.tools.JavaFileObject.Kind; /** * Provides simple implementations for most methods in JavaFileObject. * This class is designed to be subclassed and used as a basis for * JavaFileObject implementations. Subclasses can override the * implementation and specification of any method of this class as * long as the general contract of JavaFileObject is obeyed. * * @author Peter von der Ahé * @since 1.6 */ public class SimpleJavaFileObject implements JavaFileObject { /** * A URI for this file object. */ protected final URI uri; /** * The kind of this file object. */ protected final Kind kind; /** * Construct a SimpleJavaFileObject of the given kind and with the * given URI. * * @param uri the URI for this file object * @param kind the kind of this file object */ protected SimpleJavaFileObject(URI uri, Kind kind) { // null checks uri.getClass(); kind.getClass(); if (uri.getPath() == null) throw new IllegalArgumentException("URI must have a path: " + uri); this.uri = uri; this.kind = kind; } public URI toUri() { return uri; } public String getName() { return toUri().getPath(); } /** * This implementation always throws {@linkplain * UnsupportedOperationException}. Subclasses can change this * behavior as long as the contract of {@link FileObject} is * obeyed. */ public InputStream openInputStream() throws IOException { throw new UnsupportedOperationException(); } /** * This implementation always throws {@linkplain * UnsupportedOperationException}. Subclasses can change this * behavior as long as the contract of {@link FileObject} is * obeyed. */ public OutputStream openOutputStream() throws IOException { throw new UnsupportedOperationException(); } /** * Wraps the result of {@linkplain #getCharContent} in a Reader. * Subclasses can change this behavior as long as the contract of * {@link FileObject} is obeyed. * * @param ignoreEncodingErrors {@inheritDoc} * @return a Reader wrapping the result of getCharContent * @throws IllegalStateException {@inheritDoc} * @throws UnsupportedOperationException {@inheritDoc} * @throws IOException {@inheritDoc} */ public Reader openReader(boolean ignoreEncodingErrors) throws IOException { CharSequence charContent = getCharContent(ignoreEncodingErrors); if (charContent == null) throw new UnsupportedOperationException(); if (charContent instanceof CharBuffer) { CharBuffer buffer = (CharBuffer)charContent; if (buffer.hasArray()) return new CharArrayReader(buffer.array()); } return new StringReader(charContent.toString()); } /** * This implementation always throws {@linkplain * UnsupportedOperationException}. Subclasses can change this * behavior as long as the contract of {@link FileObject} is * obeyed. */ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { throw new UnsupportedOperationException(); } /** * Wraps the result of openOutputStream in a Writer. Subclasses * can change this behavior as long as the contract of {@link * FileObject} is obeyed. * * @return a Writer wrapping the result of openOutputStream * @throws IllegalStateException {@inheritDoc} * @throws UnsupportedOperationException {@inheritDoc} * @throws IOException {@inheritDoc} */ public Writer openWriter() throws IOException { return new OutputStreamWriter(openOutputStream()); } /** * This implementation returns {@code 0L}. Subclasses can change * this behavior as long as the contract of {@link FileObject} is * obeyed. * * @return {@code 0L} */ public long getLastModified() { return 0L; } /** * This implementation does nothing. Subclasses can change this * behavior as long as the contract of {@link FileObject} is * obeyed. * * @return {@code false} */ public boolean delete() { return false; } /** * @return {@code this.kind} */ public Kind getKind() { return kind; } /** * This implementation compares the path of its URI to the given * simple name. This method returns true if the given kind is * equal to the kind of this object, and if the path is equal to * {@code simpleName + kind.extension} or if it ends with {@code * "/" + simpleName + kind.extension}. * * <p>This method calls {@link #getKind} and {@link #toUri} and * does not access the fields {@link #uri} and {@link #kind} * directly. * * <p>Subclasses can change this behavior as long as the contract * of {@link JavaFileObject} is obeyed. */ public boolean isNameCompatible(String simpleName, Kind kind) { String baseName = simpleName + kind.extension; return kind.equals(getKind()) && (baseName.equals(toUri().getPath()) || toUri().getPath().endsWith("/" + baseName)); } /** * This implementation returns {@code null}. Subclasses can * change this behavior as long as the contract of * {@link JavaFileObject} is obeyed. */ public NestingKind getNestingKind() { return null; } /** * This implementation returns {@code null}. Subclasses can * change this behavior as long as the contract of * {@link JavaFileObject} is obeyed. */ public Modifier getAccessLevel() { return null; } @Override public String toString() { return uri + " from " + getClass().getSimpleName(); } }