jdk/src/share/classes/java/lang/Package.java
author jfranck
Wed, 13 Feb 2013 10:36:36 +0100
changeset 15659 e575dab44ff5
parent 15534 19228f4aedb4
child 16051 649a03329639
permissions -rw-r--r--
8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class) Reviewed-by: darcy, abuckley
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
15534
19228f4aedb4 8007113: Upgrade AnnotatedElement.isAnnotionPresent to be a default method
darcy
parents: 14676
diff changeset
     2
 * Copyright (c) 1997, 2013, 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
     *
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8543
diff changeset
   112
     * @return  The fully-qualified name of this package as defined in section 6.5.3 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8543
diff changeset
   113
     *          <cite>The Java&trade; Language Specification</cite>,
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 8543
diff changeset
   114
     *          for example, {@code java.lang}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return pkgName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
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
     * Return the title of the specification that this package implements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @return the specification title, null is returned if it is not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public String getSpecificationTitle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return specTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Returns the version number of the specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * that this package implements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * This version string must be a sequence of nonnegative decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * integers separated by "."'s and may have leading zeros.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * When version strings are compared the most significant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * numbers are compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return the specification version, null is returned if it is not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public String getSpecificationVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return specVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Return the name of the organization, vendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * or company that owns and maintains the specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * of the classes that implement this package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @return the specification vendor, null is returned if it is not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public String getSpecificationVendor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return specVendor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Return the title of this package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @return the title of the implementation, null is returned if it is not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public String getImplementationTitle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return implTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Return the version of this implementation. It consists of any string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * assigned by the vendor of this implementation and does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * not have any particular syntax specified or expected by the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * runtime. It may be compared for equality with other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * package version strings used for this implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * by this vendor for this package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return the version of the implementation, null is returned if it is not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public String getImplementationVersion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return implVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Returns the name of the organization,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * vendor or company that provided this implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @return the vendor that implemented this package..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public String getImplementationVendor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return implVendor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Returns true if this package is sealed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @return true if the package is sealed, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public boolean isSealed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return sealBase != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Returns true if this package is sealed with respect to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * code source url.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param url the code source url
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @return true if this package is sealed with respect to url
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public boolean isSealed(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return url.equals(sealBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Compare this package's specification version with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * desired version. It returns true if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * this packages specification version number is greater than or equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * to the desired version number. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Version numbers are compared by sequentially comparing corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * components of the desired and specification strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Each component is converted as a decimal integer and the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * If the specification value is greater than the desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * value true is returned. If the value is less false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * If the values are equal the period is skipped and the next pair of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * components is compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param desired the version string of the desired version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @return true if this package's version number is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *          than or equal to the desired version number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @exception NumberFormatException if the desired or current version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *          is not of the correct dotted form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public boolean isCompatibleWith(String desired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        throws NumberFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (specVersion == null || specVersion.length() < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new NumberFormatException("Empty version string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        String [] sa = specVersion.split("\\.", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        int [] si = new int[sa.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        for (int i = 0; i < sa.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            si[i] = Integer.parseInt(sa[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (si[i] < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                throw NumberFormatException.forInputString("" + si[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        String [] da = desired.split("\\.", -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int [] di = new int[da.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        for (int i = 0; i < da.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            di[i] = Integer.parseInt(da[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (di[i] < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                throw NumberFormatException.forInputString("" + di[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        int len = Math.max(di.length, si.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            int d = (i < di.length ? di[i] : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            int s = (i < si.length ? si[i] : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            if (s < d)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if (s > d)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Find a package by name in the callers {@code ClassLoader} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * The callers {@code ClassLoader} instance is used to find the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * instance corresponding to the named class. If the callers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * {@code ClassLoader} instance is null then the set of packages loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * by the system {@code ClassLoader} instance is searched to find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * named package. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Packages have attributes for versions and specifications only if the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * loader created the package instance with the appropriate attributes. Typically,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * those attributes are defined in the manifests that accompany the classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param name a package name, for example, java.lang.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return the package of the requested name. It may be null if no package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *          information is available from the archive or codebase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public static Package getPackage(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        ClassLoader l = ClassLoader.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return l.getPackage(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return getSystemPackage(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
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
     * Get all the packages currently known for the caller's {@code ClassLoader}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * instance.  Those packages correspond to classes loaded via or accessible by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * name to that {@code ClassLoader} instance.  If the caller's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * {@code ClassLoader} instance is the bootstrap {@code ClassLoader}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * instance, which may be represented by {@code null} in some implementations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * only packages corresponding to classes loaded by the bootstrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * {@code ClassLoader} instance will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @return a new array of packages known to the callers {@code ClassLoader}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * instance.  An zero length array is returned if none are known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public static Package[] getPackages() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        ClassLoader l = ClassLoader.getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return l.getPackages();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return getSystemPackages();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
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
     * Get the package for the specified class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * The class's class loader is used to find the package instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * corresponding to the specified class. If the class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * is the bootstrap class loader, which may be represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * {@code null} in some implementations, then the set of packages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * loaded by the bootstrap class loader is searched to find the package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Packages have attributes for versions and specifications only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * if the class loader created the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * instance with the appropriate attributes. Typically those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * attributes are defined in the manifests that accompany
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * the classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 11117
diff changeset
   319
     * @param c the class to get the package of.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @return the package of the class. It may be null if no package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *          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
   322
    static Package getPackage(Class<?> c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        String name = c.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            name = name.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            ClassLoader cl = c.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            if (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                return cl.getPackage(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                return getSystemPackage(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
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
     * Return the hash code computed from the package name.
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public int hashCode(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return pkgName.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Returns the string representation of this Package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Its value is the string "package " and the package name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * If the package title is defined it is appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * If the package version is defined it is appended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @return the string representation of the package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        String spec = specTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        String ver =  specVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (spec != null && spec.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            spec = ", " + spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            spec = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (ver != null && ver.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            ver = ", version " + ver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            ver = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return "package " + pkgName + spec + ver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private Class<?> getPackageInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (packageInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                packageInfo = Class.forName(pkgName + ".package-info", false, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            } catch (ClassNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                // store a proxy for the package info that has no annotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                class PackageInfoProxy {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                packageInfo = PackageInfoProxy.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return packageInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return getPackageInfo().getAnnotation(annotationClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @throws NullPointerException {@inheritDoc}
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   390
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   391
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   392
    @Override
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   393
    public  <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationClass) {
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   394
        return getPackageInfo().getAnnotationsByType(annotationClass);
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   395
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   396
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   397
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public Annotation[] getAnnotations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return getPackageInfo().getAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   405
     * @throws NullPointerException {@inheritDoc}
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   406
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   407
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   408
    @Override
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   409
    public <A extends Annotation> A getDeclaredAnnotation(Class<A> annotationClass) {
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   410
        return getPackageInfo().getDeclaredAnnotation(annotationClass);
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   411
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   412
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   413
    /**
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   414
     * @throws NullPointerException {@inheritDoc}
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   415
     * @since 1.8
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   416
     */
15659
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   417
    @Override
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   418
    public <A extends Annotation> A[] getDeclaredAnnotationsByType(Class<A> annotationClass) {
e575dab44ff5 8007278: Rename j.l.r.AnnotatedElement.getAnnotations(Class) to getAnnotationsByType(Class)
jfranck
parents: 15534
diff changeset
   419
        return getPackageInfo().getDeclaredAnnotationsByType(annotationClass);
14676
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   420
    }
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   421
985410ec95e3 7154390: Add support for repeating annotations in j.l.r.AnnotatedElement
jfranck
parents: 14342
diff changeset
   422
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public Annotation[] getDeclaredAnnotations()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        return getPackageInfo().getDeclaredAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * Construct a package instance with the specified version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * information.
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 11117
diff changeset
   432
     * @param name the name of the package
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param spectitle the title of the specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @param specversion the version of the specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @param specvendor the organization that maintains the specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @param impltitle the title of the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @param implversion the version of the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @param implvendor the organization that maintains the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    Package(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            String spectitle, String specversion, String specvendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            String impltitle, String implversion, String implvendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            URL sealbase, ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        pkgName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        implTitle = impltitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        implVersion = implversion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        implVendor = implvendor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        specTitle = spectitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        specVersion = specversion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        specVendor = specvendor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        sealBase = sealbase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        this.loader = loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * Construct a package using the attributes from the specified manifest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @param name the package name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @param man the optional manifest for the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @param url the optional code source url for the package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    private Package(String name, Manifest man, URL url, ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        String path = name.replace('.', '/').concat("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        String sealed = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        String specTitle= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        String specVersion= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        String specVendor= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        String implTitle= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        String implVersion= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        String implVendor= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        URL sealBase= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        Attributes attr = man.getAttributes(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            specTitle   = attr.getValue(Name.SPECIFICATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            specVersion = attr.getValue(Name.SPECIFICATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            specVendor  = attr.getValue(Name.SPECIFICATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            implTitle   = attr.getValue(Name.IMPLEMENTATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            implVendor  = attr.getValue(Name.IMPLEMENTATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            sealed      = attr.getValue(Name.SEALED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        attr = man.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            if (specTitle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                specTitle = attr.getValue(Name.SPECIFICATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            if (specVersion == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                specVersion = attr.getValue(Name.SPECIFICATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            if (specVendor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                specVendor = attr.getValue(Name.SPECIFICATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            if (implTitle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            if (implVersion == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            if (implVendor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            if (sealed == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                sealed = attr.getValue(Name.SEALED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if ("true".equalsIgnoreCase(sealed)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            sealBase = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        pkgName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        this.specTitle = specTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        this.specVersion = specVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        this.specVendor = specVendor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        this.implTitle = implTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        this.implVersion = implVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        this.implVendor = implVendor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        this.sealBase = sealBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        this.loader = loader;
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
     * Returns the loaded system package for the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    static Package getSystemPackage(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        synchronized (pkgs) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   526
            Package pkg = pkgs.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            if (pkg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                name = name.replace('.', '/').concat("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                String fn = getSystemPackage0(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                if (fn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    pkg = defineSystemPackage(name, fn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            return pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Return an array of loaded system packages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    static Package[] getSystemPackages() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        // First, update the system package map with new package names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        String[] names = getSystemPackages0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        synchronized (pkgs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                defineSystemPackage(names[i], getSystemPackage0(names[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   548
            return pkgs.values().toArray(new Package[pkgs.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    private static Package defineSystemPackage(final String iname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                               final String fn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   555
        return AccessController.doPrivileged(new PrivilegedAction<Package>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   556
            public Package run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                String name = iname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                // Get the cached code source url for the file name
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   559
                URL url = urls.get(fn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                if (url == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    // URL not found, so create one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    File file = new File(fn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                        url = ParseUtil.fileToEncodedURL(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                        urls.put(fn, url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                        // If loading a JAR file, then also cache the manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                        if (file.isFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                            mans.put(fn, loadManifest(fn));
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
                // Convert to "."-separated package name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                name = name.substring(0, name.length() - 1).replace('/', '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                Package pkg;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   578
                Manifest man = mans.get(fn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                if (man != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    pkg = new Package(name, man, url, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    pkg = new Package(name, null, null, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                                      null, null, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                pkgs.put(name, pkg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                return pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Returns the Manifest for the specified JAR file name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    private static Manifest loadManifest(String fn) {
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   595
        try (FileInputStream fis = new FileInputStream(fn);
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   596
             JarInputStream jis = new JarInputStream(fis, false))
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   597
        {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7803
diff changeset
   598
            return jis.getManifest();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    // 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
   605
    private static Map<String, Package> pkgs = new HashMap<>(31);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    // 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
   608
    private static Map<String, URL> urls = new HashMap<>(10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    // 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
   611
    private static Map<String, Manifest> mans = new HashMap<>(10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    private static native String getSystemPackage0(String name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    private static native String[] getSystemPackages0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * Private storage for the package name and attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    private final String pkgName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    private final String specTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    private final String specVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    private final String specVendor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    private final String implTitle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    private final String implVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    private final String implVendor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    private final URL sealBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    private transient final ClassLoader loader;
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9275
diff changeset
   628
    private transient Class<?> packageInfo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
}