Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/java/Package.java
38918 views
/*1* Copyright (c) 1995, 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.java;2627import java.util.Enumeration;28import java.io.File;29import java.io.IOException;3031/**32* This class is used to represent the classes in a package.33*34* WARNING: The contents of this source file are not part of any35* supported API. Code that depends on them does so at its own risk:36* they are subject to change or removal without notice.37*/38public39class Package {40/**41* The path which we use to locate source files.42*/43ClassPath sourcePath;4445/**46* The path which we use to locate class (binary) files.47*/48ClassPath binaryPath;4950/**51* The path name of the package.52*/53String pkg;5455/**56* Create a package given a class path, and package name.57*/58public Package(ClassPath path, Identifier pkg) throws IOException {59this(path, path, pkg);60}6162/**63* Create a package given a source path, binary path, and package64* name.65*/66public Package(ClassPath sourcePath,67ClassPath binaryPath,68Identifier pkg)69throws IOException {70if (pkg.isInner())71pkg = Identifier.lookup(pkg.getQualifier(), pkg.getFlatName());72this.sourcePath = sourcePath;73this.binaryPath = binaryPath;74this.pkg = pkg.toString().replace('.', File.separatorChar);75}7677/**78* Check if a class is defined in this package.79* (If it is an inner class name, it is assumed to exist80* only if its binary file exists. This is somewhat pessimistic.)81*/82public boolean classExists(Identifier className) {83return getBinaryFile(className) != null ||84!className.isInner() &&85getSourceFile(className) != null;86}8788/**89* Check if the package exists90*/91public boolean exists() {92// Look for the directory on our binary path.93ClassFile dir = binaryPath.getDirectory(pkg);94if (dir != null && dir.isDirectory()) {95return true;96}9798if (sourcePath != binaryPath) {99// Look for the directory on our source path.100dir = sourcePath.getDirectory(pkg);101if (dir != null && dir.isDirectory()) {102return true;103}104}105106/* Accommodate ZIP files without CEN entries for directories107* (packages): look on class path for at least one binary108* file or one source file with the right package prefix109*/110String prefix = pkg + File.separator;111112return binaryPath.getFiles(prefix, ".class").hasMoreElements()113|| sourcePath.getFiles(prefix, ".java").hasMoreElements();114}115116private String makeName(String fileName) {117return pkg.equals("") ? fileName : pkg + File.separator + fileName;118}119120/**121* Get the .class file of a class122*/123public ClassFile getBinaryFile(Identifier className) {124className = Type.mangleInnerType(className);125String fileName = className.toString() + ".class";126return binaryPath.getFile(makeName(fileName));127}128129/**130* Get the .java file of a class131*/132public ClassFile getSourceFile(Identifier className) {133// The source file of an inner class is that of its outer class.134className = className.getTopName();135String fileName = className.toString() + ".java";136return sourcePath.getFile(makeName(fileName));137}138139public ClassFile getSourceFile(String fileName) {140if (fileName.endsWith(".java")) {141return sourcePath.getFile(makeName(fileName));142}143return null;144}145146public Enumeration getSourceFiles() {147return sourcePath.getFiles(pkg, ".java");148}149150public Enumeration getBinaryFiles() {151return binaryPath.getFiles(pkg, ".class");152}153154public String toString() {155if (pkg.equals("")) {156return "unnamed package";157}158return "package " + pkg;159}160}161162163