ASMSupport教程4.7 生成關係運算符

<p>在java中,關係運算符是很經常使用的, 分別是&gt;,==,&lt;,&gt;=,&lt;=,!=這六種,咱們按照慣例看看咱們須要生成的代碼:</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(&quot;i = &quot; + i); System.out.println(&quot;j = &quot; + j); System.out.println(&quot;i &gt; j is &quot; + (i &gt; j)); System.out.println(&quot;i &lt; j is &quot; + (i &lt; j)); System.out.println(&quot;i &gt;= j is &quot; + (i &gt;= j)); System.out.println(&quot;i &lt;= j is &quot; + (i &lt;= j)); System.out.println(&quot;i == j is &quot; + (i == j)); System.out.println(&quot;i != j is &quot; + (i != j)); System.out.println(&quot;(i &lt; 10) &amp;&amp; (j &lt; 10) is &quot; + ((i &lt; 10) &amp;&amp; (j &lt; 10))); System.out.println(&quot;(i &lt; 10) || (j &lt; 10) is &quot; + ((i &lt; 10) || (j &lt; 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 , &quot;generated.operators.RelationalOperatorGenerateExample&quot;, null, null);

	/*
	 * 
	 */
	creator.createStaticMethod(&quot;main&quot;, new AClass[]{AClassFactory.getProductClass(String[].class)}, new String[]{&quot;args&quot;}, null, null,
			Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, new StaticMethodBody(){

		@Override
		public void generateBody(LocalVariable... argus) {
			//Random rand = new Random();
			LocalVariable rand = createVariable(&quot;rand&quot;, AClassFactory.getProductClass(Random.class), false, 
			    invokeConstructor(AClassFactory.getProductClass(Random.class)));
			
			//int i = rand.nextInt(100);
			LocalVariable i = createVariable(&quot;i&quot;, AClass.INT_ACLASS, false, 
			    invoke(rand, &quot;nextInt&quot;, Value.value(100)));
			
		    //int j = rand.nextInt(100);
			LocalVariable j = createVariable(&quot;j&quot;, AClass.INT_ACLASS, false, 
			    invoke(rand, &quot;nextInt&quot;, Value.value(100)));
			
		    //System.out.println(&quot;i = &quot; + i);
			invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;i = &quot;), i));
			
		    //System.out.println(&quot;j = &quot; + j);
			invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;j = &quot;), j));
			
		    //System.out.println(&quot;i &gt; j is &quot; + (i &gt; j));
			invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;i &gt; j is &quot;), greaterThan(i, j)));
			
		    //System.out.println(&quot;i &lt; j is &quot; + (i &lt; j));
			invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;i &lt; j is &quot;), lessThan(i, j)));
			
		    //System.out.println(&quot;i &gt;= j is &quot; + (i &gt;= j));
			invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;i &gt;= j is &quot;), greaterEqual(i, j)));
			
		    //System.out.println(&quot;i &lt;= j is &quot; + (i &lt;= j));
			invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;i &lt;= j is &quot;), lessEqual(i, j)));
			
		    //System.out.println(&quot;i == j is &quot; + (i == j));
			invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;i == j is &quot;), equal(i, j)));
			
		    //System.out.println(&quot;i != j is &quot; + (i != j));
			invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;i != j is &quot;), notEqual(i, j)));
			
		    //System.out.println(&quot;(i &lt; 10) &amp;&amp; (j &lt; 10) is &quot; + ((i &lt; 10) &amp;&amp; (j &lt; 10)));
			invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;(i &lt; 10) &amp;&amp; (j &lt; 10) is &quot;), 
			    conditionalAnd(lessThan(i, Value.value(10)), lessThan(j, Value.value(10)))));
			
		    //System.out.println(&quot;(i &lt; 10) || (j &lt; 10) is &quot; + ((i &lt; 10) || (j &lt; 10)));
			invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;(i &lt; 10) || (j &lt; 10) is &quot;), 
			    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>: &gt;運算符的操做。</p>

<p><font color="#f79646">public final GreaterThan greaterEqual(Parameterized factor1, Parameterized factor2) </font><font color="#000000">: &gt;=運算符。</font></p>

<p><font color="#f79646">public final GreaterThan lessThan(Parameterized factor1, Parameterized factor2) </font><font color="#000000">: &lt;運算符。</font></p>

<p><font color="#f79646">public final GreaterThan lessEqual(Parameterized factor1, Parameterized factor2) </font><font color="#000000">: &lt;=運算符。</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>&#160;</p>

<p>上面asmsupport代碼中還有conditionalAnd和conditionalOr方法,這是邏輯運算符,將在之後講解。</p>

相關文章
相關標籤/搜索