📄 bytecode.java
字号:
* Appends INVOKESPECIAL. * * @param clazz the index of <code>CONSTANT_Class_info</code> * structure. * @param name the method name * @param desc the descriptor of the method signature. * * @see Descriptor#ofMethod(CtClass,CtClass[]) * @see Descriptor#ofConstructor(CtClass[]) */ public void addInvokespecial(int clazz, String name, String desc) { add(INVOKESPECIAL); addIndex(constPool.addMethodrefInfo(clazz, name, desc)); growStack(Descriptor.dataSize(desc) - 1); } /** * Appends INVOKESTATIC. * * @param clazz the target class. * @param name the method name * @param returnType the return type. * @param paramTypes the parameter types. */ public void addInvokestatic(CtClass clazz, String name, CtClass returnType, CtClass[] paramTypes) { String desc = Descriptor.ofMethod(returnType, paramTypes); addInvokestatic(clazz, name, desc); } /** * Appends INVOKESTATIC. * * @param clazz the target class. * @param name the method name * @param desc the descriptor of the method signature. * * @see Descriptor#ofMethod(CtClass,CtClass[]) */ public void addInvokestatic(CtClass clazz, String name, String desc) { addInvokestatic(constPool.addClassInfo(clazz), name, desc); } /** * Appends INVOKESTATIC. * * @param classname the fully-qualified class name. * @param name the method name * @param desc the descriptor of the method signature. * * @see Descriptor#ofMethod(CtClass,CtClass[]) */ public void addInvokestatic(String classname, String name, String desc) { addInvokestatic(constPool.addClassInfo(classname), name, desc); } /** * Appends INVOKESTATIC. * * @param clazz the index of <code>CONSTANT_Class_info</code> * structure. * @param name the method name * @param desc the descriptor of the method signature. * * @see Descriptor#ofMethod(CtClass,CtClass[]) */ public void addInvokestatic(int clazz, String name, String desc) { add(INVOKESTATIC); addIndex(constPool.addMethodrefInfo(clazz, name, desc)); growStack(Descriptor.dataSize(desc)); } /** * Appends INVOKEVIRTUAL. * * <p>The specified method must not be an inherited method. * It must be directly declared in the class specified * in <code>clazz</code>. * * @param clazz the target class. * @param name the method name * @param returnType the return type. * @param paramTypes the parameter types. */ public void addInvokevirtual(CtClass clazz, String name, CtClass returnType, CtClass[] paramTypes) { String desc = Descriptor.ofMethod(returnType, paramTypes); addInvokevirtual(clazz, name, desc); } /** * Appends INVOKEVIRTUAL. * * <p>The specified method must not be an inherited method. * It must be directly declared in the class specified * in <code>clazz</code>. * * @param clazz the target class. * @param name the method name * @param desc the descriptor of the method signature. * * @see Descriptor#ofMethod(CtClass,CtClass[]) */ public void addInvokevirtual(CtClass clazz, String name, String desc) { addInvokevirtual(constPool.addClassInfo(clazz), name, desc); } /** * Appends INVOKEVIRTUAL. * * <p>The specified method must not be an inherited method. * It must be directly declared in the class specified * in <code>classname</code>. * * @param classname the fully-qualified class name. * @param name the method name * @param desc the descriptor of the method signature. * * @see Descriptor#ofMethod(CtClass,CtClass[]) */ public void addInvokevirtual(String classname, String name, String desc) { addInvokevirtual(constPool.addClassInfo(classname), name, desc); } /** * Appends INVOKEVIRTUAL. * * <p>The specified method must not be an inherited method. * It must be directly declared in the class specified * by <code>clazz</code>. * * @param clazz the index of <code>CONSTANT_Class_info</code> * structure. * @param name the method name * @param desc the descriptor of the method signature. * * @see Descriptor#ofMethod(CtClass,CtClass[]) */ public void addInvokevirtual(int clazz, String name, String desc) { add(INVOKEVIRTUAL); addIndex(constPool.addMethodrefInfo(clazz, name, desc)); growStack(Descriptor.dataSize(desc) - 1); } /** * Appends INVOKEINTERFACE. * * @param clazz the target class. * @param name the method name * @param returnType the return type. * @param paramTypes the parameter types. * @param count the count operand of the instruction. */ public void addInvokeinterface(CtClass clazz, String name, CtClass returnType, CtClass[] paramTypes, int count) { String desc = Descriptor.ofMethod(returnType, paramTypes); addInvokeinterface(clazz, name, desc, count); } /** * Appends INVOKEINTERFACE. * * @param clazz the target class. * @param name the method name * @param desc the descriptor of the method signature. * @param count the count operand of the instruction. * * @see Descriptor#ofMethod(CtClass,CtClass[]) */ public void addInvokeinterface(CtClass clazz, String name, String desc, int count) { addInvokeinterface(constPool.addClassInfo(clazz), name, desc, count); } /** * Appends INVOKEINTERFACE. * * @param classname the fully-qualified class name. * @param name the method name * @param desc the descriptor of the method signature. * @param count the count operand of the instruction. * * @see Descriptor#ofMethod(CtClass,CtClass[]) */ public void addInvokeinterface(String classname, String name, String desc, int count) { addInvokeinterface(constPool.addClassInfo(classname), name, desc, count); } /** * Appends INVOKEINTERFACE. * * @param clazz the index of <code>CONSTANT_Class_info</code> * structure. * @param name the method name * @param desc the descriptor of the method signature. * @param count the count operand of the instruction. * * @see Descriptor#ofMethod(CtClass,CtClass[]) */ public void addInvokeinterface(int clazz, String name, String desc, int count) { add(INVOKEINTERFACE); addIndex(constPool.addInterfaceMethodrefInfo(clazz, name, desc)); add(count); add(0); growStack(Descriptor.dataSize(desc) - 1); } /** * Appends LDC or LDC_W. The pushed item is a <code>String</code> * object. * * @param s the character string pushed by LDC or LDC_W. */ public void addLdc(String s) { addLdc(constPool.addStringInfo(s)); } /** * Appends LDC or LDC_W. * * @param i index into the constant pool. */ public void addLdc(int i) { if (i > 0xFF) { addOpcode(LDC_W); addIndex(i); } else { addOpcode(LDC); add(i); } } /** * Appends LDC2_W. The pushed item is a long value. */ public void addLdc2w(long l) { addOpcode(LDC2_W); addIndex(constPool.addLongInfo(l)); } /** * Appends LDC2_W. The pushed item is a double value. */ public void addLdc2w(double d) { addOpcode(LDC2_W); addIndex(constPool.addDoubleInfo(d)); } /** * Appends NEW. * * @param clazz the class of the created instance. */ public void addNew(CtClass clazz) { addOpcode(NEW); addIndex(constPool.addClassInfo(clazz)); } /** * Appends NEW. * * @param classname the fully-qualified class name. */ public void addNew(String classname) { addOpcode(NEW); addIndex(constPool.addClassInfo(classname)); } /** * Appends ANEWARRAY. * * @param classname the qualified class name of the element type. */ public void addAnewarray(String classname) { addOpcode(ANEWARRAY); addIndex(constPool.addClassInfo(classname)); } /** * Appends ICONST and ANEWARRAY. * * @param clazz the elememnt type. * @param length the array length. */ public void addAnewarray(CtClass clazz, int length) { addIconst(length); addOpcode(ANEWARRAY); addIndex(constPool.addClassInfo(clazz)); } /** * Appends NEWARRAY for primitive types. * * @param atype <code>T_BOOLEAN</code>, <code>T_CHAR</code>, ... * @see Opcode */ public void addNewarray(int atype, int length) { addIconst(length); addOpcode(NEWARRAY); add(atype); } /** * Appends MULTINEWARRAY. * * @param clazz the array type. * @param dimensions the sizes of all dimensions. * @return the length of <code>dimensions</code>. */ public int addMultiNewarray(CtClass clazz, int[] dimensions) { int len = dimensions.length; for (int i = 0; i < len; ++i) addIconst(dimensions[i]); growStack(len); return addMultiNewarray(clazz, len); } /** * Appends MULTINEWARRAY. The size of every dimension must have been * already pushed on the stack. * * @param clazz the array type. * @param dim the number of the dimensions. * @return the value of <code>dim</code>. */ public int addMultiNewarray(CtClass clazz, int dim) { add(MULTIANEWARRAY); addIndex(constPool.addClassInfo(clazz)); add(dim); growStack(1 - dim); return dim; } /** * Appends MULTINEWARRAY. * * @param desc the type descriptor of the created array. * @param dim dimensions. * @return the value of <code>dim</code>. */ public int addMultiNewarray(String desc, int dim) { add(MULTIANEWARRAY); addIndex(constPool.addClassInfo(desc)); add(dim); growStack(1 - dim); return dim; } /** * Appends PUTFIELD. * * @param c the target class. * @param name the field name. * @param desc the descriptor of the field type. */ public void addPutfield(CtClass c, String name, String desc) { addPutfield0(c, null, name, desc); } /** * Appends PUTFIELD. * * @param classname the fully-qualified name of the target class. * @param name the field name. * @param desc the descriptor of the field type. */ public void addPutfield(String classname, String name, String desc) { // if classnaem is null, the target class is THIS. addPutfield0(null, classname, name, desc); } private void addPutfield0(CtClass target, String classname, String name, String desc) { add(PUTFIELD); // target is null if it represents THIS. int ci = classname == null ? constPool.addClassInfo(target) : constPool.addClassInfo(classname); addIndex(constPool.addFieldrefInfo(ci, name, desc)); growStack(-1 - Descriptor.dataSize(desc)); } /** * Appends PUTSTATIC. * * @param c the target class. * @param name the field name. * @param desc the descriptor of the field type. */ public void addPutstatic(CtClass c, String name, String desc) { addPutstatic0(c, null, name, desc); } /** * Appends PUTSTATIC. * * @param classname the fully-qualified name of the target class. * @param fieldName the field name. * @param desc the descriptor of the field type. */ public void addPutstatic(String classname, String fieldName, String desc) { // if classname is null, the target class is THIS. addPutstatic0(null, classname, fieldName, desc); } private void addPutstatic0(CtClass target, String classname, String fieldName, String desc) { add(PUTSTATIC); // target is null if it represents THIS. int ci = classname == null ? constPool.addClassInfo(target) : constPool.addClassInfo(classname); addIndex(constPool.addFieldrefInfo(ci, fieldName, desc)); growStack(-Descriptor.dataSize(desc)); } /** * Appends ARETURN, IRETURN, .., or RETURN. * * @param type the return type. */ public void addReturn(CtClass type) { if (type == null) addOpcode(RETURN); else if (type.isPrimitive()) { CtPrimitiveType ptype = (CtPrimitiveType)type; addOpcode(ptype.getReturnOp()); } else addOpcode(ARETURN); } /** * Appends RET. * * @param var local variable */ public void addRet(int var) { if (var < 0x100) { addOpcode(RET); add(var); } else { addOpcode(WIDE); addOpcode(RET); addIndex(var); } } /** * Appends instructions for executing * <code>java.lang.System.println(<i>message</i>)</code>. * * @param message printed message. */ public void addPrintln(String message) { addGetstatic("java.lang.System", "err", "Ljava/io/PrintStream;"); addLdc(message); addInvokevirtual("java.io.PrintStream", "println", "(Ljava/lang/String;)V"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -