ASMSupport教程4.2

<h2>4.2 生成Return操作</h2> <p>这一节我们将讲述如何生成return操作,我们将生成如下代码:</p> <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:0041272d-4ecf-4039-a991-b5323856859d" class="wlWriterEditableSmartContent"><pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 964px; height: 263px;" style=" width: 964px; height: 263px;overflow: auto;">package generated.operators;

import java.io.PrintStream;

public class ReturnOperatorGenerateExample { public static String commonMethod() { return "I'm from commonMethod"; }

public static void main(String[] args) { System.out.println(commonMethod()); } }</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>

<p>这段代码的两个方法的返回类型分别是String和void,对应的ASMSupport的代码如下:</p> <p> <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:74055c8b-f8a1-4671-a4d4-5f47171f957e" class="wlWriterEditableSmartContent"><pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 964px; height: 471px;" style=" width: 964px; height: 471px;overflow: auto;">public static void main(String\[\] args) { ClassCreator creator = new ClassCreator(Opcodes.V1\_5, Opcodes.ACC\_PUBLIC , &quot;generated.operators.ReturnOperatorGenerateExample&quot;, null, null);
1/* 2 * 有返回类型的方法 3 */ 4creator.createStaticMethod(&quot;commonMethod&quot;, null, null, AClass.STRING_ACLASS, null, Opcodes.ACC_PUBLIC, new StaticMethodBody(){ 5 @Override 6 public void generateBody(LocalVariable... argus) { 7 runReturn(Value.value(&quot;I'm from commonMethod&quot;)); 8 } 9}); 10 11/* 12 * 无返回类型的方法 13 */ 14creator.createStaticMethod(&quot;main&quot;, new AClass[]{AClassFactory.getProductClass(String[].class)}, new String[]{&quot;args&quot;}, null, null, 15 Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC, new StaticMethodBody(){ 16 @Override 17 public void generateBody(LocalVariable... argus) { 18 invoke(systemOut, &quot;println&quot;, invokeStatic(getMethodOwner(), &quot;commonMethod&quot;)); 19 runReturn(); 20 } 21}); 22generate(creator);

}</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>

</p> <p>这里我们主要关注的是runReturn(Value.value(&quot;I'm from commonMethod&quot;))和runReturn(),前者是非void的,参数就是我们方法需要返回的值,后者就是直接return,没有返回值。</p>
点赞
收藏

评论区

加载中...

相关推荐

JavaScript设计模式之单例模式

<sectionid"nice"datatool"mdnice编辑器"datawebsite"https://www.mdnice.com"style"fontsize:16px;color:black;lineheight:1.6;wordspacing:0px;letterspacing:0px;word

全栈进阶:Nginx基本功能及其原理

<divclass"output_wrapper"id"output_wrapper_id"style"fontsize:16px;color:rgb(62,62,62);lineheight:1.6;wordspacing:0px;letterspacing:0px;fontfamily:'Helvetica

Java中Class对象详解

<divclass"htmledit\_views"id"content\_views"<phttps://blog.csdn.net/mcryeasy/article/details/52344729<br</p<p待优化整理总结</p<p</p<h1style"padding:0px;fontfamily:'apple

Nginx在Windows平台的配置与使用

<divclass"output\_wrapper"id"output\_wrapper\_id"style"fontsize:16px;color:rgb(62,62,62);lineheight:1.6;wordspacing:0px;letterspacing:0px;fontfamily:'Helvet

ASMSupport教程4.5 在Class中生成算术运算符

<h2ASMSupport教程4.5在Class中生成算术运算符</h2<p这节我们开始介绍ASMSupport如何生成算数运算符(\/%),依旧先看我们需要生成的java代码:</p<divid"scid:9D7513F9C04C4721824A2B34F0212519:22b13b06a0d64b8eb7cd768

ASMSupport教程4.9 生成三元运算符

<p这节我们介绍如何用ASMSupport生成三元运算符(...?...:...)运算符。我们预计生成如下代码:</p<divid"scid:9D7513F9C04C4721824A2B34F0212519:935e30cc33214e0093ba9834f3a4e044"class"wlWriterEditableS