1 29 30 package com.caucho.java.gen; 31 32 import com.caucho.bytecode.JMethod; 33 import com.caucho.java.JavaWriter; 34 import com.caucho.util.L10N; 35 36 import java.io.IOException ; 37 import java.util.ArrayList ; 38 39 42 public class BaseClass extends ClassComponent { 43 private static final L10N L = new L10N(BaseClass.class); 44 45 private String _className; 46 private String _superClassName; 47 48 private boolean _isStatic; 49 private String _visibility = "public"; 50 51 private ArrayList <String > _interfaceNames = 52 new ArrayList <String >(); 53 54 private ArrayList <ClassComponent> _components = 55 new ArrayList <ClassComponent>(); 56 57 private DependencyComponent _dependencyComponent; 58 59 62 public BaseClass() 63 { 64 } 65 66 69 public BaseClass(String className) 70 { 71 _className = className; 72 } 73 74 77 public BaseClass(String className, String superClassName) 78 { 79 _className = className; 80 _superClassName = superClassName; 81 } 82 83 86 public void setClassName(String className) 87 { 88 _className = className; 89 } 90 91 94 public String getClassName() 95 { 96 return _className; 97 } 98 99 102 public void setSuperClassName(String superClassName) 103 { 104 _superClassName = superClassName; 105 } 106 107 110 public void addInterfaceName(String name) 111 { 112 _interfaceNames.add(name); 113 } 114 115 118 public void setStatic(boolean isStatic) 119 { 120 _isStatic = isStatic; 121 } 122 123 126 public void setVisibility(String visibility) 127 { 128 _visibility = visibility; 129 } 130 131 134 public void addMethod(BaseMethod method) 135 { 136 addComponent(method); 137 } 138 139 142 public DependencyComponent addDependencyComponent() 143 { 144 if (_dependencyComponent == null) { 145 _dependencyComponent = new DependencyComponent(); 146 addComponent(_dependencyComponent); 147 } 148 149 return _dependencyComponent; 150 } 151 152 155 public BaseMethod findMethod(JMethod method) 156 { 157 for (ClassComponent component : _components) { 158 if (component instanceof BaseMethod) { 159 BaseMethod baseMethod = (BaseMethod) component; 160 161 if (baseMethod.getMethod().equals(method)) 162 return baseMethod; 163 } 164 } 165 166 return null; 167 } 168 169 172 public BaseMethod createMethod(JMethod method) 173 { 174 BaseMethod baseMethod = findMethod(method); 175 176 if (baseMethod == null) { 177 baseMethod = new BaseMethod(method, method); 178 179 _components.add(baseMethod); 180 } 181 182 return baseMethod; 183 } 184 185 188 public void addComponent(ClassComponent component) 189 { 190 _components.add(component); 191 } 192 193 198 public void generate(JavaWriter out) 199 throws IOException 200 { 201 if (_visibility != null && ! _visibility.equals("")) 202 out.print(_visibility + " "); 203 204 if (_isStatic) 205 out.print("static "); 206 207 out.print("class " + _className); 208 209 if (_superClassName != null) 210 out.print(" extends " + _superClassName); 211 212 if (_interfaceNames.size() > 0) { 213 out.print(" implements "); 214 215 for (int i = 0; i < _interfaceNames.size(); i++) { 216 if (i != 0) 217 out.print(", "); 218 219 out.print(_interfaceNames.get(i)); 220 } 221 } 222 223 out.println(" {"); 224 out.pushDepth(); 225 226 generateClassContent(out); 227 228 out.popDepth(); 229 out.println("}"); 230 } 231 232 235 protected void generateClassContent(JavaWriter out) 236 throws IOException 237 { 238 generateComponents(out); 239 } 240 241 244 protected void generateComponents(JavaWriter out) 245 throws IOException 246 { 247 for (int i = 0; i < _components.size(); i++) { 248 if (i != 0) 249 out.println(); 250 251 _components.get(i).generate(out); 252 } 253 } 254 } 255 | Popular Tags |