条件判断
If-then判断
/** * If-then判断 */ public void testIfThen() { ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.IfThen", null, null); creator.createMethod("method1", new AClass[] {}, new String[] {}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { ifthan(new IF(equal(Value.value(1), Value.value(2))) { @Override public void body() { GlobalVariable systemOut = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(systemOut, "println", Value.value("this is if test")); } }); runReturn(); } });
creator.setClassOutPutPath(".//target//"); creator.startup(); } |
If-then-else判断
public void testIfThenElse() { ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.IfThenElse", null, null); creator.createMethod("method1", new AClass[] {}, new String[] {}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { ifthan(new IF(equal(Value.value(1), Value.value(2))) { @Override public void body() { GlobalVariable systemOut = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(systemOut, "println", Value.value("this is if test")); invoke(systemOut, "println", Value.value("this is if test")); } }).elsethan(new Else() {
@Override public void body() { GlobalVariable systemOut = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(systemOut, "println", Value.value("this is then test")); invoke(systemOut, "println", Value.value("this is then test")); } }); runReturn(); } });
creator.setClassOutPutPath(".//target//"); creator.startup(); } |
If-then-elseif-else判断
@Test public void testIfThenElseIfThen() { ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.IfThenElseIfThen", null, null); creator.createMethod("method1", new AClass[] {}, new String[] {}, AClass.VOID_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { ifthan(new IF(equal(Value.value(1), Value.value(2))) { @Override public void body() { GlobalVariable systemOut = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(systemOut, "println", Value.value("this is if test")); invoke(systemOut, "println", Value.value("this is if test")); } }).elseif(new ElseIF(not(Value.value(true))) {
@Override public void body() { GlobalVariable systemOut = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(systemOut, "println", Value.value("this is elseif test")); invoke(systemOut, "println", Value.value("this is elseif test")); } }).elsethan(new Else() {
@Override public void body() { GlobalVariable systemOut = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(systemOut, "println", Value.value("this is then test")); invoke(systemOut, "println", Value.value("this is then test")); } }); runReturn(); } });
creator.setClassOutPutPath(".//target//"); creator.startup(); } |
方法调用
公开成员方法
/** * 公开成员方法 */ @Test public void testPublicMemberMethod() { ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.PublicMemberMethod", null, null); creator.createMethod("method2", 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 date = createVariable(AClassFactory.getProductClass(java.util.Date.class), "date", false, invokeConstructor(AClassFactory.getProductClass(java.util.Date.class))); //匿名返回值 MethodInvoker m1 = invoke(date, "getTime"); invoke(systemOut, "println", m1); runReturn(); } });
creator.setClassOutPutPath(".//target//"); creator.startup(); } |
当前类成员方法
/** * 当前类成员方法 */ @Test public void testClassInnerMethod() { ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.ClassInnerMethod", null, null); creator.createMethod("method1", new AClass[] {AClass.INT_ACLASS}, new String[] {"int1"}, AClass.STRING_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { GlobalVariable systemOut = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(systemOut, "println", argus[0]); runReturn(Value.value("this is method1 return value")); } }); creator.createMethod("method2", 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"); //匿名返回值 MethodInvoker m1 = invoke(getThis(), "method1",Value.value(2)); invoke(systemOut, "println", m1); //具名返回值 MethodInvoker m2 = invoke(getThis(), "method1",Value.value(2)); LocalVariable ret = createVariable(AClass.STRING_ACLASS, "ret", false, m2); invoke(systemOut, "println", ret); runReturn(); } });
creator.setClassOutPutPath(".//target//"); creator.startup(); } |
父类成员方法
/** * 父类内部成员方法 */ @Test public void testParentClassInnerMethod() { ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "generated.create.ParentClassInnerMethod", Date.class, null); creator.createMethod("getTime", new AClass[] {}, new String[] {}, AClass.LONG_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() {
@Override public void body(LocalVariable... argus) { GlobalVariable systemOut = AClassFactory.getProductClass(System.class).getGlobalVariable("out"); invoke(systemOut, "println", Value.value("this is a son getTime()")); runReturn(Value.value(1024L)); } }); creator.createMethod("method2", 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");
invoke(systemOut, "println", invoke(getThis(), "getTime"));
invoke(systemOut, "println", invoke(getSuper(), "getTime")); runReturn(); } });
creator.setClassOutPutPath(".//target//"); creator.startup(); } |
公开静态方法
已经在第一季举过例
当前类静态方法
已经在第一季举过例
父类静态方法
已经在第一季举过例
构造方法
已经在第一季举过例
静态代码块
已经在第一季举过例