langtools/src/share/classes/com/sun/tools/javac/file/BaseFileObject.java
changeset 3995 73af8b6fb8bc
parent 3782 ae62279eeb46
child 3998 c66be272f350
equal deleted inserted replaced
3994:7df1ecd5eadb 3995:73af8b6fb8bc
    32 import java.net.URI;
    32 import java.net.URI;
    33 import java.net.URISyntaxException;
    33 import java.net.URISyntaxException;
    34 import java.nio.charset.CharsetDecoder;
    34 import java.nio.charset.CharsetDecoder;
    35 import javax.lang.model.element.Modifier;
    35 import javax.lang.model.element.Modifier;
    36 import javax.lang.model.element.NestingKind;
    36 import javax.lang.model.element.NestingKind;
       
    37 import javax.tools.FileObject;
    37 import javax.tools.JavaFileObject;
    38 import javax.tools.JavaFileObject;
    38 
    39 
    39 import static javax.tools.JavaFileObject.Kind.*;
    40 import static javax.tools.JavaFileObject.Kind.*;
    40 
    41 
    41 /**
    42 /**
    47 public abstract class BaseFileObject implements JavaFileObject {
    48 public abstract class BaseFileObject implements JavaFileObject {
    48     protected BaseFileObject(JavacFileManager fileManager) {
    49     protected BaseFileObject(JavacFileManager fileManager) {
    49         this.fileManager = fileManager;
    50         this.fileManager = fileManager;
    50     }
    51     }
    51 
    52 
    52     public JavaFileObject.Kind getKind() {
    53     /** Return a short name for the object, such as for use in raw diagnostics
    53         String n = getName();
    54      */
    54         if (n.endsWith(CLASS.extension))
    55     public abstract String getShortName();
    55             return CLASS;
       
    56         else if (n.endsWith(SOURCE.extension))
       
    57             return SOURCE;
       
    58         else if (n.endsWith(HTML.extension))
       
    59             return HTML;
       
    60         else
       
    61             return OTHER;
       
    62     }
       
    63 
    56 
    64     @Override
    57     @Override
    65     public String toString() {
    58     public String toString() {
    66         return getPath();
    59         return getClass().getSimpleName() + "[" + getName() + "]";
    67     }
    60     }
    68 
       
    69     /** @deprecated see bug 6410637 */
       
    70     @Deprecated
       
    71     public String getPath() {
       
    72         return getName();
       
    73     }
       
    74 
       
    75     /** @deprecated see bug 6410637 */
       
    76     @Deprecated
       
    77     abstract public String getName();
       
    78 
    61 
    79     public NestingKind getNestingKind() { return null; }
    62     public NestingKind getNestingKind() { return null; }
    80 
    63 
    81     public Modifier getAccessLevel()  { return null; }
    64     public Modifier getAccessLevel()  { return null; }
    82 
    65 
    87     protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
    70     protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
    88         throw new UnsupportedOperationException();
    71         throw new UnsupportedOperationException();
    89     }
    72     }
    90 
    73 
    91     protected abstract String inferBinaryName(Iterable<? extends File> path);
    74     protected abstract String inferBinaryName(Iterable<? extends File> path);
       
    75 
       
    76     protected static JavaFileObject.Kind getKind(String filename) {
       
    77         if (filename.endsWith(CLASS.extension))
       
    78             return CLASS;
       
    79         else if (filename.endsWith(SOURCE.extension))
       
    80             return SOURCE;
       
    81         else if (filename.endsWith(HTML.extension))
       
    82             return HTML;
       
    83         else
       
    84             return OTHER;
       
    85     }
    92 
    86 
    93     protected static String removeExtension(String fileName) {
    87     protected static String removeExtension(String fileName) {
    94         int lastDot = fileName.lastIndexOf(".");
    88         int lastDot = fileName.lastIndexOf(".");
    95         return (lastDot == -1 ? fileName : fileName.substring(0, lastDot));
    89         return (lastDot == -1 ? fileName : fileName.substring(0, lastDot));
    96     }
    90     }
   113         public CannotCreateUriError(String value, Throwable cause) {
   107         public CannotCreateUriError(String value, Throwable cause) {
   114             super(value, cause);
   108             super(value, cause);
   115         }
   109         }
   116     }
   110     }
   117 
   111 
       
   112     /** Return the last component of a presumed hierarchical URI.
       
   113      *  From the scheme specific part of the URI, it returns the substring
       
   114      *  after the last "/" if any, or everything if no "/" is found.
       
   115      */
       
   116     public static String getSimpleName(FileObject fo) {
       
   117         URI uri = fo.toUri();
       
   118         String s = uri.getSchemeSpecificPart();
       
   119         return s.substring(s.lastIndexOf("/") + 1); // safe when / not found
       
   120 
       
   121     }
       
   122 
   118     /** The file manager that created this JavaFileObject. */
   123     /** The file manager that created this JavaFileObject. */
   119     protected final JavacFileManager fileManager;
   124     protected final JavacFileManager fileManager;
   120 }
   125 }