ASMSupport教程4.12 生成方法调用操作

<p>这一节我们讲如何用ASMSupport生成方法调用的操作,方法调用包括下面四种类型:</p> <ol> <li>调用构造方法 <li>调用静态方法 <li>调用非静态方法 <li>调用当前类的方法 <li>调用父类方法</li></ol> <p>首先我们需要看我们想要生成的类:</p> <p>代码1:</p> <h3> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:8ef33b82-d2bb-4577-8993-178c1b8d44f7" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 796px; height: 519px;" style=" width: 796px; height: 519px;overflow: auto;">package generated.operators;

import java.io.PrintStream;

public class MethodInvokeOperatorGenerateExample { public String toString() { return "description is &quot;" + super.toString() + "&quot;"; }

public String description() { return toString(); }

public static String getDescription(MethodInvokeOperatorGenerateExample obj) { return obj.description(); }

public static void main(String[] args) { MethodInvokeOperatorGenerateExample obj = new MethodInvokeOperatorGenerateExample(); System.out.println("Call static method : " + getDescription(obj)); } }</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div></h3>

<p>这里面包括了所有的方法调用的类型。</p><font style="font-weight: bold"></font> <h3><font style="font-weight: bold">调用构造方法</font></h3> <hr>

<p>调用构造方法我们是用的是<strong><font color="#f79646">public final MethodInvoker invokeConstructor(AClass owner, Parameterized... arguments)</font></strong>方法,同样也是ProgramBlock的方法,</p> <p>这个方法有两个参数:</p> <ol> <li>表示需要构造的类,这里我们通过调用getMethodOwner获取当前操作类 <li>表示构造方法的参数。这个参数是个变元参数</li></ol> <p>在代码1中,我们有MethodInvokeOperatorGenerateExample obj = new MethodInvokeOperatorGenerateExample();代码是调用构造方法,那我们对应的asmsupport的代码如下:</p><pre>invokeConstructor(getMethodOwner())</pre> <p>这里有个getMethodOwner()方法,这个方法就是获取当前生成的class或修改的class,由于我们正在创建class MethodInvokeOperatorGenerateExample,所以这里getMethodOwner()获得的AClass就是MethodInvokeOperatorGenerateExample</p> <h3><strong>调用静态方法</strong></h3> <hr>

<p>调用静态方法我们使用的是<strong><font color="#f79646">public final MethodInvoker invokeStatic(AClass owner, String methodName, Parameterized... arguments)</font><font color="#000000">方法,</font></strong></p> <p><strong>参数</strong>:</p> <ol> <li>表示调用的静态方法所属的class <li>调用的方法名 <li>表示方法的参数。这个参数是个变元参数</li></ol> <p>在代码1中,getDescription方法就是静态方法,而getDescription(obj)正好是调用静态方法。对应的asmsupport的代码如下:</p> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:e04f2636-f46f-4668-b83c-d56efb64b76e" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 796px; height: 35px;" style=" width: 796px; height: 35px;overflow: auto;">invokeStatic(getMethodOwner(), &quot;getDescription&quot;, obj)</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div> <p>这里的obj是上面我们通过构造函数构造的对象,这里作为getDescription的参数。</p> <h3><font style="font-weight: bold">调用非静态方法 </font></h3> <hr> 调用静态方法我们使用的是<strong><font color="#f79646">public final MethodInvoker invoke(Parameterized caller, String methodName, Parameterized... arguments)</font><font color="#000000">方法,</font></strong> <p><strong>参数</strong>:</p> <ol> <li>表示调用的静态方法所属的对象,这里的类型是 Parameterized ,说明这里可以是LocalVariable或者GlobalVariable类型,或者其他的的Parameterized类型 <li>调用的方法名 <li>表示方法的参数。这个参数是个变元参数</li></ol> <p>在代码1中的getDescription方法内,我们调用了”obj.description();”语句,那么对应的asmsupport的代码如下:</p> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:6674f327-f787-40ce-8722-a6021ec32e37" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 796px; height: 48px;" style=" width: 796px; height: 48px;overflow: auto;">invoke(argus[0], &quot;description&quot;)</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div> <p>这里的argus[0]表示当前方法的第一个参数</p> <h3><font style="font-weight: bold">调用当前类的方法和调用父类方法</font></h3><!--EndFragment--> <hr>

<p>调用当前类和调用父类的方法其实和调用非静态方法很相似,唯一的区别就是我们传入的第一个参数。在java代码中使用<font color="#ff0000">this和super</font><font color="#000000">关键字表示当前的和父类的,那么我们同样在asmsupport中也有相应的方法:</font></p> <ol> <li><strong>public final ThisVariable getThis():</strong> 返回的<strong>jw.asmsupport.definition.variableThisVariable</strong>对象,将这个对象传入invoke方法的第一个参数。 <li><strong>public final SuperVariable getSuper()</strong>:返回的是<strong>jw.asmsupport.definition.variableSuperVariable</strong>对象,将这个对象传入invoke方法的第一个参数。</li></ol> <p>当然,如果当前类中没有重写父类的方法,我们直接调用getThis()方法并将其返回值传入invoke方法作为调用父类方法。这个和java代码中一样的,比如代码1中的代码:</p> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:f18648a6-5fdb-4fc2-ba06-c938a169a649" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 796px; height: 90px;" style=" width: 796px; height: 90px;overflow: auto;">public String description() { return toString(); }</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div> <p>这里面其实就是调用了父类的方法,但是我们这里使用this.toString()或者super.toString()都可以,所以这里我们使用如下asmsupport代码:</p> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:10fb259f-c3aa-4b27-810c-fd855d1ea213" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 796px; height: 90px;" style=" width: 796px; height: 90px;overflow: auto;">/*this.toString()*/ invoke(getThis(), &quot;toString&quot;) /*super.toString()*/ invoke(getSuper(), &quot;toString&quot;)</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div> <h3><font style="font-weight: bold">代码1完整ASMSupport代码:</font></h3> <hr>

