001: /*
002: * Copyright 1995-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.java;
027:
028: import java.util.Enumeration;
029: import java.io.File;
030: import java.io.IOException;
031:
032: /**
033: * This class is used to represent the classes in a package.
034: *
035: * WARNING: The contents of this source file are not part of any
036: * supported API. Code that depends on them does so at its own risk:
037: * they are subject to change or removal without notice.
038: */
039: public class Package {
040: /**
041: * The path which we use to locate source files.
042: */
043: ClassPath sourcePath;
044:
045: /**
046: * The path which we use to locate class (binary) files.
047: */
048: ClassPath binaryPath;
049:
050: /**
051: * The path name of the package.
052: */
053: String pkg;
054:
055: /**
056: * Create a package given a class path, and package name.
057: */
058: public Package(ClassPath path, Identifier pkg) throws IOException {
059: this (path, path, pkg);
060: }
061:
062: /**
063: * Create a package given a source path, binary path, and package
064: * name.
065: */
066: public Package(ClassPath sourcePath, ClassPath binaryPath,
067: Identifier pkg) throws IOException {
068: if (pkg.isInner())
069: pkg = Identifier.lookup(pkg.getQualifier(), pkg
070: .getFlatName());
071: this .sourcePath = sourcePath;
072: this .binaryPath = binaryPath;
073: this .pkg = pkg.toString().replace('.', File.separatorChar);
074: }
075:
076: /**
077: * Check if a class is defined in this package.
078: * (If it is an inner class name, it is assumed to exist
079: * only if its binary file exists. This is somewhat pessimistic.)
080: */
081: public boolean classExists(Identifier className) {
082: return getBinaryFile(className) != null || !className.isInner()
083: && getSourceFile(className) != null;
084: }
085:
086: /**
087: * Check if the package exists
088: */
089: public boolean exists() {
090: // Look for the directory on our binary path.
091: ClassFile dir = binaryPath.getDirectory(pkg);
092: if (dir != null && dir.isDirectory()) {
093: return true;
094: }
095:
096: if (sourcePath != binaryPath) {
097: // Look for the directory on our source path.
098: dir = sourcePath.getDirectory(pkg);
099: if (dir != null && dir.isDirectory()) {
100: return true;
101: }
102: }
103:
104: /* Accommodate ZIP files without CEN entries for directories
105: * (packages): look on class path for at least one binary
106: * file or one source file with the right package prefix
107: */
108: String prefix = pkg + File.separator;
109:
110: return binaryPath.getFiles(prefix, ".class").hasMoreElements()
111: || sourcePath.getFiles(prefix, ".java")
112: .hasMoreElements();
113: }
114:
115: private String makeName(String fileName) {
116: return pkg.equals("") ? fileName : pkg + File.separator
117: + fileName;
118: }
119:
120: /**
121: * Get the .class file of a class
122: */
123: public ClassFile getBinaryFile(Identifier className) {
124: className = Type.mangleInnerType(className);
125: String fileName = className.toString() + ".class";
126: return binaryPath.getFile(makeName(fileName));
127: }
128:
129: /**
130: * Get the .java file of a class
131: */
132: public ClassFile getSourceFile(Identifier className) {
133: // The source file of an inner class is that of its outer class.
134: className = className.getTopName();
135: String fileName = className.toString() + ".java";
136: return sourcePath.getFile(makeName(fileName));
137: }
138:
139: public ClassFile getSourceFile(String fileName) {
140: if (fileName.endsWith(".java")) {
141: return sourcePath.getFile(makeName(fileName));
142: }
143: return null;
144: }
145:
146: public Enumeration getSourceFiles() {
147: return sourcePath.getFiles(pkg, ".java");
148: }
149:
150: public Enumeration getBinaryFiles() {
151: return binaryPath.getFiles(pkg, ".class");
152: }
153:
154: public String toString() {
155: if (pkg.equals("")) {
156: return "unnamed package";
157: }
158: return "package " + pkg;
159: }
160: }
|