在java中我们经常用到为运算符,我们假设有如下代码:
public static void main(String\[\] args) { String\[\] binary = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; int a = 3; int b = 6; int c = a | b; int d = a & b; int e = a ^ b; int f = (a ^ 0xFFFFFFFF) & b | a & (b ^ 0xFFFFFFFF); int g = (a ^ 0xFFFFFFFF) & 0xF; System.out.println(" a = " + binary\[a\]); System.out.println(" b = " + binary\[b\]); System.out.println(" a|b = " + binary\[c\]); System.out.println(" a&b = " + binary\[d\]); System.out.println(" a^b = " + binary\[e\]); System.out.println("~a&b|a&~b = " + binary\[f\]); System.out.println(" ~a = " + binary\[g\]); }
那么我们用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 BitwiseOperatorGenerate extends AbstractExample {
/** * @param args */ public static void main(String[] args) { ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC , "generated.operators.BitwiseOperatorGenerateExample", null, null); /* * 生成一个main方法,方法内容和main1内容相同 */ 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) { /*String binary[] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" };*/ LocalVariable binary = createArrayVariable("binary", AClassFactory.getArrayClass(String[].class), false, newArrayWithValue(AClassFactory.getArrayClass(String[].class), stringValueArray(new String[]{ "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}))); //int a = 3; LocalVariable a = createVariable("a", AClass.INT_ACLASS, false, Value.value(3)); //int b = 6; LocalVariable b = createVariable("b", AClass.INT_ACLASS, false, Value.value(6)); //int c = a | b; LocalVariable c = createVariable("c", AClass.INT_ACLASS, false, this.bitOr(a, b)); //int d = a & b; LocalVariable d = createVariable("d", AClass.INT_ACLASS, false, this.bitAnd(a, b)); //int e = a ^ b; LocalVariable e = createVariable("e", AClass.INT_ACLASS, false, this.bitXor(a, b)); //int f = (~a & b) | (a & ~b); LocalVariable f = createVariable("f", AClass.INT_ACLASS, false, bitOr(bitAnd(inverts(a), b), bitAnd(a, inverts(b)))); //int g = ~a & 0x0f; LocalVariable g = createVariable("g", AClass.INT_ACLASS, false, this.bitAnd(inverts(a), Value.value(0x0f))); //System.out.println(" a = " + binary[a]); invoke(systemOut, "println", append(Value.value(" a = "), arrayLoad(binary, a))); //System.out.println(" b = " + binary[b]); invoke(systemOut, "println", append(Value.value(" b = "), arrayLoad(binary, b))); //System.out.println(" a|b = " + binary[c]); invoke(systemOut, "println", append(Value.value(" a|b = "), arrayLoad(binary, c))); //System.out.println(" a&b = " + binary[d]); invoke(systemOut, "println", append(Value.value(" a&b = "), arrayLoad(binary, d))); //System.out.println(" a^b = " + binary[e]); invoke(systemOut, "println", append(Value.value(" a^b = "), arrayLoad(binary, e))); //System.out.println("~a&b|a&~b = " + binary[f]); invoke(systemOut, "println", append(Value.value("~a&b|a&~b = "), arrayLoad(binary, f))); //System.out.println(" ~a = " + binary[g]); invoke(systemOut, "println", append(Value.value(" ~a = "), arrayLoad(binary, g))); runReturn(); } }); generate(creator); } public static Value[] stringValueArray(String[] strs){ Value[] vals = new Value[strs.length]; for(int i=0; i<vals.length; i++){ vals[i] = Value.value(strs[i]); } return vals; }
}
这里我们主要关注下面这几行代码:
LocalVariable c = createVariable("c", AClass.INT\_ACLASS, false, this.bitOr(a, b))
这段代码的作用是创建一个名为”c”的int类型的变量,并且将a和b做或(|)运算的结果保存到这个变量。
LocalVariable d = createVariable("d", AClass.INT\_ACLASS, false, this.bitAnd(a, b));
这段代码的作用是创建一个名为”d”的int类型的变量,并且将a和b做与(&)运算的结果保存到这个变量。
LocalVariable e = createVariable("e", AClass.INT\_ACLASS, false, this.bitXor(a, b));
这段代码的作用是创建一个名为”e”的int类型的变量,并且将a和b做异或(^)运算的结果保存到这个变量。
LocalVariable f = createVariable("f", AClass.INT\_ACLASS, false, bitOr(bitAnd(inverts(a), b), bitAnd(a, inverts(b))));
这段代码的作用是创建一个名为”f”的int类型的变量,至于他的值,稍微复杂点,不过也是运算上多几个步骤,他是先将a取反(~),再和b做与(&)操作获得结果1;再将a和取反(~)后的b做与(&)操作得到结果2,然后再将结果1和结果2做或(^)操作将结果赋予f,对应的java代码就是~a&b|a&~b