1 23 24 25 26 31 32 package com.sun.jdo.spi.persistence.utility.generator; 33 34 import java.io.IOException ; 35 36 import java.lang.reflect.Method ; 37 import java.lang.reflect.Modifier ; 38 import java.util.HashMap ; 39 import java.util.StringTokenizer ; 40 41 import com.sun.jdo.spi.persistence.utility.JavaTypeHelper; 42 43 48 public class JavaClassWriterHelper extends JavaTypeHelper { 49 50 public final static String javaExtension_ = ".java"; public final static String void_ = "void"; public final static String boolean_ = "boolean"; public final static String byte_ = "byte"; public final static String byteArray_ = "byte[]"; public final static String param_ = "param"; public final static String param0_ = "param0"; public final static String null_ = "null"; public final static String home_ = "home"; public final static String delim_ = ";"; public final static String paramInitializer_ = "\"\" + "; public final static String paramSeparator_ = ", "; public final static String paramList_ = ","; public final static String paramConcatenator_ = " + \", \" + "; public final static String space_ = " "; public final static String none_ = ""; public final static String escapedEmptyString_ = "\"\""; public final static String dot_ = "."; public final static String parenleft_ = "("; public final static String parenright_ = ")"; public final static String parenthesis_ = "()"; public final static String new_ = "new"; public final static String endLine_ = "\n"; public final static String true_ = "true"; public final static String false_ = "false"; public final static String Collection_ = "java.util.Collection"; public final static String Set_ = "java.util.Set"; public final static String PersistenceCapable_ = "com.sun.jdo.api.persistence.support.PersistenceCapable"; public final static String brackets_ = "[]"; public final static String get_ = "get"; public final static String set_ = "set"; public final static String Oid_ = ".Oid"; public final static String Helper_ = "_JDOHelper"; public final static String returnNull_ = "return null;"; public final static String fileName_ = "fileName"; public final static String int_ = "int"; public final static String String_ = "java.lang.String"; public final static String Class_ = "java.lang.Class"; public final static String Date_ = "java.util.Date"; public final static String SqlDate_ = "java.sql.Date"; public final static String SqlTime_ = "java.sql.Time"; public final static String SqlTimestamp_ = "java.sql.Timestamp"; 93 public final static String Object_ = "Object"; 97 public final static String [] super_ = new String [] {"super();"}; 99 private final static String [] indentation_ = new String [] { 102 " ", " ", " ", " "}; 107 112 public static String [] getBodyAsStrings(String body) { 113 StringTokenizer st = new StringTokenizer (body, endLine_); 114 String [] rc = new String [st.countTokens()]; 115 int ii = 0; 116 while(st.hasMoreTokens()) { 117 String s = st.nextToken(); 118 int i = s.lastIndexOf('\t'); 119 if (i > -1) 120 rc[ii] = indentation_[i] + s.substring(i + 1); 121 else 122 rc[ii] = s; 123 124 ii++; 125 } 126 127 return rc; 128 } 129 130 137 public static Class getWrapperType(Class cls) { 138 Class rc = getWrapperClass(cls); 139 if (rc == null) { rc = cls; 141 } 142 143 return rc; 144 } 145 146 154 public static String getWrapperExpr(Class exprType, String expr) { 155 156 StringBuffer wrapped = new StringBuffer (); 157 158 wrapped.append(new_); 159 wrapped.append(space_); 160 wrapped.append(getWrapperType(exprType).getName()); 161 wrapped.append(parenleft_); 162 wrapped.append(expr); 163 wrapped.append(parenright_); 164 165 return wrapped.toString(); 166 } 167 168 174 public static String getUnwrapMethodName(Class primitiveType) 175 { 176 return primitiveType.getName() + "Value()"; } 178 179 188 public static String getPrimitiveType(Class cls) { 189 String rc = getPrimitiveName(cls); 190 if (rc == null) { rc = cls.getName(); 192 } 193 194 return rc; 195 } 196 197 202 public static String [] getExceptionNames(Method m) { 203 Class [] cls = m.getExceptionTypes(); 204 String [] rc = new String [cls.length]; 205 for (int ii = 0; ii < cls.length; ii++) { 206 rc[ii] = cls[ii].getName(); 207 } 208 return rc; 209 } 210 211 217 public static String getParameterTypesList(Method m) { 218 if (m == null) 219 return none_; 220 221 StringBuffer buf = new StringBuffer (); 222 Class [] paramTypes = m.getParameterTypes(); 223 for (int i = 0; i < paramTypes.length; i++) { 224 if (i > 0) 225 buf.append(paramList_); 226 buf.append(getTypeRepr(paramTypes[i])); 227 } 228 return buf.toString(); 229 230 } 231 232 238 public static String getParametersList(Method m) { 239 return getParametersListWithSeparator(m, paramSeparator_); 240 } 241 242 249 public static String getParametersListWithSeparator(Method m, String sep) { 250 int count = m.getParameterTypes().length; 251 StringBuffer rc = new StringBuffer (); 252 253 for (int ii = 0; ii < count; ii++) { 254 if (ii > 0) 255 rc.append(sep); 256 257 rc.append(param_ + ii); 258 } 259 return rc.toString(); 260 } 261 262 269 public static void addFields(String prop, int modifiers, JavaClassWriter writer) 270 throws IOException { 271 String [] v = getBodyAsStrings(prop); 272 for (int i = 0; i < v.length; i++) { 273 StringTokenizer st = new StringTokenizer (v[i], space_); 274 String type = st.nextToken(); 275 String name = st.nextToken(); 276 StringBuffer value = new StringBuffer (); 277 while(st.hasMoreTokens()) 278 value.append(st.nextToken() + space_); 279 280 int l = value.length(); 281 value.deleteCharAt(l - 1); writer.addField(name, modifiers, type, value.toString(), null); } 288 } 289 290 297 public static void addPrivateField(String prop, int modifiers, JavaClassWriter writer) 298 throws IOException { 299 addFields(prop, Modifier.PRIVATE + modifiers, writer); 300 } 301 302 303 310 public static void addGenericMethod(String mname, 311 String [] body, JavaClassWriter writer) 312 throws IOException { 313 addGenericMethod(mname, void_, body, writer); 314 } 315 316 317 326 public static void addGenericMethod(String mname, String type, 327 String [] body, JavaClassWriter writer) 328 throws IOException { 329 addGenericMethod(mname, Modifier.PRIVATE, type, body, writer); 330 } 331 332 341 public static void addGenericMethod(String mname, int modifiers, 342 String [] body, JavaClassWriter writer) 343 throws IOException { 344 addGenericMethod(mname, modifiers, void_, body, writer); 345 } 346 347 357 public static void addGenericMethod(String mname, int modifiers, 358 String type, String [] body, 359 JavaClassWriter writer) 360 throws IOException { 361 writer.addMethod(mname, modifiers, type, null, null, null, body, null); } 370 371 382 public static void addGenericMethod(Method m, String mname, 383 String mtype, String body, 384 JavaClassWriter writer) 385 throws IOException { 386 387 Class [] types = m.getParameterTypes(); 388 int count = types.length; 389 String [] parameterTypes = new String [count]; 390 String [] parameterNames = new String [count]; 391 for (int ii = 0; ii < count; ii++) { 392 parameterTypes[ii] = getTypeRepr(types[ii]); 393 parameterNames[ii] = param_ + ii; 394 } 395 396 String [] exceptionTypes = getExceptionNames(m); 397 398 int modifiers = m.getModifiers(); 399 if (Modifier.isAbstract(modifiers)) 400 modifiers -= Modifier.ABSTRACT; 401 402 writer.addMethod(mname, modifiers, mtype, parameterNames, parameterTypes, exceptionTypes, getBodyAsStrings(body), null); } 411 412 416 public static String getTypeRepr(Class clazz) 417 { 418 return (clazz.isArray() ? 419 getTypeRepr(clazz.getComponentType()) + brackets_ : 420 clazz.getName()); 421 } 422 423 } 424 | Popular Tags |