· KLDP.org · KLDP.net · KLDP Wiki · KLDP BBS ·
jcodegen


1. °³¿ä

jcodegen Àº java ¼Ò½ºÄڵ带 »ý¼ºÇÏ´Â java ¶óÀ̺귯¸®ÀÔ´Ï´Ù.

2. ¸¸µç ÀÌÀ¯

¿¹Àü¿¡ ÀÚ¹Ù ¼Ò½ºÄڵ带 ÇÁ·Î±×·¥À» ÀÌ¿ëÇؼ­ ¸¸µé¾î¾ß Çß´ø ÀûÀÌ ÀÖ½À´Ï´Ù. ±×·¡¼­ °ü·Ã ¶óÀ̺귯¸®¸¦ ã¾Æº¸¾Ò´Âµ¥ Àú´Â eclipse ÀÇ AST ¶óÀ̺귯¸®¸¦ ¾Ë¾Æº¸¾Ò½À´Ï´Ù. ±×¶§ Á¦°¡ º¸¾Ò´ø ¹®¼­´Â http://help.eclipse.org/help31/topic/org.eclipse.jdt.doc.isv/guide/jdt_api_manip.htm?resultof="ast" Àε¥, Áß°£Âë¿¡ º¸¸é Hello World ¸¦ Ãâ·ÂÇÏ´Â ¼Ò½ºÄڵ带 ¸¸µå´Â ¿¹Á¦°¡ ³ª¿É´Ï´Ù. Àú´Â ±× ¿¹Á¦¸¦ º¸°í "Á÷Á¢ ¸¸µé¾î¾ß°Ú´Ù."´Â °á½ÉÀ» Çß½À´Ï´Ù. -_-

eclipse AST ¶óÀ̺귯¸®´Â ¼Ò½ºÄÚµåÀÇ ¸ðµç ºÎºÐÀ» °´Ã¼È­Çؼ­ ó¸®ÇÕ´Ï´Ù. ±× °á°ú ÀÌ¹Ì ÀÖ´Â ¼Ò½ºÄڵ带 ºÐ¼®ÇÏ¿© ¹º°¡¸¦ Çϴµ¥´Â ÁÁÁö¸¸ ¹éÁö »óÅ¿¡¼­ ¼Ò½ºÄڵ带 ¸¸µé¾î¾ß ÇÒ °æ¿ì ÄÚµù·®ÀÌ »ó´çÈ÷ ¸¹¾Æ Áý´Ï´Ù.

jcodegen ÀÇ ±âº» ¹æħÀº Àû´çÈ÷ ¾²±â ÆíÇÏ°Ô ÀÔ´Ï´Ù. ºí·°·¹º§(Ŭ·¡½º, ¸Þ¼Òµå ¼±¾ð, °¢Á¾ Á¦¾î±¸Á¶ µî) Àº °´Ã¼¸¦ ÀÌ¿ëÇÏÁö¸¸ °³º°¹®Àå¿¡´Â ¹®ÀÚ¿­À» ¾¹´Ï´Ù. ±× °á°ú jcodegen Àº ¼Ò½ºÄڵ带 ¸¸µå´Âµ¥´Â ÁÁÁö¸¸ ÀÌ¹Ì ÀÖ´Â ¼Ò½ºÄڵ带 ºÐ¼®ÇÏ¿© ¹º°¡¸¦ ÇÏ´Â °ÍÀº °ÅÀÇ ºÒ°¡´ÉÇÕ´Ï´Ù.

3. Hello World ¿¹Á¦

import java.io.IOException;

import net.kldp.jcodegen.Modifier;
import net.kldp.jcodegen.Visibility;
import net.kldp.jcodegen.code.JavaSourceFile;
import net.kldp.jcodegen.code.Statement;
import net.kldp.jcodegen.code.type.ClassType;
import net.kldp.jcodegen.code.type.MethodBlock;


public class CreateHelloWorld {
    public static void main(String[] args) throws IOException {
        // ¼Ò½º ÆÄÀÏ »ý¼º
        JavaSourceFile source = new JavaSourceFile("example") ;
        
        // Ŭ·¡½º »ý¼º
        ClassType hello = new ClassType(source , Visibility.PUBLIC , "HelloWorld") ;
        hello.addCommentLine("'Hello World' printing example program.") ;
        
        // main ¸Þ¼Òµå
        MethodBlock main = new MethodBlock(hello , Visibility.PUBLIC , "main") ;
        main.addModifier(Modifier.STATIC) ;
        main.setReturnType(void.class) ;
        main.addParam(String[].class, "args", "command line args") ;

        // Ãâ·Â¹®
        new Statement(main , "System.out.println(\"Hello \"+\"World\")") ;
        
        source.write(System.getenv("TEMP")) ;
        
    }

}
À§ÀÇ ÄÚµå´Â $TEMP/example/HelloWorld.java ¼Ò½ºÄڵ带 ¸¸µì´Ï´Ù.
package example ;



