0N/A/*
553N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Apackage javax.tools;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.URI;
0N/Aimport java.nio.CharBuffer;
0N/Aimport javax.lang.model.element.Modifier;
0N/Aimport javax.lang.model.element.NestingKind;
0N/Aimport javax.tools.JavaFileObject.Kind;
0N/A
0N/A/**
0N/A * Provides simple implementations for most methods in JavaFileObject.
0N/A * This class is designed to be subclassed and used as a basis for
0N/A * JavaFileObject implementations. Subclasses can override the
0N/A * implementation and specification of any method of this class as
0N/A * long as the general contract of JavaFileObject is obeyed.
0N/A *
0N/A * @author Peter von der Ahé
0N/A * @since 1.6
0N/A */
0N/Apublic class SimpleJavaFileObject implements JavaFileObject {
0N/A /**
0N/A * A URI for this file object.
0N/A */
0N/A protected final URI uri;
0N/A
0N/A /**
0N/A * The kind of this file object.
0N/A */
0N/A protected final Kind kind;
0N/A
0N/A /**
0N/A * Construct a SimpleJavaFileObject of the given kind and with the
0N/A * given URI.
0N/A *
0N/A * @param uri the URI for this file object
0N/A * @param kind the kind of this file object
0N/A */
0N/A protected SimpleJavaFileObject(URI uri, Kind kind) {
0N/A // null checks
0N/A uri.getClass();
0N/A kind.getClass();
0N/A if (uri.getPath() == null)
0N/A throw new IllegalArgumentException("URI must have a path: " + uri);
0N/A this.uri = uri;
0N/A this.kind = kind;
0N/A }
0N/A
0N/A public URI toUri() {
0N/A return uri;
0N/A }
0N/A
0N/A public String getName() {
0N/A return toUri().getPath();
0N/A }
0N/A
0N/A /**
0N/A * This implementation always throws {@linkplain
0N/A * UnsupportedOperationException}. Subclasses can change this
0N/A * behavior as long as the contract of {@link FileObject} is
0N/A * obeyed.
0N/A */
0N/A public InputStream openInputStream() throws IOException {
0N/A throw new UnsupportedOperationException();
0N/A }
0N/A
0N/A /**
0N/A * This implementation always throws {@linkplain
0N/A * UnsupportedOperationException}. Subclasses can change this
0N/A * behavior as long as the contract of {@link FileObject} is
0N/A * obeyed.
0N/A */
0N/A public OutputStream openOutputStream() throws IOException {
0N/A throw new UnsupportedOperationException();
0N/A }
0N/A
0N/A /**
0N/A * Wraps the result of {@linkplain #getCharContent} in a Reader.
0N/A * Subclasses can change this behavior as long as the contract of
0N/A * {@link FileObject} is obeyed.
0N/A *
0N/A * @param ignoreEncodingErrors {@inheritDoc}
0N/A * @return a Reader wrapping the result of getCharContent
0N/A * @throws IllegalStateException {@inheritDoc}
0N/A * @throws UnsupportedOperationException {@inheritDoc}
0N/A * @throws IOException {@inheritDoc}
0N/A */
0N/A public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
0N/A CharSequence charContent = getCharContent(ignoreEncodingErrors);
0N/A if (charContent == null)
0N/A throw new UnsupportedOperationException();
0N/A if (charContent instanceof CharBuffer) {
0N/A CharBuffer buffer = (CharBuffer)charContent;
0N/A if (buffer.hasArray())
0N/A return new CharArrayReader(buffer.array());
0N/A }
0N/A return new StringReader(charContent.toString());
0N/A }
0N/A
0N/A /**
0N/A * This implementation always throws {@linkplain
0N/A * UnsupportedOperationException}. Subclasses can change this
0N/A * behavior as long as the contract of {@link FileObject} is
0N/A * obeyed.
0N/A */
0N/A public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
0N/A throw new UnsupportedOperationException();
0N/A }
0N/A
0N/A /**
0N/A * Wraps the result of openOutputStream in a Writer. Subclasses
0N/A * can change this behavior as long as the contract of {@link
0N/A * FileObject} is obeyed.
0N/A *
0N/A * @return a Writer wrapping the result of openOutputStream
0N/A * @throws IllegalStateException {@inheritDoc}
0N/A * @throws UnsupportedOperationException {@inheritDoc}
0N/A * @throws IOException {@inheritDoc}
0N/A */
0N/A public Writer openWriter() throws IOException {
0N/A return new OutputStreamWriter(openOutputStream());
0N/A }
0N/A
0N/A /**
0N/A * This implementation returns {@code 0L}. Subclasses can change
0N/A * this behavior as long as the contract of {@link FileObject} is
0N/A * obeyed.
0N/A *
0N/A * @return {@code 0L}
0N/A */
0N/A public long getLastModified() {
0N/A return 0L;
0N/A }
0N/A
0N/A /**
0N/A * This implementation does nothing. Subclasses can change this
0N/A * behavior as long as the contract of {@link FileObject} is
0N/A * obeyed.
0N/A *
0N/A * @return {@code false}
0N/A */
0N/A public boolean delete() {
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * @return {@code this.kind}
0N/A */
0N/A public Kind getKind() {
0N/A return kind;
0N/A }
0N/A
0N/A /**
0N/A * This implementation compares the path of its URI to the given
0N/A * simple name. This method returns true if the given kind is
0N/A * equal to the kind of this object, and if the path is equal to
0N/A * {@code simpleName + kind.extension} or if it ends with {@code
0N/A * "/" + simpleName + kind.extension}.
0N/A *
0N/A * <p>This method calls {@link #getKind} and {@link #toUri} and
0N/A * does not access the fields {@link #uri} and {@link #kind}
0N/A * directly.
0N/A *
0N/A * <p>Subclasses can change this behavior as long as the contract
0N/A * of {@link JavaFileObject} is obeyed.
0N/A */
0N/A public boolean isNameCompatible(String simpleName, Kind kind) {
0N/A String baseName = simpleName + kind.extension;
0N/A return kind.equals(getKind())
0N/A && (baseName.equals(toUri().getPath())
0N/A || toUri().getPath().endsWith("/" + baseName));
0N/A }
0N/A
0N/A /**
0N/A * This implementation returns {@code null}. Subclasses can
0N/A * change this behavior as long as the contract of
0N/A * {@link JavaFileObject} is obeyed.
0N/A */
0N/A public NestingKind getNestingKind() { return null; }
0N/A
0N/A /**
0N/A * This implementation returns {@code null}. Subclasses can
0N/A * change this behavior as long as the contract of
0N/A * {@link JavaFileObject} is obeyed.
0N/A */
0N/A public Modifier getAccessLevel() { return null; }
0N/A
0N/A @Override
0N/A public String toString() {
0N/A return getClass().getName() + "[" + toUri() + "]";
0N/A }
0N/A}