4.4 自增减操作(++,—)
我们知道,++和--是各种编程语言的基本操作,其作用就不解释了,直接进入正题,我们希望生成如下代码
package generated.operators;import java.io.PrintStream;
public class CrementOperatorGenerateExample { public void demonstrate() { System.out.println("***demonstrate"); int a = 1; int b = 2; b++; int c = b; int d = a; a++; c++; System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); }
public void incrementAndDecrement() { System.out.println("***incrementAndDecrement"); int count = 10; count++; count--; System.out.println("count = " + count); }
public static void main(String[] args) { CrementOperatorGenerateExample currentObj = new CrementOperatorGenerateExample(); currentObj.demonstrate(); currentObj.incrementAndDecrement(); } }
那么通过如下ASMSupport代码就能生成:
package example.operators;import org.objectweb.asm.Opcodes;
import jw.asmsupport.block.method.common.CommonMethodBody; import jw.asmsupport.block.method.common.StaticMethodBody; import jw.asmsupport.clazz.AClass; import jw.asmsupport.clazz.AClassFactory; import jw.asmsupport.creator.ClassCreator; import jw.asmsupport.definition.value.Value; import jw.asmsupport.definition.variable.LocalVariable;
import example.AbstractExample;
public class CrementOperatorGenerate extends AbstractExample {
/** * @param args */ public static void main(String[] args) { ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC , "generated.operators.CrementOperatorGenerateExample", null, null); /* * 对应java代码 * public void demonstrate() { * System.out.println("******************************demonstrate***************************"); * int a = 1; * int b = 2; * int c; * int d; * c = ++b; * d = a++; * c++; * System.out.println("a = " + a); * System.out.println("b = " + b); * System.out.println("c = " + c); * System.out.println("d = " + d); * } */ creator.createMethod("demonstrate", null, null, null, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() { @Override public void generateBody(LocalVariable... argus) { invoke(systemOut, "println", Value.value("******************************demonstrate***************************")); //int a = 1; LocalVariable a = createVariable("a", AClass.INT_ACLASS, false, Value.value(1)); //int b = 2; LocalVariable b = createVariable("b", AClass.INT_ACLASS, false, Value.value(2)); //int c = ++b; LocalVariable c = createVariable("c", AClass.INT_ACLASS, false, beforeInc(b)); //d = a++; LocalVariable d = createVariable("d", AClass.INT_ACLASS, false, afterInc(a)); //c++; afterInc(c); //System.out.println("a = " + a); invoke(systemOut, "println", append(Value.value("a = "), a)); invoke(systemOut, "println", append(Value.value("b = "), b)); invoke(systemOut, "println", append(Value.value("c = "), c)); invoke(systemOut, "println", append(Value.value("d = "), d)); runReturn(); } }); /* * java code: * public void incrementAndDecrement(String[] argv) { * System.out.println("******************************incrementAndDecrement***************************"); * int count = 10; * ++count; * --count; * System.out.println(count); * } */ creator.createMethod("incrementAndDecrement", null, null, null, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() { @Override public void generateBody(LocalVariable... argus) { invoke(systemOut, "println", Value.value("******************************incrementAndDecrement***************************")); //int count = 10; LocalVariable count = createVariable("count", AClass.INT_ACLASS, false, Value.value(10)); // ++count beforeInc(count); // --count; beforeDec(count); // System.out.println("count = " + count); invoke(systemOut, "println", append(Value.value("count = "), count)); runReturn(); } }); creator.createStaticMethod("main", new AClass[]{AClassFactory.getProductClass(String[].class)}, new String[]{"args"}, null, null, Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, new StaticMethodBody(){ @Override public void generateBody(LocalVariable... argus) { LocalVariable currentObj = createVariable("currentObj", getMethodOwner(), false, invokeConstructor(getMethodOwner())); invoke(currentObj, "demonstrate"); invoke(currentObj, "incrementAndDecrement"); runReturn(); } }); generate(creator); }
}
我们主要关注的是creator.createMethod("demonstrate", null, null, null, null, Opcodes.ACC\_PUBLIC, new CommonMethodBody()这份部分内容以及creator.createMethod("incrementAndDecrement", null, null, null, null, Opcodes.ACC\_PUBLIC, new CommonMethodBody()部分,创建变量部分就不解释了,如果不知道的可以参数前一章ASMSupport教程2动态生成类,我们这里主要关注一下四个方法:
- beforeInc: 这个方法相当于++a操作
- afterInc:这个方法相当于a++操作
- beforeDec:这个方法相当于--a操作
- afterDec:这个方法相当于a--操作
以上四个方法都只有一个参数,参数类型是继承Crementable类型的,在ASMSupport中jw.asmsupport.definition.variable.GlobalVariable和jw.asmsupport.definition.variable.LocalVariable。也就是说我们定义的类属性和局部变量都是可以的,但是得注意,这些变量必须是可以执行++或--操作的,这也和平常java语法是一样的。
本系列教程所有实例下载地址:http://www.wensiqun.com/download/118/