/** 
 * 'Hello World' printing example program.
 */ 
public class HelloWorld {
    /** 
     * 
     * @param args command line args
     */ 
    public static  void main (String[] args) {
        System.out.println("Hello "+"World");
    }
}

4. »ç¿ë¹ý

¿©±â¿¡ ³ª¿Â »ç¿ë¹ýÀº 0.2 ¹öÀü ±âÁØÀÔ´Ï´Ù. 0.1.1 °ú´Â ¸¹ÀÌ ´Ù¸¨´Ï´Ù. 0.2 ¹öÀüÀº ÇöÀç ¼Ò½ºÄÚµå ÀúÀå¼Ò¿¡¼­ Á÷Á¢ ¼Ò½º¸¦ ´Ù¿î¹ÞÀ¸¸é µË´Ï´Ù.

4.1. ÀüüÀûÀÎ ±¸Á¶

java ÀÇ ¼Ò½ºÄÚµå´Â Æ®¸®±¸Á¶·Î Ç¥ÇöµË´Ï´Ù. µû¶ó¼­ jcodegen µµ ³»ºÎÀûÀ¸·Î Æ®¸®±¸Á¶°¡ µÇ¸ç jcodegenÀÇ ´ëºÎºÐÀÇ ±¸¼º¿ä¼Ò´Â »ý¼ºÀÚÀÇ Ã¹¹ø° ¸Å°³º¯¼ö·Î ÀÚ½ÅÀÇ ºÎ¸ð¸¦ ¹Þ½À´Ï´Ù.

4.2. ¼Ò½º ÆÄÀÏ »ý¼º

jcodegen ¿¡¼­ ¼Ò½º ÆÄÀÏÀº net.kldp.jcodegen.code.JavaSourceFile Ŭ·¡½º·Î Ç¥ÇöµË´Ï´Ù.óÀ½ »ý¼ºÇÒ ¶§ ÀÌ ÆÄÀÏÀÌ ¼ÓÇÏ´Â ÆÐÅ°Áö¸íÀ» ¸Å°³º¯¼ö·Î ¹Þ½À´Ï´Ù.

¼Ò½º ÆÄÀÏÀ» ¾²·Á¸é write ¸Þ¼Òµå¸¦ È£ÃâÇÕ´Ï´Ù. ÀÌ ¸Þ¼Òµå´Â ¼Ò½º ÆÄÀÏÀ» ¸¸µé µð·ºÅ丮 °æ·Î¸¦ ¹Þ½À´Ï´Ù.

4.3. Ŭ·¡½º, ¸Þ¼Òµå, ÇÊµå »ý¼º

Ŭ·¡½º, ¸Þ¼Òµå, Çʵå Á¤ÀǸ¦ ¸¸µé ¶§´Â net.kldp.jcodegen.code.type ÆÐÅ°Áö¿¡ Àִ Ŭ·¡½º¸¦ ÀÌ¿ëÇÏ¸é µË´Ï´Ù.

4.4. Á¦¾î ±¸¹®

if , for µîÀÇ Á¦¾î±¸¹®°ú °ü·ÃµÈ °ÍÀº net.kldp.jcodegen.code ÆÐÅ°Áö¿¡ ÀÖ½À´Ï´Ù.

4.4.1. if »ç¿ë

if ¹®À» ´Üµ¶À¸·Î »ç¿ëÇÒ ¶§´Â net.kldp.jcodegen.code.IfBlock ¸¦ ±×³É ¾²¸é µË´Ï´Ù.
IfBlock is_0 = new IfBlock(parent , "num == 0") ;
new Statement (is_0 , "System.out.println(\"num is 0\")") ;
if(num == 0) {
   System.out.println("num is 0") ;
}

