ASMSupport教程4.8 生成邏輯運算操做

<p>在java中有如下邏輯運算符:</p> <ul> <li>&amp;&amp;:條件與 </li> <li>||:條件或 </li> <li>&amp; : 布爾型的邏輯與 </li> <li>| : 布爾型的邏輯或 </li> <li>^ : 布爾型的邏輯異或 </li> <li>! : 非操做 </li> </ul> <p>那麼接下來咱們將些段例子來看看各類邏輯運算的效果,而且咱們再經過ASMSupport生成這個例子的字節碼Class:</p> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:35f1314d-5424-4ebc-a478-76bc1a2750b5" 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) { // create truth table for &amp;&amp; (conditional AND) operator System.out.printf(&quot;%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n&quot;, &quot;Conditional AND (&amp;&amp;)&quot;, &quot;false &amp;&amp; false&quot;, (false &amp;&amp; false), &quot;false &amp;&amp; true&quot;, (false &amp;&amp; true), &quot;true &amp;&amp; false&quot;, (true &amp;&amp; false), &quot;true &amp;&amp; true&quot;, (true &amp;&amp; true));java

// create truth table for || (conditional OR) operator
System.out.printf(&quot;%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n&quot;,
		&quot;Conditional OR (||)&quot;, &quot;false || false&quot;, (false || false),
		&quot;false || true&quot;, (false || true), &quot;true || false&quot;,
		(true || false), &quot;true || true&quot;, (true || true));

// create truth table for &amp; (boolean logical AND) operator
System.out.printf(&quot;%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n&quot;,
		&quot;Boolean logical AND (&amp;)&quot;, &quot;false &amp; false&quot;, (false &amp; false),
		&quot;false &amp; true&quot;, (false &amp; true), &quot;true &amp; false&quot;, (true &amp; false),
		&quot;true &amp; true&quot;, (true &amp; true));

// create truth table for | (boolean logical inclusive OR) operator
System.out.printf(&quot;%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n&quot;,
		&quot;Boolean logical inclusive OR (|)&quot;, &quot;false | false&quot;,
		(false | false), &quot;false | true&quot;, (false | true),
		&quot;true | false&quot;, (true | false), &quot;true | true&quot;, (true | true));

// create truth table for ^ (boolean logical exclusive OR) operator
System.out.printf(&quot;%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n&quot;,
		&quot;Boolean logical exclusive OR (^)&quot;, &quot;false ^ false&quot;,
		(false ^ false), &quot;false ^ true&quot;, (false ^ true),
		&quot;true ^ false&quot;, (true ^ false), &quot;true ^ true&quot;, (true ^ true));

// create truth table for ! (logical negation) operator
System.out.printf(&quot;%s\n%s: %b\n%s: %b\n&quot;, &quot;Logical NOT (!)&quot;, &quot;!false&quot;,
		(!false), &quot;!true&quot;, (!true));

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

<p>咱們接下來使用ASMSupport如何生成上面的代碼:</p>ide

<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:b9a8e603-65ce-4ad8-ba7e-9bddb231310a" 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;ui

import org.objectweb.asm.Opcodes;code

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;orm

import example.AbstractExample;ci

