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>

相關文章
相關標籤/搜索