<p>在java中,關係運算符是很經常使用的, 分別是>,==,<,>=,<=,!=這六種,咱們按照慣例看看咱們須要生成的代碼:</p> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:dfec0f1c-a2ec-4eba-bc9b-91c161fbfa47" 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: 665px; height: 319px;" style=" width: 665px; height: 319px;overflow: auto;">public static void main(String[] args) { Random rand = new Random(); int i = rand.nextInt(100); int j = rand.nextInt(100); System.out.println("i = " + i); System.out.println("j = " + j); System.out.println("i > j is " + (i > j)); System.out.println("i < j is " + (i < j)); System.out.println("i >= j is " + (i >= j)); System.out.println("i <= j is " + (i <= j)); System.out.println("i == j is " + (i == j)); System.out.println("i != j is " + (i != j)); System.out.println("(i < 10) && (j < 10) is " + ((i < 10) && (j < 10))); System.out.println("(i < 10) || (j < 10) is " + ((i < 10) || (j < 10))); }</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>java
<p>這裏用到了很是多的關係運算符,那麼對應的amssupport代碼以下:</p>web
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:0ad5c71a-a754-4b30-be83-e6267e1082ff" 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: 665px; height: 319px;" style=" width: 665px; height: 319px;overflow: auto;">package example.operators;app
import java.util.Random;less
import org.objectweb.asm.Opcodes;dom
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;ide
import example.AbstractExample;code
public class RelationalOperatorGenerate extends AbstractExample {orm
public static void main(String[] args) { ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC , "generated.operators.RelationalOperatorGenerateExample", null, null); /* * */ 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) { //Random rand = new Random(); LocalVariable rand = createVariable("rand", AClassFactory.getProductClass(Random.class), false, invokeConstructor(AClassFactory.getProductClass(Random.class))); //int i = rand.nextInt(100); LocalVariable i = createVariable("i", AClass.INT_ACLASS, false, invoke(rand, "nextInt", Value.value(100))); //int j = rand.nextInt(100); LocalVariable j = createVariable("j", AClass.INT_ACLASS, false, invoke(rand, "nextInt", Value.value(100))); //System.out.println("i = " + i); invoke(systemOut, "println", append(Value.value("i = "), i)); //System.out.println("j = " + j); invoke(systemOut, "println", append(Value.value("j = "), j)); //System.out.println("i > j is " + (i > j)); invoke(systemOut, "println", append(Value.value("i > j is "), greaterThan(i, j))); //System.out.println("i < j is " + (i < j)); invoke(systemOut, "println", append(Value.value("i < j is "), lessThan(i, j))); //System.out.println("i >= j is " + (i >= j)); invoke(systemOut, "println", append(Value.value("i >= j is "), greaterEqual(i, j))); //System.out.println("i <= j is " + (i <= j)); invoke(systemOut, "println", append(Value.value("i <= j is "), lessEqual(i, j))); //System.out.println("i == j is " + (i == j)); invoke(systemOut, "println", append(Value.value("i == j is "), equal(i, j))); //System.out.println("i != j is " + (i != j)); invoke(systemOut, "println", append(Value.value("i != j is "), notEqual(i, j))); //System.out.println("(i < 10) && (j < 10) is " + ((i < 10) && (j < 10))); invoke(systemOut, "println", append(Value.value("(i < 10) && (j < 10) is "), conditionalAnd(lessThan(i, Value.value(10)), lessThan(j, Value.value(10))))); //System.out.println("(i < 10) || (j < 10) is " + ((i < 10) || (j < 10))); invoke(systemOut, "println", append(Value.value("(i < 10) || (j < 10) is "), conditionalOr(lessThan(i, Value.value(10)), lessThan(j, Value.value(10))))); runReturn(); } }); generate(creator); }
} </pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>ci
<p>這裏面咱們主要介紹這關係運算所對應的asmsupport的方法(這些方法都是屬於jw.asmsupport.block.ProgramBlock的):</p>get
<p><font color="#f79646">public final GreaterThan greaterThan(Parameterized factor1, Parameterized factor2)</font>: >運算符的操做。</p>
<p><font color="#f79646">public final GreaterThan greaterEqual(Parameterized factor1, Parameterized factor2) </font><font color="#000000">: >=運算符。</font></p>
<p><font color="#f79646">public final GreaterThan lessThan(Parameterized factor1, Parameterized factor2) </font><font color="#000000">: <運算符。</font></p>
<p><font color="#f79646">public final GreaterThan lessEqual(Parameterized factor1, Parameterized factor2) </font><font color="#000000">: <=運算符。</font></p>
<p><font color="#f79646">public final GreaterThan lessEqual(Parameterized factor1, Parameterized factor2) </font><font color="#000000">: ==運算符。</font></p>
<p><font color="#f79646">public final GreaterThan lessEqual(Parameterized factor1, Parameterized factor2) </font><font color="#000000">: !=運算符。</font></p>
<p>這些方法都有兩個參數,分別表示運算符先後的兩個參數。好比a==b,那麼factor1表示a,factor2表示b</p>
<p> </p>
<p>上面asmsupport代碼中還有conditionalAnd和conditionalOr方法,這是邏輯運算符,將在之後講解。</p>