📄 methodgen.java
字号:
/* * @return array of thrown exceptions */ public String[] getExceptions() { String[] e = new String[throws_vec.size()]; throws_vec.copyInto(e); return e; } /** * @return `Exceptions' attribute of all the exceptions thrown by this method. */ private ExceptionTable getExceptionTable(ConstantPoolGen cp) { int size = throws_vec.size(); int[] ex = new int[size]; try { for(int i=0; i < size; i++) ex[i] = cp.addClass((String)throws_vec.elementAt(i)); } catch(ArrayIndexOutOfBoundsException e) {} return new ExceptionTable(cp.addUtf8("Exceptions"), 2 + 2 * size, ex, cp.getConstantPool()); } /** * Add an attribute to the code. Currently, the JVM knows about the * LineNumberTable, LocalVariableTable and StackMap attributes, * where the former two will be generated automatically and the * latter is used for the MIDP only. Other attributes will be * ignored by the JVM but do no harm. * * @param a attribute to be added */ public void addCodeAttribute(Attribute a) { code_attrs_vec.addElement(a); } /** * Remove a code attribute. */ public void removeCodeAttribute(Attribute a) { code_attrs_vec.removeElement(a); } /** * Remove all code attributes. */ public void removeCodeAttributes() { code_attrs_vec.removeAllElements(); } /** * @return all attributes of this method. */ public Attribute[] getCodeAttributes() { Attribute[] attributes = new Attribute[code_attrs_vec.size()]; code_attrs_vec.copyInto(attributes); return attributes; } /** * Get method object. Never forget to call setMaxStack() or setMaxStack(max), respectively, * before calling this method (the same applies for max locals). * * @return method object */ public Method getMethod() { String signature = getSignature(); int name_index = cp.addUtf8(name); int signature_index = cp.addUtf8(signature); /* Also updates positions of instructions, i.e., their indices */ byte[] byte_code = null; if(il != null) byte_code = il.getByteCode(); LineNumberTable lnt = null; LocalVariableTable lvt = null; /* Create LocalVariableTable and LineNumberTable attributes (for debuggers, e.g.) */ if((variable_vec.size() > 0) && !strip_attributes) addCodeAttribute(lvt = getLocalVariableTable(cp)); if((line_number_vec.size() > 0) && !strip_attributes) addCodeAttribute(lnt = getLineNumberTable(cp)); Attribute[] code_attrs = getCodeAttributes(); /* Each attribute causes 6 additional header bytes */ int attrs_len = 0; for(int i=0; i < code_attrs.length; i++) attrs_len += (code_attrs[i].getLength() + 6); CodeException[] c_exc = getCodeExceptions(); int exc_len = c_exc.length * 8; // Every entry takes 8 bytes Code code = null; if((il != null) && !isAbstract()) { code = new Code(cp.addUtf8("Code"), 8 + byte_code.length + // prologue byte code 2 + exc_len + // exceptions 2 + attrs_len, // attributes max_stack, max_locals, byte_code, c_exc, code_attrs, cp.getConstantPool()); addAttribute(code); } ExceptionTable et = null; if(throws_vec.size() > 0) addAttribute(et = getExceptionTable(cp)); // Add `Exceptions' if there are "throws" clauses Method m = new Method(access_flags, name_index, signature_index, getAttributes(), cp.getConstantPool()); // Undo effects of adding attributes if(lvt != null) removeCodeAttribute(lvt); if(lnt != null) removeCodeAttribute(lnt); if(code != null) removeAttribute(code); if(et != null) removeAttribute(et); return m; } /** * Remove all NOPs from the instruction list (if possible) and update every * object refering to them, i.e., branch instructions, local variables and * exception handlers. */ public void removeNOPs() { if(il != null) { InstructionHandle next; /* Check branch instructions. */ for(InstructionHandle ih = il.getStart(); ih != null; ih = next) { next = ih.next; if((next != null) && (ih.getInstruction() instanceof NOP)) { try { il.delete(ih); } catch(TargetLostException e) { InstructionHandle[] targets = e.getTargets(); for(int i=0; i < targets.length; i++) { InstructionTargeter[] targeters = targets[i].getTargeters(); for(int j=0; j < targeters.length; j++) targeters[j].updateTarget(targets[i], next); } } } } } } /** * Set maximum number of local variables. */ public void setMaxLocals(int m) { max_locals = m; } public int getMaxLocals() { return max_locals; } /** * Set maximum stack size for this method. */ public void setMaxStack(int m) { max_stack = m; } public int getMaxStack() { return max_stack; } /**@deprecated Use setName() */ public void setMethodName(String method_name) { setName(method_name); } /**@deprecated Use getName() */ public String getMethodName() { return getName(); } /** @return class that contains this method */ public String getClassName() { return class_name; } public void setClassName(String class_name) { this.class_name = class_name; } public void setReturnType(Type return_type) { setType(return_type); } public Type getReturnType() { return getType(); } public void setArgumentTypes(Type[] arg_types) { this.arg_types = arg_types; } public Type[] getArgumentTypes() { return (Type[])arg_types.clone(); } public void setArgumentType(int i, Type type) { arg_types[i] = type; } public Type getArgumentType(int i) { return arg_types[i]; } public void setArgumentNames(String[] arg_names) { this.arg_names = arg_names; } public String[] getArgumentNames() { return (String[])arg_names.clone(); } public void setArgumentName(int i, String name) { arg_names[i] = name; } public String getArgumentName(int i) { return arg_names[i]; } /** @deprecated because of its ugly name */ public void setArgTypes(Type[] arg_types) { this.arg_types = arg_types; } /** @deprecated because of its ugly name */ public Type[] getArgTypes() { return (Type[])arg_types.clone(); } /** @deprecated because of its ugly name */ public void setArgType(int i, Type type) { arg_types[i] = type; } /** @deprecated because of its ugly name */ public Type getArgType(int i) { return arg_types[i]; } /** @deprecated because of its ugly name */ public void setArgNames(String[] arg_names) { this.arg_names = arg_names; } /** @deprecated because of its ugly name */ public String[] getArgNames() { return (String[])arg_names.clone(); } /** @deprecated because of its ugly name */ public void setArgName(int i, String name) { arg_names[i] = name; } /** @deprecated because of its ugly name */ public String getArgName(int i) { return arg_names[i]; } public InstructionList getInstructionList() { return il; } public void setInstructionList(InstructionList il) { this.il = il; } /**@deprecated Use getSignature() */ public String getMethodSignature() { return getSignature(); } public String getSignature() { return Type.getMethodSignature(type, arg_types); } /** * Computes max. stack size by performing control flow analysis. * @author <A HREF="http://www.vmeng.com/beard">Patrick C. Beard</A> */ public void setMaxStack() { if(il != null) max_stack = getMaxStack(cp, il, getExceptionHandlers()); else max_stack = 0; } /** * Compute maximum number of local variables. */ public void setMaxLocals() { if(il != null) { int max = isStatic()? 0 : 1; if(arg_types != null) for(int i=0; i < arg_types.length; i++) max += arg_types[i].getSize(); for(InstructionHandle ih = il.getStart(); ih != null; ih = ih.getNext()) { Instruction ins = ih.getInstruction(); if((ins instanceof LocalVariableInstruction) || (ins instanceof RET) || (ins instanceof IINC)) { int index = ((IndexedInstruction)ins).getIndex() + ((TypedInstruction)ins).getType(cp).getSize(); if(index > max) max = index; } } max_locals = max; } else max_locals = 0; } /** Do not/Do produce attributes code attributesLineNumberTable and * LocalVariableTable, like javac -O */ public void stripAttributes(boolean flag) { strip_attributes = flag; } static final class BranchTarget { InstructionHandle target; int stackDepth; BranchTarget(InstructionHandle target, int stackDepth) { this.target = target; this.stackDepth = stackDepth; } } static final class BranchStack { Stack branchTargets = new Stack(); Hashtable visitedTargets = new Hashtable(); public void push(InstructionHandle target, int stackDepth) { if(visited(target)) return; branchTargets.push(visit(target, stackDepth)); } public BranchTarget pop() { if(!branchTargets.empty()) { BranchTarget bt = (BranchTarget) branchTargets.pop(); return bt; } return null; } private final BranchTarget visit(InstructionHandle target, int stackDepth) { BranchTarget bt = new BranchTarget(target, stackDepth); visitedTargets.put(target, bt); return bt; } private final boolean visited(InstructionHandle target) { return (visitedTargets.get(target) != null); } } /** * Computes stack usage of an instruction list by performing control flow analysis. * * @return maximum stack depth used by method */ public static int getMaxStack(ConstantPoolGen cp, InstructionList il, CodeExceptionGen[] et) { BranchStack branchTargets = new BranchStack(); /* Initially, populate the branch stack with the exception * handlers, because these aren't (necessarily) branched to * explicitly. in each case, the stack will have depth 1, * containing the exception object. */ for (int i = 0; i < et.length; i++) { InstructionHandle handler_pc = et[i].getHandlerPC(); if (handler_pc != null) branchTargets.push(handler_pc, 1); } int stackDepth = 0, maxStackDepth = 0; InstructionHandle ih = il.getStart(); while(ih != null) { Instruction instruction = ih.getInstruction(); short opcode = instruction.getOpcode(); int delta = instruction.produceStack(cp) - instruction.consumeStack(cp); stackDepth += delta; if(stackDepth > maxStackDepth) maxStackDepth = stackDepth; // choose the next instruction based on whether current is a branch. if(instruction instanceof BranchInstruction) { BranchInstruction branch = (BranchInstruction) instruction; if(instruction instanceof Select) { // explore all of the select's targets. the default target is handled below. Select select = (Select) branch; InstructionHandle[] targets = select.getTargets(); for (int i = 0; i < targets.length; i++) branchTargets.push(targets[i], stackDepth); // nothing to fall through to. ih = null; } else if(!(branch instanceof IfInstruction)) { // if an instruction that comes back to following PC, // push next instruction, with stack depth reduced by 1. if(opcode == Constants.JSR || opcode == Constants.JSR_W) branchTargets.push(ih.getNext(), stackDepth - 1); ih = null; } // for all branches, the target of the branch is pushed on the branch stack. // conditional branches have a fall through case, selects don't, and // jsr/jsr_w return to the next instruction. branchTargets.push(branch.getTarget(), stackDepth); } else { // check for instructions that terminate the method. if(opcode == Constants.ATHROW || opcode == Constants.RET || (opcode >= Constants.IRETURN && opcode <= Constants.RETURN)) ih = null; } // normal case, go to the next instruction. if(ih != null) ih = ih.getNext(); // if we have no more instructions, see if there are any deferred branches to explore. if(ih == null) { BranchTarget bt = branchTargets.pop(); if (bt != null) { ih = bt.target; stackDepth = bt.stackDepth; } } } return maxStackDepth; } private Vector observers; /** Add observer for this object. */ public void addObserver(MethodObserver o) { if(observers == null) observers = new Vector(); observers.addElement(o); } /** Remove observer for this object. */ public void removeObserver(MethodObserver o) { if(observers != null) observers.removeElement(o); } /** Call notify() method on all observers. This method is not called * automatically whenever the state has changed, but has to be * called by the user after he has finished editing the object. */ public void update() { if(observers != null) for(Enumeration e = observers.elements(); e.hasMoreElements(); ) ((MethodObserver)e.nextElement()).notify(this); } /** * Return string representation close to declaration format, * `public static void main(String[]) throws IOException', e.g. * * @return String representation of the method. */ public final String toString() { String access = Utility.accessToString(access_flags); String signature = Type.getMethodSignature(type, arg_types); signature = Utility.methodSignatureToString(signature, name, access, true, getLocalVariableTable(cp)); StringBuffer buf = new StringBuffer(signature); if(throws_vec.size() > 0) { for(Enumeration e = throws_vec.elements(); e.hasMoreElements(); ) buf.append("\n\t\tthrows " + e.nextElement()); } return buf.toString(); } /** @return deep copy of this method */ public MethodGen copy(String class_name, ConstantPoolGen cp) { Method m = ((MethodGen)clone()).getMethod(); MethodGen mg = new MethodGen(m, class_name, this.cp); if(this.cp != cp) { mg.setConstantPool(cp); mg.getInstructionList().replaceConstantPool(this.cp, cp); } return mg; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -