jdk/src/share/classes/java/lang/Package.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8543 e5ec12a932da
child 9275 1df1f7dfab7f
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8543
diff changeset
     2
 * Copyright (c) 1997, 2011, 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: 3959
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: 3959
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: 3959
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3959
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 java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.jar.JarInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.jar.Manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.jar.Attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.jar.Attributes.Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.jar.JarException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.lang.annotation.Annotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * {@code Package} objects contain version information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * about the implementation and specification of a Java package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * This versioning information is retrieved and made available
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * by the {@link ClassLoader} instance that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * loaded the class(es).  Typically, it is stored in the manifest that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * distributed with the classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>The set of classes that make up the package may implement a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * particular specification and if so the specification title, version number,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * and vendor strings identify that specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * An application can ask if the package is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * compatible with a particular version, see the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * #isCompatibleWith isCompatibleWith}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * method for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <p>Specification version numbers use a syntax that consists of nonnegative
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * decimal integers separated by periods ".", for example "2.0" or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * "1.2.3.4.5.6.7".  This allows an extensible number to be used to represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * major, minor, micro, etc. versions.  The version specification is described
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * by the following formal grammar:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <dt><i>SpecificationVersion:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <dd>Digits RefinedVersion<sub>opt</sub></i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <p><dt><i>RefinedVersion:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <dd>{@code .} <i>Digits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <dd>{@code .} <i>Digits RefinedVersion</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <p><dt><i>Digits:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <dd>Digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <dd>Digits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <p><dt><i>Digit:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <dd>any character for which {@link Character#isDigit} returns {@code true},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * e.g. 0, 1, 2, ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p>The implementation title, version, and vendor strings identify an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * implementation and are made available conveniently to enable accurate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * reporting of the packages involved when a problem occurs. The contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * all three implementation strings are vendor specific. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * implementation version strings have no specified syntax and should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * only be compared for equality with desired version identifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <p>Within each {@code ClassLoader} instance all classes from the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * java package have the same Package object.  The static methods allow a package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * to be found by name or the set of all packages known to the current class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * loader to be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * @see ClassLoader#definePackage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
public class Package implements java.lang.reflect.AnnotatedElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Return the name of this package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @return  The fully-qualified name of this package as defined in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *          <em>Java Language Specification, Third Edition</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *          <a href="http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.5.3">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *          &sect;6.5.3</a>, for example, {@code java.lang}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return pkgName;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Return the title of the specification that this package implements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @return the specification title, null is returned if it is not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public String getSpecificationTitle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return specTitle;
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
     * Returns the version number of the specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * that this package implements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * This version string must be a sequence of nonnegative decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * integers separated by "."'s and may have leading zeros.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * When version strings are compared the most significant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * numbers are compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return the specification version, null is returned if it is not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public String getSpecificationVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return specVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Return the name of the organization, vendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * or company that owns and maintains the specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * of the classes that implement this package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return the specification vendor, null is returned if it is not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public String getSpecificationVendor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return specVendor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Return the title of this package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @return the title of the implementation, null is returned if it is not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public String getImplementationTitle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return implTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Return the version of this implementation. It consists of any string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * assigned by the vendor of this implementation and does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * not have any particular syntax specified or expected by the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * runtime. It may be compared for equality with other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * package version strings used for this implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * by this vendor for this package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @return the version of the implementation, null is returned if it is not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public String getImplementationVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return implVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Returns the name of the organization,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * vendor or company that provided this implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @return the vendor that implemented this package..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public String getImplementationVendor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return implVendor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Returns true if this package is sealed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @return true if the package is sealed, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public boolean isSealed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return sealBase != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Returns true if this package is sealed with respect to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * code source url.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param url the code source url
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @return true if this package is sealed with respect to url
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public boolean isSealed(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return url.equals(sealBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Compare this package's specification version with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * desired version. It returns true if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * this packages specification version number is greater than or equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * to the desired version number. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Version numbers are compared by sequentially comparing corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * components of the desired and specification strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Each component is converted as a decimal integer and the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * If the specification value is greater than the desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * value true is returned. If the value is less false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * If the values are equal the period is skipped and the next pair of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * components is compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param desired the version string of the desired version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @return true if this package's version number is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *          than or equal to the desired version number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @exception NumberFormatException if the desired or current version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *          is not of the correct dotted form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public boolean isCompatibleWith(String desired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        throws NumberFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (specVersion == null || specVersion.length() < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throw new NumberFormatException("Empty version string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        String [] sa = specVersion.split("\\.", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        int [] si = new int[sa.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        for (int i = 0; i < sa.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            si[i] = Integer.parseInt(sa[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (si[i] < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                throw NumberFormatException.forInputString("" + si[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        String [] da = desired.split("\\.", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        int [] di = new int[da.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        for (int i = 0; i < da.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            di[i] = Integer.parseInt(da[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            if (di[i] < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                throw NumberFormatException.forInputString("" + di[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int len = Math.max(di.length, si.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            int d = (i < di.length ? di[i] : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            int s = (i < si.length ? si[i] : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (s < d)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if (s > d)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Find a package by name in the callers {@code ClassLoader} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * The callers {@code ClassLoader} instance is used to find the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * instance corresponding to the named class. If the callers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * {@code ClassLoader} instance is null then the set of packages loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * by the system {@code ClassLoader} instance is searched to find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * named package. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Packages have attributes for versions and specifications only if the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * loader created the package instance with the appropriate attributes. Typically,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * those attributes are defined in the manifests that accompany the classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param name a package name, for example, java.lang.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @return the package of the requested name. It may be null if no package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *          information is available from the archive or codebase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public static Package getPackage(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        ClassLoader l = ClassLoader.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return l.getPackage(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return getSystemPackage(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Get all the packages currently known for the caller's {@code ClassLoader}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * instance.  Those packages correspond to classes loaded via or accessible by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * name to that {@code ClassLoader} instance.  If the caller's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * {@code ClassLoader} instance is the bootstrap {@code ClassLoader}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * instance, which may be represented by {@code null} in some implementations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * only packages corresponding to classes loaded by the bootstrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * {@code ClassLoader} instance will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @return a new array of packages known to the callers {@code ClassLoader}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * instance.  An zero length array is returned if none are known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public static Package[] getPackages() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        ClassLoader l = ClassLoader.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return l.getPackages();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return getSystemPackages();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Get the package for the specified class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * The class's class loader is used to find the package instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * corresponding to the specified class. If the class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * is the bootstrap class loader, which may be represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * {@code null} in some implementations, then the set of packages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * loaded by the bootstrap class loader is searched to find the package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Packages have attributes for versions and specifications only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * if the class loader created the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * instance with the appropriate attributes. Typically those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * attributes are defined in the manifests that accompany
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * the classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param class the class to get the package of.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @return the package of the class. It may be null if no package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *          information is available from the archive or codebase.  */
3959
05a07c0a273b 5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents: 715
diff changeset
   323
    static Package getPackage(Class<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        String name = c.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            name = name.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            ClassLoader cl = c.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                return cl.getPackage(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                return getSystemPackage(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Return the hash code computed from the package name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @return the hash code computed from the package name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public int hashCode(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return pkgName.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Returns the string representation of this Package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Its value is the string "package " and the package name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * If the package title is defined it is appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * If the package version is defined it is appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @return the string representation of the package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        String spec = specTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        String ver =  specVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (spec != null && spec.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            spec = ", " + spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            spec = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (ver != null && ver.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            ver = ", version " + ver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            ver = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return "package " + pkgName + spec + ver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    private Class<?> getPackageInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (packageInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                packageInfo = Class.forName(pkgName + ".package-info", false, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            } catch (ClassNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                // store a proxy for the package info that has no annotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                class PackageInfoProxy {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                packageInfo = PackageInfoProxy.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return packageInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return getPackageInfo().getAnnotation(annotationClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public boolean isAnnotationPresent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        Class<? extends Annotation> annotationClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return getPackageInfo().isAnnotationPresent(annotationClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public Annotation[] getAnnotations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return getPackageInfo().getAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public Annotation[] getDeclaredAnnotations()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return getPackageInfo().getDeclaredAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * Construct a package instance with the specified version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @param pkgName the name of the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @param spectitle the title of the specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @param specversion the version of the specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @param specvendor the organization that maintains the specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @param impltitle the title of the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @param implversion the version of the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @param implvendor the organization that maintains the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @return a new package for containing the specified information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    Package(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            String spectitle, String specversion, String specvendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            String impltitle, String implversion, String implvendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            URL sealbase, ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        pkgName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        implTitle = impltitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        implVersion = implversion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        implVendor = implvendor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        specTitle = spectitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        specVersion = specversion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        specVendor = specvendor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        sealBase = sealbase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        this.loader = loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * Construct a package using the attributes from the specified manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @param name the package name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @param man the optional manifest for the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @param url the optional code source url for the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    private Package(String name, Manifest man, URL url, ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        String path = name.replace('.', '/').concat("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        String sealed = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        String specTitle= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        String specVersion= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        String specVendor= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        String implTitle= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        String implVersion= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        String implVendor= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        URL sealBase= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        Attributes attr = man.getAttributes(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            specTitle   = attr.getValue(Name.SPECIFICATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            specVersion = attr.getValue(Name.SPECIFICATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            specVendor  = attr.getValue(Name.SPECIFICATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            implTitle   = attr.getValue(Name.IMPLEMENTATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            implVendor  = attr.getValue(Name.IMPLEMENTATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            sealed      = attr.getValue(Name.SEALED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        attr = man.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (specTitle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                specTitle = attr.getValue(Name.SPECIFICATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            if (specVersion == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                specVersion = attr.getValue(Name.SPECIFICATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            if (specVendor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                specVendor = attr.getValue(Name.SPECIFICATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if (implTitle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            if (implVersion == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            if (implVendor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            if (sealed == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                sealed = attr.getValue(Name.SEALED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if ("true".equalsIgnoreCase(sealed)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            sealBase = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        pkgName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        this.specTitle = specTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        this.specVersion = specVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        this.specVendor = specVendor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        this.implTitle = implTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        this.implVersion = implVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        this.implVendor = implVendor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        this.sealBase = sealBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        this.loader = loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Returns the loaded system package for the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    static Package getSystemPackage(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        synchronized (pkgs) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   510
            Package pkg = pkgs.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            if (pkg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                name = name.replace('.', '/').concat("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                String fn = getSystemPackage0(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                if (fn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    pkg = defineSystemPackage(name, fn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            return pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Return an array of loaded system packages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    static Package[] getSystemPackages() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        // First, update the system package map with new package names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        String[] names = getSystemPackages0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        synchronized (pkgs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                defineSystemPackage(names[i], getSystemPackage0(names[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   532
            return pkgs.values().toArray(new Package[pkgs.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    private static Package defineSystemPackage(final String iname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                                               final String fn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   539
        return AccessController.doPrivileged(new PrivilegedAction<Package>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   540
            public Package run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                String name = iname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                // Get the cached code source url for the file name
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   543
                URL url = urls.get(fn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                if (url == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    // URL not found, so create one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    File file = new File(fn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                        url = ParseUtil.fileToEncodedURL(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                        urls.put(fn, url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                        // If loading a JAR file, then also cache the manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                        if (file.isFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                            mans.put(fn, loadManifest(fn));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                // Convert to "."-separated package name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                name = name.substring(0, name.length() - 1).replace('/', '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                Package pkg;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   562
                Manifest man = mans.get(fn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                if (man != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    pkg = new Package(name, man, url, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    pkg = new Package(name, null, null, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                                      null, null, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                pkgs.put(name, pkg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                return pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * Returns the Manifest for the specified JAR file name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    private static Manifest loadManifest(String fn) {
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   579
        try (FileInputStream fis = new FileInputStream(fn);
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   580
             JarInputStream jis = new JarInputStream(fis, false))
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   581
        {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   582
            return jis.getManifest();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    // The map of loaded system packages
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   589
    private static Map<String, Package> pkgs = new HashMap<>(31);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    // Maps each directory or zip file name to its corresponding url
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   592
    private static Map<String, URL> urls = new HashMap<>(10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    // Maps each code source url for a jar file to its manifest
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   595
    private static Map<String, Manifest> mans = new HashMap<>(10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    private static native String getSystemPackage0(String name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    private static native String[] getSystemPackages0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * Private storage for the package name and attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    private final String pkgName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    private final String specTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    private final String specVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    private final String specVendor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    private final String implTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    private final String implVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    private final String implVendor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    private final URL sealBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    private transient final ClassLoader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    private transient Class packageInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
}