langtools/src/share/classes/com/sun/tools/javac/file/BaseFileObject.java
author jjg
Wed, 18 Jun 2008 07:23:25 -0700
changeset 810 e4b6a6d206e6
parent 731 1dd22bdb9ca5
child 815 bcb5c0d7c1ab
permissions -rw-r--r--
6714365: refactor JavacFileManager to move nested classes to top level Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 10
diff changeset
    26
package com.sun.tools.javac.file;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    28
import java.io.File;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.InputStreamReader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.io.Reader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.nio.charset.CharsetDecoder;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import javax.lang.model.element.Modifier;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.lang.model.element.NestingKind;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import static javax.tools.JavaFileObject.Kind.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
public abstract class BaseFileObject implements JavaFileObject {
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    40
    protected BaseFileObject(JavacFileManager fileManager) {
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    41
        this.fileManager = fileManager;
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    42
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    public JavaFileObject.Kind getKind() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
        String n = getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
        if (n.endsWith(CLASS.extension))
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
            return CLASS;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
        else if (n.endsWith(SOURCE.extension))
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
            return SOURCE;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        else if (n.endsWith(HTML.extension))
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
            return HTML;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
            return OTHER;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        return getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    /** @deprecated see bug 6410637 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    public String getPath() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        return getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    /** @deprecated see bug 6410637 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    abstract public String getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    public NestingKind getNestingKind() { return null; }
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    public Modifier getAccessLevel()  { return null; }
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        return new InputStreamReader(openInputStream(), getDecoder(ignoreEncodingErrors));
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    83
    protected abstract String inferBinaryName(Iterable<? extends File> path);
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    84
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    85
    protected static String removeExtension(String fileName) {
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    86
        int lastDot = fileName.lastIndexOf(".");
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    87
        return (lastDot == -1 ? fileName : fileName.substring(0, lastDot));
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    88
    }
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    89
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    90
    /** The file manager that created this JavaFileObject. */
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    91
    protected final JavacFileManager fileManager;
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    92
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
}