import java.io.PrintStream; import java.util.Random;
public class ArithmeticOperatorGenerateExample { static void printInt(String s, int i) { System.out.println(s + " = " + i); }
static void printFloat(String s, float f) { System.out.println(s + " = " + f); }
public static void main(String[] args) { Random rand = new Random(); int j = rand.nextInt(100) + 1; int k = rand.nextInt(100) + 1; printInt("j", j); printInt("k", k); int i = j + k; printInt("j + k", i); i = j - k; printInt("j - k", i); i = k / j; printInt("k / j", i); i = k * j; printInt("k * j", i); i = k % j; printInt("k % j", i); j %= k; printInt("j %= k", j); float v = rand.nextFloat(); float w = rand.nextFloat(); printFloat("v", v); printFloat("w", w); float u = v + w; printFloat("v + w", u); u = v - w; printFloat("v - w", u); u = v * w; printFloat("v * w", u); u = v / w; printFloat("v / w", u); u += v; printFloat("u += v", u); u -= v; printFloat("u -= v", u); u *= v; printFloat("u *= v", u); u /= v; printFloat("u /= v", u); } }</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>
<p>对应的代码如下,由于代码比较多,我们将对应的java代码写在asmsupport代码之上,便于对比。</p> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:1bfe82d6-deba-48f1-853a-a748666c5148" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 462px; height: 3000px;" style=" width: 462px; height: 3000px;overflow: auto;">package example.operators;import java.util.Random;
import org.objectweb.asm.Opcodes;
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 jw.asmsupport.operators.method.MethodInvoker; import jw.asmsupport.operators.numerical.arithmetic.Addition;
import example.AbstractExample;
public class ArithmeticOperatorGenerate extends AbstractExample {
1/** 2 * @param args 3 */ 4public static void main(String[] args) { 5 ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.operators.ArithmeticOperatorGenerateExample", null, null); 6 7 //printIn方法 8 creator.createStaticMethod("printInt", new AClass[]{AClass.STRING_ACLASS, AClass.INT_ACLASS}, new String[]{"s", "i"}, null, null, 0, new StaticMethodBody(){ 9 10 @Override 11 public void generateBody(LocalVariable... argus) { 12 invoke(systemOut, "println", append(argus[0], Value.value(" = "), argus[1])); 13 runReturn(); 14 } 15 16 }); 17 18 //printIn方法 19 creator.createStaticMethod("printFloat", new AClass[]{AClass.STRING_ACLASS, AClass.FLOAT_ACLASS}, new String[]{"s", "f"}, null, null, 0, new StaticMethodBody(){ 20 21 @Override 22 public void generateBody(LocalVariable... argus) { 23 invoke(systemOut, "println", append(argus[0], Value.value(" = "), argus[1])); 24 runReturn(); 25 } 26 27 }); 28 29 creator.createStaticMethod("main", new AClass[] { AClassFactory.getProductClass(String[].class) }, new String[] { "args" }, null, null, Opcodes.ACC_PUBLIC + 30 31 Opcodes.ACC_STATIC, new StaticMethodBody() { 32 33 @Override 34 public void generateBody(LocalVariable... argus) { 35 //Random rand = new Random(); 36 LocalVariable rand = createVariable("rand", AClassFactory.getProductClass(Random.class), false, invokeConstructor(AClassFactory.getProductClass(Random.class))); 37 38 //rand.nextInt(100) + 1 39 Addition add1 = add(invoke(rand, "nextInt", Value.value(100)), Value.value(1)); 40 41 //int j = rand.nextInt(100) + 1; 42 LocalVariable j = createVariable("j", AClass.INT_ACLASS, false, add1); 43 44 //int k = rand.nextInt(100) + 1; 45 LocalVariable k = createVariable("k", AClass.INT_ACLASS, false, add1); 46 47 //printInt("j", j); 48 invokeStatic(getMethodOwner(), "printInt", Value.value("j"), j); 49 50 //printInt("k", k); 51 invokeStatic(getMethodOwner(), "printInt", Value.value("k"), k); 52 53 //j + k 54 Addition add2 = add(j, k); 55 //int i = j + k; 56 LocalVariable i = createVariable("i", AClass.INT_ACLASS, false, add2); 57 58 //printInt("j + k", i); 59 invokeStatic(getMethodOwner(), "printInt", Value.value("j + k"), i); 60 61 //i = j - k; 62 assign(i, sub(j, k)); 63 64 //printInt("j - k", i); 65 invokeStatic(getMethodOwner(), "printInt", Value.value("j - k"), i); 66 67 //i = k / j; 68 assign(i, div(k, j)); 69 70 //printInt("k / j", i); 71 invokeStatic(getMethodOwner(), "printInt", Value.value("k / j"), i); 72 73 //i = k * j; 74 assign(i, mul(k, j)); 75 76 //printInt("k * j", i); 77 invokeStatic(getMethodOwner(), "printInt", Value.value("k * j"), i); 78 79 //i = k % j; 80 assign(i, mod(k, j)); 81 82 //printInt("k % j", i); 83 invokeStatic(getMethodOwner(), "printInt", Value.value("k % j"), i); 84 85 //j %= k; 86 assign(j, mod(j, k)); 87 88 //printInt("j %= k", j); 89 invokeStatic(getMethodOwner(), "printInt", Value.value("j %= k"), j); 90 91 92 //rand.nextFloat() 93 MethodInvoker nextFloat = invoke(rand, "nextFloat"); 94 95 //v = rand.nextFloat(); 96 LocalVariable v = createVariable("v", AClass.FLOAT_ACLASS, false, nextFloat); 97 98 //w = rand.nextFloat(); 99 LocalVariable w = createVariable("w", AClass.FLOAT_ACLASS, false, nextFloat); 100 101 //printFloat("v", v); 102 invokeStatic(getMethodOwner(), "printFloat", Value.value("v"), v); 103 104 //printFloat("w", w); 105 invokeStatic(getMethodOwner(), "printFloat", Value.value("w"), w); 106 107 //u = v + w; 108 LocalVariable u = createVariable("u", AClass.FLOAT_ACLASS, false, add(v,w)); 109 110 //printFloat("v + w", u); 111 invokeStatic(getMethodOwner(), "printFloat", Value.value("v + w"), u); 112 113 //u = v - w; 114 assign(u, sub(v, w)); 115 116 //printFloat("v - w", u); 117 invokeStatic(getMethodOwner(), "printFloat", Value.value("v - w"), u); 118 119 //u = v * w; 120 assign(u, mul(v, w)); 121 122 //printFloat("v * w", u); 123 invokeStatic(getMethodOwner(), "printFloat", Value.value("v * w"), u); 124 125 //u = v / w; 126 assign(u, div(v, w)); 127 128 //printFloat("v / w", u); 129 invokeStatic(getMethodOwner(), "printFloat", Value.value("v / w"), u); 130 131 //u += v; 132 assign(u, add(u, v)); 133 134 //printFloat("u += v", u); 135 invokeStatic(getMethodOwner(), "printFloat", Value.value("u += v"), u); 136 137 //u -= v; 138 assign(u, sub(u, v)); 139 140 //printFloat("u -= v", u); 141 invokeStatic(getMethodOwner(), "printFloat", Value.value("u -= v"), u); 142 143 //u *= v; 144 assign(u, mul(u, v)); 145 146 //printFloat("u *= v", u); 147 invokeStatic(getMethodOwner(), "printFloat", Value.value("u *= v"), u); 148 149 //u /= v; 150 assign(u, div(u, v)); 151 152 //printFloat("u /= v", u); 153 invokeStatic(getMethodOwner(), "printFloat", Value.value("u /= v"), u); 154 155 runReturn(); 156 } 157 }); 158 generate(creator); 159}
} </pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>
<p>这里我们不逐行解释,只对生成算术操作的方法进行解释:</p> <ul> <li><strong>jw.asmsupport.block.ProgramBlock.add(Parameterized factor1, Parameterized factor2) :</strong> 生成加法操作,生成的代码是factor1+factor2,返回类型<strong><font color="#ffc000">jw.asmsupport.operators.numerical.arithmetic.Addition</font></strong> </li> <li><strong>jw.asmsupport.block.ProgramBlock.sub(Parameterized factor1, Parameterized factor2) :</strong> 生成减法操作,生成的代码是factor1-factor2 , 返回类型<strong><font color="#ffc000">jw.asmsupport.operators.numerical.arithmetic.Subtraction</font></strong> </li> <li><strong>jw.asmsupport.block.ProgramBlock.mul(Parameterized factor1, Parameterized factor2):</strong> 生成乘法操作,生成的代码是factor1\*factor2 , 返回类型<strong><font color="#ffc000">jw.asmsupport.operators.numerical.arithmetic.Multiplication</font></strong> </li> <li><strong>jw.asmsupport.block.ProgramBlock.div(Parameterized factor1, Parameterized factor2):</strong>  生成除法操作,生成的代码是factor1/factor2 , 返回类型<strong><font color="#ffc000">jw.asmsupport.operators.numerical.arithmetic.Division</font></strong> </li> <li><strong>jw.asmsupport.block.ProgramBlock.mod(Parameterized factor1, Parameterized factor2):</strong>生成取模操作,生成的代码是factor1%factor2 , 返回类型<strong><font color="#ffc000">jw.asmsupport.operators.numerical.arithmetic.Modulus</font></strong> </li> </ul> <p>这些方法的返回值都是继承自jw.asmsupport.Parameterized接口的,这个接口意味着这个类还可以被其他的操作所使用,这个很容易理解,我可以将a+b这个运算作为参数传入某个方法,比如有个方法是c(int a),我们可以这样调用:c(a+b)。 </p>