1 | // |
2 | // (c) Copyright, Moebius Solutions, 2002 |
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: JniTypePrinter.java,v 1.1.1.5 2004/05/25 20:23:30 hastings Exp $ |
12 | */ |
13 | package com.moesol.generator.printer; |
14 | |
15 | import java.io.*; |
16 | |
17 | /** |
18 | * Print types out in a manner suitable for interfacing with JNI, |
19 | * including return types and parameters. |
20 | */ |
21 | public class JniTypePrinter extends TypePrinter { |
22 | protected JniTypePrinter() { } |
23 | private static JniTypePrinter instance = new JniTypePrinter(); |
24 | public static JniTypePrinter getInstance() { |
25 | return instance; |
26 | } |
27 | public void print(Class type, Writer out) throws IOException { |
28 | if (type.isPrimitive()) { |
29 | primitiveCxxType(out, type); |
30 | } else if (type.isArray()) { |
31 | arrayCxxType(out, type); |
32 | } else { |
33 | objectCxxType(out, type); |
34 | } |
35 | } |
36 | protected void primitiveCxxType(Writer out, Class type) throws IOException { |
37 | if (type == Boolean.TYPE) { |
38 | out.write("jboolean"); |
39 | } else if (type == Byte.TYPE) { |
40 | out.write("jbyte"); |
41 | } else if (type == Character.TYPE) { |
42 | out.write("jchar"); |
43 | } else if (type == Short.TYPE) { |
44 | out.write("jshort"); |
45 | } else if (type == Integer.TYPE) { |
46 | out.write("jint"); |
47 | } else if (type == Long.TYPE) { |
48 | out.write("jlong"); |
49 | } else if (type == Float.TYPE) { |
50 | out.write("jfloat"); |
51 | } else if (type == Double.TYPE) { |
52 | out.write("jdouble"); |
53 | } else if (type == Void.TYPE) { |
54 | out.write("void"); |
55 | } else { |
56 | throw new IllegalArgumentException("Not a known primitive type" |
57 | + type.getName()); |
58 | } |
59 | } |
60 | protected void arrayCxxType(Writer out, Class type) throws IOException { |
61 | if (type.getComponentType().isPrimitive()) { |
62 | primitiveCxxType(out, type.getComponentType()); |
63 | } else { |
64 | out.write("jobject"); |
65 | } |
66 | out.write("Array"); |
67 | } |
68 | protected void objectCxxType(Writer out, Class type) throws IOException { |
69 | if (String.class.isAssignableFrom(type)) { |
70 | out.write("jstring"); |
71 | } else if (Throwable.class.isAssignableFrom(type)) { |
72 | out.write("jthrowable"); |
73 | } else { |
74 | out.write("jobject"); |
75 | } |
76 | } |
77 | } |