vmtype.java

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

JAVA
1,918
字号
    public final int getAccessFlags() {
        return modifiers;
    }

    /**
     * Find the field within the given class that has the given name and
     * signature.
     * 
     * @param name
     * @param signature
     * @return The field
     */
    public final VmField getDeclaredField(String name, String signature) {
        if (fieldTable != null) {
            int count = fieldTable.length;
            for (int i = 0; i < count; i++) {
                VmField fs = fieldTable[ i];
                if (fs.nameEquals(name)) {
                    if ((signature == null) || fs.signatureEquals(signature)) { return fs; }
                }
            }
        }
        return null;
    }

    /**
     * Find the field within the given class that has the given name and
     * signature.
     * 
     * @param name
     * @return The field
     */
    public final VmField getDeclaredField(String name) {
        return getDeclaredField(name, null);
    }

    /**
     * Find the field within the given class (or super-classes) that has the
     * given name and signature.
     * 
     * @param name
     * @param signature
     * @return The field
     */
    public final VmField getField(String name, String signature) {
        VmField f = getDeclaredField(name, signature);
        if (f != null) { return f; }
        if (superClass != null) {
            f = superClass.getField(name, signature);
            if (f != null) { return f; }
        }
        final int cnt = getNoInterfaces();
        for (int i = 0; i < cnt; i++) {
            f = allInterfaceTable[ i].getField(name, signature);
            if (f != null) { return f; }
        }

        return null;
    }

    /**
     * Find the field within the given class (or super-classes) that has the
     * given name.
     * 
     * @param name
     * @return The field
     */
    public final VmField getField(String name) {
        return getField(name, null);
    }

    /**
     * Find the field within the given class (or super-classes) that matches
     * the given fieldRef.
     * 
     * @param fieldRef
     * @return The field
     */
    public final VmField getField(VmConstFieldRef fieldRef) {
        return getField(fieldRef.getName(), fieldRef.getSignature());
    }

    /**
     * Find the method within the given class (or super-classes) that has the
     * given name and signature.
     * 
     * @param name
     * @param signature
     * @param onlyThisClass
     * @param searchInterfaces
     * @param hashCode
     * @return The method
     */
    final VmMethod getMethod(String name, String signature,
            boolean onlyThisClass, boolean searchInterfaces, int hashCode) {
        /* Search in my own method table */
        final VmMethod[] mt = this.methodTable;
        if (mt != null) {
            final int count = mt.length;
            for (int i = 0; i < count; i++) {
                final VmMethod mts = mt[ i];
                /**
                 * Use the hascode as first match, to make this as fast as
                 * possible
                 */
                int mtsHashCode = mts.getMemberHashCode();
                if (mtsHashCode == hashCode) {
                    if (mts.nameEquals(name) && mts.signatureEquals(signature)) { return mts; }
                } else if (mtsHashCode > hashCode) {
                    /**
                     * The methods are sorted on hashcode, so we can stop
                     * searching here
                     */
                    break;
                }
            }
        }

        // Is it a synthetic abstract method?
        if (isAbstract()) {
            final VmMethod method = getSyntheticAbstractMethod(name, signature,
                    hashCode);
            if (method != null) { return method; }
        }

        // Look in the superclass
        if ((superClass != null) && (!onlyThisClass)) {
            final VmMethod method = superClass.getMethod(name, signature,
                    false, false, hashCode);
            if (method != null) { return method; }
        }

        // Look in the super interfaces
        if (isInterface() || searchInterfaces) {
            final VmInterfaceClass[] ait = allInterfaceTable;
            if (ait != null) {
                final int count = ait.length;
                for (int i = 0; i < count; i++) {
                    final VmInterfaceClass intf = ait[ i];
                    final VmMethod method = intf.getMethod(name, signature,
                            true, false, hashCode);
                    if (method != null) { return method; }
                }
            }
        }

        // Not found
        return null;
    }

    /**
     * Search for an synthetic abstract class, that is not in this class, but
     * is a method of one of the implemented interfaces. Synthetic abstract
     * methods are added when the VMT is created.
     * 
     * @param name
     * @param signature
     * @param hashCode
     * @return The method
     */
    protected abstract VmMethod getSyntheticAbstractMethod(String name,
            String signature, int hashCode);

    /**
     * Find the method within the given class (or super-classes) that has the
     * given name and list of argument types.
     * 
     * @param name
     * @param argTypes
     * @param declaredOnly
     * @return The method
     */
    final VmMethod getMethod(String name, VmType[] argTypes,
            boolean declaredOnly) {

        /* Search in my own method table */
        final VmMethod[] mt = this.methodTable;
        if (mt != null) {
            final int count = mt.length;
            for (int i = 0; i < count; i++) {
                final VmMethod mts = mt[ i];
                if (mts.nameEquals(name)) {
                    if (mts.matchArgumentTypes(argTypes)) { return mts; }
                }
            }
        }

        if (isInterface() && (!declaredOnly)) {
            // Look in the super interfaces
            final VmImplementedInterface[] it = interfaceTable;
            if (it != null) {
                int count = it.length;
                for (int i = 0; i < count; i++) {
                    VmImplementedInterface intf = it[ i];
                    VmMethod method = intf.getResolvedVmClass().getMethod(name,
                            argTypes, false);
                    if (method != null) { return method; }
                }
            }
        }

        if ((superClass != null) && (!declaredOnly)) { 
        // Look in the superclass
        return superClass.getMethod(name, argTypes, false); }

        // Not found
        return null;
    }

    /**
     * Find the method within the given class that has the given name and list
     * of argument types.
     * 
     * @param name
     * @param argTypes
     * @return The method
     */
    public final VmMethod getDeclaredMethod(String name, VmType[] argTypes) {
        return getMethod(name, argTypes, true);
    }

    /**
     * Find the method within the given class and its super-classes that has
     * the given name and list of argument types.
     * 
     * @param name
     * @param argTypes
     * @return The method
     */
    public final VmMethod getMethod(String name, VmType[] argTypes) {
        return getMethod(name, argTypes, false);
    }

    /**
     * Find the method within the given class (or super-classes) that has the
     * given name and signature.
     * 
     * @param name
     * @param signature
     * @param onlyThisClass
     * @param searchInterfaces
     * @return The method
     */
    private final VmMethod getMethod(String name, String signature,
            boolean onlyThisClass, boolean searchInterfaces) {
        return getMethod(name, signature, onlyThisClass, searchInterfaces,
                VmMember.calcHashCode(name, signature));
    }

    /**
     * Find the method within this class that has the given name and signature.
     * 
     * @param name
     * @param signature
     * @return The method
     */
    public final VmMethod getDeclaredMethod(String name, String signature) {
        return getMethod(name, signature, true, false, VmMember.calcHashCode(
                name, signature));
    }

    /**
     * Find the method within the given class (or super-classes) that has the
     * given name and signature.
     * 
     * @param name
     * @param signature
     * @return The method
     */
    public final VmMethod getMethod(String name, String signature) {
        return getMethod(name, signature, false, true, VmMember.calcHashCode(
                name, signature));
    }

    /**
     * Find the method within the given class (or super-classes) that matches
     * the given methodRef.
     * 
     * @param methodRef
     * @return The method
     */
    public final VmMethod getMethod(VmConstMethodRef methodRef) {
        return getMethod(methodRef.getName(), methodRef.getSignature(), false,
                true, methodRef.getMemberHashCode());
    }

    /**
     * Do in the following order: Verification, Preparation, Resolution
     * 
     * @return VmClass This class
     */
    public final VmType link() {
        prepare();
        verify();
        compile();
        if (isInvalid()) {
            if (errorMsg != null) {
                throw new LinkageError(errorMsg);
            } else {
                throw new LinkageError("Class invalid");
            }
        }
        if (arrayClass != null) {
            //arrayClass.link();
        }
        return this;
    }

    /**
     * Prepare this class. This method is not synchronized since it is called
     * frequently. A simple test is done to see if the class has already been
     * prepared, if not a synchronized helper method is called to do the actual
     * prepare.
     */
    void prepare() {
        if (!isPrepared()) {
            doPrepare();
        }
    }

    /**
     * Compile this class. This method is not synchronized since it is called
     * frequently. A simple test is done to see if the class has already been
     * compiled, if not a synchronized helper method is called to do the actual
     * compile.
     */
    void compile() {
        if (!isCompiled()) {
            doCompile();
        }
    }

    /**
     * Verify this class. This method is not synchronized since it is called
     * frequently. A simple test is done to see if the class has already been
     * verfied, if not a synchronized helper method is called to do the actual
     * verify.
     */
    void verify() {
        if (!isVerified()) {
            doVerify();
        }
    }

    /**
     * Prepare this class. The following steps will be taken.
     * <ul>
     * <li>Load the super class (if any)
     * <li>Load the implemented interface classes (if any)
     * <li>Calculate the object size
     * <li>Fix the offset for all declared non-static fields
     * <li>Create the VMT
     * </ul>
     */
    private synchronized void doPrepare() {
        if (isPrepared()) { return; }
        if (isPreparing()) { throw new Error("Recursive prepare in "
                + getName()); }

        state |= VmTypeState.ST_PREPARING;

        try {
            // Step 1a: Load the super class
            if ((superClass == null) && (superClassName != null)) {
                setSuperClass((VmNormalClass) loader.loadClass(superClassName,
                        false));
            }

            // Step 1b: Resolve the super class
            if (superClass != null) {
                superClass.prepare();
                superClassDepth = superClass.getSuperClassDepth() + 1;
            }

            /**
             * Step 2a: Load the implemented interface classes (if any)
             */
            final int cnt = getNoDeclaredInterfaces();
            for (int i = 0; i < cnt; i++) {
                final VmImplementedInterface intf = interfaceTable[ i];
                intf.resolve(loader);
            }

            // Now we're at the DEFINED state
            state |= VmTypeState.ST_DEFINED;
        } catch (ClassNotFoundException ex) {
            state |= VmTypeState.ST_INVALID;
            state &= ~VmTypeState.ST_PREPARING;
            errorMsg = ex.toString();
            throw new NoClassDefFoundError(ex.getMessage());
        }

        // Step 3: Calculate the object size
        // Step 4a: Fix the offset for all declared non-static fields
        // Step 4b: Create the referenceOffsets field
        prepareForInstantiation();

        /* Build the allInterfaceTable */
        final HashSet all = new HashSet();
        getAllInterfaces(all, this);
        this.allInterfaceTable = new VmInterfaceClass[ all.size()];
        all.toArray(allInterfaceTable);

        // Step 5: Create the TIB
        final Object[] tib = prepareTIB(all);

        /* Build the interface method table */
        if (all.size() > 0) {
            final IMTBuilder imtBuilder = prepareIMT(all);
            if (imtBuilder != null) {
                tib[ TIBLayout.IMT_INDEX] = imtBuilder.getImt();
                tib[ TIBLayout.IMTCOLLISIONS_INDEX] = imtBuilder
                        .getImtCollisions();
            }

            // Look for Uninterruptible
            final int length = all.size();
            final String uninterruptibleName = Uninterruptible.class.getName();
            for (int i = 0; i < length; i++) {
                if (allInterfaceTable[ i].getName().equals(uninterruptibleName)) {
                    final int mCount = getNoDeclaredMethods();
                    for (int m = 0; m < mCount; m++) {
                        getDeclaredMethod(m).setUninterruptible();
                    }
                    break;
                }
            }
        }

        /* Build the super classes array */
        this.superClassesArray = createSuperClassesArray(all);
        if (tib != null) {
            tib[ TIBLayout.SUPERCLASSES_INDEX] = superClassesArray;
        }

        /* Is there a finalizer method other then in java.lang.Object? */
        if (superClass != null) {
            // superClass != null, so we're not in java.lang.Object
            finalizeMethod = getMethod("finalize", "()V", true, false);
            if (finalizeMethod == null) {
                finalizeMethod = superClass.getFinalizeMethod();
            }
            if (finalizeMethod != null) {
                modifiers |= Modifier.ACC_FINALIZER;
            }
        }

        /* If there is no static initializer, this class is initialized */
        if (getMethod("<clinit>", "()V", true, false) == null) {
            state |= VmTypeState.ST_INITIALIZED;
        }

        // Now we're in the PREPARED state
        state |= VmTypeState.ST_PREPARED;
        state &= ~VmTypeState.ST_PREPARING;

        // Notify all threads that are waiting for me
        notifyAll();
    }

    /**
     * Compile this type.
     */
    private synchronized void doCompile() {
        if (isCompiled()) { return; }
        if (!isCompiling()) {
            if (loader.isCompileRequired()) {
                state |= VmTypeState.ST_COMPILING;
                //BootLog.debug("Compiling " + getName());

                // Compile the superclass (if any)
                if (superClass != null) {
                    superClass.compile();
                }

                // Compile the methods with the least optimizing compiler
                final int count;
                try {
                    count = doCompileRuntime(0);
                } catch (Throwable ex) {

⌨️ 快捷键说明

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