📄 classloader.java
字号:
* @return the requested Class * @throws ClassNotFoundException when the class can not be found * @since 1.2 */ protected Class findClass (String name) throws ClassNotFoundException { throw new ClassNotFoundException (name); } // Protection Domain definitions // FIXME: should there be a special protection domain used for native code? // The permission required to check what a classes protection domain is. static final Permission protectionDomainPermission = new RuntimePermission("getProtectionDomain"); // The protection domain returned if we cannot determine it. static ProtectionDomain unknownProtectionDomain; // Protection domain to use when a class is defined without one specified. static ProtectionDomain defaultProtectionDomain; static { Permissions permissions = new Permissions(); permissions.add(new AllPermission()); unknownProtectionDomain = new ProtectionDomain(null, permissions); CodeSource cs = new CodeSource(null, null); defaultProtectionDomain = new ProtectionDomain(cs, Policy.getPolicy().getPermissions(cs)); } /** * Defines a class, given the class-data. According to the JVM, this * method should not be used; instead use the variant of this method * in which the name of the class being defined is specified * explicitly. * <P> * If the name of the class, as specified (implicitly) in the class * data, denotes a class which has already been loaded by this class * loader, an instance of * <code>java.lang.ClassNotFoundException</code> will be thrown. * * @param data bytes in class file format. * @param off offset to start interpreting data. * @param len length of data in class file. * @return the class defined. * @exception java.lang.ClassNotFoundException * @exception java.lang.LinkageError * @see ClassLoader#defineClass(String,byte[],int,int) */ protected final Class defineClass(byte[] data, int off, int len) throws ClassFormatError { return defineClass (null, data, off, len, defaultProtectionDomain); } /** * Helper to define a class using a string of bytes without a * ProtectionDomain. Subclasses should call this method from their * <code>findClass()</code> implementation. The name should use '.' * separators, and discard the trailing ".class". The default protection * domain has the permissions of * <code>Policy.getPolicy().getPermissions(new CodeSource(null, null))<code>. * * @param name the name to give the class, or null if unknown * @param data the data representing the classfile, in classfile format * @param offset the offset into the data where the classfile starts * @param len the length of the classfile data in the array * @return the class that was defined * @throws ClassFormatError if data is not in proper classfile format * @throws IndexOutOfBoundsException if offset or len is negative, or * offset + len exceeds data * @throws SecurityException if name starts with "java." * @since 1.1 */ protected final Class defineClass(String name, byte[] data, int off, int len) throws ClassFormatError { return defineClass (name, data, off, len, defaultProtectionDomain); } /** * Defines a class, given the class-data. This is preferable * over <code>defineClass(byte[],off,len)</code> since it is more * secure. If the expected name does not match that of the class * file, <code>ClassNotFoundException</code> is thrown. If * <code>name</code> denotes the name of an already loaded class, a * <code>LinkageError</code> is thrown. * <p> * * FIXME: How do we assure that the class-file data is not being * modified, simultaneously with the class loader running!? If this * was done in some very clever way, it might break security. * Right now I am thinking that defineclass should make sure never to * read an element of this array more than once, and that that would * assure the ``immutable'' appearance. It is still to be determined * if this is in fact how defineClass operates. * * @param name the expected name. * @param data bytes in class file format. * @param off offset to start interpreting data. * @param len length of data in class file. * @param protectionDomain security protection domain for the class. * @return the class defined. * @exception java.lang.ClassNotFoundException * @exception java.lang.LinkageError */ protected final synchronized Class defineClass(String name, byte[] data, int off, int len, ProtectionDomain protectionDomain) throws ClassFormatError { if (data==null || data.length < off+len || off<0 || len<0) throw new ClassFormatError ("arguments to defineClass " + "are meaningless"); // as per 5.3.5.1 if (name != null && findLoadedClass (name) != null) throw new java.lang.LinkageError ("class " + name + " already loaded"); if (protectionDomain == null) protectionDomain = defaultProtectionDomain; try { Class retval = defineClass0 (name, data, off, len, protectionDomain); loadedClasses.put(retval.getName(), retval); return retval; } catch (LinkageError x) { throw x; // rethrow } catch (VirtualMachineError x) { throw x; // rethrow } catch (Throwable x) { // This should never happen, or we are beyond spec. InternalError r = new InternalError ("Unexpected exception " + "while defining class " + name); r.initCause(x); throw r; } } /** This is the entry point of defineClass into the native code */ private native Class defineClass0 (String name, byte[] data, int off, int len, ProtectionDomain protectionDomain) throws ClassFormatError; /** * Link the given class. This will bring the class to a state where * the class initializer can be run. Linking involves the following * steps: * <UL> * <LI> Prepare (allocate and internalize) the constant strings that * are used in this class. * <LI> Allocate storage for static fields, and define the layout * of instance fields. * <LI> Perform static initialization of ``static final'' int, * long, float, double and String fields for which there is a * compile-time constant initializer. * <LI> Create the internal representation of the ``vtable''. * </UL> * For <code>gcj</code>-compiled classes, only the first step is * performed. The compiler will have done the rest already. * <P> * This is called by the system automatically, * as part of class initialization; there is no reason to ever call * this method directly. * <P> * For historical reasons, this method has a name which is easily * misunderstood. Java classes are never ``resolved''. Classes are * linked; whereas method and field references are resolved. * * @param clazz the class to link. * @exception java.lang.LinkageError */ protected final void resolveClass(Class clazz) { resolveClass0(clazz); } static void resolveClass0(Class clazz) { synchronized (clazz) { try { linkClass0 (clazz); } catch (Throwable x) { markClassErrorState0 (clazz); LinkageError e; if (x instanceof LinkageError) e = (LinkageError)x; else if (x instanceof ClassNotFoundException) { e = new NoClassDefFoundError("while resolving class: " + clazz.getName()); e.initCause (x); } else { e = new LinkageError ("unexpected exception during linking: " + clazz.getName()); e.initCause (x); } throw e; } } } /** Internal method. Calls _Jv_PrepareClass and * _Jv_PrepareCompiledClass. This is only called from resolveClass. */ private static native void linkClass0(Class clazz); /** Internal method. Marks the given clazz to be in an erroneous * state, and calls notifyAll() on the class object. This should only * be called when the caller has the lock on the class object. */ private static native void markClassErrorState0(Class clazz); /** * Defines a new package and creates a Package object. * The package should be defined before any class in the package is * defined with <code>defineClass()</code>. The package should not yet * be defined before in this classloader or in one of its parents (which * means that <code>getPackage()</code> should return <code>null</code>). * All parameters except the <code>name</code> of the package may be * <code>null</code>. * <p> * Subclasses should call this method from their <code>findClass()</code> * implementation before calling <code>defineClass()</code> on a Class * in a not yet defined Package (which can be checked by calling * <code>getPackage()</code>). * * @param name The name of the Package * @param specTitle The name of the specification * @param specVendor The name of the specification designer * @param specVersion The version of this specification * @param implTitle The name of the implementation * @param implVendor The vendor that wrote this implementation * @param implVersion The version of this implementation * @param sealed If sealed the origin of the package classes * @return the Package object for the specified package * * @exception IllegalArgumentException if the package name is null or if * it was already defined by this classloader or one of its parents. * * @see Package * @since 1.2 */ protected Package definePackage(String name, String specTitle, String specVendor, String specVersion, String implTitle, String implVendor, String implVersion, URL sealed) { if (getPackage(name) != null) throw new IllegalArgumentException("Package " + name + " already defined"); Package p = new Package(name, specTitle, specVendor, specVersion, implTitle, implVendor, implVersion, sealed); synchronized (definedPackages) { definedPackages.put(name, p); } return p; } /** * Returns the Package object for the requested package name. It returns * null when the package is not defined by this classloader or one of its * parents. * * @param name the package name to find * @return the package, if defined * @since 1.2 */ protected Package getPackage(String name) { Package p; if (parent == null) // XXX - Should we use the bootstrap classloader? p = null; else p = parent.getPackage(name); if (p == null) { synchronized (definedPackages) { p = (Package) definedPackages.get(name); } } return p; } /** * Returns all Package objects defined by this classloader and its parents. * * @return an array of all defined packages * @since 1.2 */ protected Package[] getPackages() { Package[] allPackages; // Get all our packages. Package[] packages; synchronized(definedPackages) { packages = new Package[definedPackages.size()]; definedPackages.values().toArray(packages); } // If we have a parent get all packages defined by our parents. if (parent != null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -