在java中有以下逻辑运算符:
- &&:条件与
- ||:条件或
- & : 布尔型的逻辑与
- | : 布尔型的逻辑或
- ^ : 布尔型的逻辑异或
- ! : 非操作
那么接下来我们将些段例子来看看各种逻辑运算的效果,并且我们再通过ASMSupport生成这个例子的字节码Class:
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));// 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));
}
我们接下来使用ASMSupport如何生成上面的代码:
package example.operators;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 LogicalOperatorGenerate extends AbstractExample {
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); }
}
我们在每段ASMSupport的代码上分别用注释说明了这段代码生成的内容是什么,我们就不对其进行详细的解说,在这只说明下这些逻辑运算符所对应的asmsupport方法,这些方法都是存在与jw.asmsupport.block.ProgramBlock类中:
- public ShortCircuitAnd conditionalAnd(Parameterized factor1, Parameterized factor2) : 条件与(&&)操作
- public ShortCircuitOr conditionalOr(Parameterized factor1, Parameterized factor2) : 条件或(||)操作
- public LogicalAnd logicalAnd(Parameterized factor1, Parameterized factor2) : 逻辑与(&)操作
- public LogicalOr logicalOr(Parameterized factor1, Parameterized factor2) : 逻辑或(|)操作
- public LogicalXor logicalXor(Parameterized factor1, Parameterized factor2) : 逻辑异或(^)操作
- public Not not(Parameterized factor) : 非(!)操作
以上除了not方法都有两个参数,分别表示运算符前后的两个参数。比如a&&b,那么factor1表示a,factor2表示b, 至于not方法,其效果就是!factor的效果。