ASMSupport教程4.9 生成三元运算符

这节我们介绍如何用ASMSupport生成三元运算符(... ? ... : ... )运算符。我们预计生成如下代码:

public static void main(String args[]) {
int i = 10;
int k = i < 0 ? -i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);

i = -10;
k = i < 0 ? -i : i; // get absolute value of i
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);

}

对应的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 TernaryOperatorGenerate extends AbstractExample {


    /**
     * @param args
     */
    public static void main(String[] args) {

        ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC , "generated.operators.TernaryOperatorGenerateExample", 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) {
                //int i = 10;
                LocalVariable i = createVariable("i", AClass.INT_ACLASS, false, Value.value(10));

                //ternary方法将生成三元操作
                //int k = i < 0 ? -i : i;
                LocalVariable k = createVariable("k", AClass.INT_ACLASS, false, ternary(lessThan(i, Value.value(0)), neg(i), i));

                //System.out.print("Absolute value of ");
                invoke(systemOut, "print", Value.value("Absolute value of "));

                //System.out.println(i + " is " + k);
                invoke(systemOut, "println", append(i, Value.value(" is "), k));

                //i = -10;
                assign(i, Value.value(-10));

                //k = i < 0 ? -i : i;
                assign(k, ternary(lessThan(i, Value.value(0)), neg(i), i));

                //System.out.print("Absolute value of ");
                invoke(systemOut, "print", Value.value("Absolute value of "));

                //System.out.println(i + " is " + k);
                invoke(systemOut, "println", append(i, Value.value(" is "), k));

                runReturn();
            }
        });
        generate(creator);
    }

}

其他的都很简单。关键是方法:ternary。下面我们就介绍下这个方法:

public final TernaryOperator ternary(Parameterized exp1, Parameterized exp2, Parameterized exp3):这里有三个参数,分别对应的是三元操作从左至右填写的参数,比如a?b:c';那么exp1表示a,exp2表示b,exp3表示c。这里有三点需要:

  • exp1它对于的java代码必须返回的是布尔型的。
  • exp2和exp3对应的java代码都是需要有返回值的。
  • 这个方法会返回一个jw.asmsupport.operators.ternary.TernaryOperator,这个对象必须被其他asmsupport的操作方法所调用。这个很好理解,比如我们不会单独写一行带你 a?b:c,通常我们是要将这个三元表达式复制个变量或者作为参数传个别的方法或者其他的操作总之,不会独立存在的。

更多实例下载

你可能感兴趣的:(ASMSupport教程4.9 生成三元运算符)