¸¸¾à else if ³ª else µµ ÇÊ¿äÇÑ °æ¿ì net.kldp.jcodegen.code.IfWrapper ¸¦ ¸¸µç´ÙÀ½ ÀÌ °´Ã¼¿¡ net.kldp.jcodegen.code.IfBlock ³ª net.kldp.jcodegen.code.ElseBlock ¸¦ Ãß°¡ÇØ¾ß ÇÕ´Ï´Ù.
        String num_name = "num" ;
        new Statement(main , "int @[num_name] = 3").setVariable("num_name", num_name) ;
        
        IfWrapper if_list = new IfWrapper(main) ;
        for(int i = 0 ; i < 5 ; ++i) {
            IfBlock num_check = new IfBlock(if_list , "@{num_name} == @{i}") ;
            num_check.setVariable("num_name", num_name) ;
            num_check.setVariable("i", i) ;
            
            Statement print = new Statement(num_check , "System.out.println(\"@{i}\")") ;
            print.setVariable("i", i) ;
            
        }
        
        ElseBlock else_block = new ElseBlock(if_list) ;
        new Statement(else_block , "System.out.println(\"Unknown\")") ;
        int num = 3;
        if(num == 0) {
            System.out.println("0");
        }
        else if(num == 1) {
            System.out.println("1");
        }
        else if(num == 2) {
            System.out.println("2");
        }
        else if(num == 3) {
            System.out.println("3");
        }
        else if(num == 4) {
            System.out.println("4");
        }
        else {
            System.out.println("Unknown");
        }
jcodegen ¿¡´Â else if ¸¦ ³ªÅ¸³»´Â Ŭ·¡½º´Â µû·Î ¾ø°í IfBlock ÀÌ Ãß°¡µÈ ¼ø¼­¿¡ µû¶ó if ÀÎÁö else if ÀÎÁö°¡ °áÁ¤µË´Ï´Ù.

4.4.2. try »ç¿ë

try , catch , finally ´Â °¢°¢ net.kldp.jcodegen.code.TryBlock , net.kldp.jcodegen.code.CatchBlock , net.kldp.jcodegen.code.FinallyBlock ·Î ³ªÅ¸³À´Ï´Ù. »ç¿ëÇÒ ¶§´Â ¸ÕÀú net.kldp.jcodegen.code.TryWrapper °´Ã¼¸¦ ¸¸µç µÚ ¿©±â¿¡ Ãß°¡ÇØ¾ß ÇÕ´Ï´Ù.
        TryWrapper trys = new TryWrapper(main) ;
        
        TryBlock try_block = new TryBlock(trys) ;
        new Statement(try_block , "System.out.println(\"TRY\")") ;
        
        CatchBlock catch_runtime = new CatchBlock(trys , RuntimeException.class , "e") ;
        new Statement(catch_runtime , "System.out.println(\"CATCH\")") ;
        
        FinallyBlock final_block = new FinallyBlock(trys) ;
        new Statement(final_block  , "System.out.println(\"FINALLY\")") ;
        try {
            System.out.println("TRY");
        }
        catch (RuntimeException e) {
            System.out.println("CATCH");
        }
        finally {
            System.out.println("FINALLY");
        }

4.5. import ¹® ó¸®Çϱâ

jcodegen Àº ³»ºÎÀûÀ¸·Î import ¹®°ú °ü·ÃµÈ °ÍÀ» ÀÚµ¿À¸·Î ó¸®ÇÕ´Ï´Ù. ¿¹¸¦ µé¾î ƯÁ¤ ¸Þ¼Òµå°¡ java.util.ArrayList ŸÀÔÀ» ¸Å°³º¯¼ö·Î ¹Þ´Â °æ¿ì
    method.addParam(java.util.ArrayList.class , "list" , "") ;
ÀÌ·¸°Ô Çϸé ArrayList ¿¡ ´ëÇÑ import ¹®ÀÌ ÀûÀýÇÏ°Ô Ãß°¡µË´Ï´Ù.

¾î¶² °æ¿ì¿¡´Â ¹®ÀÚ¿­ ³»¿¡¼­ ŸÀÔ¸íÀ» »ç¿ëÇØ¾ß ÇÒ °æ¿ì°¡ ÀÖ½À´Ï´Ù. ¿¹¸¦ µé¾î
    List list = new ArrayList() ;
ÀÌ·± ¹®ÀåÀ» ¸¸µé¾î¾ß ÇÒ °æ¿ì°¡ ÀÖ½À´Ï´Ù. ÀÌ·± °æ¿ì¸¦ À§ÇØ jcodegen Àº net.kldp.jcodegen.base.TypeName Ŭ·¡½º¸¦ Á¦°øÇÕ´Ï´Ù.
    TypeName list = TypeName.create(parent , java.util.List.class) ;
    TypeName arr_list = TypeName.create(parent , java.util.ArrayList.class) ;
    new Statement(parent , list + " list = new " + arr_list + "()") ;