public class LogicalOperatorGenerate extends AbstractExample {get

public static void main(String[] args) {
	//willGenerate(args);
	ClassCreator creator = new ClassCreator(Opcodes.V1_5,
			Opcodes.ACC_PUBLIC,
			&quot;generated.operators.LogicalOperatorGenerateExample&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) {
			/*System.out.printf(&quot;%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n&quot;,
		     &quot;Conditional AND (&amp;&amp;)&quot;, &quot;false &amp;&amp; false&quot;, (false &amp;&amp; false),
		     &quot;false &amp;&amp; true&quot;, (false &amp;&amp; true), &quot;true &amp;&amp; false&quot;,
		     (true &amp;&amp; false), &quot;true &amp;&amp; true&quot;, (true &amp;&amp; true));*/
			invoke(systemOut, &quot;printf&quot;, 
		     Value.value(&quot;%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n&quot;), 
		     Value.value(&quot;Conditional AND (&amp;&amp;)&quot;), 
		     Value.value(&quot;false &amp;&amp; false&quot;), 
		     conditionalAnd(Value.value(false), Value.value(false)), 
		     Value.value(&quot;false &amp;&amp; true&quot;), 
		     conditionalAnd(Value.value(false), Value.value(true)), 
		     Value.value(&quot;true &amp;&amp; false&quot;), 
		     conditionalAnd(Value.value(true), Value.value(false)),  
		     Value.value(&quot;true &amp;&amp; true&quot;), 
		     conditionalAnd(Value.value(true), Value.value(true))
		     );

					/*System.out.printf(&quot;%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n&quot;,
		     &quot;Conditional OR (||)&quot;, &quot;false || false&quot;, (false || false),
		     &quot;false || true&quot;, (false || true), &quot;true || false&quot;,
		     (true || false), &quot;true || true&quot;, (true || true));*/
			invoke(systemOut, &quot;printf&quot;, 
		     Value.value(&quot;%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n&quot;), 
		     Value.value(&quot;Conditional OR (||)&quot;), 
		     Value.value(&quot;false || false&quot;),  conditionalOr(Value.value(false), Value.value(false)), 
		     Value.value(&quot;false || true&quot;),  conditionalOr(Value.value(false), Value.value(true)), 
		     Value.value(&quot;true || false&quot;),  conditionalOr(Value.value(true), Value.value(false)),  
		     Value.value(&quot;true || true&quot;),  conditionalOr(Value.value(true), Value.value(true))
		     );
					
					/*System.out.printf(&quot;%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n&quot;,
		     &quot;Boolean logical AND (&amp;)&quot;, &quot;false &amp; false&quot;, (false &amp; false),
		     &quot;false &amp; true&quot;, (false &amp; true), &quot;true &amp; false&quot;, (true &amp; false),
		     &quot;true &amp; true&quot;, (true &amp; true));*/
			invoke(systemOut, &quot;printf&quot;, 
		     Value.value(&quot;%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n&quot;), 
		     Value.value(&quot;Boolean logical AND (&amp;)&quot;), 
		     Value.value(&quot;false &amp; false&quot;),  logicalAnd(Value.value(false), Value.value(false)), 
		     Value.value(&quot;false &amp; true&quot;),  logicalAnd(Value.value(false), Value.value(true)), 
		     Value.value(&quot;true &amp; false&quot;),  logicalAnd(Value.value(true), Value.value(false)),  
		     Value.value(&quot;true &amp; true&quot;),  logicalAnd(Value.value(true), Value.value(true))
		     );
					
					/*System.out.printf(&quot;%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n&quot;,
		     &quot;Boolean logical inclusive OR (|)&quot;, &quot;false | false&quot;,
		     (false | false), &quot;false | true&quot;, (false | true),
		     &quot;true | false&quot;, (true | false), &quot;true | true&quot;, (true | true));*/
			invoke(systemOut, &quot;printf&quot;, 
		     Value.value(&quot;%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n&quot;), 
		     Value.value(&quot;Boolean logical inclusive OR (|)&quot;), 
		     Value.value(&quot;false | false&quot;),  logicalOr(Value.value(false), Value.value(false)), 
		     Value.value(&quot;false | true&quot;),  logicalOr(Value.value(false), Value.value(true)), 
		     Value.value(&quot;true | false&quot;),  logicalOr(Value.value(true), Value.value(false)),  
		     Value.value(&quot;true | true&quot;),  logicalOr(Value.value(true), Value.value(true))
		     );
					
			/*System.out.printf(&quot;%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n&quot;,
		     &quot;Boolean logical exclusive OR (^)&quot;, &quot;false ^ false&quot;,
		     (false ^ false), &quot;false ^ true&quot;, (false ^ true),
		     &quot;true ^ false&quot;, (true ^ false), &quot;true ^ true&quot;, (true ^ true));*/
			 invoke(systemOut, &quot;printf&quot;, 
		     Value.value(&quot;%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n&quot;), 
		     Value.value(&quot;Boolean logical exclusive OR (^)&quot;), 
		     Value.value(&quot;false ^ false&quot;),  logicalXor(Value.value(false), Value.value(false)), 
		     Value.value(&quot;false ^ true&quot;),  logicalXor(Value.value(false), Value.value(true)), 
		     Value.value(&quot;true ^ false&quot;),  logicalXor(Value.value(true), Value.value(false)),  
		     Value.value(&quot;true ^ true&quot;),  logicalXor(Value.value(true), Value.value(true))
		     );
					
			 /*System.out.printf(&quot;%s\n%s: %b\n%s: %b\n&quot;, &quot;Logical NOT (!)&quot;, &quot;!false&quot;,
		     (!false), &quot;!true&quot;, (!true));*/
			 invoke(systemOut, &quot;printf&quot;, 
		     Value.value(&quot;%s\n%s: %b\n%s: %b\n&quot;), 
		     Value.value(&quot;Logical NOT (!)&quot;), 
		     Value.value(&quot;!false&quot;),  not(Value.value(false)), 
		     Value.value(&quot;!true&quot;),  not(Value.value(true))
		     );
					
			runReturn();
		}
	});
	generate(creator);
}

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

<p>咱們在每段ASMSupport的代碼上分別用註釋說明了這段代碼生成的內容是什麼,咱們就不對其進行詳細的解說,在這隻說明下這些邏輯運算符所對應的asmsupport方法,這些方法都是存在與jw.asmsupport.block.ProgramBlock類中:</p>io

<ul> <li><font color="#f79646"><strong>public ShortCircuitAnd conditionalAnd(Parameterized factor1, Parameterized factor2) :</strong></font> 條件與(&amp;&amp;)操做 </li>

<li><font color="#f79646"><strong>public ShortCircuitOr conditionalOr(Parameterized factor1, Parameterized factor2) :</strong></font> 條件或(||)操做 </li>

<li><font color="#f79646"><strong>public LogicalAnd logicalAnd(Parameterized factor1, Parameterized factor2) :</strong></font> 邏輯與(&amp;)操做 </li>

<li><strong><font color="#f79646">public LogicalOr logicalOr(Parameterized factor1, Parameterized factor2) :</font></strong> 邏輯或(|)操做 </li>

<li><font color="#f79646"><strong>public LogicalXor logicalXor(Parameterized factor1, Parameterized factor2) :</strong></font> 邏輯異或(^)操做 </li>

<li><strong><font color="#f79646">public Not not(Parameterized factor) :</font></strong> 非(!)操做 </li> </ul>

<p>&#160;</p>

<p>以上除了not方法都有兩個參數,分別表示運算符先後的兩個參數。好比a&amp;&amp;b,那麼factor1表示a,factor2表示b, 至於not方法,其效果就是!factor的效果。</p>

<p><a href="http://www.wensiqun.com/download/118/">更多實例下載</a></p>

相關文章
相關標籤/搜索