⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classfile.java

📁 JPC: x86 PC Hardware Emulator. 牛津大学开发的一个纯JAVA的x86系统结构硬件模拟器。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        {
        case 'V':
            return 0;
        case 'B':
        case 'C':
        case 'F':
        case 'I':
        case 'L':
        case 'S':
        case 'Z':
        case '[':
            return 1;
        case 'D':
        case 'J':
            return 2;
        }
        throw new IllegalStateException();
    }

    protected String getConstantPoolUtf8(int index)
    {
        return ((ConstantPoolInfo.Utf8Info)constantPool[index]).getBytes();
    }

    protected String getConstantPoolMethodDescriptor(int index)
    {
        ConstantPoolInfo cpi = constantPool[index];
        //get name and type index from method ref
        index = ((ConstantPoolInfo.MethodRefInfo) cpi).getNameAndTypeIndex();        
        cpi = constantPool[index];
        //get descriptor index from name and type
        index = ((ConstantPoolInfo.NameAndTypeInfo) cpi).getDescriptorIndex();
        cpi = constantPool[index];
        
        return ((ConstantPoolInfo.Utf8Info) cpi).getBytes();
    }

    /** @return stack delta caused by an invoke on this method descriptor -- delta = within () - outside () */
    protected int getMethodStackDelta(String methodDescriptor)
    {
//         System.out.println("methodDescriptor = " + methodDescriptor);
//         int end = methodDescriptor.lastIndexOf(")");
//         int begin = methodDescriptor.lastIndexOf("(", end);
//         String s = methodDescriptor.substring(begin + 1, end);
//         System.out.println("methodDescriptor = " + methodDescriptor);

        int argLength = getMethodArgLength(methodDescriptor);
        
        int count = 0;
        int delta = 0;
        boolean inReference = false;
        boolean inParameterDescriptor = false;
        char ch;
        for(int i = 0; i < methodDescriptor.length(); i++)
        {
            ch = methodDescriptor.charAt(i);
            if (ch == ')')
                return argLength - getFieldLength(methodDescriptor.charAt(i + 1));
        }
        throw new IllegalStateException("Invalid method descriptor");
    }

    /** @return count of arguments -- within () */
    int getMethodArgLength(String methodDescriptor)
    {
        int count = 0;
        boolean inReference = false;

        for(int i = 0; i < methodDescriptor.length(); i++)
        {
            char ch = methodDescriptor.charAt(i);
            switch(ch)
            {
            case '[':
                while((ch = methodDescriptor.charAt(++i)) == '[');
                if (ch != 'L') {
                    count += 1;
                    break;
                }
            case 'L':
                while(methodDescriptor.charAt(++i) != ';');
                count += 1;
                break;
            case 'B':
            case 'C':
            case 'F':
            case 'I':
            case 'S':
            case 'Z':
                count += 1;
                break;
            case 'D':
            case 'J':
                count += 2;
                break;
            case ')':
                return count;
            default:
                break;
            }
        }
        throw new IllegalStateException("Invalid method descriptor");
    }


    private static String getDescriptor(Class cls)
    {
        if (cls.isArray())
            return cls.getName().replace('.','/');
        else
        {
            if (cls.isPrimitive()) {
                if (cls.equals(Byte.TYPE))
                    return "B";
                else if (cls.equals(Character.TYPE))
                    return "C";
                else if (cls.equals(Double.TYPE))
                    return "D";
                else if (cls.equals(Float.TYPE))
                    return "F";
                else if (cls.equals(Integer.TYPE))
                    return "I";
                else if (cls.equals(Long.TYPE))
                    return "J";
                else if (cls.equals(Short.TYPE))
                    return "S";
                else if (cls.equals(Boolean.TYPE))
                    return "Z";
                else if (cls.equals(Void.TYPE))
                    return "V";
            } else {
                return 'L' + cls.getName().replace('.','/') + ';';
            }
        }    
        throw new IllegalStateException("They added a primitive!!! Is it unsigned!!! " + cls.getName());
    }

    private int searchConstantPool(ConstantPoolInfo query)
    {
	Integer value = (Integer)(constantPoolMap.get(query));
	if (value != null)
	    return value.intValue();
	else
	    return -1;

//         for (int i = 1; i < constantPoolCount; i++) {
//             if (constantPool[i].equals(query))
//                 return i;
//         }
//         return -1;
    }

    private MethodInfo getMethodInfo(String methodName)
    {
        for(int i = 0; (i < methodsCount); i++)
        {
            int index = methods[i].getNameIndex();
            if (constantPool[index].getTag() == ConstantPoolInfo.UTF8)
            {
                if (((ConstantPoolInfo.Utf8Info) constantPool[index]).getBytes().equals(methodName))
                    return methods[i];
            }
        }
        return null;
    }

    private void readMagic(DataInputStream in) throws IOException
    {
        magic = in.readInt();
    }

    private void writeMagic(DataOutputStream out) throws IOException
    {
        out.writeInt(magic);
    }

    private void readVersion(DataInputStream in) throws IOException
    {
        minorVersion = in.readUnsignedShort();
        majorVersion = in.readUnsignedShort();
    }

    private void writeVersion(DataOutputStream out) throws IOException
    {
        out.writeShort(minorVersion);
        out.writeShort(majorVersion);
    }

    private void readConstantPool(DataInputStream in) throws IOException
    {
        constantPoolCount = in.readUnsignedShort();
        // be aware that constant pool indices start at 1!! (not 0)
	constantPool = new ConstantPoolInfo[MAX_CONSTANT_POOL_SIZE];
	constantPoolMap = new HashMap();
//         constantPool = new ConstantPoolInfo[constantPoolCount];
        for(int i = 1; i < constantPoolCount; i++)
        {
	    ConstantPoolInfo cpInfo = ConstantPoolInfo.construct(in);
	    constantPool[i] = cpInfo;
	    constantPoolMap.put(cpInfo, new Integer(i));
            if ((constantPool[i] instanceof ConstantPoolInfo.DoubleInfo) || 
                (constantPool[i] instanceof ConstantPoolInfo.LongInfo))
            {
                i++;
                constantPool[i] = constantPool[i-1];
            }
        }
    }

    private void writeConstantPool(DataOutputStream out) throws IOException
    {
        out.writeShort(constantPoolCount);
        // be aware that constant pool indices start at 1!! (not 0)
        for(int i = 1; i < constantPoolCount; i++)
        {
            constantPool[i].write(out);
            if ((constantPool[i] instanceof ConstantPoolInfo.DoubleInfo) || 
                (constantPool[i] instanceof ConstantPoolInfo.LongInfo))
                i++;
        }
    }

    private void readAccessFlags(DataInputStream in) throws IOException
    {
        accessFlags = in.readUnsignedShort();
    }

    private void writeAccessFlags(DataOutputStream out) throws IOException
    {
        out.writeShort(accessFlags);
    }

    private void readThisClass(DataInputStream in) throws IOException
    {
        thisClass = in.readUnsignedShort();
    }

    private void writeThisClass(DataOutputStream out) throws IOException
    {
        out.writeShort(thisClass);
    }

    private void readSuperClass(DataInputStream in) throws IOException
    {
        superClass = in.readUnsignedShort();
    }

    private void writeSuperClass(DataOutputStream out) throws IOException
    {
        out.writeShort(superClass);
    }

    private void readInterfaces(DataInputStream in) throws IOException
    {
        interfacesCount = in.readUnsignedShort();
        interfaces = new int[interfacesCount];
        for(int i = 0; i < interfacesCount; i++)
        {
            interfaces[i] = in.readUnsignedShort();
        }
    }

    private void writeInterfaces(DataOutputStream out) throws IOException
    {
        out.writeShort(interfacesCount);
        for(int i = 0; i < interfacesCount; i++)
            out.writeShort(interfaces[i]);
    }

    private void readFields(DataInputStream in, ConstantPoolInfo[] pool) throws IOException
    {
        fieldsCount = in.readUnsignedShort();
        fields = new FieldInfo[fieldsCount];
        for(int i = 0; (i < fieldsCount); i++)
        {
            fields[i] = new FieldInfo(in, pool);
        }
    }

    private void writeFields(DataOutputStream out) throws IOException
    {
        out.writeShort(fieldsCount);
        for(int i = 0; (i < fieldsCount); i++)
            fields[i].write(out);
    }

    private void readMethods(DataInputStream in, ConstantPoolInfo[] pool) throws IOException
    {
        methodsCount = in.readUnsignedShort();
        methods = new MethodInfo[methodsCount];
        for(int i = 0; (i < methodsCount); i++)
            methods[i] = new MethodInfo(in, pool);
    }

    private void writeMethods(DataOutputStream out) throws IOException
    {
        out.writeShort(methodsCount);
        for(int i = 0; (i < methodsCount); i++)
            methods[i].write(out);
    }

    private void readAttributes(DataInputStream in, ConstantPoolInfo[] pool) throws IOException
    {
        attributesCount = in.readUnsignedShort();
        attributes = new AttributeInfo[attributesCount];
        for(int i = 0; (i < attributesCount); i++)
            attributes[i] = AttributeInfo.construct(in, pool);
    }

    private void writeAttributes(DataOutputStream out) throws IOException
    {
        out.writeShort(attributesCount);
        for(int i = 0; (i < attributesCount); i++)
            attributes[i].write(out);
    }
}

⌨️ 快捷键说明

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