Path: blob/master/src/java.compiler/share/classes/javax/tools/SimpleJavaFileObject.java
41152 views
/*1* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.tools;2627import java.io.*;28import java.net.URI;29import java.nio.CharBuffer;30import java.util.Objects;31import javax.lang.model.element.Modifier;32import javax.lang.model.element.NestingKind;3334/**35* Provides simple implementations for most methods in JavaFileObject.36* This class is designed to be subclassed and used as a basis for37* JavaFileObject implementations. Subclasses can override the38* implementation and specification of any method of this class as39* long as the general contract of JavaFileObject is obeyed.40*41* @author Peter von der Ahé42* @since 1.643*/44public class SimpleJavaFileObject implements JavaFileObject {45/**46* A URI for this file object.47*/48protected final URI uri;4950/**51* The kind of this file object.52*/53protected final Kind kind;5455/**56* Construct a SimpleJavaFileObject of the given kind and with the57* given URI.58*59* @param uri the URI for this file object60* @param kind the kind of this file object61*/62protected SimpleJavaFileObject(URI uri, Kind kind) {63Objects.requireNonNull(uri);64Objects.requireNonNull(kind);65if (uri.getPath() == null)66throw new IllegalArgumentException("URI must have a path: " + uri);67this.uri = uri;68this.kind = kind;69}7071public URI toUri() {72return uri;73}7475public String getName() {76return toUri().getPath();77}7879/**80* This implementation always throws {@linkplain81* UnsupportedOperationException}. Subclasses can change this82* behavior as long as the contract of {@link FileObject} is83* obeyed.84*/85public InputStream openInputStream() throws IOException {86throw new UnsupportedOperationException();87}8889/**90* This implementation always throws {@linkplain91* UnsupportedOperationException}. Subclasses can change this92* behavior as long as the contract of {@link FileObject} is93* obeyed.94*/95public OutputStream openOutputStream() throws IOException {96throw new UnsupportedOperationException();97}9899/**100* Wraps the result of {@linkplain #getCharContent} in a Reader.101* Subclasses can change this behavior as long as the contract of102* {@link FileObject} is obeyed.103*104* @param ignoreEncodingErrors {@inheritDoc}105* @return a Reader wrapping the result of getCharContent106* @throws IllegalStateException {@inheritDoc}107* @throws UnsupportedOperationException {@inheritDoc}108* @throws IOException {@inheritDoc}109*/110public Reader openReader(boolean ignoreEncodingErrors) throws IOException {111CharSequence charContent = getCharContent(ignoreEncodingErrors);112if (charContent == null)113throw new UnsupportedOperationException();114if (charContent instanceof CharBuffer) {115CharBuffer buffer = (CharBuffer)charContent;116if (buffer.hasArray())117return new CharArrayReader(buffer.array());118}119return new StringReader(charContent.toString());120}121122/**123* This implementation always throws {@linkplain124* UnsupportedOperationException}. Subclasses can change this125* behavior as long as the contract of {@link FileObject} is126* obeyed.127*/128public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {129throw new UnsupportedOperationException();130}131132/**133* Wraps the result of openOutputStream in a Writer. Subclasses134* can change this behavior as long as the contract of {@link135* FileObject} is obeyed.136*137* @return a Writer wrapping the result of openOutputStream138* @throws IllegalStateException {@inheritDoc}139* @throws UnsupportedOperationException {@inheritDoc}140* @throws IOException {@inheritDoc}141*/142public Writer openWriter() throws IOException {143return new OutputStreamWriter(openOutputStream());144}145146/**147* This implementation returns {@code 0L}. Subclasses can change148* this behavior as long as the contract of {@link FileObject} is149* obeyed.150*151* @return {@code 0L}152*/153public long getLastModified() {154return 0L;155}156157/**158* This implementation does nothing. Subclasses can change this159* behavior as long as the contract of {@link FileObject} is160* obeyed.161*162* @return {@code false}163*/164public boolean delete() {165return false;166}167168/**169* @return {@code this.kind}170*/171public Kind getKind() {172return kind;173}174175/**176* This implementation compares the path of its URI to the given177* simple name. This method returns true if the given kind is178* equal to the kind of this object, and if the path is equal to179* {@code simpleName + kind.extension} or if it ends with {@code180* "/" + simpleName + kind.extension}.181*182* <p>This method calls {@link #getKind} and {@link #toUri} and183* does not access the fields {@link #uri} and {@link #kind}184* directly.185*186* <p>Subclasses can change this behavior as long as the contract187* of {@link JavaFileObject} is obeyed.188*/189public boolean isNameCompatible(String simpleName, Kind kind) {190String baseName = simpleName + kind.extension;191return kind.equals(getKind())192&& (baseName.equals(toUri().getPath())193|| toUri().getPath().endsWith("/" + baseName));194}195196/**197* This implementation returns {@code null}. Subclasses can198* change this behavior as long as the contract of199* {@link JavaFileObject} is obeyed.200*/201public NestingKind getNestingKind() { return null; }202203/**204* This implementation returns {@code null}. Subclasses can205* change this behavior as long as the contract of206* {@link JavaFileObject} is obeyed.207*/208public Modifier getAccessLevel() { return null; }209210@Override211public String toString() {212return getClass().getName() + "[" + toUri() + "]";213}214}215216217