반응형

무작정 따라해보자, 필드를 만드는 방법도 무진장 쉽다. clazz에 field 메소드를 통해서 생성이 가능하다.



File rootDir = new File(outputConfig.getRootDirectory());
JCodeModel codeModel = new JCodeModel();

try {
JDefinedClass clazz = codeModel._class(outputConfig.getClassName());
clazz.field(JMod.PRIVATE, String.class, "name");
clazz.field(JMod.PRIVATE, int.class, "age");
clazz.field(JMod.PRIVATE, ArrayList.class, "test");

codeModel.build(rootDir);
} catch (JClassAlreadyExistsException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}


결과 클래스는 다음과 같다.



package net.daum.domaingen.json;

import java.util.ArrayList;

public class SimpleClass {

private String name;
private int age;
private ArrayList test;

}


반응형

'Programming > JCodeModel' 카테고리의 다른 글

JCodeModel로 클래스 생성하기  (0) 2015.01.07
반응형

JCodeModel은 쉽게 클래스 소스를 생성할 수 있게 도와준다.

아래와 같이 단 3줄의 코딩으로도 간단하게 클래스 소스를 생성할 수 있다. 무작정 따라해보자.



@Test
public void testCreateClass() throws JClassAlreadyExistsException, IOException {
JCodeModel codeModel = new JCodeModel();
codeModel._class("net.daum.domaingen.TestClass");
codeModel.build(new File("/Users/devsun/dev/projects/domain-generator/src/test/java"));
}


생성된 소스는 다음과 같다.


package net.daum.domaingen;


public class TestClass {


}


반응형

'Programming > JCodeModel' 카테고리의 다른 글

JCodeModel 필드 선언하기  (0) 2015.01.07

+ Recent posts