langtools/src/share/classes/com/sun/tools/javac/file/BaseFileObject.java
author jjg
Thu, 10 Jun 2010 16:08:01 -0700
changeset 5847 1908176fd6e3
parent 5520 86e4b9a9da40
permissions -rw-r--r--
6944312: Potential rebranding issues in openjdk/langtools repository sources Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4548
diff changeset
     2
 * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
10
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
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4548
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4548
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4548
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4548
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4548
diff changeset
    23
 * questions.
10
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;
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    32
import java.net.URI;
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    33
import java.net.URISyntaxException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.nio.charset.CharsetDecoder;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.lang.model.element.Modifier;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.lang.model.element.NestingKind;
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
    37
import javax.tools.FileObject;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import static javax.tools.JavaFileObject.Kind.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents: 4073
diff changeset
    42
import com.sun.tools.javac.util.BaseFileManager;
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents: 4073
diff changeset
    43
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 815
diff changeset
    44
/**
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    45
 * <p><b>This is NOT part of any supported API.
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 815
diff changeset
    46
 * If you write code that depends on this, you do so at your own risk.
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 815
diff changeset
    47
 * This code and its internal interfaces are subject to change or
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 815
diff changeset
    48
 * deletion without notice.</b>
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 815
diff changeset
    49
*/
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
public abstract class BaseFileObject implements JavaFileObject {
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    51
    protected BaseFileObject(JavacFileManager fileManager) {
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    52
        this.fileManager = fileManager;
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    53
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
    55
    /** Return a short name for the object, such as for use in raw diagnostics
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
    56
     */
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
    57
    public abstract String getShortName();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    public String toString() {
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
    61
        return getClass().getSimpleName() + "[" + getName() + "]";
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    public NestingKind getNestingKind() { return null; }
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    public Modifier getAccessLevel()  { return null; }
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        return new InputStreamReader(openInputStream(), getDecoder(ignoreEncodingErrors));
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    76
    protected abstract String inferBinaryName(Iterable<? extends File> path);
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    77
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
    78
    protected static JavaFileObject.Kind getKind(String filename) {
4548
bc0d5b3c3b2d 6906175: bridge JSR199 and JSR 203 APIs
jjg
parents: 4073
diff changeset
    79
        return BaseFileManager.getKind(filename);
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
    80
    }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
    81
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    82
    protected static String removeExtension(String fileName) {
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    83
        int lastDot = fileName.lastIndexOf(".");
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    84
        return (lastDot == -1 ? fileName : fileName.substring(0, lastDot));
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    85
    }
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    86
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    87
    protected static URI createJarUri(File jarFile, String entryName) {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    88
        URI jarURI = jarFile.toURI().normalize();
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    89
        String separator = entryName.startsWith("/") ? "!" : "!/";
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    90
        try {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    91
            // The jar URI convention appears to be not to re-encode the jarURI
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    92
            return new URI("jar:" + jarURI + separator + entryName);
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    93
        } catch (URISyntaxException e) {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    94
            throw new CannotCreateUriError(jarURI + separator + entryName, e);
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    95
        }
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    96
    }
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    97
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    98
    /** Used when URLSyntaxException is thrown unexpectedly during
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    99
     *  implementations of (Base)FileObject.toURI(). */
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   100
    protected static class CannotCreateUriError extends Error {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   101
        private static final long serialVersionUID = 9101708840997613546L;
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   102
        public CannotCreateUriError(String value, Throwable cause) {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   103
            super(value, cause);
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   104
        }
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   105
    }
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   106
3995
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
   107
    /** Return the last component of a presumed hierarchical URI.
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
   108
     *  From the scheme specific part of the URI, it returns the substring
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
   109
     *  after the last "/" if any, or everything if no "/" is found.
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
   110
     */
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
   111
    public static String getSimpleName(FileObject fo) {
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
   112
        URI uri = fo.toUri();
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
   113
        String s = uri.getSchemeSpecificPart();
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
   114
        return s.substring(s.lastIndexOf("/") + 1); // safe when / not found
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
   115
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
   116
    }
73af8b6fb8bc 6410637: Make decision on deprecated methods in DefaultFileManager and BaseFileObject.
jjg
parents: 3782
diff changeset
   117
4073
9788f4549740 6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents: 3998
diff changeset
   118
    // force subtypes to define equals
9788f4549740 6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents: 3998
diff changeset
   119
    @Override
9788f4549740 6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents: 3998
diff changeset
   120
    public abstract boolean equals(Object other);
9788f4549740 6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents: 3998
diff changeset
   121
9788f4549740 6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents: 3998
diff changeset
   122
    // force subtypes to define hashCode
9788f4549740 6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents: 3998
diff changeset
   123
    @Override
9788f4549740 6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents: 3998
diff changeset
   124
    public abstract int hashCode();
9788f4549740 6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents: 3998
diff changeset
   125
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
   126
    /** The file manager that created this JavaFileObject. */
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
   127
    protected final JavacFileManager fileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
}