vmsystemclassloader.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 695 行 · 第 1/2 页

JAVA
695
字号
     * 
     * @param clsName
     * @return InputStream
     * @throws MalformedURLException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private byte[] getClassStream(String clsName) throws MalformedURLException,
            IOException, ClassNotFoundException {
        final String fName = clsName.replace('.', '/') + ".class";
        final InputStream is = getResourceAsStream(fName);
        if (is == null) {
            throw new ClassNotFoundException("Class resource of " + clsName
                    + " not found.");
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int len;
            byte[] buf = new byte[ 4096];
            while ((len = is.read(buf)) > 0) {
                bos.write(buf, 0, len);
            }
            buf = null;
            is.close();

            return bos.toByteArray();
        }
    }

    /**
     * @see org.jnode.vm.classmgr.VmClassLoader#resourceExists(java.lang.String)
     */
    public final boolean resourceExists(String resName) {
        try {
            return (getResourceAsStream(resName) != null);
        } catch (IOException ex) {
            return false;
        }
    }

    /**
     * Gets an inputstream for a resource with the given name.
     * 
     * @param name
     *            The name of the resource
     * @return An opened inputstream to the resource with the given name, or
     *         null if not found.
     * @throws MalformedURLException
     * @throws IOException
     */
    public InputStream getResourceAsStream(String name)
            throws MalformedURLException, IOException {
        if (name.startsWith("/")) {
            name = name.substring(1);
        }
        if (classesURL != null) {
            if (verbose) {
                System.out.println("Loading resource " + name);
            }
            URL url = new URL(classesURL, name);
            //System.out.println("url=" + url);
            return url.openStream();
        } else if ((systemJarFile != null) || (systemRtJar != null)) {
            if (systemJarFile == null) {
                systemJarFile = new JarFile(systemRtJar);
            }
            JarEntry entry = systemJarFile.getJarEntry(name);
            if (entry != null) {
                return systemJarFile.getInputStream(entry);
            } else {
                return null;
            }
        } else {
            throw new IOException("Don't no how to load " + name);
        }
    }

    /**
     * Returns the verbose.
     * 
     * @return boolean
     */
    public boolean isVerbose() {
        return verbose;
    }

    /**
     * Sets the verbose.
     * 
     * @param verbose
     *            The verbose to set
     */
    public void setVerbose(boolean verbose) {
        this.verbose = verbose;
    }

    /**
     * Returns the failOnNewLoad.
     * 
     * @return boolean
     */
    public boolean isFailOnNewLoad() {
        return failOnNewLoad;
    }

    /**
     * Sets the failOnNewLoad.
     * 
     * @param failOnNewLoad
     *            The failOnNewLoad to set
     */
    public void setFailOnNewLoad(boolean failOnNewLoad) {
        if (classesURL != null) {
            this.failOnNewLoad = failOnNewLoad;
        }
    }

    /**
     * Compile the given method
     * 
     * @param vmMethod
     *            The method to compile
     * @param optLevel
     *            The optimization level
     */
    public void compileRuntime(VmMethod vmMethod, int optLevel) {
        final NativeCodeCompiler cmps[] = arch.getCompilers();
        final NativeCodeCompiler cmp;
        if (optLevel < 0) {
            optLevel = 0;
        } else if (optLevel >= cmps.length) {
            optLevel = cmps.length - 1;
        }
        if (vmMethod.getNativeCodeOptLevel() < optLevel) {
            cmp = cmps[ optLevel];
            cmp.compileRuntime(vmMethod, getResolver(), optLevel, null);
        }
    }

    /**
     * Initialize this classloader during the initialization of the VM. If
     * needed, the tree of classes is generated from the boot class list.
     */
    protected void initialize() {
        if (classInfos == null) {
            final TreeMap classInfos = new TreeMap();
            final VmType[] list = bootClasses;
            final int count = list.length;
            for (int i = 0; i < count; i++) {
                final VmType vmClass = list[ i];
                final ClassInfo ci = new ClassInfo(vmClass);
                classInfos.put(ci.getName(), ci);
            }
            this.classInfos = classInfos;
        }
    }

    /**
     * @return ObjectResolver
     */
    public ObjectResolver getResolver() {
        if (resolver == null) {
            resolver = new Unsafe.UnsafeObjectResolver();
        }
        return resolver;
    }

    /**
     * Set the object resolver. This can be called only once.
     */
    public void setResolver(ObjectResolver resolver) {
        if (this.resolver == null) {
            this.resolver = resolver;
        } else {
            throw new SecurityException("Cannot overwrite resolver");
        }
    }

    /**
     * Sets the systemRtJar.
     * 
     * @param systemRtJar
     *            The systemRtJar to set
     */
    public void setSystemRtJar(byte[] systemRtJar) {
        if (this.systemRtJar == null) {
            this.systemRtJar = systemRtJar;
        }
    }

    /**
     * Is this loader the system classloader?
     * 
     * @return boolean
     */
    public boolean isSystemClassLoader() {
        VmSystemClassLoader systemLoader = VmSystem.getSystemClassLoader();
        return ((systemLoader == this) || (systemLoader == null));
    }

    static class ClassLoaderWrapper extends ClassLoader {

        public ClassLoaderWrapper(VmSystemClassLoader vmClassLoader) {
            super(vmClassLoader);
        }
    }

    /**
     * Class that holds information of a loading &amp; loaded class.
     * 
     * @author epr
     */
    static class ClassInfo {

        /** Name of the class */
        private final String name;

        /** The class itself */
        private VmType vmClass;

        /** Classloading got an error? */
        private boolean error = false;

        private String errorMsg;

        /**
         * Create a new instance
         * 
         * @param name
         */
        public ClassInfo(String name) {
            this.name = name;
        }

        /**
         * Create a new instance
         * 
         * @param vmClass
         */
        public ClassInfo(VmType vmClass) {
            this.name = vmClass.getName();
            this.vmClass = vmClass;
            if (name.indexOf('/') >= 0) { throw new IllegalArgumentException(
                    "vmClass.getName() contains '/'"); }
        }

        /**
         * @return
         */
        public final String getName() {
            return name;
        }

        /**
         * @return Type
         * @throws ClassNotFoundException
         */
        public final synchronized VmType getVmClass()
                throws ClassNotFoundException {
            while (vmClass == null) {
                if (error) { throw new ClassNotFoundException(name + "; "
                        + errorMsg); }
                try {
                    wait();
                } catch (InterruptedException ex) {
                    // Just ignore
                }
            }
            return vmClass;
        }

        /**
         * @param class1
         */
        public final synchronized void setVmClass(VmType class1) {
            if (this.vmClass == null) {
                this.vmClass = class1;
                notifyAll();
            } else {
                throw new SecurityException("Can only set the VmClass once.");
            }
        }

        /**
         * Signal a class loading error. This will release other threads
         * waiting for this class with a ClassNotFoundException.
         */
        public final synchronized void setLoadError(String errorMsg) {
            this.error = true;
            this.errorMsg = errorMsg;
            notifyAll();
        }

        /**
         * Has the class wrapped by this object been loaded?
         * 
         * @return
         */
        public boolean isLoaded() {
            return (vmClass != null);
        }
    }

    /**
     * Gets the mapping between method name&types and selectors.
     * 
     * @return The map
     */
    public final SelectorMap getSelectorMap() {
        return selectorMap;
    }

    /**
     * Gets the statics table.
     * 
     * @return The statics table
     */
    public final VmStatics getStatics() {
        return statics;
    }

    /**
     * Gets the architecture used by this loader.
     * 
     * @return The architecture
     */
    public final VmArchitecture getArchitecture() {
        return arch;
    }

    /**
     * Should prepared classes be compiled.
     * 
     * @return boolean
     */
    public boolean isCompileRequired() {
        return requiresCompile;
    }

    /**
     * Should prepared classes be compiled.
     */
    public void setCompileRequired() {
        requiresCompile = true;
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?