src/share/classes/com/sun/tools/javac/file/BaseFileObject.java

Fri, 11 Dec 2009 14:26:27 -0800

author
jjg
date
Fri, 11 Dec 2009 14:26:27 -0800
changeset 450
4011f49b4af8
parent 424
86b773b7cb40
child 554
9d9f26857129
permissions
-rw-r--r--

6906175: bridge JSR199 and JSR 203 APIs
Reviewed-by: darcy, alanb

duke@1 1 /*
xdono@404 2 * Copyright 2005-2009 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
jjg@50 26 package com.sun.tools.javac.file;
duke@1 27
jjg@57 28 import java.io.File;
duke@1 29 import java.io.IOException;
duke@1 30 import java.io.InputStreamReader;
duke@1 31 import java.io.Reader;
jjg@400 32 import java.net.URI;
jjg@400 33 import java.net.URISyntaxException;
duke@1 34 import java.nio.charset.CharsetDecoder;
duke@1 35 import javax.lang.model.element.Modifier;
duke@1 36 import javax.lang.model.element.NestingKind;
jjg@415 37 import javax.tools.FileObject;
duke@1 38 import javax.tools.JavaFileObject;
duke@1 39
duke@1 40 import static javax.tools.JavaFileObject.Kind.*;
duke@1 41
jjg@450 42 import com.sun.tools.javac.util.BaseFileManager;
jjg@450 43
jjg@333 44 /**
jjg@333 45 * <p><b>This is NOT part of any API supported by Sun Microsystems.
jjg@333 46 * If you write code that depends on this, you do so at your own risk.
jjg@333 47 * This code and its internal interfaces are subject to change or
jjg@333 48 * deletion without notice.</b>
jjg@333 49 */
duke@1 50 public abstract class BaseFileObject implements JavaFileObject {
jjg@57 51 protected BaseFileObject(JavacFileManager fileManager) {
jjg@57 52 this.fileManager = fileManager;
jjg@57 53 }
duke@1 54
jjg@415 55 /** Return a short name for the object, such as for use in raw diagnostics
jjg@415 56 */
jjg@415 57 public abstract String getShortName();
duke@1 58
duke@1 59 @Override
duke@1 60 public String toString() {
jjg@415 61 return getClass().getSimpleName() + "[" + getName() + "]";
duke@1 62 }
duke@1 63
duke@1 64 public NestingKind getNestingKind() { return null; }
duke@1 65
duke@1 66 public Modifier getAccessLevel() { return null; }
duke@1 67
duke@1 68 public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
duke@1 69 return new InputStreamReader(openInputStream(), getDecoder(ignoreEncodingErrors));
duke@1 70 }
duke@1 71
duke@1 72 protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
duke@1 73 throw new UnsupportedOperationException();
duke@1 74 }
duke@1 75
jjg@57 76 protected abstract String inferBinaryName(Iterable<? extends File> path);
jjg@57 77
jjg@415 78 protected static JavaFileObject.Kind getKind(String filename) {
jjg@450 79 return BaseFileManager.getKind(filename);
jjg@415 80 }
jjg@415 81
jjg@57 82 protected static String removeExtension(String fileName) {
jjg@57 83 int lastDot = fileName.lastIndexOf(".");
jjg@57 84 return (lastDot == -1 ? fileName : fileName.substring(0, lastDot));
jjg@57 85 }
jjg@57 86
jjg@400 87 protected static URI createJarUri(File jarFile, String entryName) {
jjg@400 88 URI jarURI = jarFile.toURI().normalize();
jjg@400 89 String separator = entryName.startsWith("/") ? "!" : "!/";
jjg@400 90 try {
jjg@400 91 // The jar URI convention appears to be not to re-encode the jarURI
jjg@400 92 return new URI("jar:" + jarURI + separator + entryName);
jjg@400 93 } catch (URISyntaxException e) {
jjg@400 94 throw new CannotCreateUriError(jarURI + separator + entryName, e);
jjg@400 95 }
jjg@400 96 }
jjg@400 97
jjg@400 98 /** Used when URLSyntaxException is thrown unexpectedly during
jjg@400 99 * implementations of (Base)FileObject.toURI(). */
jjg@400 100 protected static class CannotCreateUriError extends Error {
jjg@400 101 private static final long serialVersionUID = 9101708840997613546L;
jjg@400 102 public CannotCreateUriError(String value, Throwable cause) {
jjg@400 103 super(value, cause);
jjg@400 104 }
jjg@400 105 }
jjg@400 106
jjg@415 107 /** Return the last component of a presumed hierarchical URI.
jjg@415 108 * From the scheme specific part of the URI, it returns the substring
jjg@415 109 * after the last "/" if any, or everything if no "/" is found.
jjg@415 110 */
jjg@415 111 public static String getSimpleName(FileObject fo) {
jjg@415 112 URI uri = fo.toUri();
jjg@415 113 String s = uri.getSchemeSpecificPart();
jjg@415 114 return s.substring(s.lastIndexOf("/") + 1); // safe when / not found
jjg@415 115
jjg@415 116 }
jjg@415 117
jjg@424 118 // force subtypes to define equals
jjg@424 119 @Override
jjg@424 120 public abstract boolean equals(Object other);
jjg@424 121
jjg@424 122 // force subtypes to define hashCode
jjg@424 123 @Override
jjg@424 124 public abstract int hashCode();
jjg@424 125
jjg@57 126 /** The file manager that created this JavaFileObject. */
jjg@57 127 protected final JavacFileManager fileManager;
duke@1 128 }

mercurial