AsmSupport研究心得(四)

数组

声明数组分配内存不赋值

       /**

        * 声明数组分配内存不赋值

        */

       @Test

       public void testDefineArray() {

              ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.DefineArray", null, null);

              creator.createMethod("method1", new AClass[] {}, new String[] {}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {

 

                     @Override

                     public void body(LocalVariable... argus) {

                            //声明new int[2][3];

                            ArrayValue av = newArray(AClassFactory.getArrayClass(int[][].class), Value.value(2), Value.value(2));

                            LocalVariable array1 = createArrayVariable(AClassFactory.getArrayClass(int[][].class), "array1", false, null);

                            assign(array1, av);

                           

                            LocalVariable array2 = createArrayVariable(AClassFactory.getArrayClass(int[][].class), "array2", false, newArray(AClassFactory.getArrayClass(int[][].class), Value.value(2), Value.value(2)));

                     }

              });

 

              creator.setClassOutPutPath(".//target//");

              creator.startup();

       }

 

声明数组分配内存赋值

       /**

        * 声明数组分配内存赋值

        */

       @Test

       public void testDefineArrayAndAssign() {

              ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.DefineArrayAndAssign", null, null);

              creator.createMethod("method1", new AClass[] {}, new String[] {}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {

                    

                     @Override

                     public void body(LocalVariable... argus) {

                            //声明new int[2][3];

                            ArrayValue av = newArrayWithValue(AClassFactory.getArrayClass(String[].class), new Parameterized[]{Value.value("one"),Value.value("two") });

                            LocalVariable array1 = createArrayVariable(AClassFactory.getArrayClass(String[].class), "array1", false, null);

                            assign(array1, av);

                            LocalVariable array2 = createArrayVariable(AClassFactory.getArrayClass(String[].class), "array2", false, newArrayWithValue(AClassFactory.getArrayClass(String[].class), new Parameterized[]{Value.value("one"),Value.value("two") }));

                           

                     }

              });

             

              creator.setClassOutPutPath(".//target//");

              creator.startup();

       }

 

 

 

访问数组内容

       /**

        * 获取数组内容

        */

       @Test

       public void testGetArray() {

              ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.GetArray", null, null);

              creator.createMethod("method1", new AClass[] {}, new String[] {}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {

                    

                     @Override

                     public void body(LocalVariable... argus) {

                            GlobalVariable systemOut = AClassFactory.getProductClass(System.class).getGlobalVariable("out");

                            LocalVariable array2 = createArrayVariable(AClassFactory.getArrayClass(String[][].class), "array2", false, newArrayWithValue(AClassFactory.getArrayClass(String[][].class), new Parameterized[][]{{Value.value("one"),Value.value("two")},{Value.value("1")} }));

                            invoke(systemOut, "println", arrayLoad(array2, Value.value(0), Value.value(0)));

                            invoke(systemOut, "println", arrayLoad(array2, Value.value(0), Value.value(1)));

                            invoke(systemOut, "println", arrayLoad(array2, Value.value(1), Value.value(0)));

                            runReturn();

                     }

              });

             

              creator.setClassOutPutPath(".//target//");

              Class clazz = creator.startup();

              try {

                     Object obj = clazz.newInstance();

                     Method m = clazz.getMethod("method1");

                     m.invoke(obj);

              } catch (Exception e) {

                     e.printStackTrace();

              }

       }

 

赋值数组内容

       /**

        * 赋值给数组

        */

       @Test

       public void testSetArray() {

              ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.SetArray", null, null);

              creator.createMethod("method1", new AClass[] {}, new String[] {}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {

                    

                     @Override

                     public void body(LocalVariable... argus) {

                            GlobalVariable systemOut = AClassFactory.getProductClass(System.class).getGlobalVariable("out");

                            LocalVariable array2 = createArrayVariable(AClassFactory.getArrayClass(String[][].class), "array2", false, newArrayWithValue(AClassFactory.getArrayClass(String[][].class), new Parameterized[][]{{Value.value("one"),Value.value("two")},{Value.value("1")} }));

                            arrayStore(array2, Value.value("this is a fixed1"), Value.value(0), Value.value(0));

                            arrayStore(array2, Value.value("this is a fixed2"), Value.value(0), Value.value(1));

                            arrayStore(array2, Value.value("this is a fixed3"), Value.value(1), Value.value(0));

                            invoke(systemOut, "println", arrayLoad(array2, Value.value(0), Value.value(0)));

                            invoke(systemOut, "println", arrayLoad(array2, Value.value(0), Value.value(1)));

                            invoke(systemOut, "println", arrayLoad(array2, Value.value(1), Value.value(0)));

                            runReturn();

                     }

              });

             

              creator.setClassOutPutPath(".//target//");

              Class clazz = creator.startup();

              try {

                     Object obj = clazz.newInstance();

                     Method m = clazz.getMethod("method1");

                     m.invoke(obj);

              } catch (Exception e) {

                     e.printStackTrace();

              }

       }

 

