1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.seasar.tuigwaa.model.common;
23
24 import javassist.CannotCompileException;
25 import javassist.ClassPool;
26 import javassist.CtClass;
27 import javassist.CtField;
28 import javassist.CtMethod;
29 import javassist.CtNewMethod;
30 import javassist.NotFoundException;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.seasar.framework.aop.javassist.AbstractGenerator;
35
36 import com.isenshi.util.CharUtil;
37
38 /***
39 * @author nishioka
40 *
41 * TODO To change the template for this generated type comment go to
42 * Window - Preferences - Java - Code Style - Code Templates
43 */
44 public class PojoClassGenerator extends AbstractGenerator {
45
46 Log log = LogFactory.getLog(getClass());
47
48 private CtClass clazz_;
49
50 /***
51 *
52 */
53 public PojoClassGenerator(ClassPool cp, String className) {
54 super(cp);
55 clazz_ = getCtClass(className);
56 setSeriarizable(cp);
57 }
58
59 /***
60 * extends srcClass
61 */
62 public PojoClassGenerator(ClassPool cp, Class srcClass, String enhancedClassname){
63 super(cp);
64 Class superClass = (srcClass.isInterface())? Object.class : srcClass;
65 clazz_ = createCtClass(enhancedClassname,superClass);
66 setSeriarizable(cp);
67 }
68
69 private void setSeriarizable(ClassPool cp){
70 CtClass serializable = cp.makeInterface("java.io.Serializable");
71 clazz_.setInterfaces(new CtClass[]{serializable});
72 }
73
74 private CtClass getCtClass(String className){
75 CtClass ctClass = null;
76 try {
77 ctClass =classPool.get(className);
78 } catch (NotFoundException e) {
79 ctClass = classPool.makeClass(className);
80 }
81 return ctClass;
82 }
83
84 public void addProperty(String name, String type) throws CannotCompileException, ClassNotFoundException{
85
86 if(name==null||type==null){
87 throw new ClassNotFoundException();
88 }
89
90 CtClass fieldClazz = getCtClass(type);
91 CtField field = new CtField(fieldClazz, name, clazz_);
92 clazz_.addField(field);
93
94 CtMethod setter = getSetterMethod(field);
95 CtMethod getter = getGetterMethod(field);
96
97 clazz_.addMethod(getter);
98 clazz_.addMethod(setter);
99 }
100
101 public void overrideMethod(String methodName, String methodBody)
102 throws NotFoundException, CannotCompileException{
103
104 CtMethod[] mz = clazz_.getMethods();
105 CtMethod method = null;
106
107 for(int i=0;i<mz.length;i++){
108 if(mz[i].getName().equals(methodName)){
109 method = mz[i];
110 }
111 }
112
113 if(method != null){
114 CtMethod newMethod= CtNewMethod.copy(method,clazz_,null);
115 newMethod.setBody(methodBody);
116 clazz_.addMethod(newMethod);
117 }
118 }
119
120
121 public Class toClass(){
122 log.info("Creating ...." + clazz_.getName());
123 return toClass(getClassLoader(), clazz_);
124 }
125
126 private CtMethod getSetterMethod(CtField field) throws CannotCompileException{
127 String name = field.getName();
128 String upperPropName = CharUtil.toUpperCaseAtFisrtChar(name);
129 return CtNewMethod.setter("set" + upperPropName, field);
130 }
131
132 private CtMethod getGetterMethod(CtField field) throws CannotCompileException{
133 String name = field.getName();
134 String upperPropName = CharUtil.toUpperCaseAtFisrtChar(name);
135 return CtNewMethod.getter("get" + upperPropName, field);
136 }
137
138 public ClassLoader getClassLoader() {
139 ClassLoader cl = Thread.currentThread().getContextClassLoader();
140 if (cl == null) {
141 cl = getClass().getClassLoader();
142 }
143 return cl;
144 }
145
146 public void reModify(){
147 clazz_.defrost();
148 }
149 }