Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/javax/tools/SimpleJavaFileObject.java
38900 views
/*1* Copyright (c) 2005, 2006, 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 javax.lang.model.element.Modifier;31import javax.lang.model.element.NestingKind;32import javax.tools.JavaFileObject.Kind;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) {63// null checks64uri.getClass();65kind.getClass();66if (uri.getPath() == null)67throw new IllegalArgumentException("URI must have a path: " + uri);68this.uri = uri;69this.kind = kind;70}7172public URI toUri() {73return uri;74}7576public String getName() {77return toUri().getPath();78}7980/**81* This implementation always throws {@linkplain82* UnsupportedOperationException}. Subclasses can change this83* behavior as long as the contract of {@link FileObject} is84* obeyed.85*/86public InputStream openInputStream() throws IOException {87throw new UnsupportedOperationException();88}8990/**91* This implementation always throws {@linkplain92* UnsupportedOperationException}. Subclasses can change this93* behavior as long as the contract of {@link FileObject} is94* obeyed.95*/96public OutputStream openOutputStream() throws IOException {97throw new UnsupportedOperationException();98}99100/**101* Wraps the result of {@linkplain #getCharContent} in a Reader.102* Subclasses can change this behavior as long as the contract of103* {@link FileObject} is obeyed.104*105* @param ignoreEncodingErrors {@inheritDoc}106* @return a Reader wrapping the result of getCharContent107* @throws IllegalStateException {@inheritDoc}108* @throws UnsupportedOperationException {@inheritDoc}109* @throws IOException {@inheritDoc}110*/111public Reader openReader(boolean ignoreEncodingErrors) throws IOException {112CharSequence charContent = getCharContent(ignoreEncodingErrors);113if (charContent == null)114throw new UnsupportedOperationException();115if (charContent instanceof CharBuffer) {116CharBuffer buffer = (CharBuffer)charContent;117if (buffer.hasArray())118return new CharArrayReader(buffer.array());119}120return new StringReader(charContent.toString());121}122123/**124* This implementation always throws {@linkplain125* UnsupportedOperationException}. Subclasses can change this126* behavior as long as the contract of {@link FileObject} is127* obeyed.128*/129public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {130throw new UnsupportedOperationException();131}132133/**134* Wraps the result of openOutputStream in a Writer. Subclasses135* can change this behavior as long as the contract of {@link136* FileObject} is obeyed.137*138* @return a Writer wrapping the result of openOutputStream139* @throws IllegalStateException {@inheritDoc}140* @throws UnsupportedOperationException {@inheritDoc}141* @throws IOException {@inheritDoc}142*/143public Writer openWriter() throws IOException {144return new OutputStreamWriter(openOutputStream());145}146147/**148* This implementation returns {@code 0L}. Subclasses can change149* this behavior as long as the contract of {@link FileObject} is150* obeyed.151*152* @return {@code 0L}153*/154public long getLastModified() {155return 0L;156}157158/**159* This implementation does nothing. Subclasses can change this160* behavior as long as the contract of {@link FileObject} is161* obeyed.162*163* @return {@code false}164*/165public boolean delete() {166return false;167}168169/**170* @return {@code this.kind}171*/172public Kind getKind() {173return kind;174}175176/**177* This implementation compares the path of its URI to the given178* simple name. This method returns true if the given kind is179* equal to the kind of this object, and if the path is equal to180* {@code simpleName + kind.extension} or if it ends with {@code181* "/" + simpleName + kind.extension}.182*183* <p>This method calls {@link #getKind} and {@link #toUri} and184* does not access the fields {@link #uri} and {@link #kind}185* directly.186*187* <p>Subclasses can change this behavior as long as the contract188* of {@link JavaFileObject} is obeyed.189*/190public boolean isNameCompatible(String simpleName, Kind kind) {191String baseName = simpleName + kind.extension;192return kind.equals(getKind())193&& (baseName.equals(toUri().getPath())194|| toUri().getPath().endsWith("/" + baseName));195}196197/**198* This implementation returns {@code null}. Subclasses can199* change this behavior as long as the contract of200* {@link JavaFileObject} is obeyed.201*/202public NestingKind getNestingKind() { return null; }203204/**205* This implementation returns {@code null}. Subclasses can206* change this behavior as long as the contract of207* {@link JavaFileObject} is obeyed.208*/209public Modifier getAccessLevel() { return null; }210211@Override212public String toString() {213return getClass().getName() + "[" + toUri() + "]";214}215}216217218