TypeName °´Ã¼¿¡ ´ëÇØ toString() ¸Þ¼Òµå°¡ È£ÃâµÇ¸é, ³»ºÎÀûÀ¸·Î import ¿¡ ´ëÇÑ Ã³¸®¸¦ ÇÑ ÈÄ ¼Ò½ºÄڵ忡¼­ »ç¿ëÇÒ ¼ö Àִ ŸÀÔ¸íÀ» ¸®ÅÏÇÕ´Ï´Ù.

TypeName Ŭ·¡½º´Â generic type µµ Áö¿øÇÕ´Ï´Ù.
    TypeName list_of_string = TypeName.create(parent , java.util.List.class , java.lang.String.class) ;
ÀÌ ÄÚµå´Â ¼Ò½ºÄڵ忡 List<String> À» ¸¸µì´Ï´Ù.

TypeName ÀÇ ÆÑÅ丮 ¸Þ¼Òµå´Â Class °´Ã¼³ª ŸÀÔÀÇ ¹®ÀÚ¿­À̸§À» ¹Þ½À´Ï´Ù. generic type ºÎºÐ¿¡´Â ´Ù¸¥ TypeName °´Ã¼¸¦ ¹Þ´Â °Íµµ °¡´ÉÇÕ´Ï´Ù.

4.6. º¯¼ö¸í Æò°¡ ±â´É »ç¿ë

¿¹¸¦ µé¾î
   List<String> list = new ArrayList<String>() ; 
ÀÌ·± ¹®ÀåÀ» ¸¸µå´Â °æ¿ì ´Ü¼øÈ÷ ¹®ÀÚ¿­ ¿¬°á ¿¬»êÀ» ÀÌ¿ëÇϸé
    TypeName list_of_string = TypeName.create(parent , java.util.List.class , java.lang.String.class) ;
    TypeName arr_list_of_string = TypeName.create(parent , "java.util.ArrayList" , "java.lang.String") ;
    new Statement(parent , list_of_string + " list = new " + arr_list_of_string + "()") ;
ÀÌ·¸°Ô µË´Ï´Ù. ÇÏÁö¸¸ ÀÌ°æ¿ì ¹®ÀÚ¿­ µ¡¼À ¿¬»ê ¶§¹®¿¡ »ý¼ºµÇ´Â Äڵ尡 ÇÑ´«¿¡ µé¾î¿ÀÁö ¾Ê½À´Ï´Ù. ÀÌ´Â »ý¼ºÇØ¾ß ÇÏ´Â Äڵ尡 º¹ÀâÇØÁú¼ö·Ï ´õ ½ÉÇØÁú °ÍÀÔ´Ï´Ù. ÀÌ·± °æ¿ì¸¦ À§ÇØ º¯¼ö¸í Æò°¡ ±â´ÉÀ» Á¦°øÇÕ´Ï´Ù.

¹®ÀÚ¿­ ³»¿¡ @{} ·Î º¯¼ö°¡ »ç¿ëµÇ´Â ºÎºÐÀ» ¸¸µç ´ÙÀ½, setVariable ·Î ÇØ´ç º¯¼ö¿¡ ġȯµÇ´Â °´Ã¼¸¦ ¼³Á¤ÇÏ¸é µË´Ï´Ù.
    TypeName list_of_string = TypeName.create(parent , java.util.List.class , java.lang.String.class) ;
    TypeName arr_list_of_string = TypeName.create(parent , "java.util.ArrayList" , "java.lang.String") ;
    
    Statement stmt = (parent , "@{list} = new @{arr_list}()") ;
    stmt.setVariable("list" , list_of_string) ;
    stmt.setVariable("arr_list" , arr_list_of_string)

setVariableÀÇ Å°´Â ¿Ã¹Ù¸¥ ÀÚ¹Ù º¯¼ö¸í ¹®ÀÚ¿­ÀÌ¸é µÇ°í, °ªÀº ÀÓÀÇÀÇ java °´Ã¼°¡ µË´Ï´Ù. ³»ºÎÀûÀ¸·Î Å°¿¡ ´ëÇÑ °ªÀ» ã¾Æ toString()À» È£ÃâÇÑ °á°ú·Î ġȯÇÕ´Ï´Ù.


5. ÁøÇà»óȲ

2007/04/05 0.2 ¹öÀü ¸±¸®Áî


ID
Password
Join
Like winter snow on summer lawn, time past is time gone.


sponsored by andamiro
sponsored by cdnetworks
sponsored by HP

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2007-09-07 21:21:25
Processing time 0.0074 sec