创建对象

创建无参构造

       /**

        * 创建一个无参构造

        */

       @Test

       public void testNewNoParamConstructor(){

              ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.NewNoParamConstructor", null, null);

              creator.createConstructor(new AClass[]{}, new String[]{}, new InitBody() {

                     @Override

                     public void body(LocalVariable... argus) {

                            //此句必须有,调用父类构造函数

                            invokeSuperConstructor(argus);

                            GlobalVariable systemOut = AClassFactory.getProductClass(System.class).getGlobalVariable("out");

                            invoke(systemOut, "println", Value.value("this is a no param Constructor"));

                            //退出块

                            runReturn();

                     }

              }, Opcodes.ACC_PUBLIC);

              creator.setClassOutPutPath(".//target//");

              Class clazz = creator.startup();

              try {

                     Object obj = clazz.newInstance();

              } catch (Exception e) {

                     e.printStackTrace();

              }

       }

 

 

创建有参构造

       /**

        * 创建一个有参构造

        */

       @Test

       public void testNewHasParamConstructor(){

              ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.NewHasParamConstructor", Exception.class, null);

              creator.createConstructor(new AClass[]{AClass.STRING_ACLASS}, new String[]{"message"}, new InitBody() {

                     @Override

                     public void body(LocalVariable... argus) {

                            //此句必须有,调用父类构造函数

                            invokeSuperConstructor(argus[0]);

                            GlobalVariable systemOut = AClassFactory.getProductClass(System.class).getGlobalVariable("out");

                            invoke(systemOut, "println", argus[0]);

                            invoke(systemOut, "println", Value.value("this is a no param Constructor"));

                            //退出块

                            runReturn();

                     }

              }, Opcodes.ACC_PUBLIC);

              creator.setClassOutPutPath(".//target//");

              Class clazz = creator.startup();

              try {

                     Constructor c= clazz.getConstructor(String.class);

                     Object obj = c.newInstance("this is a test");

              } catch (Exception e) {

                     e.printStackTrace();

              }

       }

 

 

调用构造函数创建一个匿名对象

       /**

        * 创建一个匿名对象

        */

       @Test

       public void testNewObject(){

              ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.NewObject", Exception.class, null);

              creator.createConstructor(new AClass[]{AClass.STRING_ACLASS}, new String[]{"message"}, new InitBody() {

                     @Override

                     public void body(LocalVariable... argus) {

                            //此句必须有,调用父类构造函数

                            invokeSuperConstructor(argus[0]);

                            GlobalVariable systemOut = AClassFactory.getProductClass(System.class).getGlobalVariable("out");

                            invoke(systemOut, "println", argus[0]);

                            invoke(systemOut, "println", Value.value("this is a no param Constructor"));

                            invoke(systemOut, "println", invokeConstructor(AClass.STRING_ACLASS, Value.value("this is String")));

                            //退出块

                            runReturn();

                     }

              }, Opcodes.ACC_PUBLIC);

              creator.setClassOutPutPath(".//target//");

              Class clazz = creator.startup();

              try {

                     Constructor c= clazz.getConstructor(String.class);

                     Object obj = c.newInstance("this is a test");

              } catch (Exception e) {

                     e.printStackTrace();

              }

       }

调用构造函数创建一个具名对象

       /**

        * 调用构造函数,创建一个具名对象

        */

       @Test

       public void testNewObject2(){

              ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.NewObject2", Exception.class, null);

              creator.createConstructor(new AClass[]{AClass.STRING_ACLASS}, new String[]{"message"}, new InitBody() {

                     @Override

                     public void body(LocalVariable... argus) {

                            //此句必须有,调用父类构造函数

                            invokeSuperConstructor(argus[0]);

                            GlobalVariable systemOut = AClassFactory.getProductClass(System.class).getGlobalVariable("out");

                            invoke(systemOut, "println", argus[0]);

                            invoke(systemOut, "println", Value.value("this is a no param Constructor"));

                            MethodInvoker mi = invokeConstructor(AClass.STRING_ACLASS, Value.value("this is String"));

                            LocalVariable str = createVariable(AClass.STRING_ACLASS, "str", false, mi);

                            invoke(systemOut, "println",str);

                            //退出块

                            runReturn();

                     }

              }, Opcodes.ACC_PUBLIC);

              creator.setClassOutPutPath(".//target//");

              Class clazz = creator.startup();

              try {

                     Constructor c= clazz.getConstructor(String.class);

                     Object obj = c.newInstance("this is a test");

              } catch (Exception e) {

                     e.printStackTrace();

              }

       }

 

 

你可能感兴趣的:(ASMSupport)