<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:fae928e6-f052-4ee6-ab45-a17630d41028" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 796px; height: 2080px;" style=" width: 796px; height: 2080px;overflow: auto;">package example.operators;

import org.objectweb.asm.Opcodes;

import jw.asmsupport.block.method.common.CommonMethodBody; 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 jw.asmsupport.operators.method.MethodInvoker;

import example.AbstractExample;

public class MethodInvokeOperatorGenerate extends AbstractExample {

/**
 * @param args
 */
public static void main(String[] args) {
    
    ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC , &quot;generated.operators.MethodInvokeOperatorGenerateExample&quot;, null, null);

    creator.createMethod(&quot;toString&quot;, null, null, AClass.STRING_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody(){

        @Override
        public void generateBody(LocalVariable... argus) {
            //通常我们将super看作是一个变量所以我们用invoke(Parameterized caller, String methodName, Parameterized... arguments)
            //方法实现super.xxxx。通过getSuper方法获取super变量
            MethodInvoker superToString = invoke(getSuper(), &quot;toString&quot;);
            runReturn(append(Value.value(&quot;description is \&quot;&quot;), superToString, Value.value(&quot;\&quot;&quot;)));
        }
        
    });
    
    /**
     * 实现如下方法
     * public String description(){
     *    return toString();
     * }
     * 
     */
    creator.createMethod(&quot;description&quot;, null, null, AClass.STRING_ACLASS, null, Opcodes.ACC_PUBLIC, new CommonMethodBody(){

        @Override
        public void generateBody(LocalVariable... argus) {
            /**
             * 这里和调用super.xxx是一样的。调用当前类中的非静态方法都是通过
             * invoke(Parameterized caller, String methodName, Parameterized... arguments)方法调用。
             * 通过getThis方法获取this变量
             */
            runReturn(invoke(getThis(), &quot;toString&quot;));                
        }
        
    });
    
    /**
     * 生成如下方法的字节码:
     * public static String getDescription(MyObject obj){
     *     return obj.description();
     * }
     */
    creator.createStaticMethod(&quot;getDescription&quot;, new AClass[]{creator.getCurrentClass()}, new String[]{&quot;obj&quot;}, AClass.STRING_ACLASS, null,
            Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, new StaticMethodBody(){

        @Override
        public void generateBody(LocalVariable... argus) {
            /**
             * 和上面。
             * 这里argus[0]就是我们定义的参数obj。如果需要传递参数可以直接在
             * 方法调用的时候添加需要传递的参数,因为这是个变元方法
             */
            runReturn(invoke(argus[0], &quot;description&quot;));
        }
    });
    
    /**
     * public static void main(String[] args){
     *     MyObject obj = new MyObject();
     *     System.out.println(&quot;Call static method : &quot; + MyObject.getDescription(obj));
     * }
     */
    creator.createStaticMethod(&quot;main&quot;, new AClass[]{AClassFactory.getProductClass(String[].class)}, new String[]{&quot;args&quot;}, null, null,
            Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, new StaticMethodBody(){

        @Override
        public void generateBody(LocalVariable... argus) {
            /**
             * 首先调用构造方法生成MethodInvokeOperatorGenerateExample obj = new MethodInvokeOperatorGenerateExample();
             * 通过invokeConstructor方法实现调用构造方法
             * 这个方法后两个参数。
             * 1.表示需要构造的类,这里我们通过调用getMethodOwner获取当前操作类
             * 2.表示构造方法的参数。这个参数是个变元参数
             */
        	LocalVariable obj = createVariable(&quot;obj&quot;, getMethodOwner(), false, invokeConstructor(getMethodOwner()));
            
        	/**
        	 * 实现System.out.println(&quot;Call static method : &quot; + MyObject.getDescription(obj));
        	 * 这里将学习到如何生成调用静态方法
        	 * 
        	 * 调用静态方法是通过invokeStatic(AClass owner, String methodName, Parameterized... arguments)
        	 * 实现的。
        	 * 这个方法有三个参数
        	 * 1.通过那个Class调用静态方法。
        	 * 2.静态方法的名称
        	 * 3.参数
        	 */
        	MethodInvoker getDescriptionInvoker = invokeStatic(getMethodOwner(), &quot;getDescription&quot;, obj);
        	
        	invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;Call static method : &quot;), getDescriptionInvoker));
            
        	runReturn();
        }
    });
    generate(creator);
}

} </pre></div> 更多教程

更多教程

转载于:https://my.oschina.net/wensiqun/blog/143963

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值