在java中,关系运算符是很常用的, 分别是>,==,<,>=,<=,!=这六种,我们按照惯例看看我们需要生成的代码:< /p>
public static void main(String[] args) {Random rand = new Random(); int i = rand.nextInt(100); int j = rand.nextInt(100); System.out.println("i = " + i); System.out.println("j = " + j); System.out.println("i > j is " + (i > j)); System.out.println("i < j is " + (i < j)); System.out.println("i >= j is " + (i >= j)); System.out.println("i <= j is " + (i <= j)); System.out.println("i == j is " + (i == j)); System.out.println("i != j is " + (i != j)); System.out.println("(i < 10) && (j < 10) is " + ((i < 10) && (j < 10))); System.out.println("(i < 10) || (j < 10) is " + ((i < 10) || (j < 10)));
}
这里用到了非常多的关系运算符,那么对应的amssupport代码如下:
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 { public static void main(String[] args) { ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC , "generated.operators.RelationalOperatorGenerateExample", 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) { //Random rand = new Random(); LocalVariable rand = createVariable("rand", AClassFactory.getProductClass(Random.class), false, invokeConstructor(AClassFactory.getProductClass(Random.class))); //int i = rand.nextInt(100); LocalVariable i = createVariable("i", AClass.INT_ACLASS, false, invoke(rand, "nextInt", Value.value(100))); //int j = rand.nextInt(100); LocalVariable j = createVariable("j", AClass.INT_ACLASS, false, invoke(rand, "nextInt", Value.value(100))); //System.out.println("i = " + i); invoke(systemOut, "println", append(Value.value("i = "), i)); //System.out.println("j = " + j); invoke(systemOut, "println", append(Value.value("j = "), j)); //System.out.println("i > j is " + (i > j)); invoke(systemOut, "println", append(Value.value("i > j is "), greaterThan(i, j))); //System.out.println("i < j is " + (i < j)); invoke(systemOut, "println", append(Value.value("i < j is "), lessThan(i, j))); //System.out.println("i >= j is " + (i >= j)); invoke(systemOut, "println", append(Value.value("i >= j is "), greaterEqual(i, j))); //System.out.println("i <= j is " + (i <= j)); invoke(systemOut, "println", append(Value.value("i <= j is "), lessEqual(i, j))); //System.out.println("i == j is " + (i == j)); invoke(systemOut, "println", append(Value.value("i == j is "), equal(i, j))); //System.out.println("i != j is " + (i != j)); invoke(systemOut, "println", append(Value.value("i != j is "), notEqual(i, j))); //System.out.println("(i < 10) && (j < 10) is " + ((i < 10) && (j < 10))); invoke(systemOut, "println", append(Value.value("(i < 10) && (j < 10) is "), conditionalAnd(lessThan(i, Value.value(10)), lessThan(j, Value.value(10))))); //System.out.println("(i < 10) || (j < 10) is " + ((i < 10) || (j < 10))); invoke(systemOut, "println", append(Value.value("(i < 10) || (j < 10) is "), conditionalOr(lessThan(i, Value.value(10)), lessThan(j, Value.value(10))))); runReturn(); } }); generate(creator); } }
这里面我们主要介绍这关系运算所对应的asmsupport的方法(这些方法都是属于jw.asmsupport.block.ProgramBlock的):
public final GreaterThan greaterThan(Parameterized factor1, Parameterized factor2): >运算符的操作。
public final GreaterThan greaterEqual(Parameterized factor1, Parameterized factor2) : >=运算符。
public final GreaterThan lessThan(Parameterized factor1, Parameterized factor2) : <运算符。
public final GreaterThan lessEqual(Parameterized factor1, Parameterized factor2) : <=运算符。
public final GreaterThan lessEqual(Parameterized factor1, Parameterized factor2) : ==运算符。
public final GreaterThan lessEqual(Parameterized factor1, Parameterized factor2) : !=运算符。
这些方法都有两个参数,分别表示运算符前后的两个参数。比如a==b,那么factor1表示a,factor2表示b
上面asmsupport代码中还有conditionalAnd和conditionalOr方法,这是逻辑运算符,将在以后讲解。