jdk/src/share/classes/java/lang/ClassLoader.java
author martin
Mon, 10 Mar 2008 15:07:09 -0700
changeset 51 6fe31bc95bbc
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6600143: Remove another 450 unnecessary casts Reviewed-by: alanb, iris, lmalvent, bristor, peterjones, darcy, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1994-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.CodeSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Policy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.ProtectionDomain;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    42
import java.security.cert.Certificate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.misc.ClassFileTransformer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.misc.CompoundEnumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.misc.Resource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import sun.misc.URLClassPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import sun.misc.VM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.reflect.Reflection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * A class loader is an object that is responsible for loading classes. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * class <tt>ClassLoader</tt> is an abstract class.  Given the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * href="#name">binary name</a> of a class, a class loader should attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * locate or generate data that constitutes a definition for the class.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * typical strategy is to transform the name into a file name and then read a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * "class file" of that name from a file system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p> Every {@link Class <tt>Class</tt>} object contains a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Class#getClassLoader() reference} to the <tt>ClassLoader</tt> that defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p> <tt>Class</tt> objects for array classes are not created by class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * loaders, but are created automatically as required by the Java runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * The class loader for an array class, as returned by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Class#getClassLoader()} is the same as the class loader for its element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * type; if the element type is a primitive type, then the array class has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p> Applications implement subclasses of <tt>ClassLoader</tt> in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * extend the manner in which the Java virtual machine dynamically loads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p> Class loaders may typically be used by security managers to indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * security domains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <p> The <tt>ClassLoader</tt> class uses a delegation model to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * classes and resources.  Each instance of <tt>ClassLoader</tt> has an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * associated parent class loader.  When requested to find a class or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * resource, a <tt>ClassLoader</tt> instance will delegate the search for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * class or resource to its parent class loader before attempting to find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * class or resource itself.  The virtual machine's built-in class loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * called the "bootstrap class loader", does not itself have a parent but may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * serve as the parent of a <tt>ClassLoader</tt> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p> Normally, the Java virtual machine loads classes from the local file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * system in a platform-dependent manner.  For example, on UNIX systems, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * virtual machine loads classes from the directory defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <tt>CLASSPATH</tt> environment variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <p> However, some classes may not originate from a file; they may originate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * from other sources, such as the network, or they could be constructed by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * application.  The method {@link #defineClass(String, byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <tt>defineClass</tt>} converts an array of bytes into an instance of class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <tt>Class</tt>. Instances of this newly defined class can be created using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * {@link Class#newInstance <tt>Class.newInstance</tt>}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <p> The methods and constructors of objects created by a class loader may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * reference other classes.  To determine the class(es) referred to, the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * virtual machine invokes the {@link #loadClass <tt>loadClass</tt>} method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * the class loader that originally created the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <p> For example, an application could create a network class loader to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * download class files from a server.  Sample code might look like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *   ClassLoader loader&nbsp;= new NetworkClassLoader(host,&nbsp;port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *   Object main&nbsp;= loader.loadClass("Main", true).newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *       &nbsp;.&nbsp;.&nbsp;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <p> The network class loader subclass must define the methods {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * #findClass <tt>findClass</tt>} and <tt>loadClassData</tt> to load a class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * from the network.  Once it has downloaded the bytes that make up the class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * it should use the method {@link #defineClass <tt>defineClass</tt>} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * create a class instance.  A sample implementation is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *     class NetworkClassLoader extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *         String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *         int port;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *         public Class findClass(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *             byte[] b = loadClassData(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *             return defineClass(name, b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *         private byte[] loadClassData(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *             // load the class data from the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *             &nbsp;.&nbsp;.&nbsp;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * <h4> <a name="name">Binary names</a> </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * <p> Any class name provided as a {@link String} parameter to methods in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * <tt>ClassLoader</tt> must be a binary name as defined by the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * href="http://java.sun.com/docs/books/jls/">Java Language Specification</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * <p> Examples of valid class names include:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *   "java.lang.String"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *   "javax.swing.JSpinner$DefaultEditor"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *   "java.security.KeyStore$Builder$FileBuilder$1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *   "java.net.URLClassLoader$3$1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * @see      #resolveClass(Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * @since 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
public abstract class ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private static native void registerNatives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        registerNatives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // If initialization succeed this is set to true and security checks will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    // succeed.  Otherwise the object is not initialized and the object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    // useless.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private boolean initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    // The parent class loader for delegation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private ClassLoader parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    // Hashtable that maps packages to certs
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   176
    private Hashtable<String, Certificate[]> package2certs
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   177
        = new Hashtable<String, Certificate[]>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // Shared among all packages with unsigned classes
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   180
    Certificate[] nocerts;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    // The classes loaded by this class loader.  The only purpose of this table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    // is to keep the classes from being GC'ed until the loader is GC'ed.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   184
    private Vector<Class<?>> classes = new Vector<Class<?>>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    // The initiating protection domains for all classes loaded by this loader
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   187
    private Set<ProtectionDomain> domains = new HashSet<ProtectionDomain>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    // Invoked by the VM to record every loaded class with this loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    void addClass(Class c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        classes.addElement(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    // The packages defined in this class loader.  Each package name is mapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    // to its corresponding Package object.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   196
    private HashMap<String, Package> packages = new HashMap<String, Package>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Creates a new class loader using the specified parent class loader for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * delegation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <p> If there is a security manager, its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * SecurityManager#checkCreateClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <tt>checkCreateClassLoader</tt>} method is invoked.  This may result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * a security exception.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param  parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *         The parent class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *          If a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *          <tt>checkCreateClassLoader</tt> method doesn't allow creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *          of a new class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    protected ClassLoader(ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Creates a new class loader using the <tt>ClassLoader</tt> returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * the method {@link #getSystemClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <tt>getSystemClassLoader()</tt>} as the parent class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * <p> If there is a security manager, its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * SecurityManager#checkCreateClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <tt>checkCreateClassLoader</tt>} method is invoked.  This may result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * a security exception.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *          If a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *          <tt>checkCreateClassLoader</tt> method doesn't allow creation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *          of a new class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    protected ClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            security.checkCreateClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        this.parent = getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    // -- Class --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Loads the class with the specified <a href="#name">binary name</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * This method searches for classes in the same manner as the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * #loadClass(String, boolean)} method.  It is invoked by the Java virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * machine to resolve class references.  Invoking this method is equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * to invoking {@link #loadClass(String, boolean) <tt>loadClass(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * false)</tt>}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *         The <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return  The resulting <tt>Class</tt> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @throws  ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *          If the class was not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public Class<?> loadClass(String name) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return loadClass(name, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Loads the class with the specified <a href="#name">binary name</a>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * default implementation of this method searches for classes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <p><ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *   <li><p> Invoke {@link #findLoadedClass(String)} to check if the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *   has already been loaded.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *   <li><p> Invoke the {@link #loadClass(String) <tt>loadClass</tt>} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *   on the parent class loader.  If the parent is <tt>null</tt> the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *   loader built-in to the virtual machine is used, instead.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *   <li><p> Invoke the {@link #findClass(String)} method to find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *   class.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <p> If the class was found using the above steps, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * <tt>resolve</tt> flag is true, this method will then invoke the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * #resolveClass(Class)} method on the resulting <tt>Class</tt> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <p> Subclasses of <tt>ClassLoader</tt> are encouraged to override {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * #findClass(String)}, rather than this method.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *         The <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param  resolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *         If <tt>true</tt> then resolve the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @return  The resulting <tt>Class</tt> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @throws  ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *          If the class could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    protected synchronized Class<?> loadClass(String name, boolean resolve)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // First, check if the class has already been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        Class c = findLoadedClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    c = parent.loadClass(name, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    c = findBootstrapClass0(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                // If still not found, then invoke findClass in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                // to find the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                c = findClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (resolve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            resolveClass(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    // This method is invoked by the virtual machine to load a class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private synchronized Class loadClassInternal(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return loadClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    private void checkPackageAccess(Class cls, ProtectionDomain pd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        final SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            final String name = cls.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            final int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (i != -1) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   347
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   348
                    public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        sm.checkPackageAccess(name.substring(0, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                }, new AccessControlContext(new ProtectionDomain[] {pd}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        domains.add(pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Finds the class with the specified <a href="#name">binary name</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * This method should be overridden by class loader implementations that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * follow the delegation model for loading classes, and will be invoked by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * the {@link #loadClass <tt>loadClass</tt>} method after checking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * parent class loader for the requested class.  The default implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * throws a <tt>ClassNotFoundException</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *         The <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @return  The resulting <tt>Class</tt> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @throws  ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *          If the class could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    protected Class<?> findClass(String name) throws ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        throw new ClassNotFoundException(name);
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
     * Converts an array of bytes into an instance of class <tt>Class</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Before the <tt>Class</tt> can be used it must be resolved.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * is deprecated in favor of the version that takes a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * href="#name">binary name</a> as its first argument, and is more secure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @param  b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *         The bytes that make up the class data.  The bytes in positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *         <tt>off</tt> through <tt>off+len-1</tt> should have the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *         of a valid class file as defined by the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *         href="http://java.sun.com/docs/books/vmspec/">Java Virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *         Machine Specification</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @param  off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *         The start offset in <tt>b</tt> of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @param  len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *         The length of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @return  The <tt>Class</tt> object that was created from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *          class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @throws  ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *          If the data did not contain a valid class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *          If either <tt>off</tt> or <tt>len</tt> is negative, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *          <tt>off+len</tt> is greater than <tt>b.length</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @see  #loadClass(String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @see  #resolveClass(Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @deprecated  Replaced by {@link #defineClass(String, byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * defineClass(String, byte[], int, int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    protected final Class<?> defineClass(byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        return defineClass(null, b, off, len, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * Converts an array of bytes into an instance of class <tt>Class</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Before the <tt>Class</tt> can be used it must be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * <p> This method assigns a default {@link java.security.ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * <tt>ProtectionDomain</tt>} to the newly defined class.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * <tt>ProtectionDomain</tt> is effectively granted the same set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * permissions returned when {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * java.security.Policy#getPermissions(java.security.CodeSource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * <tt>Policy.getPolicy().getPermissions(new CodeSource(null, null))</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * is invoked.  The default domain is created on the first invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * {@link #defineClass(String, byte[], int, int) <tt>defineClass</tt>},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * and re-used on subsequent invocations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * <p> To assign a specific <tt>ProtectionDomain</tt> to the class, use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * the {@link #defineClass(String, byte[], int, int,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * java.security.ProtectionDomain) <tt>defineClass</tt>} method that takes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * <tt>ProtectionDomain</tt> as one of its arguments.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *         The expected <a href="#name">binary name</a> of the class, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *         <tt>null</tt> if not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @param  b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *         The bytes that make up the class data.  The bytes in positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *         <tt>off</tt> through <tt>off+len-1</tt> should have the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *         of a valid class file as defined by the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *         href="http://java.sun.com/docs/books/vmspec/">Java Virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *         Machine Specification</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @param  off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *         The start offset in <tt>b</tt> of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @param  len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *         The length of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @return  The <tt>Class</tt> object that was created from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *          class data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @throws  ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *          If the data did not contain a valid class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *          If either <tt>off</tt> or <tt>len</tt> is negative, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *          <tt>off+len</tt> is greater than <tt>b.length</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *          If an attempt is made to add this class to a package that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *          contains classes that were signed by a different set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *          certificates than this class (which is unsigned), or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *          <tt>name</tt> begins with "<tt>java.</tt>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @see  #loadClass(String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @see  #resolveClass(Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @see  java.security.CodeSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @see  java.security.SecureClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    protected final Class<?> defineClass(String name, byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return defineClass(name, b, off, len, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /* Determine protection domain, and check that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        - not define java.* class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        - signer of this class matches signers for the rest of the classes in package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    private ProtectionDomain preDefineClass(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                                            ProtectionDomain protectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (!checkName(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            throw new NoClassDefFoundError("IllegalName: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if ((name != null) && name.startsWith("java.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            throw new SecurityException("Prohibited package name: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                                        name.substring(0, name.lastIndexOf('.')));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        if (protectionDomain == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            protectionDomain = getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (name != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            checkCerts(name, protectionDomain.getCodeSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        return protectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    private String defineClassSourceLocation(ProtectionDomain protectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        CodeSource cs = protectionDomain.getCodeSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        String source = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        if (cs != null && cs.getLocation() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            source = cs.getLocation().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        return source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    private Class defineTransformedClass(String name, byte[] b, int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                                         ProtectionDomain protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                                         ClassFormatError cfe, String source)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
      throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        // Class format error - try to transform the bytecode and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        // define the class again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        //
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   529
        ClassFileTransformer[] transformers = ClassFileTransformer.getTransformers();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        Class c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   532
        if (transformers != null) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   533
            for (ClassFileTransformer transformer : transformers) {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   534
                try {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   535
                    // Transform byte code using transformer
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   536
                    byte[] tb = transformer.transform(b, off, len);
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   537
                    c = defineClass1(name, tb, 0, tb.length,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   538
                                     protectionDomain, source);
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   539
                    break;
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   540
                } catch (ClassFormatError cfe2)     {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   541
                    // If ClassFormatError occurs, try next transformer
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   542
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        // Rethrow original ClassFormatError if unable to transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        // bytecode to well-formed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        if (c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            throw cfe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    private void postDefineClass(Class c, ProtectionDomain protectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        if (protectionDomain.getCodeSource() != null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   558
            Certificate certs[] =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                protectionDomain.getCodeSource().getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            if (certs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                setSigners(c, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * Converts an array of bytes into an instance of class <tt>Class</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * with an optional <tt>ProtectionDomain</tt>.  If the domain is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * <tt>null</tt>, then a default domain will be assigned to the class as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * specified in the documentation for {@link #defineClass(String, byte[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * int, int)}.  Before the class can be used it must be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * <p> The first class defined in a package determines the exact set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * certificates that all subsequent classes defined in that package must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * contain.  The set of certificates for a class is obtained from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * {@link java.security.CodeSource <tt>CodeSource</tt>} within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * <tt>ProtectionDomain</tt> of the class.  Any classes added to that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * package must contain the same set of certificates or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * <tt>SecurityException</tt> will be thrown.  Note that if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * <tt>name</tt> is <tt>null</tt>, this check is not performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * You should always pass in the <a href="#name">binary name</a> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * class you are defining as well as the bytes.  This ensures that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * class you are defining is indeed the class you think it is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <p> The specified <tt>name</tt> cannot begin with "<tt>java.</tt>", since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * all classes in the "<tt>java.*</tt> packages can only be defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * bootstrap class loader.  If <tt>name</tt> is not <tt>null</tt>, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * must be equal to the <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * specified by the byte array "<tt>b</tt>", otherwise a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * <tt>NoClassDefFoundError</tt>} will be thrown.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *         The expected <a href="#name">binary name</a> of the class, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *         <tt>null</tt> if not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @param  b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *         The bytes that make up the class data. The bytes in positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *         <tt>off</tt> through <tt>off+len-1</tt> should have the format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *         of a valid class file as defined by the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *         href="http://java.sun.com/docs/books/vmspec/">Java Virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *         Machine Specification</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @param  off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *         The start offset in <tt>b</tt> of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @param  len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *         The length of the class data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @param  protectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *         The ProtectionDomain of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @return  The <tt>Class</tt> object created from the data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *          and optional <tt>ProtectionDomain</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @throws  ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *          If the data did not contain a valid class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * @throws  NoClassDefFoundError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *          If <tt>name</tt> is not equal to the <a href="#name">binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *          name</a> of the class specified by <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *          If either <tt>off</tt> or <tt>len</tt> is negative, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *          <tt>off+len</tt> is greater than <tt>b.length</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *          If an attempt is made to add this class to a package that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *          contains classes that were signed by a different set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *          certificates than this class, or if <tt>name</tt> begins with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *          "<tt>java.</tt>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    protected final Class<?> defineClass(String name, byte[] b, int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                                         ProtectionDomain protectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        protectionDomain = preDefineClass(name, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        Class c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        String source = defineClassSourceLocation(protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            c = defineClass1(name, b, off, len, protectionDomain, source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        } catch (ClassFormatError cfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            c = defineTransformedClass(name, b, off, len, protectionDomain, cfe, source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        postDefineClass(c, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * Converts a {@link java.nio.ByteBuffer <tt>ByteBuffer</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * into an instance of class <tt>Class</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * with an optional <tt>ProtectionDomain</tt>.  If the domain is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * <tt>null</tt>, then a default domain will be assigned to the class as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * specified in the documentation for {@link #defineClass(String, byte[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * int, int)}.  Before the class can be used it must be resolved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * <p>The rules about the first class defined in a package determining the set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * certificates for the package, and the restrictions on class names are identical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * to those specified in the documentation for {@link #defineClass(String, byte[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * int, int, ProtectionDomain)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * <p> An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * <i>cl</i><tt>.defineClass(</tt><i>name</i><tt>,</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * <i>bBuffer</i><tt>,</tt> <i>pd</i><tt>)</tt> yields exactly the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * result as the statements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * <blockquote><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * ...<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * byte[] temp = new byte[</tt><i>bBuffer</i><tt>.{@link java.nio.ByteBuffer#remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * remaining}()];<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *     </tt><i>bBuffer</i><tt>.{@link java.nio.ByteBuffer#get(byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * get}(temp);<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *     return {@link #defineClass(String, byte[], int, int, ProtectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * </tt><i>cl</i><tt>.defineClass}(</tt><i>name</i><tt>, temp, 0, temp.length, </tt><i>pd</i><tt>);<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * </tt></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *         The expected <a href="#name">binary name</a. of the class, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *         <tt>null</tt> if not known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @param  b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *         The bytes that make up the class data. The bytes from positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     *         <tt>b.position()</tt> through <tt>b.position() + b.limit() -1 </tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *         should have the format of a valid class file as defined by the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     *         href="http://java.sun.com/docs/books/vmspec/">Java Virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *         Machine Specification</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @param  protectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *         The ProtectionDomain of the class, or <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @return  The <tt>Class</tt> object created from the data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *          and optional <tt>ProtectionDomain</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @throws  ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *          If the data did not contain a valid class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @throws  NoClassDefFoundError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *          If <tt>name</tt> is not equal to the <a href="#name">binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *          name</a> of the class specified by <tt>b</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *          If an attempt is made to add this class to a package that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *          contains classes that were signed by a different set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *          certificates than this class, or if <tt>name</tt> begins with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     *          "<tt>java.</tt>".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * @see      #defineClass(String, byte[], int, int, ProtectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    protected final Class<?> defineClass(String name, java.nio.ByteBuffer b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                                         ProtectionDomain protectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        throws ClassFormatError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        int len = b.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        // Use byte[] if not a direct ByteBufer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        if (!b.isDirect()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            if (b.hasArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                return defineClass(name, b.array(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                                   b.position() + b.arrayOffset(), len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                                   protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                // no array, or read-only array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                byte[] tb = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                b.get(tb);  // get bytes out of byte buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                return defineClass(name, tb, 0, len, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        protectionDomain = preDefineClass(name, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        Class c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        String source = defineClassSourceLocation(protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            c = defineClass2(name, b, b.position(), len, protectionDomain, source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        } catch (ClassFormatError cfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            byte[] tb = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            b.get(tb);  // get bytes out of byte buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            c = defineTransformedClass(name, tb, 0, len, protectionDomain, cfe, source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        postDefineClass(c, protectionDomain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    private native Class defineClass0(String name, byte[] b, int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                                      ProtectionDomain pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    private native Class defineClass1(String name, byte[] b, int off, int len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                                      ProtectionDomain pd, String source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    private native Class defineClass2(String name, java.nio.ByteBuffer b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                                      int off, int len, ProtectionDomain pd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                                      String source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    // true if the name is null or has the potential to be a valid binary name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    private boolean checkName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        if ((name == null) || (name.length() == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        if ((name.indexOf('/') != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            || (!VM.allowArraySyntax() && (name.charAt(0) == '[')))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    private synchronized void checkCerts(String name, CodeSource cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        String pname = (i == -1) ? "" : name.substring(0, i);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   775
        Certificate[] pcerts = package2certs.get(pname);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        if (pcerts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            // first class in this package gets to define which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            // certificates must be the same for all other classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            // in this package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            if (cs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                pcerts = cs.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            if (pcerts == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                if (nocerts == null)
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   785
                    nocerts = new Certificate[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                pcerts = nocerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            package2certs.put(pname, pcerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   790
            Certificate[] certs = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            if (cs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                certs = cs.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            if (!compareCerts(pcerts, certs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                throw new SecurityException("class \""+ name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                                            "\"'s signer information does not match signer information of other classes in the same package");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * check to make sure the certs for the new class (certs) are the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * the certs for the first class inserted in the package (pcerts)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   806
    private boolean compareCerts(Certificate[] pcerts,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   807
                                 Certificate[] certs)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        // certs can be null, indicating no certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        if ((certs == null) || (certs.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            return pcerts.length == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        // the length must be the same at this point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        if (certs.length != pcerts.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        // go through and make sure all the certs in one array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        // are in the other and vice-versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        boolean match;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        for (int i = 0; i < certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            for (int j = 0; j < pcerts.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                if (certs[i].equals(pcerts[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        // now do the same for pcerts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        for (int i = 0; i < pcerts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            for (int j = 0; j < certs.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                if (pcerts[i].equals(certs[j])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                    match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            if (!match) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * Links the specified class.  This (misleadingly named) method may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * used by a class loader to link a class.  If the class <tt>c</tt> has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * already been linked, then this method simply returns. Otherwise, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * class is linked as described in the "Execution" chapter of the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * href="http://java.sun.com/docs/books/jls/">Java Language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * Specification</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @param  c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     *         The class to link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @throws  NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *          If <tt>c</tt> is <tt>null</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * @see  #defineClass(String, byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    protected final void resolveClass(Class<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        resolveClass0(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    private native void resolveClass0(Class c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * Finds a class with the specified <a href="#name">binary name</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * loading it if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * <p> This method loads the class through the system class loader (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * {@link #getSystemClassLoader()}).  The <tt>Class</tt> object returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * might have more than one <tt>ClassLoader</tt> associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * Subclasses of <tt>ClassLoader</tt> need not usually invoke this method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * because most class loaders need to override just {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * #findClass(String)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     *         The <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * @return  The <tt>Class</tt> object for the specified <tt>name</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @throws  ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     *          If the class could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @see  #ClassLoader(ClassLoader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * @see  #getParent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    protected final Class<?> findSystemClass(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        ClassLoader system = getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        if (system == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            if (!checkName(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                throw new ClassNotFoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            return findBootstrapClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        return system.loadClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    private Class findBootstrapClass0(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        if (!checkName(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            throw new ClassNotFoundException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        return findBootstrapClass(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    private native Class findBootstrapClass(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        throws ClassNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    // Check to make sure the class loader has been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    private void check() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            throw new SecurityException("ClassLoader object not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * Returns the class with the given <a href="#name">binary name</a> if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * loader has been recorded by the Java virtual machine as an initiating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * loader of a class with that <a href="#name">binary name</a>.  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * <tt>null</tt> is returned.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     *         The <a href="#name">binary name</a> of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * @return  The <tt>Class</tt> object, or <tt>null</tt> if the class has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     *          not been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    protected final Class<?> findLoadedClass(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        if (!checkName(name))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        return findLoadedClass0(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    private native final Class findLoadedClass0(String name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * Sets the signers of a class.  This should be invoked after defining a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * class.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * @param  c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *         The <tt>Class</tt> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * @param  signers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     *         The signers for the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    protected final void setSigners(Class<?> c, Object[] signers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        c.setSigners(signers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    // -- Resource --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * Finds the resource with the given name.  A resource is some data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * (images, audio, text, etc) that can be accessed by class code in a way
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * that is independent of the location of the code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * <p> The name of a resource is a '<tt>/</tt>'-separated path name that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * identifies the resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * <p> This method will first search the parent class loader for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * resource; if the parent is <tt>null</tt> the path of the class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * built-in to the virtual machine is searched.  That failing, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * will invoke {@link #findResource(String)} to find the resource.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * @return  A <tt>URL</tt> object for reading the resource, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *          <tt>null</tt> if the resource could not be found or the invoker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     *          doesn't have adequate  privileges to get the resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    public URL getResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            url = parent.getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            url = getBootstrapResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        if (url == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            url = findResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * Finds all the resources with the given name. A resource is some data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * (images, audio, text, etc) that can be accessed by class code in a way
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * that is independent of the location of the code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * <p>The name of a resource is a <tt>/</tt>-separated path name that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * identifies the resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * <p> The search order is described in the documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * #getResource(String)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * @return  An enumeration of {@link java.net.URL <tt>URL</tt>} objects for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     *          the resource.  If no resources could  be found, the enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     *          will be empty.  Resources that the class loader doesn't have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *          access to will not be in the enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *          If I/O errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * @see  #findResources(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    public Enumeration<URL> getResources(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        Enumeration[] tmp = new Enumeration[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            tmp[0] = parent.getResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            tmp[0] = getBootstrapResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        tmp[1] = findResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1038
        return new CompoundEnumeration<URL>(tmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * Finds the resource with the given name. Class loader implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * should override this method to specify where to find resources.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * @return  A <tt>URL</tt> object for reading the resource, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *          <tt>null</tt> if the resource could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    protected URL findResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * Returns an enumeration of {@link java.net.URL <tt>URL</tt>} objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * representing all the resources with the given name. Class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * implementations should override this method to specify where to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * resources from.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @return  An enumeration of {@link java.net.URL <tt>URL</tt>} objects for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     *          the resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     *          If I/O errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    protected Enumeration<URL> findResources(String name) throws IOException {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1075
        return java.util.Collections.emptyEnumeration();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * Find a resource of the specified name from the search path used to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * classes.  This method locates the resource through the system class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * loader (see {@link #getSystemClassLoader()}).  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * @return  A {@link java.net.URL <tt>URL</tt>} object for reading the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     *          resource, or <tt>null</tt> if the resource could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    public static URL getSystemResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        ClassLoader system = getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        if (system == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            return getBootstrapResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        return system.getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * Finds all resources of the specified name from the search path used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * load classes.  The resources thus found are returned as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * {@link java.util.Enumeration <tt>Enumeration</tt>} of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * java.net.URL <tt>URL</tt>} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * <p> The search order is described in the documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * #getSystemResource(String)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * @return  An enumeration of resource {@link java.net.URL <tt>URL</tt>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     *          objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     *          If I/O errors occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    public static Enumeration<URL> getSystemResources(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        ClassLoader system = getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        if (system == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            return getBootstrapResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        return system.getResources(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * Find resources from the VM's built-in classloader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    private static URL getBootstrapResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        URLClassPath ucp = getBootstrapClassPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        Resource res = ucp.getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        return res != null ? res.getURL() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * Find resources from the VM's built-in classloader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1141
    private static Enumeration<URL> getBootstrapResources(String name)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1144
        final Enumeration<Resource> e = getBootstrapClassPath().getResources(name);
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1145
        return new Enumeration<URL> () {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1146
            public URL nextElement() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1147
                return e.nextElement().getURL();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                return e.hasMoreElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    // Returns the URLClassPath that is used for finding system resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    static URLClassPath getBootstrapClassPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        if (bootstrapClassPath == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            bootstrapClassPath = sun.misc.Launcher.getBootstrapClassPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        return bootstrapClassPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    private static URLClassPath bootstrapClassPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * Returns an input stream for reading the specified resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * <p> The search order is described in the documentation for {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * #getResource(String)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * @return  An input stream for reading the resource, or <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     *          if the resource could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    public InputStream getResourceAsStream(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        URL url = getResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            return url != null ? url.openStream() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * Open for reading, a resource of the specified name from the search path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * used to load classes.  This method locates the resource through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * system class loader (see {@link #getSystemClassLoader()}).  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     *         The resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * @return  An input stream for reading the resource, or <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     *          if the resource could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * @since  1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
    public static InputStream getSystemResourceAsStream(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        URL url = getSystemResource(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            return url != null ? url.openStream() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    // -- Hierarchy --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * Returns the parent class loader for delegation. Some implementations may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * use <tt>null</tt> to represent the bootstrap class loader. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * will return <tt>null</tt> in such implementations if this class loader's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * parent is the bootstrap class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * <p> If a security manager is present, and the invoker's class loader is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * not <tt>null</tt> and is not an ancestor of this class loader, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * method invokes the security manager's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * SecurityManager#checkPermission(java.security.Permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * <tt>checkPermission</tt>} method with a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * RuntimePermission#RuntimePermission(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * <tt>RuntimePermission("getClassLoader")</tt>} permission to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * access to the parent class loader is permitted.  If not, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * <tt>SecurityException</tt> will be thrown.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * @return  The parent <tt>ClassLoader</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     *          If a security manager exists and its <tt>checkPermission</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *          method doesn't allow access to this class loader's parent class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     *          loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    public final ClassLoader getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        if (parent == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            ClassLoader ccl = getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            if (ccl != null && !isAncestor(ccl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * Returns the system class loader for delegation.  This is the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * delegation parent for new <tt>ClassLoader</tt> instances, and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * typically the class loader used to start the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * <p> This method is first invoked early in the runtime's startup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * sequence, at which point it creates the system class loader and sets it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * as the context class loader of the invoking <tt>Thread</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * <p> The default system class loader is an implementation-dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * <p> If the system property "<tt>java.system.class.loader</tt>" is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * when this method is first invoked then the value of that property is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * taken to be the name of a class that will be returned as the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * class loader.  The class is loaded using the default system class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * and must define a public constructor that takes a single parameter of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * type <tt>ClassLoader</tt> which is used as the delegation parent.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * instance is then created using this constructor with the default system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * class loader as the parameter.  The resulting class loader is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * to be the system class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * <p> If a security manager is present, and the invoker's class loader is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * not <tt>null</tt> and the invoker's class loader is not the same as or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * an ancestor of the system class loader, then this method invokes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * security manager's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * SecurityManager#checkPermission(java.security.Permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * <tt>checkPermission</tt>} method with a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * RuntimePermission#RuntimePermission(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * <tt>RuntimePermission("getClassLoader")</tt>} permission to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * access to the system class loader.  If not, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * <tt>SecurityException</tt> will be thrown.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * @return  The system <tt>ClassLoader</tt> for delegation, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     *          <tt>null</tt> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     *          If a security manager exists and its <tt>checkPermission</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     *          method doesn't allow access to the system class loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     *          If invoked recursively during the construction of the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     *          loader specified by the "<tt>java.system.class.loader</tt>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     *          property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * @throws  Error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     *          If the system property "<tt>java.system.class.loader</tt>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     *          is defined but the named class could not be loaded, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     *          provider class does not define the required constructor, or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     *          exception is thrown by that constructor when it is invoked. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     *          underlying cause of the error can be retrieved via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     *          {@link Throwable#getCause()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * @revised  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    public static ClassLoader getSystemClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        initSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        if (scl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            ClassLoader ccl = getCallerClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
            if (ccl != null && ccl != scl && !scl.isAncestor(ccl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        return scl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
    private static synchronized void initSystemClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
        if (!sclSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            if (scl != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                throw new IllegalStateException("recursive invocation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            sun.misc.Launcher l = sun.misc.Launcher.getLauncher();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                Throwable oops = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                scl = l.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1330
                    scl = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1331
                        new SystemClassLoaderAction(scl));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                    oops = pae.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                    if (oops instanceof InvocationTargetException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                        oops = oops.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                if (oops != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                    if (oops instanceof Error) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                        throw (Error) oops;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                        // wrap the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                        throw new Error(oops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            sclSet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    // Returns true if the specified class loader can be found in this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    // loader's delegation chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    boolean isAncestor(ClassLoader cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        ClassLoader acl = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            acl = acl.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            if (cl == acl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        } while (acl != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    // Returns the invoker's class loader, or null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    // NOTE: This must always be invoked when there is exactly one intervening
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    // frame from the core libraries on the stack between this method's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    // invocation and the desired invoker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    static ClassLoader getCallerClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        // NOTE use of more generic Reflection.getCallerClass()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        Class caller = Reflection.getCallerClass(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        // This can be null if the VM is requesting it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        if (caller == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        // Circumvent security check since this is package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        return caller.getClassLoader0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    // The class loader for the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    private static ClassLoader scl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    // Set to true once the system class loader has been set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    private static boolean sclSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    // -- Package --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * Defines a package by name in this <tt>ClassLoader</tt>.  This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * class loaders to define the packages for their classes. Packages must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * be created before the class is defined, and package names must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * unique within a class loader and cannot be redefined or changed once
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * created.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     *         The package name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * @param  specTitle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     *         The specification title
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * @param  specVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     *         The specification version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * @param  specVendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     *         The specification vendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * @param  implTitle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     *         The implementation title
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     * @param  implVersion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     *         The implementation version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * @param  implVendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     *         The implementation vendor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * @param  sealBase
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     *         If not <tt>null</tt>, then this package is sealed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     *         respect to the given code source {@link java.net.URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     *         <tt>URL</tt>}  object.  Otherwise, the package is not sealed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * @return  The newly defined <tt>Package</tt> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     *          If package name duplicates an existing package either in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     *          class loader or one of its ancestors
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    protected Package definePackage(String name, String specTitle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                                    String specVersion, String specVendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                                    String implTitle, String implVersion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                                    String implVendor, URL sealBase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        synchronized (packages) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            Package pkg = getPackage(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            if (pkg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                throw new IllegalArgumentException(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            pkg = new Package(name, specTitle, specVersion, specVendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                              implTitle, implVersion, implVendor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                              sealBase, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            packages.put(name, pkg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            return pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * Returns a <tt>Package</tt> that has been defined by this class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * or any of its ancestors.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * @param  name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     *         The package name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * @return  The <tt>Package</tt> corresponding to the given name, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     *          <tt>null</tt> if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    protected Package getPackage(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        synchronized (packages) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1462
            Package pkg = packages.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            if (pkg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                    pkg = parent.getPackage(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                    pkg = Package.getSystemPackage(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                if (pkg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                    packages.put(name, pkg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            return pkg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * Returns all of the <tt>Packages</tt> defined by this class loader and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * its ancestors.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * @return  The array of <tt>Package</tt> objects defined by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     *          <tt>ClassLoader</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    protected Package[] getPackages() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1487
        Map<String, Package> map;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        synchronized (packages) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1489
            map = new HashMap<String, Package>(packages);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        Package[] pkgs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            pkgs = parent.getPackages();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            pkgs = Package.getSystemPackages();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        if (pkgs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            for (int i = 0; i < pkgs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                String pkgName = pkgs[i].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                if (map.get(pkgName) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                    map.put(pkgName, pkgs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1505
        return map.values().toArray(new Package[map.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
    // -- Native library access --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * Returns the absolute path name of a native library.  The VM invokes this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     * method to locate the native libraries that belong to classes loaded with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * this class loader. If this method returns <tt>null</tt>, the VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * searches the library along the path specified as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * "<tt>java.library.path</tt>" property.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * @param  libname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     *         The library name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     * @return  The absolute path of the native library
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     * @see  System#loadLibrary(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     * @see  System#mapLibraryName(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    protected String findLibrary(String libname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * The inner class NativeLibrary denotes a loaded native library instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * Every classloader contains a vector of loaded native libraries in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * private field <tt>nativeLibraries</tt>.  The native libraries loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * into the system are entered into the <tt>systemNativeLibraries</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * <p> Every native library requires a particular version of JNI. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * denoted by the private <tt>jniVersion</tt> field.  This field is set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * the VM when it loads the library, and used by the VM to pass the correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * version of JNI to the native methods.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * @see      ClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * @since    1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    static class NativeLibrary {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        // opaque handle to native library, used in native code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        long handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        // the version of JNI environment the native library requires.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        private int jniVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        // the class from which the library is loaded, also indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        // the loader this native library belongs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        private Class fromClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        // the canonicalized name of the native library.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        native void load(String name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        native long find(String name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        native void unload();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        public NativeLibrary(Class fromClass, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            this.fromClass = fromClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            synchronized (loadedLibraryNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                if (fromClass.getClassLoader() != null && handle != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                    /* remove the native library name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                    int size = loadedLibraryNames.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                    for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                        if (name.equals(loadedLibraryNames.elementAt(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                            loadedLibraryNames.removeElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                    /* unload the library. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                    ClassLoader.nativeLibraryContext.push(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                        unload();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                        ClassLoader.nativeLibraryContext.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        // Invoked in the VM to determine the context class in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        // JNI_Load/JNI_Unload
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        static Class getFromClass() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1591
            return ClassLoader.nativeLibraryContext.peek().fromClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
    // The "default" domain. Set as the default ProtectionDomain on newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
    // created classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
    private ProtectionDomain defaultDomain = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
    // Returns (and initializes) the default domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
    private synchronized ProtectionDomain getDefaultDomain() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        if (defaultDomain == null) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1602
            CodeSource cs = new CodeSource(null, (Certificate[]) null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
            defaultDomain = new ProtectionDomain(cs, null, this, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        return defaultDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
    // All native library names we've loaded.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1609
    private static Vector<String> loadedLibraryNames
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1610
        = new Vector<String>();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1611
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    // Native libraries belonging to system classes.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1613
    private static Vector<NativeLibrary> systemNativeLibraries
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1614
        = new Vector<NativeLibrary>();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1615
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
    // Native libraries associated with the class loader.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1617
    private Vector<NativeLibrary> nativeLibraries
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1618
        = new Vector<NativeLibrary>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    // native libraries being loaded/unloaded.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1621
    private static Stack<NativeLibrary> nativeLibraryContext
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1622
        = new Stack<NativeLibrary>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
    // The paths searched for libraries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
    static private String usr_paths[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
    static private String sys_paths[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
    private static String[] initializePath(String propname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        String ldpath = System.getProperty(propname, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        String ps = File.pathSeparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        int ldlen = ldpath.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        int i, j, n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        // Count the separators in the path
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        i = ldpath.indexOf(ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        while (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            i = ldpath.indexOf(ps, i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        // allocate the array of paths - n :'s = n + 1 path elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        String[] paths = new String[n + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        // Fill the array with paths from the ldpath
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        n = i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        j = ldpath.indexOf(ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        while (j >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
            if (j - i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                paths[n++] = ldpath.substring(i, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            } else if (j - i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                paths[n++] = ".";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
            i = j + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
            j = ldpath.indexOf(ps, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        paths[n] = ldpath.substring(i, ldlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        return paths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
    // Invoked in the java.lang.Runtime class to implement load and loadLibrary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    static void loadLibrary(Class fromClass, String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                            boolean isAbsolute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        ClassLoader loader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            (fromClass == null) ? null : fromClass.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        if (sys_paths == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            usr_paths = initializePath("java.library.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            sys_paths = initializePath("sun.boot.library.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        if (isAbsolute) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            if (loadLibrary0(fromClass, new File(name))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            throw new UnsatisfiedLinkError("Can't load library: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            String libfilename = loader.findLibrary(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            if (libfilename != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                File libfile = new File(libfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                if (!libfile.isAbsolute()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                    throw new UnsatisfiedLinkError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    "ClassLoader.findLibrary failed to return an absolute path: " + libfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                if (loadLibrary0(fromClass, libfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                throw new UnsatisfiedLinkError("Can't load " + libfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        for (int i = 0 ; i < sys_paths.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            File libfile = new File(sys_paths[i], System.mapLibraryName(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            if (loadLibrary0(fromClass, libfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
            for (int i = 0 ; i < usr_paths.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                File libfile = new File(usr_paths[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                                        System.mapLibraryName(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                if (loadLibrary0(fromClass, libfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        // Oops, it failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        throw new UnsatisfiedLinkError("no " + name + " in java.library.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
    private static boolean loadLibrary0(Class fromClass, final File file) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1709
        boolean exists = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1710
            new PrivilegedAction<Object>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                public Object run() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1712
                    return file.exists() ? Boolean.TRUE : null;
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1713
                }})
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1714
            != null;
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1715
        if (!exists) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
            name = file.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
        ClassLoader loader =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
            (fromClass == null) ? null : fromClass.getClassLoader();
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1726
        Vector<NativeLibrary> libs =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
            loader != null ? loader.nativeLibraries : systemNativeLibraries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
        synchronized (libs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            int size = libs.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
            for (int i = 0; i < size; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1731
                NativeLibrary lib = libs.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                if (name.equals(lib.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            synchronized (loadedLibraryNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                if (loadedLibraryNames.contains(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                    throw new UnsatisfiedLinkError
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                        ("Native Library " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                         name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                         " already loaded in another classloader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                /* If the library is being loaded (must be by the same thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                 * because Runtime.load and Runtime.loadLibrary are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                 * synchronous). The reason is can occur is that the JNI_OnLoad
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                 * function can cause another loadLibrary invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                 * Thus we can use a static stack to hold the list of libraries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
                 * we are loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                 * If there is a pending load operation for the library, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                 * immediately return success; otherwise, we raise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                 * UnsatisfiedLinkError.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                int n = nativeLibraryContext.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                for (int i = 0; i < n; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1758
                    NativeLibrary lib = nativeLibraryContext.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                    if (name.equals(lib.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                        if (loader == lib.fromClass.getClassLoader()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                            throw new UnsatisfiedLinkError
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                                ("Native Library " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                                 name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                                 " is being loaded in another classloader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                NativeLibrary lib = new NativeLibrary(fromClass, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
                nativeLibraryContext.push(lib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
                    lib.load(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                    nativeLibraryContext.pop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                if (lib.handle != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
                    loadedLibraryNames.addElement(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                    libs.addElement(lib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
    // Invoked in the VM class linking code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
    static long findNative(ClassLoader loader, String name) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1789
        Vector<NativeLibrary> libs =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            loader != null ? loader.nativeLibraries : systemNativeLibraries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        synchronized (libs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
            int size = libs.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
            for (int i = 0; i < size; i++) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1794
                NativeLibrary lib = libs.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                long entry = lib.find(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                if (entry != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                    return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
    // -- Assertion management --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
    // The default toggle for assertion checking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
    private boolean defaultAssertionStatus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
    // Maps String packageName to Boolean package default assertion status Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    // that the default package is placed under a null map key.  If this field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
    // is null then we are delegating assertion status queries to the VM, i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
    // none of this ClassLoader's assertion status modification methods have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    // been invoked.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1814
    private Map<String, Boolean> packageAssertionStatus = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
    // Maps String fullyQualifiedClassName to Boolean assertionStatus If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
    // field is null then we are delegating assertion status queries to the VM,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
    // i.e., none of this ClassLoader's assertion status modification methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
    // have been invoked.
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1820
    Map<String, Boolean> classAssertionStatus = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
     * Sets the default assertion status for this class loader.  This setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     * determines whether classes loaded by this class loader and initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     * in the future will have assertions enabled or disabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
     * This setting may be overridden on a per-package or per-class basis by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     * invoking {@link #setPackageAssertionStatus(String, boolean)} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     * #setClassAssertionStatus(String, boolean)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     * @param  enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     *         <tt>true</tt> if classes loaded by this class loader will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     *         henceforth have assertions enabled by default, <tt>false</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     *         if they will have assertions disabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
    public synchronized void setDefaultAssertionStatus(boolean enabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        if (classAssertionStatus == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            initializeJavaAssertionMaps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
        defaultAssertionStatus = enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     * Sets the package default assertion status for the named package.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     * package default assertion status determines the assertion status for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     * classes initialized in the future that belong to the named package or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     * any of its "subpackages".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * <p> A subpackage of a package named p is any package whose name begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * with "<tt>p.</tt>".  For example, <tt>javax.swing.text</tt> is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     * subpackage of <tt>javax.swing</tt>, and both <tt>java.util</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * <tt>java.lang.reflect</tt> are subpackages of <tt>java</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     * <p> In the event that multiple package defaults apply to a given class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     * the package default pertaining to the most specific package takes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     * precedence over the others.  For example, if <tt>javax.lang</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     * <tt>javax.lang.reflect</tt> both have package defaults associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * them, the latter package default applies to classes in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     * <tt>javax.lang.reflect</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * <p> Package defaults take precedence over the class loader's default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     * assertion status, and may be overridden on a per-class basis by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     * {@link #setClassAssertionStatus(String, boolean)}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     * @param  packageName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     *         The name of the package whose package default assertion status
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     *         is to be set. A <tt>null</tt> value indicates the unnamed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     *         package that is "current"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     *         (<a href="http://java.sun.com/docs/books/jls/">Java Language
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     *         Specification</a>, section 7.4.2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * @param  enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     *         <tt>true</tt> if classes loaded by this classloader and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     *         belonging to the named package or any of its subpackages will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     *         have assertions enabled by default, <tt>false</tt> if they will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     *         have assertions disabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
    public synchronized void setPackageAssertionStatus(String packageName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                                                       boolean enabled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        if (packageAssertionStatus == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            initializeJavaAssertionMaps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1887
        packageAssertionStatus.put(packageName, enabled);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * Sets the desired assertion status for the named top-level class in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * class loader and any nested classes contained therein.  This setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     * takes precedence over the class loader's default assertion status, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     * over any applicable per-package default.  This method has no effect if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     * the named class has already been initialized.  (Once a class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     * initialized, its assertion status cannot change.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     * <p> If the named class is not a top-level class, this invocation will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     * have no effect on the actual assertion status of any class. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     * @param  className
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     *         The fully qualified class name of the top-level class whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     *         assertion status is to be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     * @param  enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     *         <tt>true</tt> if the named class is to have assertions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     *         enabled when (and if) it is initialized, <tt>false</tt> if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     *         class is to have assertions disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
    public synchronized void setClassAssertionStatus(String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
                                                     boolean enabled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        if (classAssertionStatus == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            initializeJavaAssertionMaps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1918
        classAssertionStatus.put(className, enabled);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     * Sets the default assertion status for this class loader to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     * <tt>false</tt> and discards any package defaults or class assertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     * status settings associated with the class loader.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     * provided so that class loaders can be made to ignore any command line or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
     * persistent assertion status settings and "start with a clean slate."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
    public synchronized void clearAssertionStatus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
         * Whether or not "Java assertion maps" are initialized, set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
         * them to empty maps, effectively ignoring any present settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
         */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1936
        classAssertionStatus = new HashMap<String, Boolean>();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1937
        packageAssertionStatus = new HashMap<String, Boolean>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
        defaultAssertionStatus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     * Returns the assertion status that would be assigned to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     * class if it were to be initialized at the time this method is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * If the named class has had its assertion status set, the most recent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     * setting will be returned; otherwise, if any package default assertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     * status pertains to this class, the most recent setting for the most
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     * specific pertinent package default assertion status is returned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     * otherwise, this class loader's default assertion status is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
     * @param  className
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
     *         The fully qualified class name of the class whose desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     *         assertion status is being queried.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     * @return  The desired assertion status of the specified class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * @see  #setClassAssertionStatus(String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * @see  #setPackageAssertionStatus(String, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     * @see  #setDefaultAssertionStatus(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
    synchronized boolean desiredAssertionStatus(String className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        Boolean result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        // assert classAssertionStatus   != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        // assert packageAssertionStatus != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        // Check for a class entry
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1971
        result = classAssertionStatus.get(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        if (result != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            return result.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        // Check for most specific package entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        int dotIndex = className.lastIndexOf(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        if (dotIndex < 0) { // default package
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1978
            result = packageAssertionStatus.get(null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            if (result != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                return result.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        while(dotIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            className = className.substring(0, dotIndex);
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1984
            result = packageAssertionStatus.get(className);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
            if (result != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                return result.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
            dotIndex = className.lastIndexOf(".", dotIndex-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        // Return the classloader default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        return defaultAssertionStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
    // Set up the assertions with information provided by the VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
    private void initializeJavaAssertionMaps() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        // assert Thread.holdsLock(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1998
        classAssertionStatus = new HashMap<String, Boolean>();
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1999
        packageAssertionStatus = new HashMap<String, Boolean>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        AssertionStatusDirectives directives = retrieveDirectives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        for(int i = 0; i < directives.classes.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
            classAssertionStatus.put(directives.classes[i],
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2004
                                     directives.classEnabled[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        for(int i = 0; i < directives.packages.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
            packageAssertionStatus.put(directives.packages[i],
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2008
                                       directives.packageEnabled[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        defaultAssertionStatus = directives.deflt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
    // Retrieves the assertion directives from the VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
    private static native AssertionStatusDirectives retrieveDirectives();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2018
class SystemClassLoaderAction
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2019
    implements PrivilegedExceptionAction<ClassLoader> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
    private ClassLoader parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
    SystemClassLoaderAction(ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
        this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2026
    public ClassLoader run() throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        String cls = System.getProperty("java.system.class.loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        if (cls == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2032
        Constructor ctor = Class.forName(cls, true, parent)
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2033
            .getDeclaredConstructor(new Class[] { ClassLoader.class });
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2034
        ClassLoader sys = (ClassLoader) ctor.newInstance(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2035
            new Object[] { parent });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
        Thread.currentThread().setContextClassLoader(sys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
        return sys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
}