ASMSupport教程4.3赋值操做

<h2>4.3 生成复制操做</h2> <p>这一节将讲述如何生成,咱们预计上生成以下代码:</p> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:151a4d6f-63d0-44ee-a51e-78dc06d51396" 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: 539px; height: 275px;" style=" width: 539px; height: 275px;overflow: auto;">public class AssignmentGenerateExample { public static String commonMethod() { return &quot;I'm from commonMethod&quot;; }html

public static void main(String[] args) { String string = null; string = commonMethod(); System.out.println("first asign :" + string); string = "second assing value"; System.out.println("second asign :" + string); } }</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>java

<p>这段代码然咱们看到了为变量赋值null,将方法返回值复制给变量,以及将常量复制给null。那么对应的ASMSupport的代码以下:</p>app

<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:612dcef2-94de-4375-b5e4-c5aee98fea1d" 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: 539px; height: 560px;" style=" width: 539px; height: 560px;overflow: auto;">public static void main(String[] args) { ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC , &quot;generated.operators.AssignmentGenerateExample&quot;, null, null);ide

creator.createStaticMethod(&quot;commonMethod&quot;, null, null, AClass.STRING_ACLASS, null, Opcodes.ACC_PUBLIC, new StaticMethodBody(){

	@Override
	public void generateBody(LocalVariable... argus) {
		runReturn(Value.value(&quot;I'm from commonMethod&quot;));
	}
});

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) {
		//position 1建立个String变量默认赋值为null
		LocalVariable string = createVariable(&quot;string&quot;, AClass.STRING_ACLASS, false, null);
		//position 2
		assign(string, invokeStatic(getMethodOwner(), &quot;commonMethod&quot;));
		invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;first asign :&quot;), string));
		//position 3
		assign(string, Value.value(&quot;second assing value&quot;));
		invoke(systemOut, &quot;println&quot;, append(Value.value(&quot;second asign :&quot;), string));
		
		runReturn();
	}
});
generate(creator);

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

<p>上面的代码中systemOut是AClassFactory.getProductClass(System.class).getGlobalVariable(&quot;out&quot;)的返回值。</p>code

<p>上面的的代码中,position 1下面的一行就是建立名为string的变量,类型是String类型,而且将null赋值给变量,下面咱们将解释下这行代码。</p>orm

<p><strong>createVariable(&quot;string&quot;, AClass.STRING_ACLASS, false, null)</strong>:</p>htm

<ul> <li>第一参数是变量名; </li>教程

<li>第二个参数是变量类型, 其类型是AClass的类型(参见<a href="http://www.wensiqun.com/2013/06/08/asmsupport_tutorial_1.html">ASMSupport教程1之动态生成接口</a>了解更多关于AClass第一段) </li>接口

<li>第三个参数表示当前建立的变量名是不是匿名的,若是是ture,表示此变量为匿名变量即第一个参数设置的变量名无效 </li>

<li>第四个参数表示建立变量的时候赋予的默认值,这里是null,固然若是咱们调用方法,并将返回值赋值给变量,咱们这里也能够直接只用position 2下面的代码中的invokeStatic(getMethodOwner(), &quot;commonMethod&quot;)放在这里 </li> </ul>

<p>在上面咱们讲了如何在建立变量的同时赋值,接下来是咱们将值赋予给已经存在的变量,这个操做须要的就是assign方法, 咱们能够看到position 2下的代码以下:</p>

<p><strong>assign(string, invokeStatic(getMethodOwner(), &quot;commonMethod&quot;))</strong></p>

<ul> <li>第一个参数,咱们须要赋值的变量,这里能够是局部变量,也能够是全局变量(关于ASMSupport中如何建立局部变量和全局变量可参考<a href="http://www.wensiqun.com/2013/06/23/asmsupport_tutorial_4_2.html">ASMSupport教程2动态生成类</a>) </li>

<li>第二个参数,这个参数就是表示咱们须要传递个变量的值,能够是null,能够是方法调用,能够是另外一个变量,也能够是常量,只要是Parameterized类型的就能够,固然,这些值都必须和变量的类型相同或者是其子类,这和java代码是同样的。 </li> </ul>

<p>在position 3下的代码和position 2的大体相同只不过这里直接使用Value.value(&quot;second assing value&quot;)定义常量,而上面的是一个方法调用操做。</p>

<p>&#160;</p>

<p>本系列教程全部实例代码在<a href="http://www.wensiqun.com/amssupport">下载页面</a>最下面,有两种下载方式,推荐第二个,而第一个是trunk上的代码,第二个是对于0.2版本的, 最近空间可能有点不稳定,多刷新下吧</p>

相关文章
相关标签/搜索