SimpleJavaFileObject.java revision 553
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches/*
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches *
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * This code is free software; you can redistribute it and/or modify it
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * under the terms of the GNU General Public License version 2 only, as
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * published by the Free Software Foundation. Oracle designates this
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * particular file as subject to the "Classpath" exception as provided
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * by Oracle in the LICENSE file that accompanied this code.
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches *
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * This code is distributed in the hope that it will be useful, but WITHOUT
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * version 2 for more details (a copy is included in the LICENSE file that
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * accompanied this code).
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches *
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * You should have received a copy of the GNU General Public License version
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * 2 along with this work; if not, write to the Free Software Foundation,
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches *
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * or visit www.oracle.com if you need additional information or have any
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * questions.
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches */
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanchespackage javax.tools;
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanchesimport java.io.*;
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanchesimport java.net.URI;
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanchesimport java.nio.CharBuffer;
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanchesimport javax.lang.model.element.Modifier;
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanchesimport javax.lang.model.element.NestingKind;
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanchesimport javax.tools.JavaFileObject.Kind;
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches
6c97635ad1185cbd08910ee5042001e243496503Josh Andler/**
6c97635ad1185cbd08910ee5042001e243496503Josh Andler * Provides simple implementations for most methods in JavaFileObject.
6c97635ad1185cbd08910ee5042001e243496503Josh Andler * This class is designed to be subclassed and used as a basis for
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * JavaFileObject implementations. Subclasses can override the
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * implementation and specification of any method of this class as
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * long as the general contract of JavaFileObject is obeyed.
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches *
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * @author Peter von der Ahé
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * @since 1.6
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches */
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanchespublic class SimpleJavaFileObject implements JavaFileObject {
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches /**
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * A URI for this file object.
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches */
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches protected final URI uri;
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches /**
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * The kind of this file object.
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches */
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches protected final Kind kind;
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches /**
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * Construct a SimpleJavaFileObject of the given kind and with the
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * given URI.
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches *
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * @param uri the URI for this file object
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * @param kind the kind of this file object
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches */
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches protected SimpleJavaFileObject(URI uri, Kind kind) {
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches // null checks
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches uri.getClass();
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches kind.getClass();
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches if (uri.getPath() == null)
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches throw new IllegalArgumentException("URI must have a path: " + uri);
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches this.uri = uri;
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches this.kind = kind;
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches }
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches public URI toUri() {
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches return uri;
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches }
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches public String getName() {
aea39058a556f160194bf12f84ee7254af96278dSlagvi Public return toUri().getPath();
aea39058a556f160194bf12f84ee7254af96278dSlagvi Public }
aea39058a556f160194bf12f84ee7254af96278dSlagvi Public
e03bf3058955e0f649829d7329cf529582aaa828Slagvi Public /**
aea39058a556f160194bf12f84ee7254af96278dSlagvi Public * This implementation always throws {@linkplain
aea39058a556f160194bf12f84ee7254af96278dSlagvi Public * UnsupportedOperationException}. Subclasses can change this
aea39058a556f160194bf12f84ee7254af96278dSlagvi Public * behavior as long as the contract of {@link FileObject} is
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches * obeyed.
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches */
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches public InputStream openInputStream() throws IOException {
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches throw new UnsupportedOperationException();
d062f2b3edd1cd9ef77eabc4491fe9ef4206c699Felipe C. da S. Sanches }
/**
* 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 getClass().getName() + "[" + toUri() + "]";
}
}