package com.woate.learn;
import org.objectweb.asm.Opcodes;
import cn.wensiqun.asmsupport.block.method.common.StaticMethodBody;
import cn.wensiqun.asmsupport.clazz.AClass;
import cn.wensiqun.asmsupport.clazz.AClassFactory;
import cn.wensiqun.asmsupport.creator.ClassCreator;
import cn.wensiqun.asmsupport.creator.IClassContext;
import cn.wensiqun.asmsupport.definition.value.Value;
import cn.wensiqun.asmsupport.definition.variable.GlobalVariable;
import cn.wensiqun.asmsupport.definition.variable.LocalVariable;
public class HelloWorld {
public static void main(String[] args) {
ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC, \"generated.helloworld.HelloWorldExample\", 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 body(LocalVariable... argus) {
invoke(systemOut, \"println\", Value.value(\"Hello World\"));
// don\'t forget return.
runReturn();
}
});
generate(creator);
}
public static GlobalVariable systemOut = AClassFactory.getProductClass(System.class).getGlobalVariable(\"out\");
public static Class ? generate(IClassContext creator) {
// _这是Class的输出路径。主要为了调试作用。我们通过asmsupport生成的class将获输出到这个路径
// 你可以通过反编译软件看看我们生成的结果
creator.setClassOutPutPath(\".//target//\");
// 这个就是个开关。前面我们把该创建的方法变量都放到了传送带上了。调用startup
// 启动传送带,将上面的东西一个个处理给我返回一个我们需要的成品(就是class了)
Class ? cls = creator.startup();
try {
cls.getMethod(\"main\", String[].class).invoke(cls, new Object[] { null });
} catch (Exception e) {
e.printStackTrace();
}
return cls;
}
}
本文链接: http://asmresearch.immuno-online.com/view-683716.html