<p>在java中有如下邏輯運算符:</p> <ul> <li>&&:條件與 </li> <li>||:條件或 </li> <li>& : 布爾型的邏輯與 </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 && (conditional AND) operator System.out.printf("%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n", "Conditional AND (&&)", "false && false", (false && false), "false && true", (false && true), "true && false", (true && false), "true && true", (true && true));java
// create truth table for || (conditional OR) operator System.out.printf("%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n", "Conditional OR (||)", "false || false", (false || false), "false || true", (false || true), "true || false", (true || false), "true || true", (true || true)); // create truth table for & (boolean logical AND) operator System.out.printf("%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n", "Boolean logical AND (&)", "false & false", (false & false), "false & true", (false & true), "true & false", (true & false), "true & true", (true & true)); // create truth table for | (boolean logical inclusive OR) operator System.out.printf("%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n", "Boolean logical inclusive OR (|)", "false | false", (false | false), "false | true", (false | true), "true | false", (true | false), "true | true", (true | true)); // create truth table for ^ (boolean logical exclusive OR) operator System.out.printf("%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n", "Boolean logical exclusive OR (^)", "false ^ false", (false ^ false), "false ^ true", (false ^ true), "true ^ false", (true ^ false), "true ^ true", (true ^ true)); // create truth table for ! (logical negation) operator System.out.printf("%s\n%s: %b\n%s: %b\n", "Logical NOT (!)", "!false", (!false), "!true", (!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, "generated.operators.LogicalOperatorGenerateExample", 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) { /*System.out.printf("%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n", "Conditional AND (&&)", "false && false", (false && false), "false && true", (false && true), "true && false", (true && false), "true && true", (true && true));*/ invoke(systemOut, "printf", Value.value("%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n"), Value.value("Conditional AND (&&)"), Value.value("false && false"), conditionalAnd(Value.value(false), Value.value(false)), Value.value("false && true"), conditionalAnd(Value.value(false), Value.value(true)), Value.value("true && false"), conditionalAnd(Value.value(true), Value.value(false)), Value.value("true && true"), conditionalAnd(Value.value(true), Value.value(true)) ); /*System.out.printf("%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n", "Conditional OR (||)", "false || false", (false || false), "false || true", (false || true), "true || false", (true || false), "true || true", (true || true));*/ invoke(systemOut, "printf", Value.value("%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n"), Value.value("Conditional OR (||)"), Value.value("false || false"), conditionalOr(Value.value(false), Value.value(false)), Value.value("false || true"), conditionalOr(Value.value(false), Value.value(true)), Value.value("true || false"), conditionalOr(Value.value(true), Value.value(false)), Value.value("true || true"), conditionalOr(Value.value(true), Value.value(true)) ); /*System.out.printf("%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n", "Boolean logical AND (&)", "false & false", (false & false), "false & true", (false & true), "true & false", (true & false), "true & true", (true & true));*/ invoke(systemOut, "printf", Value.value("%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n"), Value.value("Boolean logical AND (&)"), Value.value("false & false"), logicalAnd(Value.value(false), Value.value(false)), Value.value("false & true"), logicalAnd(Value.value(false), Value.value(true)), Value.value("true & false"), logicalAnd(Value.value(true), Value.value(false)), Value.value("true & true"), logicalAnd(Value.value(true), Value.value(true)) ); /*System.out.printf("%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n", "Boolean logical inclusive OR (|)", "false | false", (false | false), "false | true", (false | true), "true | false", (true | false), "true | true", (true | true));*/ invoke(systemOut, "printf", Value.value("%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n"), Value.value("Boolean logical inclusive OR (|)"), Value.value("false | false"), logicalOr(Value.value(false), Value.value(false)), Value.value("false | true"), logicalOr(Value.value(false), Value.value(true)), Value.value("true | false"), logicalOr(Value.value(true), Value.value(false)), Value.value("true | true"), logicalOr(Value.value(true), Value.value(true)) ); /*System.out.printf("%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n", "Boolean logical exclusive OR (^)", "false ^ false", (false ^ false), "false ^ true", (false ^ true), "true ^ false", (true ^ false), "true ^ true", (true ^ true));*/ invoke(systemOut, "printf", Value.value("%s\n%s: %b\n%s: %b\n%s: %b\n%s: %b\n\n"), Value.value("Boolean logical exclusive OR (^)"), Value.value("false ^ false"), logicalXor(Value.value(false), Value.value(false)), Value.value("false ^ true"), logicalXor(Value.value(false), Value.value(true)), Value.value("true ^ false"), logicalXor(Value.value(true), Value.value(false)), Value.value("true ^ true"), logicalXor(Value.value(true), Value.value(true)) ); /*System.out.printf("%s\n%s: %b\n%s: %b\n", "Logical NOT (!)", "!false", (!false), "!true", (!true));*/ invoke(systemOut, "printf", Value.value("%s\n%s: %b\n%s: %b\n"), Value.value("Logical NOT (!)"), Value.value("!false"), not(Value.value(false)), Value.value("!true"), 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> 條件與(&&)操做 </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> 邏輯與(&)操做 </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> </p>
<p>以上除了not方法都有兩個參數,分別表示運算符先後的兩個參數。好比a&&b,那麼factor1表示a,factor2表示b, 至於not方法,其效果就是!factor的效果。</p>
<p><a href="http://www.wensiqun.com/download/118/">更多實例下載</a></p>