1 | // |
2 | // (c) Copyright, Moebius Solutions, 2002, 2003 |
3 | // |
4 | // All Rights Reserved |
5 | // |
6 | // This material may be reproduced by or for the U. S. Government |
7 | // pursuant to the copyright license under the clause at |
8 | // DFARS 252.227-7013 (OCT 1988). |
9 | // |
10 | /* |
11 | * $Id: ClassGeneratorVisitor.java,v 1.5 2005/06/13 19:10:56 hastings Exp $ |
12 | */ |
13 | package com.moesol.generator; |
14 | |
15 | import java.io.*; |
16 | import java.lang.reflect.*; |
17 | import java.util.*; |
18 | import com.moesol.generator.core.*; |
19 | import com.moesol.generator.printer.*; |
20 | import com.moesol.generator.visitor.*; |
21 | |
22 | public abstract class ClassGeneratorVisitor implements ReflectVisitor { |
23 | public ClassGeneratorVisitor(Writer out, TranslationContext ctx) { |
24 | this.out = out; |
25 | m_trans_ctx = ctx; |
26 | } |
27 | /** override to change visit strategy */ |
28 | public void visit(Class a_class) throws ApplyException { |
29 | VisitClass.visitReflect(a_class, this); |
30 | } |
31 | public void setPassNumber(int pass_number) { |
32 | if (pass_number > 0) { |
33 | setPassAppend(Integer.toString(pass_number)); |
34 | } else { |
35 | m_pass_append = null; |
36 | } |
37 | } |
38 | public void setPassAppend(String pass) { |
39 | if (pass != null && pass.length() > 0 && pass.charAt(0) != '.') { |
40 | m_pass_append = "." + pass; |
41 | } else { |
42 | m_pass_append = pass; |
43 | } |
44 | } |
45 | public String getPassAppend() { |
46 | return m_pass_append == null ? "" : m_pass_append; |
47 | } |
48 | |
49 | /** Template method */ |
50 | protected Callable createCallable(Class a_class, Method method) throws ApplyException { |
51 | return new MethodCallable(a_class, method); |
52 | } |
53 | /** Template method */ |
54 | protected boolean shouldGenerateField(Field field) { |
55 | return Modifier.isPublic(field.getModifiers()); |
56 | } |
57 | /** Template method */ |
58 | protected void extendFieldProperties(Field field, Properties p) throws ApplyException { } |
59 | /** Template method */ |
60 | protected abstract String getFieldTemplateName(Field field) throws ApplyException; |
61 | |
62 | |
63 | /** Template method */ |
64 | protected boolean shouldGenerateMethod(Method method) throws ApplyException { |
65 | return Modifier.isPublic(method.getModifiers()); |
66 | } |
67 | /** Template method */ |
68 | protected void extendMethodProperties(Method method, Properties p) throws ApplyException { } |
69 | /** Template method */ |
70 | protected abstract String getTemplateName(Method method) throws ApplyException; |
71 | |
72 | |
73 | /** Template method */ |
74 | protected boolean shouldGenerateConstructor(Constructor ctor) throws ApplyException { |
75 | // TODO private constructors are important for limiting if new |
76 | // can be called. |
77 | return Modifier.isPublic(ctor.getModifiers()); |
78 | } |
79 | /** Template method */ |
80 | protected void extendConstructorProperties(Constructor ctor, Properties p) throws ApplyException |
81 | { } |
82 | /** Template method */ |
83 | protected abstract String getTemplateName(Constructor ctor) throws ApplyException; |
84 | |
85 | |
86 | public TranslationContext getTranslationContext() { |
87 | return m_trans_ctx; |
88 | } |
89 | public void visitField(final Class c, final Field field) throws ApplyException { |
90 | if (shouldGenerateField(field) != true) { return; } |
91 | if (field.getName().indexOf('$') != -1) { return; } // TODO source="1.4" caused $assertionsDisabled to show up |
92 | |
93 | Properties p = createFieldProperties(field); |
94 | extendFieldProperties(field, p); |
95 | Template t = createFieldTemplate(field); |
96 | try { |
97 | t.apply(p, out); |
98 | } catch (ApplyException e) { |
99 | throw new ApplyException(c.getName() + "." + field.getName(), e); |
100 | } |
101 | } |
102 | // TODO Remove Parameter, just pass method, see Method.getDeclaringClass |
103 | // TODO Reconsider above, when we override a method we want to specify |
104 | // a different class. MethodCallable allows a class override. |
105 | public void visitMethod(Class a_class, Method method) throws ApplyException { |
106 | if (!shouldGenerateMethod(method)) { return; } |
107 | |
108 | current_callable = createCallable(a_class, method); // TODO, thread saftey? |
109 | |
110 | Properties p = createMethodProperties(current_callable); |
111 | extendMethodProperties(method, p); |
112 | Template t = createMethodTemplate(method); |
113 | t.apply(p, out); |
114 | } |
115 | public void visitConstructor(Class c, Constructor ctor) throws ApplyException { |
116 | if (shouldGenerateConstructor(ctor) != true) { return; } |
117 | |
118 | current_callable = new ConstructorCallable(ctor); // TODO, thread saftey? |
119 | |
120 | Properties p = createConstructorProperties(ctor); |
121 | extendConstructorProperties(ctor, p); |
122 | Template t = createConstructorTemplate(ctor); |
123 | t.apply(p, out); |
124 | } |
125 | |
126 | /** Impl */ |
127 | private Properties createFieldProperties(Field field) throws ApplyException { |
128 | Properties p = new Properties(); |
129 | p.setProperty("modifiers", Modifier.toString(field.getModifiers())); |
130 | p.setProperty("type", JavaTypePrinter.getInstance().toString(field.getType())); |
131 | p.setProperty("name", field.getName()); |
132 | return p; |
133 | } |
134 | /** Impl */ |
135 | private Properties createMethodProperties(Callable callable) |
136 | throws ApplyException |
137 | { |
138 | Properties p = new Properties(); |
139 | p.setProperty("modifiers", Modifier.toString(callable.getModifiers())); |
140 | p.setProperty("static", Modifier.isStatic(callable.getModifiers()) ? "static" : ""); |
141 | p.setProperty("return", JavaTypePrinter.getInstance().toString(callable.getReturnType())); |
142 | p.setProperty("name", callable.getName()); |
143 | p.setProperty("prop_name", getPropertyName(callable)); |
144 | p.setProperty("class_name", ClassGenerator.stripPackageName(callable.getDeclaringClass().getName())); |
145 | p.setProperty("jni_self", getJniSelf(callable)); |
146 | p.setProperty("jni_name", getJniName(callable)); |
147 | p.setProperty("jni_overload", getJniOverload(callable)); |
148 | p.setProperty(",", getSeparator(callable)); |
149 | return p; |
150 | } |
151 | |
152 | |
153 | private String getCniName(Callable current_callable) { |
154 | return CniPointerTypePrinter.getInstance().toString(current_callable.getDeclaringClass()); |
155 | } |
156 | |
157 | /** Impl */ |
158 | private Properties createConstructorProperties(Constructor ctor) { |
159 | Properties p = new Properties(); |
160 | p.setProperty("modifiers", Modifier.toString(ctor.getModifiers())); |
161 | p.setProperty("return", ""); |
162 | p.setProperty("name", ClassGenerator.stripPackageName(ctor.getName())); |
163 | p.setProperty("class_name", ClassGenerator.stripPackageName(ctor.getDeclaringClass().getName())); |
164 | p.setProperty(",", getSeparator(new ConstructorCallable(ctor))); |
165 | return p; |
166 | } |
167 | /** Impl */ |
168 | private Template createFieldTemplate(Field field) throws ApplyException { |
169 | String tmpl_name = getFieldTemplateName(field); |
170 | if (tmpl_name == null) { |
171 | throw new ApplyException("No template found for " + field.getName()); |
172 | } |
173 | return createTemplate(tmpl_name); |
174 | } |
175 | /** Impl */ |
176 | private Template createMethodTemplate(Method method) throws ApplyException { |
177 | String tmpl_name = getTemplateName(method); |
178 | if (tmpl_name == null) { |
179 | throw new ApplyException("No template found for " + method.getName()); |
180 | } |
181 | return createTemplate(tmpl_name); |
182 | } |
183 | /** Impl */ |
184 | private Template createConstructorTemplate(Constructor ctor) throws ApplyException { |
185 | String tmpl_name = getTemplateName(ctor); |
186 | if (tmpl_name == null) { |
187 | throw new ApplyException("No template found for " + ctor.getName()); |
188 | } |
189 | return createTemplate(tmpl_name); |
190 | } |
191 | /** Impl */ |
192 | private Template createTemplate(String tmpl_name) throws ApplyException { |
193 | InputStream tmpl_strm = getTranslationContext().getTmplAsStream(tmpl_name); |
194 | if (tmpl_strm == null) { |
195 | throw new ApplyException("No resource file found: " + tmpl_name); |
196 | } |
197 | |
198 | Template t = new Template(tmpl_strm); |
199 | t.setObject(this); |
200 | t.setErrorPrefix(tmpl_name + ": "); |
201 | return t; |
202 | } |
203 | |
204 | |
205 | /** |
206 | * @return the property name if the method starts with get/set/is |
207 | */ |
208 | protected String getPropertyName(Callable callable) { |
209 | for (int i = 0; i < getPrefixList().length; i++) { |
210 | if (callable.getName().startsWith(getPrefixList()[i])) { |
211 | return callable.getName().substring(getPrefixList()[i].length()); |
212 | } |
213 | } |
214 | return ""; |
215 | } |
216 | |
217 | /** |
218 | * @return "," if there are at least one parameter, otherwise return "" |
219 | */ |
220 | protected String getSeparator(Callable callable) { |
221 | if (callable.getParameterTypes().length > 0) { |
222 | return ","; |
223 | } else { |
224 | return ""; |
225 | } |
226 | } |
227 | |
228 | // TODO extract method/class? We don't use any fields here. |
229 | /** |
230 | * Helper for derived classes. |
231 | */ |
232 | protected String findFieldTranslation(String prefix, String type, Field field) |
233 | throws ApplyException |
234 | { |
235 | String suffixes[] = { |
236 | field.getDeclaringClass().getName(), |
237 | field.getName(), |
238 | (Modifier.isStatic(field.getModifiers()) ? "static.*" : "instance.*"), |
239 | "*", |
240 | }; |
241 | return findTranslation(prefix, type, suffixes, field.getName()); |
242 | } |
243 | |
244 | // TODO extract method/class? We don't use any fields here. |
245 | /** |
246 | * Helper for derived classes. |
247 | */ |
248 | protected String findMeTranslation(String prefix, String type, Callable callable) |
249 | throws ApplyException |
250 | { |
251 | String suffixes[] = { |
252 | callable.getDeclaringClass().getName(), |
253 | "super." + getSuperclassName(callable), |
254 | "*", |
255 | }; |
256 | return findTranslation(prefix, type, suffixes, callable.getDeclaringClass().getName()); |
257 | } |
258 | |
259 | // TODO extract class? We don't use any fields here. |
260 | /** |
261 | * Helper for derived classes. |
262 | */ |
263 | protected String findMethodTranslation(String prefix, String type, String suffix, Callable callable) |
264 | throws ApplyException |
265 | { |
266 | return findTranslation(prefix, type, getMethodSuffixes(suffix, callable), callable.getName()); |
267 | } |
268 | |
269 | // TODO extract class? We don't use any fields here. |
270 | /** |
271 | * Helper for derived classes. |
272 | */ |
273 | protected String queryMethodTranslation(String prefix, String type, String suffix, Callable callable) |
274 | throws ApplyException |
275 | { |
276 | return queryTranslation(prefix, type, getMethodSuffixes(suffix, callable), callable.getName()); |
277 | } |
278 | private String [] getMethodSuffixes(String suffix, Callable callable) |
279 | throws ApplyException |
280 | { |
281 | String suffixes[] = { |
282 | callable.getKeyString(Callable.PACKAGE | Callable.CLASS | Callable.METHOD | Callable.PARAMETERS), |
283 | callable.getKeyString(Callable.PACKAGE | Callable.CLASS | Callable.METHOD), |
284 | callable.getKeyString( Callable.CLASS | Callable.METHOD | Callable.PARAMETERS), |
285 | callable.getKeyString( Callable.CLASS | Callable.METHOD), |
286 | callable.getKeyString( Callable.METHOD | Callable.PARAMETERS), |
287 | callable.getKeyString( Callable.METHOD), |
288 | callable.getKeyString(Callable.PACKAGE | Callable.CLASS | Callable.WILD), |
289 | callable.getKeyString( Callable.CLASS | Callable.WILD), |
290 | |
291 | getPropertyPrefix(callable), |
292 | (isStaticNative(callable)) ? "static.native.*" : "non-static-native.*", |
293 | (Modifier.isNative(callable.getModifiers()) ? "native.*" : "non-native.*"), |
294 | (Modifier.isStatic(callable.getModifiers()) ? "static.*" : "instance.*"), |
295 | suffix, |
296 | }; |
297 | return suffixes; |
298 | } |
299 | private boolean isStaticNative(Callable callable) { |
300 | if (Modifier.isStatic(callable.getModifiers()) |
301 | && Modifier.isNative(callable.getModifiers())) |
302 | { |
303 | return true; |
304 | } |
305 | return false; |
306 | } |
307 | |
308 | // TODO extract class? We don't use any fields here. |
309 | // TODO this is just findMethodTranslation! |
310 | /** |
311 | * Helper for derived classes. |
312 | * |
313 | * @param prefix |
314 | * @param type |
315 | * @param suffix |
316 | * @param callable |
317 | * @return the translation found. |
318 | * @throws ApplyException |
319 | */ |
320 | protected String findConstructorTranslation(String prefix, String type, String suffix, Callable callable) |
321 | throws ApplyException |
322 | { |
323 | String[] suffixes = buildCtorSearch(callable, suffix); |
324 | return findTranslation(prefix, type, suffixes, callable.getName()); |
325 | } |
326 | // JUnit |
327 | String[] buildCtorSearch(Callable callable, String suffix) throws ApplyException { |
328 | Vector suffixes = new Vector(); |
329 | suffixes.add(callable.getKeyString(Callable.PACKAGE | Callable.CLASS | Callable.METHOD | Callable.PARAMETERS)); |
330 | suffixes.add(callable.getKeyString(Callable.PACKAGE | Callable.CLASS | Callable.METHOD)); |
331 | suffixes.add(callable.getKeyString( Callable.CLASS | Callable.METHOD | Callable.PARAMETERS)); |
332 | suffixes.add(callable.getKeyString( Callable.CLASS | Callable.METHOD)); |
333 | suffixes.add(callable.getKeyString( Callable.METHOD | Callable.PARAMETERS)); |
334 | suffixes.add(callable.getKeyString( Callable.METHOD)); |
335 | /** |
336 | * Insert supers |
337 | */ |
338 | Class c = callable.getDeclaringClass(); |
339 | while (c != Object.class) { |
340 | suffixes.add("super." + c.getName() + ".<init>"); |
341 | suffixes.add("super." + ClassGenerator.stripPackageName(c.getName()) + ".<init>"); |
342 | c = c.getSuperclass(); |
343 | } |
344 | suffixes.add("constructor.*"); |
345 | suffixes.add("*"); |
346 | suffixes.add(suffix); |
347 | |
348 | String[] array_prototype = new String[0]; |
349 | return (String[]) suffixes.toArray(array_prototype); |
350 | } |
351 | /* |
352 | * TODO extract class? We don't use any fields here. |
353 | */ |
354 | protected String findParameterTranslation(String prefix, String type, Class parameter_type) |
355 | throws ApplyException |
356 | { |
357 | String suffixes[] = { |
358 | parameter_type.getName(), |
359 | getShortClassName(parameter_type), |
360 | "*", |
361 | }; |
362 | |
363 | return findTranslation(prefix, type, suffixes, parameter_type.getName()); |
364 | } |
365 | /** |
366 | * @return the property init string if the method starts with get/set/is |
367 | */ |
368 | protected String findPropInitTranslation(String prefix, String type, Callable callable) |
369 | throws ApplyException |
370 | { |
371 | String name = getPropertyName(callable); |
372 | if (name.length() == 0) { return ""; } |
373 | String suffixes[] = { |
374 | callable.getKeyString(Callable.PACKAGE | Callable.CLASS) + name, |
375 | callable.getKeyString( Callable.CLASS) + name, |
376 | name, |
377 | "*", |
378 | }; |
379 | return findTranslation(prefix, type, suffixes, name); |
380 | } |
381 | private String findTranslation(String prefix, String type, String suffixes[], String errName) |
382 | throws ApplyException |
383 | { |
384 | String result = queryTranslation(prefix, type, suffixes, errName); |
385 | if (result == null) { |
386 | // We are about to fail, dump all the searches |
387 | for (int i = 0; i < suffixes.length; i++) { |
388 | System.out.println(getLookupString(prefix, type, suffixes[i])); |
389 | } |
390 | throw new ApplyException("No "+ prefix +" translation property for " + errName); |
391 | } |
392 | return result; |
393 | } |
394 | protected String queryTranslation(String prefix, String type, String suffixes[], String errName) |
395 | throws ApplyException |
396 | { |
397 | String translation; |
398 | for (int i = 0; i < suffixes.length; i++) { |
399 | if (m_trans_ctx.getTraceFinds() != null && errName.equals(m_trans_ctx.getTraceFinds())) { |
400 | /* TODO unit test for list of strings coming out. */ |
401 | System.out.println(getLookupString(prefix, type, suffixes[i])); |
402 | } |
403 | |
404 | translation = getTransProperty(getLookupString(prefix, type, suffixes[i])); |
405 | if (translation != null) { |
406 | return translation; |
407 | } |
408 | } |
409 | return null; |
410 | } |
411 | |
412 | private String getLookupString(String default_prefix, String type, String suffix) { |
413 | String prefix = m_trans_ctx.getPrefix(); |
414 | if (prefix == null) { |
415 | prefix = default_prefix; |
416 | } |
417 | StringBuffer result = new StringBuffer(prefix); |
418 | result.append('.'); |
419 | result.append(type); |
420 | result.append('.'); |
421 | result.append(suffix); |
422 | result.append(getPassAppend()); |
423 | return result.toString(); |
424 | // suffix never null? return suffix == null ? prefix : prefix + "." + suffix; |
425 | } |
426 | /** |
427 | * Helper for derived classes. |
428 | */ |
429 | protected String getTransProperty(String key) throws ApplyException { |
430 | return m_trans_ctx.getProperty(key); |
431 | } |
432 | /** |
433 | * Helper for derived classes. |
434 | */ |
435 | protected String getJniSelf(Callable callable) throws ApplyException { |
436 | if (!Modifier.isStatic(callable.getModifiers())) { |
437 | return "jobject self"; |
438 | } else { |
439 | return "jclass self"; |
440 | } |
441 | } |
442 | protected String getJniReturnError(Callable callable) { |
443 | // TODO might need a method by method way to return an error. |
444 | if (callable.getReturnType() == Void.TYPE) { |
445 | return ""; |
446 | } |
447 | return "return 0;"; |
448 | } |
449 | protected String getJniName(Callable callable) throws ApplyException { |
450 | try { |
451 | StringWriter out = new StringWriter(); |
452 | out.write("Java_"); |
453 | JniMangledTypePrinter.getInstance().print(callable.getDeclaringClass(), out); |
454 | out.write("_"); |
455 | |
456 | JniMangleFilterWriter mangle = new JniMangleFilterWriter(out); |
457 | mangle.write(callable.getName()); |
458 | |
459 | if (callable.isOverloaded()) { |
460 | writeOverloadSignature(callable, out); |
461 | } |
462 | return out.toString(); |
463 | } catch (IOException e) { |
464 | throw new ApplyException(e); |
465 | } |
466 | } |
467 | protected String getJniOverload(Callable callable) throws ApplyException { |
468 | try { |
469 | StringWriter out = new StringWriter(); |
470 | |
471 | if (callable.isOverloaded()) { |
472 | writeOverloadSignature(callable, out); |
473 | } |
474 | return out.toString(); |
475 | } catch (IOException e) { |
476 | throw new ApplyException(e); |
477 | } |
478 | } |
479 | private void writeOverloadSignature(Callable callable, Writer out) throws IOException { |
480 | writeOverloadSignature(callable.getParameterTypes(), out); |
481 | } |
482 | private void writeOverloadSignature(Class params[], Writer out) throws IOException { |
483 | out.write("__"); |
484 | for (int i = 0; i < params.length; i++) { |
485 | writeOverloadParam(params[i], out); |
486 | } |
487 | } |
488 | private void writeOverloadParam(Class type, Writer out) throws IOException { |
489 | JniMangleFilterWriter fw = new JniMangleFilterWriter(out); |
490 | JniSignatureTypePrinter.getInstance().print(type, fw); |
491 | } |
492 | |
493 | private String getShortClassName(Class a_class) { |
494 | return ClassGenerator.stripPackageName(a_class.getName()); |
495 | } |
496 | private String getSuperclassName(Callable callable) { |
497 | Class super_class = callable.getDeclaringClass().getSuperclass(); |
498 | return super_class != null ? super_class.getName() : "<no superclass>"; |
499 | } |
500 | /** |
501 | * @return a list of possible prefixes |
502 | */ |
503 | private static String[] getPrefixList() { |
504 | // TODO cmdline args for extending prefix list. |
505 | String prefixes[] = { |
506 | "get", "is", "has", "set", "show", "hide", "clear", "add", "insert", "delete" |
507 | }; |
508 | return prefixes; |
509 | } |
510 | /** |
511 | * @return "get*" for set methods, "set*" for set methods, etc. |
512 | */ |
513 | private String getPropertyPrefix(Callable callable) { |
514 | // String prefixes[] = { |
515 | // "get", "is", "set", "show", "hide", "clear", "add", "insert", "delete" |
516 | // }; |
517 | for (int i = 0; i < getPrefixList().length; i++) { |
518 | if (callable.getName().startsWith(getPrefixList()[i])) { |
519 | return getPrefixList()[i] + "*"; |
520 | } |
521 | } |
522 | return ""; |
523 | } |
524 | /** |
525 | * @param callable callable to print signature for. |
526 | * @return "(char[],offset,len)" for the parameters for <code>callable</code>. |
527 | */ |
528 | private String getParamSignature(Callable callable) throws ApplyException { |
529 | final StringWriter sw = new StringWriter(); |
530 | ParamTypeVisitor v = new JavaSignatureParamsVisitor(sw); |
531 | VisitClass.visitParamTypes(callable.getParameterTypes(), v); |
532 | return sw.toString(); |
533 | } |
534 | |
535 | protected Callable current_callable = null; |
536 | private Writer out = null; |
537 | private TranslationContext m_trans_ctx = null; |
538 | private String m_pass_append = null; |
539 | } |