src/share/classes/java/lang/Package.java

Print this page




 303         }
 304     }
 305 
 306     /**
 307      * Get the package for the specified class.
 308      * The class's class loader is used to find the package instance
 309      * corresponding to the specified class. If the class loader
 310      * is the bootstrap class loader, which may be represented by
 311      * {@code null} in some implementations, then the set of packages
 312      * loaded by the bootstrap class loader is searched to find the package.
 313      * <p>
 314      * Packages have attributes for versions and specifications only
 315      * if the class loader created the package
 316      * instance with the appropriate attributes. Typically those
 317      * attributes are defined in the manifests that accompany
 318      * the classes.
 319      *
 320      * @param class the class to get the package of.
 321      * @return the package of the class. It may be null if no package
 322      *          information is available from the archive or codebase.  */
 323     static Package getPackage(Class c) {
 324         String name = c.getName();
 325         int i = name.lastIndexOf('.');
 326         if (i != -1) {
 327             name = name.substring(0, i);
 328             ClassLoader cl = c.getClassLoader();
 329             if (cl != null) {
 330                 return cl.getPackage(name);
 331             } else {
 332                 return getSystemPackage(name);
 333             }
 334         } else {
 335             return null;
 336         }
 337     }
 338 
 339     /**
 340      * Return the hash code computed from the package name.
 341      * @return the hash code computed from the package name.
 342      */
 343     public int hashCode(){




 303         }
 304     }
 305 
 306     /**
 307      * Get the package for the specified class.
 308      * The class's class loader is used to find the package instance
 309      * corresponding to the specified class. If the class loader
 310      * is the bootstrap class loader, which may be represented by
 311      * {@code null} in some implementations, then the set of packages
 312      * loaded by the bootstrap class loader is searched to find the package.
 313      * <p>
 314      * Packages have attributes for versions and specifications only
 315      * if the class loader created the package
 316      * instance with the appropriate attributes. Typically those
 317      * attributes are defined in the manifests that accompany
 318      * the classes.
 319      *
 320      * @param class the class to get the package of.
 321      * @return the package of the class. It may be null if no package
 322      *          information is available from the archive or codebase.  */
 323     static Package getPackage(Class<?> c) {
 324         String name = c.getName();
 325         int i = name.lastIndexOf('.');
 326         if (i != -1) {
 327             name = name.substring(0, i);
 328             ClassLoader cl = c.getClassLoader();
 329             if (cl != null) {
 330                 return cl.getPackage(name);
 331             } else {
 332                 return getSystemPackage(name);
 333             }
 334         } else {
 335             return null;
 336         }
 337     }
 338 
 339     /**
 340      * Return the hash code computed from the package name.
 341      * @return the hash code computed from the package name.
 342      */
 343     public int hashCode(){