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> <p>这里用到了非常多的关系运算符,那么对应的amssupport代码如下:</p> <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;

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 example.AbstractExample;

public class RelationalOperatorGenerate extends AbstractExample {

1public static void main(String[] args) { 2 ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC , &quot;generated.operators.RelationalOperatorGenerateExample&quot;, null, null); 3 4 /* 5 * 6 */ 7 creator.createStaticMethod(&quot;main&quot;, new AClass[]{AClassFactory.getProductClass(String[].class)}, new String[]{&quot;args&quot;}, null, null, 8 Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, new StaticMethodBody(){ 9 10 @Override 11 public void generateBody(LocalVariable... argus) { 12 //Random rand = new Random(); 13 LocalVariable rand = createVariable(&quot;rand&quot;, AClassFactory.getProductClass(Random.class), false, 14 invokeConstructor(AClassFactory.getProductClass(Random.class))); 15 16 //int i = rand.nextInt(100); 17 LocalVariable i = createVariable(&quot;i&quot;, AClass.INT_ACLASS, false, 18 invoke(rand, &quot;nextInt&quot;, Value.value(100))); 19 20 //int j = rand.nextInt(100); 21 LocalVariable j = createVariable(&quot;j&quot;, AClass.INT_ACLASS, false, 22 invoke(rand, &quot;nextInt&quot;, Value.value(100))); 23 24 //System.out.println(&quot;i = &quot; + i); 25 invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;i = &quot;), i)); 26 27 //System.out.println(&quot;j = &quot; + j); 28 invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;j = &quot;), j)); 29 30 //System.out.println(&quot;i &gt; j is &quot; + (i &gt; j)); 31 invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;i &gt; j is &quot;), greaterThan(i, j))); 32 33 //System.out.println(&quot;i &lt; j is &quot; + (i &lt; j)); 34 invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;i &lt; j is &quot;), lessThan(i, j))); 35 36 //System.out.println(&quot;i &gt;= j is &quot; + (i &gt;= j)); 37 invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;i &gt;= j is &quot;), greaterEqual(i, j))); 38 39 //System.out.println(&quot;i &lt;= j is &quot; + (i &lt;= j)); 40 invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;i &lt;= j is &quot;), lessEqual(i, j))); 41 42 //System.out.println(&quot;i == j is &quot; + (i == j)); 43 invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;i == j is &quot;), equal(i, j))); 44 45 //System.out.println(&quot;i != j is &quot; + (i != j)); 46 invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;i != j is &quot;), notEqual(i, j))); 47 48 //System.out.println(&quot;(i &lt; 10) &amp;&amp; (j &lt; 10) is &quot; + ((i &lt; 10) &amp;&amp; (j &lt; 10))); 49 invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;(i &lt; 10) &amp;&amp; (j &lt; 10) is &quot;), 50 conditionalAnd(lessThan(i, Value.value(10)), lessThan(j, Value.value(10))))); 51 52 //System.out.println(&quot;(i &lt; 10) || (j &lt; 10) is &quot; + ((i &lt; 10) || (j &lt; 10))); 53 invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;(i &lt; 10) || (j &lt; 10) is &quot;), 54 conditionalOr(lessThan(i, Value.value(10)), lessThan(j, Value.value(10))))); 55 56 runReturn(); 57 } 58 }); 59 generate(creator); 60}

} </pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>

<p>这里面我们主要介绍这关系运算所对应的asmsupport的方法(这些方法都是属于jw.asmsupport.block.ProgramBlock的):</p> <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>
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

thinkphp3.2.3模板渲染支持三元表达式

thinkphp3.2.3模板渲染支持三元表达式{$status?'正常':'错误'}{$info'status'?$info'msg':$info'error'}注意:三元运算符中暂时不支持点语法。如下:           <divclass"modalhidefade"id'myModa

Twitter的分布式自增ID算法snowflake (Java版)

概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移