jdk/src/share/classes/sun/tools/java/Package.java
author ntoda
Thu, 31 Jul 2014 17:01:24 -0700
changeset 25799 1afc4675dc75
parent 5506 202f599c92aa
permissions -rw-r--r--
8044867: Fix raw and unchecked lint warnings in sun.tools.* Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1995, 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.java;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This class is used to represent the classes in a package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * WARNING: The contents of this source file are not part of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * supported API.  Code that depends on them does so at its own risk:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * they are subject to change or removal without notice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
class Package {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * The path which we use to locate source files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    ClassPath sourcePath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * The path which we use to locate class (binary) files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    ClassPath binaryPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * The path name of the package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    String pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Create a package given a class path, and package name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public Package(ClassPath path, Identifier pkg) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this(path, path, pkg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Create a package given a source path, binary path, and package
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public Package(ClassPath sourcePath,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                   ClassPath binaryPath,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                   Identifier pkg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (pkg.isInner())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            pkg = Identifier.lookup(pkg.getQualifier(), pkg.getFlatName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        this.sourcePath = sourcePath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.binaryPath = binaryPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.pkg = pkg.toString().replace('.', File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Check if a class is defined in this package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * (If it is an inner class name, it is assumed to exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * only if its binary file exists.  This is somewhat pessimistic.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public boolean classExists(Identifier className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return getBinaryFile(className) != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                !className.isInner() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
               getSourceFile(className) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Check if the package exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public boolean exists() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // Look for the directory on our binary path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        ClassFile dir = binaryPath.getDirectory(pkg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (dir != null && dir.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (sourcePath != binaryPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            // Look for the directory on our source path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            dir = sourcePath.getDirectory(pkg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (dir != null && dir.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        /* Accommodate ZIP files without CEN entries for directories
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         * (packages): look on class path for at least one binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         * file or one source file with the right package prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        String prefix = pkg + File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return binaryPath.getFiles(prefix, ".class").hasMoreElements()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            || sourcePath.getFiles(prefix, ".java").hasMoreElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private String makeName(String fileName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return pkg.equals("") ? fileName : pkg + File.separator + fileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Get the .class file of a class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public ClassFile getBinaryFile(Identifier className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        className = Type.mangleInnerType(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        String fileName = className.toString() + ".class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return binaryPath.getFile(makeName(fileName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Get the .java file of a class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public ClassFile getSourceFile(Identifier className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // The source file of an inner class is that of its outer class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        className = className.getTopName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        String fileName = className.toString() + ".java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return sourcePath.getFile(makeName(fileName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public ClassFile getSourceFile(String fileName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (fileName.endsWith(".java")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return sourcePath.getFile(makeName(fileName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 5506
diff changeset
   147
    public Enumeration<ClassFile> getSourceFiles() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return sourcePath.getFiles(pkg, ".java");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
25799
1afc4675dc75 8044867: Fix raw and unchecked lint warnings in sun.tools.*
ntoda
parents: 5506
diff changeset
   151
    public Enumeration<ClassFile> getBinaryFiles() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return binaryPath.getFiles(pkg, ".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (pkg.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return "unnamed package";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return "package " + pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}