📄 bytecode.java
字号:
* @param n an index into the local variable array. */ public void addAstore(int n) { if (n < 4) addOpcode(75 + n); // astore_<n> else if (n < 0x100) { addOpcode(ASTORE); // astore add(n); } else { addOpcode(WIDE); addOpcode(ASTORE); addIndex(n); } } /** * Appends ICONST or ICONST_<n> * * @param n the pushed integer constant. */ public void addIconst(int n) { if (n < 6 && -2 < n) addOpcode(3 + n); // iconst_<i> -1..5 else if (n <= 127 && -128 <= n) { addOpcode(16); // bipush add(n); } else if (n <= 32767 && -32768 <= n) { addOpcode(17); // sipush add(n >> 8); add(n); } else addLdc(constPool.addIntegerInfo(n)); } /** * Appends an instruction for pushing zero or null on the stack. * If the type is void, this method does not append any instruction. * * @param type the type of the zero value (or null). */ public void addConstZero(CtClass type) { if (type.isPrimitive()) { if (type == CtClass.longType) addOpcode(LCONST_0); else if (type == CtClass.floatType) addOpcode(FCONST_0); else if (type == CtClass.doubleType) addOpcode(DCONST_0); else if (type == CtClass.voidType) throw new RuntimeException("void type?"); else addOpcode(ICONST_0); } else addOpcode(ACONST_NULL); } /** * Appends ILOAD or (WIDE) ILOAD_<n> * * @param n an index into the local variable array. */ public void addIload(int n) { if (n < 4) addOpcode(26 + n); // iload_<n> else if (n < 0x100) { addOpcode(ILOAD); // iload add(n); } else { addOpcode(WIDE); addOpcode(ILOAD); addIndex(n); } } /** * Appends ISTORE or (WIDE) ISTORE_<n> * * @param n an index into the local variable array. */ public void addIstore(int n) { if (n < 4) addOpcode(59 + n); // istore_<n> else if (n < 0x100) { addOpcode(ISTORE); // istore add(n); } else { addOpcode(WIDE); addOpcode(ISTORE); addIndex(n); } } /** * Appends LCONST or LCONST_<n> * * @param n the pushed long integer constant. */ public void addLconst(long n) { if (n == 0 || n == 1) addOpcode(9 + (int)n); // lconst_<n> else addLdc2w(n); } /** * Appends LLOAD or (WIDE) LLOAD_<n> * * @param n an index into the local variable array. */ public void addLload(int n) { if (n < 4) addOpcode(30 + n); // lload_<n> else if (n < 0x100) { addOpcode(LLOAD); // lload add(n); } else { addOpcode(WIDE); addOpcode(LLOAD); addIndex(n); } } /** * Appends LSTORE or LSTORE_<n> * * @param n an index into the local variable array. */ public void addLstore(int n) { if (n < 4) addOpcode(63 + n); // lstore_<n> else if (n < 0x100) { addOpcode(LSTORE); // lstore add(n); } else { addOpcode(WIDE); addOpcode(LSTORE); addIndex(n); } } /** * Appends DCONST or DCONST_<n> * * @param d the pushed double constant. */ public void addDconst(double d) { if (d == 0.0 || d == 1.0) addOpcode(14 + (int)d); // dconst_<n> else addLdc2w(d); } /** * Appends DLOAD or (WIDE) DLOAD_<n> * * @param n an index into the local variable array. */ public void addDload(int n) { if (n < 4) addOpcode(38 + n); // dload_<n> else if (n < 0x100) { addOpcode(DLOAD); // dload add(n); } else { addOpcode(WIDE); addOpcode(DLOAD); addIndex(n); } } /** * Appends DSTORE or (WIDE) DSTORE_<n> * * @param n an index into the local variable array. */ public void addDstore(int n) { if (n < 4) addOpcode(71 + n); // dstore_<n> else if (n < 0x100) { addOpcode(DSTORE); // dstore add(n); } else { addOpcode(WIDE); addOpcode(DSTORE); addIndex(n); } } /** * Appends FCONST or FCONST_<n> * * @param f the pushed float constant. */ public void addFconst(float f) { if (f == 0.0f || f == 1.0f || f == 2.0f) addOpcode(11 + (int)f); // fconst_<n> else addLdc(constPool.addFloatInfo(f)); } /** * Appends FLOAD or (WIDE) FLOAD_<n> * * @param n an index into the local variable array. */ public void addFload(int n) { if (n < 4) addOpcode(34 + n); // fload_<n> else if (n < 0x100) { addOpcode(FLOAD); // fload add(n); } else { addOpcode(WIDE); addOpcode(FLOAD); addIndex(n); } } /** * Appends FSTORE or FSTORE_<n> * * @param n an index into the local variable array. */ public void addFstore(int n) { if (n < 4) addOpcode(67 + n); // fstore_<n> else if (n < 0x100) { addOpcode(FSTORE); // fstore add(n); } else { addOpcode(WIDE); addOpcode(FSTORE); addIndex(n); } } /** * Appends an instruction for loading a value from the * local variable at the index <code>n</code>. * * @param n the index. * @param type the type of the loaded value. * @return the size of the value (1 or 2 word). */ public int addLoad(int n, CtClass type) { if (type.isPrimitive()) { if (type == CtClass.booleanType || type == CtClass.charType || type == CtClass.byteType || type == CtClass.shortType || type == CtClass.intType) addIload(n); else if (type == CtClass.longType) { addLload(n); return 2; } else if(type == CtClass.floatType) addFload(n); else if(type == CtClass.doubleType) { addDload(n); return 2; } else throw new RuntimeException("void type?"); } else addAload(n); return 1; } /** * Appends an instruction for storing a value into the * local variable at the index <code>n</code>. * * @param n the index. * @param type the type of the stored value. * @return 2 if the type is long or double. Otherwise 1. */ public int addStore(int n, CtClass type) { if (type.isPrimitive()) { if (type == CtClass.booleanType || type == CtClass.charType || type == CtClass.byteType || type == CtClass.shortType || type == CtClass.intType) addIstore(n); else if (type == CtClass.longType) { addLstore(n); return 2; } else if (type == CtClass.floatType) addFstore(n); else if (type == CtClass.doubleType) { addDstore(n); return 2; } else throw new RuntimeException("void type?"); } else addAstore(n); return 1; } /** * Appends instructions for loading all the parameters onto the * operand stack. * * @param offset the index of the first parameter. It is 0 * if the method is static. Otherwise, it is 1. */ public int addLoadParameters(CtClass[] params, int offset) { int stacksize = 0; if (params != null) { int n = params.length; for (int i = 0; i < n; ++i) stacksize += addLoad(stacksize + offset, params[i]); } return stacksize; } /** * Appends CHECKCAST. * * @param c the type. */ public void addCheckcast(CtClass c) { addOpcode(CHECKCAST); addIndex(constPool.addClassInfo(c)); } /** * Appends CHECKCAST. * * @param classname a fully-qualified class name. */ public void addCheckcast(String classname) { addOpcode(CHECKCAST); addIndex(constPool.addClassInfo(classname)); } /** * Appends INSTANCEOF. * * @param classname the class name. */ public void addInstanceof(String classname) { addOpcode(INSTANCEOF); addIndex(constPool.addClassInfo(classname)); } /** * Appends GETFIELD. * * @param c the class. * @param name the field name. * @param type the descriptor of the field type. * * @see Descriptor#of(CtClass) */ public void addGetfield(CtClass c, String name, String type) { add(GETFIELD); int ci = constPool.addClassInfo(c); addIndex(constPool.addFieldrefInfo(ci, name, type)); growStack(Descriptor.dataSize(type) - 1); } /** * Appends GETFIELD. * * @param c the fully-qualified class name. * @param name the field name. * @param type the descriptor of the field type. * * @see Descriptor#of(CtClass) */ public void addGetfield(String c, String name, String type) { add(GETFIELD); int ci = constPool.addClassInfo(c); addIndex(constPool.addFieldrefInfo(ci, name, type)); growStack(Descriptor.dataSize(type) - 1); } /** * Appends GETSTATIC. * * @param c the class * @param name the field name * @param type the descriptor of the field type. * * @see Descriptor#of(CtClass) */ public void addGetstatic(CtClass c, String name, String type) { add(GETSTATIC); int ci = constPool.addClassInfo(c); addIndex(constPool.addFieldrefInfo(ci, name, type)); growStack(Descriptor.dataSize(type)); } /** * Appends GETSTATIC. * * @param c the fully-qualified class name * @param name the field name * @param type the descriptor of the field type. * * @see Descriptor#of(CtClass) */ public void addGetstatic(String c, String name, String type) { add(GETSTATIC); int ci = constPool.addClassInfo(c); addIndex(constPool.addFieldrefInfo(ci, name, type)); growStack(Descriptor.dataSize(type)); } /** * Appends INVOKESPECIAL. * * @param clazz the target class. * @param name the method name. * @param returnType the return type. * @param paramTypes the parameter types. */ public void addInvokespecial(CtClass clazz, String name, CtClass returnType, CtClass[] paramTypes) { String desc = Descriptor.ofMethod(returnType, paramTypes); addInvokespecial(clazz, name, desc); } /** * Appends INVOKESPECIAL. * * @param clazz the target class. * @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(CtClass clazz, String name, String desc) { addInvokespecial(constPool.addClassInfo(clazz), name, desc); } /** * Appends INVOKESPECIAL. * * @param clazz the fully-qualified class name. * @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(String clazz, String name, String desc) { addInvokespecial(constPool.addClassInfo(clazz), name, desc); } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -