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

📄 methodinfo.java

📁 本人根据自己的实际情况
💻 JAVA
字号:
/**
 *
 */
package gen.info;

import gen.ClassFile;
import gen.info.attr.AttributeInfo;
import gen.info.attr.CodeAttr;
import gen.info.attr.ExceptionsAttr;
import gen.info.constant.ConstantMethodrefInfo;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liuyi
 *
 */
public class MethodInfo {
    
    private short accessFlags;
    
    /** index of constant pool, referring to a ConstantUtf8Info */
    private short nameIndex;
    
    /** index of constant pool, referring to a ConstantUtf8Info */
    private short descriptorIndex;
    
    /**
     * only allows Code, Exceptions, Synthetic, Deprecated four attributes here
     * attributes count = attributes.size()
     */
    //private List attributes;
    private CodeAttr codeAttr;
    private ExceptionsAttr exception;
    
    //convenience for MethodrefInfo seeking
    private ConstantMethodrefInfo constMethodInfo;
    
    private ClassFile classFile;
    
    public MethodInfo(ClassFile classFile) {
        this.classFile = classFile;
        
        //attributes = new ArrayList();
        codeAttr = new CodeAttr(classFile);
    }
    
    public void setAccessFlags(int accessFlags){
        this.accessFlags = (short)accessFlags;
    }
    
    public void setNameIndex(int nameIndex){
        this.nameIndex = (short)nameIndex;
    }
    
    public void setDescriptorIndex(int descriptorIndex){
        this.descriptorIndex = (short)descriptorIndex;
    }
    
    public void setConstMethodrefInfo(ConstantMethodrefInfo cmri){
        this.constMethodInfo = cmri;
    }
    
    public int getConstMethodrefInfoIndex(){
        return constMethodInfo.getIndex();
    }
    
    public ConstantMethodrefInfo getConstMethodrefInfo(){
        return constMethodInfo;
    }
    
    public int getNameIndex(){
        return nameIndex;
    }
    
    public int getDescriptorIndex(){
        return descriptorIndex;
    }
    
        /*public void addAttribute(AttributeInfo attr){
            attributes.add(attr);
        }*/
    
    public void setCodeAttr(CodeAttr codeAttr){
        this.codeAttr = codeAttr;
    }
    
    public CodeAttr getCodeAttr(){
        return codeAttr;
    }
    
    public void addExceptionsAttr(ExceptionsAttr exception){
        this.exception = exception;
    }
    
    public void removeExceptionsAttr(){
        exception = null;
    }
    
    public ExceptionsAttr getExceptionsAttr(){
        return this.exception;
    }

    public void toBinary(DataOutputStream writer) throws IOException{
        writer.writeShort(accessFlags);
        writer.writeShort(nameIndex);
        writer.writeShort(descriptorIndex);
        
        if(exception == null){
            writer.writeShort(1);   //attributes count
            codeAttr.toBinary(writer);
        }else{
            writer.writeShort(2);   //attributes count
            codeAttr.toBinary(writer);
            exception.toBinary(writer);
        }
    }
    
    public String toString(){
        StringBuffer buffer = new StringBuffer();
        buffer.append("access:" + accessFlags + " " + classFile.getRefString(nameIndex) 
                        + classFile.getRefString(this.descriptorIndex) + ";\n");
        buffer.append(codeAttr.toString() + "\n");
        if(exception != null){
            buffer.append(classFile.getRefString(exception.getNameIndex()) + ":\n  throws "
                    + classFile.getRefString(exception.getExceptionIndex()) + "\n");
        }
        return buffer.toString();
    }
}

⌨️ 快捷键说明

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