0N/A/*
3909N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.lang;
0N/A
0N/Aimport java.io.InputStream;
0N/Aimport java.util.Enumeration;
0N/A
0N/Aimport java.util.StringTokenizer;
0N/Aimport java.io.File;
0N/Aimport java.io.FileInputStream;
0N/Aimport java.io.FileNotFoundException;
0N/Aimport java.io.IOException;
0N/Aimport java.net.URL;
0N/Aimport java.net.MalformedURLException;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/A
0N/Aimport java.util.jar.JarInputStream;
0N/Aimport java.util.jar.Manifest;
0N/Aimport java.util.jar.Attributes;
0N/Aimport java.util.jar.Attributes.Name;
0N/Aimport java.util.jar.JarException;
0N/Aimport java.util.Map;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Iterator;
0N/A
6338N/Aimport java.lang.annotation.Annotation;
0N/Aimport sun.net.www.ParseUtil;
6338N/Aimport sun.reflect.CallerSensitive;
6338N/Aimport sun.reflect.Reflection;
0N/A
0N/A/**
0N/A * {@code Package} objects contain version information
0N/A * about the implementation and specification of a Java package.
0N/A * This versioning information is retrieved and made available
0N/A * by the {@link ClassLoader} instance that
0N/A * loaded the class(es). Typically, it is stored in the manifest that is
0N/A * distributed with the classes.
0N/A *
0N/A * <p>The set of classes that make up the package may implement a
0N/A * particular specification and if so the specification title, version number,
0N/A * and vendor strings identify that specification.
0N/A * An application can ask if the package is
0N/A * compatible with a particular version, see the {@link
0N/A * #isCompatibleWith isCompatibleWith}
0N/A * method for details.
0N/A *
0N/A * <p>Specification version numbers use a syntax that consists of nonnegative
0N/A * decimal integers separated by periods ".", for example "2.0" or
0N/A * "1.2.3.4.5.6.7". This allows an extensible number to be used to represent
0N/A * major, minor, micro, etc. versions. The version specification is described
0N/A * by the following formal grammar:
0N/A * <blockquote>
0N/A * <dl>
0N/A * <dt><i>SpecificationVersion:
0N/A * <dd>Digits RefinedVersion<sub>opt</sub></i>
0N/A
0N/A * <p><dt><i>RefinedVersion:</i>
0N/A * <dd>{@code .} <i>Digits</i>
0N/A * <dd>{@code .} <i>Digits RefinedVersion</i>
0N/A *
0N/A * <p><dt><i>Digits:
0N/A * <dd>Digit
0N/A * <dd>Digits</i>
0N/A *
0N/A * <p><dt><i>Digit:</i>
0N/A * <dd>any character for which {@link Character#isDigit} returns {@code true},
0N/A * e.g. 0, 1, 2, ...
0N/A * </dl>
0N/A * </blockquote>
0N/A *
0N/A * <p>The implementation title, version, and vendor strings identify an
0N/A * implementation and are made available conveniently to enable accurate
0N/A * reporting of the packages involved when a problem occurs. The contents
0N/A * all three implementation strings are vendor specific. The
0N/A * implementation version strings have no specified syntax and should
0N/A * only be compared for equality with desired version identifiers.
0N/A *
0N/A * <p>Within each {@code ClassLoader} instance all classes from the same
0N/A * java package have the same Package object. The static methods allow a package
0N/A * to be found by name or the set of all packages known to the current class
0N/A * loader to be found.
0N/A *
0N/A * @see ClassLoader#definePackage
0N/A */
0N/Apublic class Package implements java.lang.reflect.AnnotatedElement {
0N/A /**
0N/A * Return the name of this package.
0N/A *
4008N/A * @return The fully-qualified name of this package as defined in section 6.5.3 of
4008N/A * <cite>The Java&trade; Language Specification</cite>,
4008N/A * for example, {@code java.lang}
0N/A */
0N/A public String getName() {
0N/A return pkgName;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Return the title of the specification that this package implements.
0N/A * @return the specification title, null is returned if it is not known.
0N/A */
0N/A public String getSpecificationTitle() {
0N/A return specTitle;
0N/A }
0N/A
0N/A /**
0N/A * Returns the version number of the specification
0N/A * that this package implements.
0N/A * This version string must be a sequence of nonnegative decimal
0N/A * integers separated by "."'s and may have leading zeros.
0N/A * When version strings are compared the most significant
0N/A * numbers are compared.
0N/A * @return the specification version, null is returned if it is not known.
0N/A */
0N/A public String getSpecificationVersion() {
0N/A return specVersion;
0N/A }
0N/A
0N/A /**
0N/A * Return the name of the organization, vendor,
0N/A * or company that owns and maintains the specification
0N/A * of the classes that implement this package.
0N/A * @return the specification vendor, null is returned if it is not known.
0N/A */
0N/A public String getSpecificationVendor() {
0N/A return specVendor;
0N/A }
0N/A
0N/A /**
0N/A * Return the title of this package.
0N/A * @return the title of the implementation, null is returned if it is not known.
0N/A */
0N/A public String getImplementationTitle() {
0N/A return implTitle;
0N/A }
0N/A
0N/A /**
0N/A * Return the version of this implementation. It consists of any string
0N/A * assigned by the vendor of this implementation and does
0N/A * not have any particular syntax specified or expected by the Java
0N/A * runtime. It may be compared for equality with other
0N/A * package version strings used for this implementation
0N/A * by this vendor for this package.
0N/A * @return the version of the implementation, null is returned if it is not known.
0N/A */
0N/A public String getImplementationVersion() {
0N/A return implVersion;
0N/A }
0N/A
0N/A /**
0N/A * Returns the name of the organization,
0N/A * vendor or company that provided this implementation.
0N/A * @return the vendor that implemented this package..
0N/A */
0N/A public String getImplementationVendor() {
0N/A return implVendor;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if this package is sealed.
0N/A *
0N/A * @return true if the package is sealed, false otherwise
0N/A */
0N/A public boolean isSealed() {
0N/A return sealBase != null;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if this package is sealed with respect to the specified
0N/A * code source url.
0N/A *
0N/A * @param url the code source url
0N/A * @return true if this package is sealed with respect to url
0N/A */
0N/A public boolean isSealed(URL url) {
0N/A return url.equals(sealBase);
0N/A }
0N/A
0N/A /**
0N/A * Compare this package's specification version with a
0N/A * desired version. It returns true if
0N/A * this packages specification version number is greater than or equal
0N/A * to the desired version number. <p>
0N/A *
0N/A * Version numbers are compared by sequentially comparing corresponding
0N/A * components of the desired and specification strings.
0N/A * Each component is converted as a decimal integer and the values
0N/A * compared.
0N/A * If the specification value is greater than the desired
0N/A * value true is returned. If the value is less false is returned.
0N/A * If the values are equal the period is skipped and the next pair of
0N/A * components is compared.
0N/A *
0N/A * @param desired the version string of the desired version.
0N/A * @return true if this package's version number is greater
0N/A * than or equal to the desired version number
0N/A *
0N/A * @exception NumberFormatException if the desired or current version
0N/A * is not of the correct dotted form.
0N/A */
0N/A public boolean isCompatibleWith(String desired)
0N/A throws NumberFormatException
0N/A {
0N/A if (specVersion == null || specVersion.length() < 1) {
0N/A throw new NumberFormatException("Empty version string");
0N/A }
0N/A
0N/A String [] sa = specVersion.split("\\.", -1);
0N/A int [] si = new int[sa.length];
0N/A for (int i = 0; i < sa.length; i++) {
0N/A si[i] = Integer.parseInt(sa[i]);
0N/A if (si[i] < 0)
0N/A throw NumberFormatException.forInputString("" + si[i]);
0N/A }
0N/A
0N/A String [] da = desired.split("\\.", -1);
0N/A int [] di = new int[da.length];
0N/A for (int i = 0; i < da.length; i++) {
0N/A di[i] = Integer.parseInt(da[i]);
0N/A if (di[i] < 0)
0N/A throw NumberFormatException.forInputString("" + di[i]);
0N/A }
0N/A
0N/A int len = Math.max(di.length, si.length);
0N/A for (int i = 0; i < len; i++) {
0N/A int d = (i < di.length ? di[i] : 0);
0N/A int s = (i < si.length ? si[i] : 0);
0N/A if (s < d)
0N/A return false;
0N/A if (s > d)
0N/A return true;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Find a package by name in the callers {@code ClassLoader} instance.
0N/A * The callers {@code ClassLoader} instance is used to find the package
0N/A * instance corresponding to the named class. If the callers
0N/A * {@code ClassLoader} instance is null then the set of packages loaded
0N/A * by the system {@code ClassLoader} instance is searched to find the
0N/A * named package. <p>
0N/A *
0N/A * Packages have attributes for versions and specifications only if the class
0N/A * loader created the package instance with the appropriate attributes. Typically,
0N/A * those attributes are defined in the manifests that accompany the classes.
0N/A *
0N/A * @param name a package name, for example, java.lang.
0N/A * @return the package of the requested name. It may be null if no package
0N/A * information is available from the archive or codebase.
0N/A */
6338N/A @CallerSensitive
0N/A public static Package getPackage(String name) {
6338N/A ClassLoader l = ClassLoader.getClassLoader(Reflection.getCallerClass());
0N/A if (l != null) {
0N/A return l.getPackage(name);
0N/A } else {
0N/A return getSystemPackage(name);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get all the packages currently known for the caller's {@code ClassLoader}
0N/A * instance. Those packages correspond to classes loaded via or accessible by
0N/A * name to that {@code ClassLoader} instance. If the caller's
0N/A * {@code ClassLoader} instance is the bootstrap {@code ClassLoader}
0N/A * instance, which may be represented by {@code null} in some implementations,
0N/A * only packages corresponding to classes loaded by the bootstrap
0N/A * {@code ClassLoader} instance will be returned.
0N/A *
0N/A * @return a new array of packages known to the callers {@code ClassLoader}
0N/A * instance. An zero length array is returned if none are known.
0N/A */
6338N/A @CallerSensitive
0N/A public static Package[] getPackages() {
6338N/A ClassLoader l = ClassLoader.getClassLoader(Reflection.getCallerClass());
0N/A if (l != null) {
0N/A return l.getPackages();
0N/A } else {
0N/A return getSystemPackages();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the package for the specified class.
0N/A * The class's class loader is used to find the package instance
0N/A * corresponding to the specified class. If the class loader
0N/A * is the bootstrap class loader, which may be represented by
0N/A * {@code null} in some implementations, then the set of packages
0N/A * loaded by the bootstrap class loader is searched to find the package.
0N/A * <p>
0N/A * Packages have attributes for versions and specifications only
0N/A * if the class loader created the package
0N/A * instance with the appropriate attributes. Typically those
0N/A * attributes are defined in the manifests that accompany
0N/A * the classes.
0N/A *
0N/A * @param class the class to get the package of.
0N/A * @return the package of the class. It may be null if no package
0N/A * information is available from the archive or codebase. */
1717N/A static Package getPackage(Class<?> c) {
0N/A String name = c.getName();
0N/A int i = name.lastIndexOf('.');
0N/A if (i != -1) {
0N/A name = name.substring(0, i);
0N/A ClassLoader cl = c.getClassLoader();
0N/A if (cl != null) {
0N/A return cl.getPackage(name);
0N/A } else {
0N/A return getSystemPackage(name);
0N/A }
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return the hash code computed from the package name.
0N/A * @return the hash code computed from the package name.
0N/A */
0N/A public int hashCode(){
0N/A return pkgName.hashCode();
0N/A }
0N/A
0N/A /**
0N/A * Returns the string representation of this Package.
0N/A * Its value is the string "package " and the package name.
0N/A * If the package title is defined it is appended.
0N/A * If the package version is defined it is appended.
0N/A * @return the string representation of the package.
0N/A */
0N/A public String toString() {
0N/A String spec = specTitle;
0N/A String ver = specVersion;
0N/A if (spec != null && spec.length() > 0)
0N/A spec = ", " + spec;
0N/A else
0N/A spec = "";
0N/A if (ver != null && ver.length() > 0)
0N/A ver = ", version " + ver;
0N/A else
0N/A ver = "";
0N/A return "package " + pkgName + spec + ver;
0N/A }
0N/A
0N/A private Class<?> getPackageInfo() {
0N/A if (packageInfo == null) {
0N/A try {
0N/A packageInfo = Class.forName(pkgName + ".package-info", false, loader);
0N/A } catch (ClassNotFoundException ex) {
0N/A // store a proxy for the package info that has no annotations
0N/A class PackageInfoProxy {}
0N/A packageInfo = PackageInfoProxy.class;
0N/A }
0N/A }
0N/A return packageInfo;
0N/A }
0N/A
0N/A /**
0N/A * @throws NullPointerException {@inheritDoc}
0N/A * @since 1.5
0N/A */
0N/A public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
0N/A return getPackageInfo().getAnnotation(annotationClass);
0N/A }
0N/A
0N/A /**
0N/A * @throws NullPointerException {@inheritDoc}
0N/A * @since 1.5
0N/A */
0N/A public boolean isAnnotationPresent(
0N/A Class<? extends Annotation> annotationClass) {
0N/A return getPackageInfo().isAnnotationPresent(annotationClass);
0N/A }
0N/A
0N/A /**
0N/A * @since 1.5
0N/A */
0N/A public Annotation[] getAnnotations() {
0N/A return getPackageInfo().getAnnotations();
0N/A }
0N/A
0N/A /**
0N/A * @since 1.5
0N/A */
0N/A public Annotation[] getDeclaredAnnotations() {
0N/A return getPackageInfo().getDeclaredAnnotations();
0N/A }
0N/A
0N/A /**
0N/A * Construct a package instance with the specified version
0N/A * information.
0N/A * @param pkgName the name of the package
0N/A * @param spectitle the title of the specification
0N/A * @param specversion the version of the specification
0N/A * @param specvendor the organization that maintains the specification
0N/A * @param impltitle the title of the implementation
0N/A * @param implversion the version of the implementation
0N/A * @param implvendor the organization that maintains the implementation
0N/A * @return a new package for containing the specified information.
0N/A */
0N/A Package(String name,
0N/A String spectitle, String specversion, String specvendor,
0N/A String impltitle, String implversion, String implvendor,
0N/A URL sealbase, ClassLoader loader)
0N/A {
0N/A pkgName = name;
0N/A implTitle = impltitle;
0N/A implVersion = implversion;
0N/A implVendor = implvendor;
0N/A specTitle = spectitle;
0N/A specVersion = specversion;
0N/A specVendor = specvendor;
0N/A sealBase = sealbase;
0N/A this.loader = loader;
0N/A }
0N/A
0N/A /*
0N/A * Construct a package using the attributes from the specified manifest.
0N/A *
0N/A * @param name the package name
0N/A * @param man the optional manifest for the package
0N/A * @param url the optional code source url for the package
0N/A */
0N/A private Package(String name, Manifest man, URL url, ClassLoader loader) {
0N/A String path = name.replace('.', '/').concat("/");
0N/A String sealed = null;
0N/A String specTitle= null;
0N/A String specVersion= null;
0N/A String specVendor= null;
0N/A String implTitle= null;
0N/A String implVersion= null;
0N/A String implVendor= null;
0N/A URL sealBase= null;
0N/A Attributes attr = man.getAttributes(path);
0N/A if (attr != null) {
0N/A specTitle = attr.getValue(Name.SPECIFICATION_TITLE);
0N/A specVersion = attr.getValue(Name.SPECIFICATION_VERSION);
0N/A specVendor = attr.getValue(Name.SPECIFICATION_VENDOR);
0N/A implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE);
0N/A implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION);
0N/A implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR);
0N/A sealed = attr.getValue(Name.SEALED);
0N/A }
0N/A attr = man.getMainAttributes();
0N/A if (attr != null) {
0N/A if (specTitle == null) {
0N/A specTitle = attr.getValue(Name.SPECIFICATION_TITLE);
0N/A }
0N/A if (specVersion == null) {
0N/A specVersion = attr.getValue(Name.SPECIFICATION_VERSION);
0N/A }
0N/A if (specVendor == null) {
0N/A specVendor = attr.getValue(Name.SPECIFICATION_VENDOR);
0N/A }
0N/A if (implTitle == null) {
0N/A implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE);
0N/A }
0N/A if (implVersion == null) {
0N/A implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION);
0N/A }
0N/A if (implVendor == null) {
0N/A implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR);
0N/A }
0N/A if (sealed == null) {
0N/A sealed = attr.getValue(Name.SEALED);
0N/A }
0N/A }
0N/A if ("true".equalsIgnoreCase(sealed)) {
0N/A sealBase = url;
0N/A }
0N/A pkgName = name;
0N/A this.specTitle = specTitle;
0N/A this.specVersion = specVersion;
0N/A this.specVendor = specVendor;
0N/A this.implTitle = implTitle;
0N/A this.implVersion = implVersion;
0N/A this.implVendor = implVendor;
0N/A this.sealBase = sealBase;
0N/A this.loader = loader;
0N/A }
0N/A
0N/A /*
0N/A * Returns the loaded system package for the specified name.
0N/A */
0N/A static Package getSystemPackage(String name) {
0N/A synchronized (pkgs) {
28N/A Package pkg = pkgs.get(name);
0N/A if (pkg == null) {
0N/A name = name.replace('.', '/').concat("/");
0N/A String fn = getSystemPackage0(name);
0N/A if (fn != null) {
0N/A pkg = defineSystemPackage(name, fn);
0N/A }
0N/A }
0N/A return pkg;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Return an array of loaded system packages.
0N/A */
0N/A static Package[] getSystemPackages() {
0N/A // First, update the system package map with new package names
0N/A String[] names = getSystemPackages0();
0N/A synchronized (pkgs) {
0N/A for (int i = 0; i < names.length; i++) {
0N/A defineSystemPackage(names[i], getSystemPackage0(names[i]));
0N/A }
28N/A return pkgs.values().toArray(new Package[pkgs.size()]);
0N/A }
0N/A }
0N/A
0N/A private static Package defineSystemPackage(final String iname,
0N/A final String fn)
0N/A {
28N/A return AccessController.doPrivileged(new PrivilegedAction<Package>() {
28N/A public Package run() {
0N/A String name = iname;
0N/A // Get the cached code source url for the file name
28N/A URL url = urls.get(fn);
0N/A if (url == null) {
0N/A // URL not found, so create one
0N/A File file = new File(fn);
0N/A try {
0N/A url = ParseUtil.fileToEncodedURL(file);
0N/A } catch (MalformedURLException e) {
0N/A }
0N/A if (url != null) {
0N/A urls.put(fn, url);
0N/A // If loading a JAR file, then also cache the manifest
0N/A if (file.isFile()) {
0N/A mans.put(fn, loadManifest(fn));
0N/A }
0N/A }
0N/A }
0N/A // Convert to "."-separated package name
0N/A name = name.substring(0, name.length() - 1).replace('/', '.');
0N/A Package pkg;
28N/A Manifest man = mans.get(fn);
0N/A if (man != null) {
0N/A pkg = new Package(name, man, url, null);
0N/A } else {
0N/A pkg = new Package(name, null, null, null,
0N/A null, null, null, null, null);
0N/A }
0N/A pkgs.put(name, pkg);
0N/A return pkg;
0N/A }
0N/A });
0N/A }
0N/A
0N/A /*
0N/A * Returns the Manifest for the specified JAR file name.
0N/A */
0N/A private static Manifest loadManifest(String fn) {
3646N/A try (FileInputStream fis = new FileInputStream(fn);
3646N/A JarInputStream jis = new JarInputStream(fis, false))
3646N/A {
3646N/A return jis.getManifest();
0N/A } catch (IOException e) {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A // The map of loaded system packages
3323N/A private static Map<String, Package> pkgs = new HashMap<>(31);
0N/A
0N/A // Maps each directory or zip file name to its corresponding url
3323N/A private static Map<String, URL> urls = new HashMap<>(10);
0N/A
0N/A // Maps each code source url for a jar file to its manifest
3323N/A private static Map<String, Manifest> mans = new HashMap<>(10);
0N/A
0N/A private static native String getSystemPackage0(String name);
0N/A private static native String[] getSystemPackages0();
0N/A
0N/A /*
0N/A * Private storage for the package name and attributes.
0N/A */
0N/A private final String pkgName;
0N/A private final String specTitle;
0N/A private final String specVersion;
0N/A private final String specVendor;
0N/A private final String implTitle;
0N/A private final String implVersion;
0N/A private final String implVendor;
0N/A private final URL sealBase;
0N/A private transient final ClassLoader loader;
0N/A private transient Class packageInfo;
0N/A}