package com.coderising.jvm.method; import com.coderising.jvm.clz.ClassFile; //import com.coderising.jvm.cmd.ByteCodeCommand; import com.coderising.jvm.attr.AttributeInfo; import com.coderising.jvm.attr.CodeAttr; import com.coderising.jvm.attr.LineNumberTable; import com.coderising.jvm.attr.LocalVariableTable; import com.coderising.jvm.attr.StackMapTable; import com.coderising.jvm.constant.ConstantPool; import com.coderising.jvm.constant.UTF8Info; import com.coderising.jvm.loader.ByteCodeIterator; public class Method { private int accessFlag; private int nameIndex; private int descriptorIndex; private CodeAttr codeAttr; private ClassFile clzFile; public ClassFile getClzFile() { return clzFile; } public int getNameIndex() { return nameIndex; } public int getDescriptorIndex() { return descriptorIndex; } public CodeAttr getCodeAttr() { return codeAttr; } public void setCodeAttr(CodeAttr code) { this.codeAttr = code; } public Method(ClassFile clzFile,int accessFlag, int nameIndex, int descriptorIndex) { this.clzFile = clzFile; this.accessFlag = accessFlag; this.nameIndex = nameIndex; this.descriptorIndex = descriptorIndex; } public String toString() { ConstantPool pool = this.clzFile.getConstantPool(); StringBuilder buffer = new StringBuilder(); String name = ((UTF8Info)pool.getConstantInfo(this.nameIndex)).getValue(); String desc = ((UTF8Info)pool.getConstantInfo(this.descriptorIndex)).getValue(); buffer.append(name).append(":").append(desc).append("\n"); buffer.append(this.codeAttr.toString(pool)); return buffer.toString(); } public static Method parse(ClassFile clzFile,ConstantPool pool, ByteCodeIterator iter){ int accessFlag = iter.nextU2AsInt(); int nameIndex = iter.nextU2AsInt(); System.out.println("++>MethodName="+pool.getUTF8String(nameIndex)); int descIndex = iter.nextU2AsInt(); Method method = new Method(clzFile,accessFlag,nameIndex,descIndex); int attrCount = iter.nextU2AsInt(); System.out.println("AttrCount="+attrCount); for(int i=0;i<attrCount;i++){ CodeAttr codeAttr = CodeAttr.parse(clzFile,pool, iter); method.setCodeAttr(codeAttr); } return method; } // public ByteCodeCommand[] getCmds() { // return this.getCodeAttr().getCmds(); // } }