KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > utility > generator > JavaClassWriterHelper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25
26 /*
27  * JavaClassWriterHelper.java
28  *
29  * Created on December 03, 2001
30  */

31
32 package com.sun.jdo.spi.persistence.utility.generator;
33
34 import java.io.IOException JavaDoc;
35
36 import java.lang.reflect.Method JavaDoc;
37 import java.lang.reflect.Modifier JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.StringTokenizer JavaDoc;
40
41 import com.sun.jdo.spi.persistence.utility.JavaTypeHelper;
42
43 /*
44  * This is the utility class for helper strings and type convertion.
45  *
46  * @author Marina Vatkina
47  */

48 public class JavaClassWriterHelper extends JavaTypeHelper {
49
50     public final static String JavaDoc javaExtension_ = ".java"; // NOI18N
51
public final static String JavaDoc void_ = "void"; // NOI18N
52
public final static String JavaDoc boolean_ = "boolean"; // NOI18N
53
public final static String JavaDoc byte_ = "byte"; // NOI18N
54
public final static String JavaDoc byteArray_ = "byte[]"; // NOI18N
55
public final static String JavaDoc param_ = "param"; // NOI18N
56
public final static String JavaDoc param0_ = "param0"; // NOI18N
57
public final static String JavaDoc null_ = "null"; // NOI18N
58
public final static String JavaDoc home_ = "home"; // NOI18N
59
public final static String JavaDoc delim_ = ";"; // NOI18N
60
public final static String JavaDoc paramInitializer_ = "\"\" + "; // NOI18N
61
public final static String JavaDoc paramSeparator_ = ", "; // NOI18N
62
public final static String JavaDoc paramList_ = ","; // NOI18N
63
public final static String JavaDoc paramConcatenator_ = " + \", \" + "; // NOI18N
64
public final static String JavaDoc space_ = " "; // NOI18N
65
public final static String JavaDoc none_ = ""; // NOI18N
66
public final static String JavaDoc escapedEmptyString_ = "\"\""; // NOI18N
67
public final static String JavaDoc dot_ = "."; // NOI18N
68
public final static String JavaDoc parenleft_ = "("; // NOI18N
69
public final static String JavaDoc parenright_ = ")"; // NOI18N
70
public final static String JavaDoc parenthesis_ = "()"; // NOI18N
71
public final static String JavaDoc new_ = "new"; // NOI18N
72
public final static String JavaDoc endLine_ = "\n"; // NOI18N
73
public final static String JavaDoc true_ = "true"; // NOI18N
74
public final static String JavaDoc false_ = "false"; // NOI18N
75
public final static String JavaDoc Collection_ = "java.util.Collection"; // NOI18N
76
public final static String JavaDoc Set_ = "java.util.Set"; // NOI18N
77
public final static String JavaDoc PersistenceCapable_ = "com.sun.jdo.api.persistence.support.PersistenceCapable"; // NOI18N
78
public final static String JavaDoc brackets_ = "[]"; // NOI18N
79
public final static String JavaDoc get_ = "get"; // NOI18N
80
public final static String JavaDoc set_ = "set"; // NOI18N
81
public final static String JavaDoc Oid_ = ".Oid"; // NOI18N
82
public final static String JavaDoc Helper_ = "_JDOHelper"; // NOI18N
83
public final static String JavaDoc returnNull_ = "return null;"; // NOI18N
84
public final static String JavaDoc fileName_ = "fileName"; // NOI18N
85
public final static String JavaDoc int_ = "int"; // NOI18N
86
public final static String JavaDoc String_ = "java.lang.String"; // NOI18N
87
public final static String JavaDoc Class_ = "java.lang.Class"; // NOI18N
88
public final static String JavaDoc Date_ = "java.util.Date"; // NOI18N
89
public final static String JavaDoc SqlDate_ = "java.sql.Date"; // NOI18N
90
public final static String JavaDoc SqlTime_ = "java.sql.Time"; // NOI18N
91
public final static String JavaDoc SqlTimestamp_ = "java.sql.Timestamp"; // NOI18N
92

93     // This variable is used to construct both the type and method name
94
// e.g. setObjectField(), so it should be kept in a short version.
95
public final static String JavaDoc Object_ = "Object"; // NOI18N
96

97     public final static String JavaDoc[] super_ = new String JavaDoc[] {"super();"}; // NOI18N
98

99     // This String[] is used internally to replace "\t" with the element of this array
100
// that represents the corresponding indentation in spaces.
101
private final static String JavaDoc[] indentation_ = new String JavaDoc[] {
102         " ", // NOI18N
103
" ", // NOI18N
104
" ", // NOI18N
105
" "}; // NOI18N
106

107     /**
108      * Converts method body into String array.
109      * @param body as String with each substring separated by "\n"
110      * @return method body as String array.
111      */

112     public static String JavaDoc[] getBodyAsStrings(String JavaDoc body) {
113         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(body, endLine_);
114         String JavaDoc[] rc = new String JavaDoc[st.countTokens()];
115         int ii = 0;
116         while(st.hasMoreTokens()) {
117             String JavaDoc 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     /**
131      * Returns java Object wrapper Class corresponding to
132      * the primitive Class if the passed class represents a primitive.
133      * If the parameter is of Object type, it is returned.
134      * @param cls the primitive Class to find Object wrapper for.
135      * @return Object type Class.
136      */

137     public static Class JavaDoc getWrapperType(Class JavaDoc cls) {
138         Class JavaDoc rc = getWrapperClass(cls);
139         if (rc == null) { // not a primitive
140
rc = cls;
141         }
142
143         return rc;
144     }
145
146     /**
147      * A helper method which generates an expression to wrap a primitive
148      * datatype to its corresponding wrapper class.
149      * @param exprType The class of the primitive type
150      * @param expr The expression representing a primtive typevalue
151      * @return A String containing the expression for wrapping the
152      * primitive datatype in its corresponding wrapperclass
153      */

154     public static String JavaDoc getWrapperExpr(Class JavaDoc exprType, String JavaDoc expr) {
155
156         StringBuffer JavaDoc wrapped = new StringBuffer JavaDoc();
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     /** Returns the name of the method to access the value of the primitive type
169      * of the wrapper class.
170      * example: boolean.class is mapped to "booleanValue()".
171      * @param primitiveType the class object of the primitive type.
172      * @return the name of the method to access the primitive value of the wrapper
173      */

174     public static String JavaDoc getUnwrapMethodName(Class JavaDoc primitiveType)
175     {
176         return primitiveType.getName() + "Value()"; //NOI18N
177
}
178
179     /**
180      * Returns name of the primitive type corresponding to
181      * the Object wrapper Class passed as a parameter.
182      * If the parameter is of primitive type, its name is
183      * returned.
184      * @param cls the Object wrapper Class to find name of
185      * the primitive type for.
186      * @return name of the primitive type as String.
187      */

188     public static String JavaDoc getPrimitiveType(Class JavaDoc cls) {
189         String JavaDoc rc = getPrimitiveName(cls);
190         if (rc == null) { // not an Object
191
rc = cls.getName();
192         }
193
194         return rc;
195     }
196
197     /**
198      * Returns exception type names as String[].
199      * @param m the Method to identify exception types for.
200      * @return exception type names as String[].
201      */

202     public static String JavaDoc[] getExceptionNames(Method JavaDoc m) {
203         Class JavaDoc[] cls = m.getExceptionTypes();
204         String JavaDoc[] rc = new String JavaDoc[cls.length];
205         for (int ii = 0; ii < cls.length; ii++) {
206             rc[ii] = cls[ii].getName();
207         }
208         return rc;
209     }
210
211     /**
212      * Returns list of method parameter types in format
213      * <code>type0[,type1[,...]]</code>
214      * @param m the Method to identify list of method parameters for.
215      * @return list of method parameter types as String
216      */

217     public static String JavaDoc getParameterTypesList(Method JavaDoc m) {
218        if (m == null)
219             return none_;
220  
221         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
222         Class JavaDoc[] 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     /**
233      * Returns list of method parameters in format
234      * <code>param0[, param1[,...]]</code>
235      * @param m the Method to identify list of method parameters for.
236      * @return list of method parameters as String
237      */

238     public static String JavaDoc getParametersList(Method JavaDoc m) {
239         return getParametersListWithSeparator(m, paramSeparator_);
240     }
241
242     /**
243      * Returns list of method parameters delimited by specified
244      * separator
245      * @param m the Method to identify list of method parameters for.
246      * @param sep the separator to be used to delimit the parameter names
247      * @return list of method parameters as String
248      */

249     public static String JavaDoc getParametersListWithSeparator(Method JavaDoc m, String JavaDoc sep) {
250         int count = m.getParameterTypes().length;
251         StringBuffer JavaDoc rc = new StringBuffer JavaDoc();
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     /**
263      * Adds fields to a class parsing the multi-String property.
264      * @param prop String to use for field generation.
265      * @param modifiers other field modifiers for these fields.
266      * @param JavaClassWriter the Class writer.
267      * @throws IOException if writer fails to add a field.
268      */

269     public static void addFields(String JavaDoc prop, int modifiers, JavaClassWriter writer)
270                                                                  throws IOException JavaDoc{
271         String JavaDoc[] v = getBodyAsStrings(prop);
272         for (int i = 0; i < v.length; i++) {
273             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(v[i], space_);
274             String JavaDoc type = st.nextToken();
275             String JavaDoc name = st.nextToken();
276             StringBuffer JavaDoc value = new StringBuffer JavaDoc();
277             while(st.hasMoreTokens())
278                value.append(st.nextToken() + space_);
279
280             int l = value.length();
281             value.deleteCharAt(l - 1); // last space
282
writer.addField(name, // name
283
modifiers, // modifiers
284
type, // type
285
value.toString(), // value,
286
null); // comments
287
}
288     }
289
290     /**
291      * Adds private fields to a class parsing the multi-String property.
292      * @param prop String to use for field generation.
293      * @param modifiers field modifiers for these fields.
294      * @param JavaClassWriter the Class writer.
295      * @throws IOException if writer fails to add a field.
296      */

297     public static void addPrivateField(String JavaDoc prop, int modifiers, JavaClassWriter writer)
298                                                                  throws IOException JavaDoc{
299         addFields(prop, Modifier.PRIVATE + modifiers, writer);
300     }
301
302
303     /**
304      * Adds a private void no-args method to a class with this method body.
305      * @param mname method name
306      * @param body the method body as String[]
307      * @param JavaClassWriter the Class writer.
308      * @throws IOException if writer fails to add a field.
309      */

310     public static void addGenericMethod(String JavaDoc mname,
311                                  String JavaDoc[] body, JavaClassWriter writer)
312                                  throws IOException JavaDoc{
313         addGenericMethod(mname, void_, body, writer);
314     }
315
316
317     /**
318      * Adds a private no-args method to a class with this method body
319      * and return type.
320      * @param mname method name
321      * @param type return type of the method
322      * @param body the method body as String[]
323      * @param JavaClassWriter the Class writer.
324      * @throws IOException if writer fails to add a field.
325      */

326     public static void addGenericMethod(String JavaDoc mname, String JavaDoc type,
327                                  String JavaDoc[] body, JavaClassWriter writer)
328                                  throws IOException JavaDoc{
329         addGenericMethod(mname, Modifier.PRIVATE, type, body, writer);
330     }
331
332     /**
333      * Adds a private void no-args method to a class with this method body
334      * and modifiers.
335      * @param mname method name
336      * @param modifiers the method modifiers
337      * @param body the method body as String[]
338      * @param JavaClassWriter the Class writer.
339      * @throws IOException if writer fails to add a field.
340      */

341     public static void addGenericMethod(String JavaDoc mname, int modifiers,
342                                  String JavaDoc[] body, JavaClassWriter writer)
343                                  throws IOException JavaDoc{
344         addGenericMethod(mname, modifiers, void_, body, writer);
345     }
346
347     /**
348      * Adds a private void no-args method to a class with this method body,
349      * modifiers, and return type.
350      * @param mname method name
351      * @param modifiers the method modifiers
352      * @param type return type of the method
353      * @param body the method body as String[]
354      * @param JavaClassWriter the Class writer.
355      * @throws IOException if writer fails to add a field.
356      */

357     public static void addGenericMethod(String JavaDoc mname, int modifiers,
358                                  String JavaDoc type, String JavaDoc[] body,
359                                  JavaClassWriter writer)
360                                  throws IOException JavaDoc{
361         writer.addMethod(mname, // name
362
modifiers, // modifiers
363
type, // returnType
364
null, // parameterNames
365
null,// parameterTypes
366
null,// exceptions
367
body, // body
368
null);// comments
369
}
370
371     /**
372      * Adds a method to a class parsing the multi-String property.
373      * @param m Method that describes one to be added to the bean.
374      * @param mname method name if different from Method#getName(). This
375      * will be true for finder/selector/create methods.
376      * @param type return type of the method, that can differ from the
377      * bean's local or remote interface.
378      * @param body String to use for method body generation.
379      * @param JavaClassWriter the Class writer.
380      * @throws IOException if writer fails to add a field.
381      */

382     public static void addGenericMethod(Method JavaDoc m, String JavaDoc mname,
383                                  String JavaDoc mtype, String JavaDoc body,
384                                  JavaClassWriter writer)
385                                  throws IOException JavaDoc{
386
387         Class JavaDoc[] types = m.getParameterTypes();
388         int count = types.length;
389         String JavaDoc[] parameterTypes = new String JavaDoc[count];
390         String JavaDoc[] parameterNames = new String JavaDoc[count];
391         for (int ii = 0; ii < count; ii++) {
392             parameterTypes[ii] = getTypeRepr(types[ii]);
393             parameterNames[ii] = param_ + ii;
394         }
395
396         String JavaDoc[] exceptionTypes = getExceptionNames(m);
397
398         int modifiers = m.getModifiers();
399         if (Modifier.isAbstract(modifiers))
400             modifiers -= Modifier.ABSTRACT;
401
402         writer.addMethod(mname, // name
403
modifiers, // modifiers
404
mtype, // returnType
405
parameterNames, // parameterNames
406
parameterTypes,// parameterTypes
407
exceptionTypes,// exceptions
408
getBodyAsStrings(body), // body
409
null);// comments
410
}
411  
412     /**
413      * Returns the String representation of the specified class instance.
414      * An array is represented as the name of the element type followed by [].
415      */

416     public static String JavaDoc getTypeRepr(Class JavaDoc clazz)
417     {
418         return (clazz.isArray() ?
419                 getTypeRepr(clazz.getComponentType()) + brackets_ :
420                 clazz.getName());
421     }
422
423 }
424
Popular Tags