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