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