<h2>4.4 自增減操做(++,—)</h2> <p>咱們知道,++和--是各類編程語言的基本操做,其做用就不解釋了,直接進入正題,咱們但願生成以下代碼</p> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:030ff93f-6abb-4574-b183-2e855d30580b" 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: 612px; height: 676px;" style=" width: 612px; height: 676px;overflow: auto;">package generated.operators;html
import java.io.PrintStream;java
public class CrementOperatorGenerateExample { public void demonstrate() { System.out.println("***demonstrate"); int a = 1; int b = 2; b++; int c = b; int d = a; a++; c++; System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); }c++
public void incrementAndDecrement() { System.out.println("***incrementAndDecrement"); int count = 10; count++; count--; System.out.println("count = " + count); }web
public static void main(String[] args) { CrementOperatorGenerateExample currentObj = new CrementOperatorGenerateExample(); currentObj.demonstrate(); currentObj.incrementAndDecrement(); } }</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>編程
<p>那麼經過以下ASMSupport代碼就能生成:</p>app
<div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:17542434-1605-4aed-99c4-e804419553c5" 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: 612px; height: 1897px;" style=" width: 612px; height: 1897px;overflow: auto;">package example.operators;編程語言
import org.objectweb.asm.Opcodes;ide
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;code
import example.AbstractExample;orm
public class CrementOperatorGenerate extends AbstractExample {
/** * @param args */ public static void main(String[] args) { ClassCreator creator = new ClassCreator(Opcodes.V1_5, Opcodes.ACC_PUBLIC , "generated.operators.CrementOperatorGenerateExample", null, null); /* * 對應java代碼 * public void demonstrate() { * System.out.println("******************************demonstrate***************************"); * int a = 1; * int b = 2; * int c; * int d; * c = ++b; * d = a++; * c++; * System.out.println("a = " + a); * System.out.println("b = " + b); * System.out.println("c = " + c); * System.out.println("d = " + d); * } */ creator.createMethod("demonstrate", null, null, null, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() { @Override public void generateBody(LocalVariable... argus) { invoke(systemOut, "println", Value.value("******************************demonstrate***************************")); //int a = 1; LocalVariable a = createVariable("a", AClass.INT_ACLASS, false, Value.value(1)); //int b = 2; LocalVariable b = createVariable("b", AClass.INT_ACLASS, false, Value.value(2)); //int c = ++b; LocalVariable c = createVariable("c", AClass.INT_ACLASS, false, beforeInc(b)); //d = a++; LocalVariable d = createVariable("d", AClass.INT_ACLASS, false, afterInc(a)); //c++; afterInc(c); //System.out.println("a = " + a); invoke(systemOut, "println", append(Value.value("a = "), a)); invoke(systemOut, "println", append(Value.value("b = "), b)); invoke(systemOut, "println", append(Value.value("c = "), c)); invoke(systemOut, "println", append(Value.value("d = "), d)); runReturn(); } }); /* * java code: * public void incrementAndDecrement(String[] argv) { * System.out.println("******************************incrementAndDecrement***************************"); * int count = 10; * ++count; * --count; * System.out.println(count); * } */ creator.createMethod("incrementAndDecrement", null, null, null, null, Opcodes.ACC_PUBLIC, new CommonMethodBody() { @Override public void generateBody(LocalVariable... argus) { invoke(systemOut, "println", Value.value("******************************incrementAndDecrement***************************")); //int count = 10; LocalVariable count = createVariable("count", AClass.INT_ACLASS, false, Value.value(10)); // ++count beforeInc(count); // --count; beforeDec(count); // System.out.println("count = " + count); invoke(systemOut, "println", append(Value.value("count = "), count)); runReturn(); } }); 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 generateBody(LocalVariable... argus) { LocalVariable currentObj = createVariable("currentObj", getMethodOwner(), false, invokeConstructor(getMethodOwner())); invoke(currentObj, "demonstrate"); invoke(currentObj, "incrementAndDecrement"); runReturn(); } }); generate(creator); }
} </pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>
<p>咱們主要關注的是creator.createMethod("demonstrate", null, null, null, null, Opcodes.ACC_PUBLIC, new CommonMethodBody()這份部份內容以及creator.createMethod("incrementAndDecrement", null, null, null, null, Opcodes.ACC_PUBLIC, new CommonMethodBody()部分,建立變量部分就不解釋了,若是不知道的能夠參數前一章<a href="http://www.wensiqun.com/2013/06/23/asmsupport_tutorial_4_2.html">ASMSupport教程2動態生成類</a>,咱們這裏主要關注一下四個方法:</p>
<ul> <li><strong>beforeInc: </strong>這個方法至關於++a操做 </li>
<li><strong>afterInc:</strong>這個方法至關於a++操做 </li>
<li><strong>beforeDec:</strong>這個方法至關於--a操做 </li>
<li><strong>afterDec:</strong>這個方法至關於a--操做 </li> </ul>
<p>以上四個方法都只有一個參數,參數類型是繼承Crementable類型的,在ASMSupport中jw.asmsupport.definition.variable.GlobalVariable和jw.asmsupport.definition.variable.LocalVariable。也就是說咱們定義的類屬性和局部變量都是能夠的,可是得注意,這些變量必須是能夠執行++或--操做的,這也和日常java語法是同樣的。</p>
<p>本系列教程全部實例下載地址:<a title="http://www.wensiqun.com/download/118/" href="http://www.wensiqun.com/download/118/">http://www.wensiqun.com/download/118/</a></p>