/** * 使用算术运算进行加、减、乘、除、取余、取相反数 */ @Test public void testAritmeticOperation(){ ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.AritmeticOperation", null, null); creator.createMethod("method1", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { //进行加法运算,未指定赋值给第三个变量 LocalVariable i_var = createVariable(AClass.INT_ACLASS, "i", false, Value.value(1)); LocalVariable j_var = createVariable(AClass.INT_ACLASS, "j", false, Value.value(4)); Addition add2 = add(i_var, j_var); GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", add2); } }); creator.createMethod("method2", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { //进行减法运算,指定赋值给第三个变量 LocalVariable i_var = createVariable(AClass.INT_ACLASS, "i", false, Value.value(1)); LocalVariable j_var = createVariable(AClass.INT_ACLASS, "j", false, Value.value(4)); LocalVariable k_var = createVariable(AClass.INT_ACLASS, "k", false, sub(i_var, j_var)); GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", k_var); } }); creator.createMethod("method3", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { //进行减法运算,指定赋值给第三个变量 LocalVariable i_var = createVariable(AClass.INT_ACLASS, "i", false, Value.value(1)); LocalVariable j_var = createVariable(AClass.INT_ACLASS, "j", false, Value.value(4)); LocalVariable k_var = createVariable(AClass.INT_ACLASS, "k", false, Value.value(5));
GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", div(mul(i_var, j_var), k_var)); } }); creator.createMethod("method4", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { //进行减法运算,指定赋值给第三个变量 LocalVariable i_var = createVariable(AClass.INT_ACLASS, "i", false, Value.value(1)); LocalVariable j_var = createVariable(AClass.INT_ACLASS, "j", false, Value.value(4)); LocalVariable k_var = createVariable(AClass.INT_ACLASS, "k", false, Value.value(5));
GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", div(mul(i_var, j_var), k_var)); } }); creator.createMethod("method5", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { //进行减法运算,指定赋值给第三个变量 LocalVariable i_var = createVariable(AClass.INT_ACLASS, "i", false, Value.value(1)); LocalVariable j_var = createVariable(AClass.INT_ACLASS, "j", false, Value.value(4)); LocalVariable k_var = createVariable(AClass.INT_ACLASS, "k", false, mod(j_var, i_var));
GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", k_var); } }); creator.createMethod("method6", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", neg(Value.value(1))); //这个用法有问题 invoke(sysout, "println", neg(Value.value(-1)));
} }); creator.setClassOutPutPath(".//target//"); creator.startup(); } |
这里我们不逐行解释,只对生成算术操作的方法进行解释:
add(Parameterized factor1, Parameterized factor2) : 生成加法操作,生成的代码是factor1+factor2,返回类型Addition
sub(Parameterized factor1, Parameterized factor2) : 生成减法操作,生成的代码是factor1-factor2 , 返回类型Subtraction
mul(Parameterized factor1, Parameterized factor2): 生成乘法操作,生成的代码是factor1*factor2 , 返回类型Multiplication
div(Parameterized factor1, Parameterized factor2): 生成除法操作,生成的代码是factor1/factor2 , 返回类型Division
mod(Parameterized factor1, Parameterized factor2):生成取模操作,生成的代码是factor1%factor2 , 返回类型Modulus
/** * 使用组合运算,进行加、减、乘、除、取余、 */ @Test public void testAritmeticOperation2(){ ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.AritmeticOperation2", null, null); creator.createMethod("method1", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { //进行加法运算,组合运算 LocalVariable i_var = createVariable(AClass.INT_ACLASS, "i", true, Value.value(1)); assign(i_var, add(i_var, Value.value(5))); GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", i_var); } }); creator.createMethod("method2", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { //进行减法运算,组合运算 LocalVariable i_var = createVariable(AClass.INT_ACLASS, "i", false, Value.value(1)); assign(i_var, sub(i_var, Value.value(5))); GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", i_var); } }); creator.createMethod("method3", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { //进行乘法运算,组合运算 LocalVariable i_var = createVariable(AClass.INT_ACLASS, "i", false, Value.value(1)); assign(i_var, mul(i_var, Value.value(5))); GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", i_var); } }); creator.createMethod("method4", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { //进行除法运算,组合运算 LocalVariable i_var = createVariable(AClass.INT_ACLASS, "i", false, Value.value(1)); assign(i_var, div(i_var, Value.value(5))); GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", i_var); } }); creator.createMethod("method5", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { //进行除法运算,组合运算 LocalVariable i_var = createVariable(AClass.INT_ACLASS, "i", false, Value.value(1)); assign(i_var, mod(i_var, Value.value(5))); GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", i_var); } }); creator.setClassOutPutPath(".//target//"); creator.startup(); } |
|
l beforeInc: 这个方法相当于++a操作
l afterInc:这个方法相当于a++操作
l beforeDec:这个方法相当于--a操作
l afterDec:这个方法相当于a--操作
/** * 使用逻辑运算,进行&& || & | ! */ @Test public void testLogicOperation(){ ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.LogicOperation", null, null); creator.createMethod("method1", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", not(Value.value(false))); } }); creator.createMethod("method2", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", not(not(Value.value(false)))); } }); creator.createMethod("method3", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", conditionalAnd(Value.value(false), Value.value(false))); invoke(sysout, "println", conditionalAnd(Value.value(true), Value.value(false))); } }); creator.createMethod("method4", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", conditionalOr(Value.value(false), Value.value(false))); invoke(sysout, "println", conditionalOr(Value.value(true), Value.value(false))); } }); creator.createMethod("method5", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", logicalAnd(Value.value(false), Value.value(false))); invoke(sysout, "println", logicalAnd(Value.value(true), Value.value(false))); } }); creator.createMethod("method6", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", logicalOr(Value.value(false), Value.value(false))); invoke(sysout, "println", logicalOr(Value.value(true), Value.value(false))); } }); creator.createMethod("method7", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", logicalXor(Value.value(false), Value.value(false))); invoke(sysout, "println", logicalXor(Value.value(true), Value.value(false))); } }); creator.setClassOutPutPath(".//target//"); creator.startup(); } |
l public ShortCircuitAnd conditionalAnd(Parameterized factor1, Parameterized factor2) : 条件与(&&)操作
l public ShortCircuitOr conditionalOr(Parameterized factor1, Parameterized factor2) : 条件或(||)操作
l public LogicalAnd logicalAnd(Parameterized factor1, Parameterized factor2) : 逻辑与(&)操作
l public LogicalOr logicalOr(Parameterized factor1, Parameterized factor2) : 逻辑或(|)操作
l public LogicalXor logicalXor(Parameterized factor1, Parameterized factor2) : 逻辑异或(^)操作
l public Not not(Parameterized factor) : 非(!)操作
/** * 使用关系判断 > >= < <= == != */ @Test public void testRelationalOperation(){ ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.RelationalOperation", null, null); creator.createMethod("method1", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", greaterThan(Value.value(1), Value.value(2))); invoke(sysout, "println", greaterEqual(Value.value(1), Value.value(2))); invoke(sysout, "println", lessThan(Value.value(1), Value.value(2))); invoke(sysout, "println", lessEqual(Value.value(1), Value.value(2))); invoke(sysout, "println", equal(Value.value(1), Value.value(2))); invoke(sysout, "println", lessEqual(Value.value(1), Value.value(2))); } });
creator.setClassOutPutPath(".//target//"); creator.startup(); } |
l public final GreaterThan greaterThan(Parameterized factor1, Parameterized factor2): >运算符的操作。
l public final GreaterThan greaterEqual(Parameterized factor1, Parameterized factor2) : >=运算符。
l public final GreaterThan lessThan(Parameterized factor1, Parameterized factor2) : <运算符。
l public final GreaterThan lessEqual(Parameterized factor1, Parameterized factor2) : <=运算符。
l public final GreaterThan equal(Parameterized factor1, Parameterized factor2) : ==运算符。
l public final GreaterThan lessEqual(Parameterized factor1, Parameterized factor2) : !=运算符。
l 这些方法都有两个参数,分别表示运算符前后的两个参数。比如a==b,那么factor1表示a,factor2表示b
/** * 使用三目运算 */ @Test public void testThreeItemOperation(){ ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.ThreeItemOperation", null, null); creator.createMethod("method1", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", ternary(notEqual(Value.value(1), Value.value(2)),Value.value(3),Value.value(4))); invoke(sysout, "println", ternary(equal(Value.value(1), Value.value(2)),Value.value(3),Value.value(4))); LocalVariable i = createVariable(AClass.INT_ACLASS, "i", false, ternary(notEqual(Value.value(1), Value.value(2)),Value.value(3),Value.value(4))); invoke(sysout, "println", i); } });
creator.setClassOutPutPath(".//target//"); creator.startup(); } |
/** * instanceof运算符 */ @Test public void testInstanceofOperation(){ ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.InstanceofOperation", null, null); creator.createMethod("method1", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", instanceOf(Value.value("this is a test instanceof"), AClass.STRING_ACLASS)); } });
creator.setClassOutPutPath(".//target//"); creator.startup(); } |
/** * 位运算符 ~ | & ^ */ @Test public void testBitOperation(){ ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.BitOperation", null, null); creator.createMethod("method1", new AClass[]{}, new String[]{}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { GlobalVariable sysout = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(sysout, "println", inverts(Value.value(2))); invoke(sysout, "println", bitOr(Value.value(2), Value.value(3))); invoke(sysout, "println", bitAnd(Value.value(2), Value.value(3))); invoke(sysout, "println", bitXor(Value.value(2), Value.value(3))); } });
creator.setClassOutPutPath(".//target//"); creator.startup(); } |