abstractbootimagebuilder.java

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

JAVA
1,109
字号
        addCompileHighOptLevel("java.util.zip");

        addCompileHighOptLevel("javax.naming");

        addCompileHighOptLevel("gnu.java.io");
        addCompileHighOptLevel("gnu.java.io.decode");
        addCompileHighOptLevel("gnu.java.io.encode");
        addCompileHighOptLevel("gnu.java.lang");
        addCompileHighOptLevel("gnu.java.lang.reflect");

        addCompileHighOptLevel("org.jnode.assembler");
        addCompileHighOptLevel("org.jnode.boot");
        addCompileHighOptLevel("org.jnode.plugin");
        addCompileHighOptLevel("org.jnode.plugin.manager");
        addCompileHighOptLevel("org.jnode.plugin.model");
        addCompileHighOptLevel("org.jnode.protocol.plugin");
        addCompileHighOptLevel("org.jnode.protocol.system");
        addCompileHighOptLevel("org.jnode.system");
        addCompileHighOptLevel("org.jnode.system.event");
        addCompileHighOptLevel("org.jnode.system.util");
        addCompileHighOptLevel("org.jnode.util");
        addCompileHighOptLevel("org.jnode.vm");
        addCompileHighOptLevel("org.jnode.vm.bytecode");
        addCompileHighOptLevel("org.jnode.vm.classmgr");
        addCompileHighOptLevel("org.jnode.vm.compiler");
        addCompileHighOptLevel("org.jnode.vm.compiler.ir");
        addCompileHighOptLevel("org.jnode.vm.compiler.ir.quad");
        addCompileHighOptLevel("org.jnode.vm.memmgr");
        addCompileHighOptLevel("org.jnode.vm.memmgr.def");
    }

    protected final void addCompileHighOptLevel(String name) {
        compileHighOptLevelPackages.add(name);
    }

    /**
     * Create a set of the names of those classes that can be safely
     * instantiated during the boot process (and written as instance to the
     * boot image). Usually java.xxx classes cannot be used, since Sun may have
     * implemented them different from our implementation. If the
     * implementation is difference, the image will contain incorrect fiels and
     * values.
     * 
     * @return Set<String>
     */
    protected Set setupLegalInstanceClasses() {
        final HashSet set = new HashSet();
        set.add("java.lang.Integer");
        set.add("java.lang.Long");
        set.add("java.lang.String");
        set.add("org.jnode.util.Logger");
        return set;
    }

    /**
     * Patch any fields in the header, just before the image is written to
     * disk.
     * 
     * @param os
     * @throws BuildException
     */
    protected abstract void patchHeader(NativeStream os) throws BuildException;

    /**
     * Print any unresolved labels to the out stream and generate a list file
     * for all public labels
     * 
     * @param os
     * @param bootClasses
     * @throws BuildException
     * @throws UnresolvedObjectRefException
     */
    protected final void printLabels(NativeStream os, VmType[] bootClasses,
            VmStatics statics) throws BuildException,
            UnresolvedObjectRefException {
        try {
            int unresolvedCount = 0;
            final PrintWriter w = new PrintWriter(new FileWriter(listFile));
            // Print a list of boot classes.
            for (int i = 0; i < bootClasses.length; i++) {
                final VmType vmClass = bootClasses[ i];
                w.print("bootclass ");
                w.print(i);
                w.print(": ");
                w.print(vmClass.getName());
                if (vmClass instanceof VmClassType) {
                    final int cnt = ((VmClassType) vmClass).getInstanceCount();
                    if (cnt > 0) {
                        w.print(", ");
                        w.print(cnt);
                        w.print(" instances");
                    }
                }
                int cnt = vmClass.getNoInterfaces();
                if (cnt > 0) {
                    w.print(", ");
                    w.print(cnt);
                    w.print(" interfaces");
                }
                w.print(vmClass.isInitialized() ? "" : ", not initialized");
                w.println();
            }
            w.println();

            // Print the statics table
            final int[] table = (int[]) statics.getTable();
            for (int i = 0; i < table.length; i++) {
                w.print(NumberUtils.hex((VmArray.DATA_OFFSET + i) << 2));
                w.print(":");
                w.print(NumberUtils.hex(statics.getType(i), 2));
                w.print("\t");
                w.print(NumberUtils.hex(table[ i]));
                w.println();
            }

            // Look for unresolved labels and put all resolved
            // label into the sorted map. This will be used later
            // to print to the listing file.
            final Collection xrefs = os.getObjectRefs();
            final SortedMap map = new TreeMap();
            for (Iterator i = xrefs.iterator(); i.hasNext();) {
                NativeStream.ObjectRef ref;
                ref = (NativeStream.ObjectRef) i.next();
                if (!ref.isResolved()) {
                    StringBuffer buf = new StringBuffer();
                    buf.append("  $" + Integer.toHexString(ref.getOffset()));
                    buf.append("\t" + ref.getObject());
                    System.err.println("Unresolved label " + buf.toString());
                    unresolvedCount++;
                } else {
                    map.put(new Integer(ref.getOffset()), ref);
                }
            }

            if (unresolvedCount > 0) { throw new BuildException("There are "
                    + unresolvedCount + " unresolved labels"); } // Print the
            // listing
            // file.
            for (Iterator i = map.values().iterator(); i.hasNext();) {
                final NativeStream.ObjectRef ref;
                ref = (NativeStream.ObjectRef) i.next();
                final Object object = ref.getObject();
                w.print('$');
                w.print(hex(ref.getOffset() + os.getBaseAddr()));
                w.print('\t');
                w.print(object);
                w.print(" (");
                if (object instanceof VmSystemObject) {
                    final String info = ((VmSystemObject) object)
                            .getExtraInfo();
                    if (info != null) {
                        w.print(info);
                        w.print(", ");
                    }
                }
                w.print(object.getClass().getName());
                w.println(')');
            }
            w.close();
        } catch (IOException ex) {
            throw new BuildException("Writing list", ex);
        }
    }

    private static final String zero8 = "00000000";

    private static final String zero16 = zero8 + zero8;

    /**
     * Convert a given int to an hexidecimal representation of 8 characters
     * long.
     * 
     * @param v
     * @return The hex string
     */
    protected final String hex(int v) {
        String s = Integer.toHexString(v);
        return zero8.substring(s.length()) + s;
    }

    /**
     * Convert a given int to an hexidecimal representation of 16 characters
     * long.
     * 
     * @param v
     * @return The hex string
     */
    protected final String hex(long v) {
        String s = Long.toHexString(v);
        return zero16.substring(s.length()) + s;
    }

    /**
     * Returns the classesURL.
     * 
     * @return URL
     */
    public final URL getClassesURL() {
        return classesURL;
    }

    /**
     * Returns the debugFile.
     * 
     * @return File
     */
    public final File getDebugFile() {
        return debugFile;
    }

    /**
     * Returns the destFile.
     * 
     * @return File
     */
    public final File getDestFile() {
        return destFile;
    }

    /**
     * Returns the kernelFile.
     * 
     * @return File
     */
    public final File getKernelFile() {
        return kernelFile;
    }

    /**
     * Returns the listFile.
     * 
     * @return File
     */
    public final File getListFile() {
        return listFile;
    }

    /**
     * Sets the classesURL.
     * 
     * @param classesURL
     *            The classesURL to set
     */
    public final void setClassesURL(URL classesURL) {
        this.classesURL = classesURL;
    }

    /**
     * Sets the debugFile.
     * 
     * @param debugFile
     *            The debugFile to set
     */
    public final void setDebugFile(File debugFile) {
        this.debugFile = debugFile;
    }

    /**
     * Sets the destFile.
     * 
     * @param destFile
     *            The destFile to set
     */
    public final void setDestFile(File destFile) {
        this.destFile = destFile;
    }

    /**
     * Sets the kernelFile.
     * 
     * @param kernelFile
     *            The kernelFile to set
     */
    public final void setKernelFile(File kernelFile) {
        this.kernelFile = kernelFile;
    }

    /**
     * Sets the listFile.
     * 
     * @param listFile
     *            The listFile to set
     */
    public void setListFile(File listFile) {
        this.listFile = listFile;
    }

    /**
     * @return File
     */
    public final File getJarFile() {
        return jarFile;
    }

    /**
     * Sets the jarFile.
     * 
     * @param jarFile
     *            The jarFile to set
     */
    public final void setJarFile(File jarFile) {
        this.jarFile = jarFile;
    }

    /**
     * Gets the internal class loader
     * 
     * @return The class loader
     */
    public VmSystemClassLoader getClsMgr() {
        return clsMgr;
    }

    protected abstract void logStatistics(NativeStream os);

    /**
     * Load all classes from the bootjar.
     */
    protected void loadSystemClasses() throws IOException,
            ClassNotFoundException {
        final JarInputStream jis = new JarInputStream(new FileInputStream(
                jarFile));
        JarEntry entry;
        while ((entry = jis.getNextJarEntry()) != null) {
            final String eName = entry.getName();
            if (eName.endsWith(".class")) {
                final String cName = eName.substring(0,
                        eName.length() - ".class".length()).replace('/', '.');
                boolean load = false;

                if (compileHighOptLevelPackages.contains(cName)) {
                    load = true;
                }

                final int lastDotIdx = cName.lastIndexOf('.');
                final String pkg = (lastDotIdx > 0) ? cName.substring(0,
                        lastDotIdx) : "";

                if (compileHighOptLevelPackages.contains(pkg)) {
                    load = true;
                }

                if (load) {
                    loadClass(cName, true);
                }
            }
        }

    }
    
    protected void cleanup() {
        clsMgr = null;
        blockedObjects.clear();
    }
    
    /**
     * @return Returns the version.
     */
    public final String getVersion() {
        return this.version;
    }
    /**
     * @param version The version to set.
     */
    public final void setVersion(String version) {
        this.version = version;
    }
}

⌨️ 快捷键说明

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