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: JniMangledTypePrinter.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 | public class JniMangledTypePrinter extends TypePrinter { |
18 | private static JniMangledTypePrinter instance = new JniMangledTypePrinter(); |
19 | public static JniMangledTypePrinter getInstance() { |
20 | return instance; |
21 | } |
22 | |
23 | public void print(Class type, Writer out) throws IOException { |
24 | if (type.isPrimitive()) { |
25 | throw new IllegalArgumentException("primitive types cannot be mangled: " + type.getName()); |
26 | } else if (type.isArray()) { |
27 | throw new IllegalArgumentException("array types cannot be mangled: " + type.getName()); |
28 | } else { |
29 | printMangledType(type, out); |
30 | } |
31 | } |
32 | |
33 | private static void printMangledType(Class type, Writer out) throws IOException { |
34 | String s = type.getName(); |
35 | |
36 | for (int i = 0; i < s.length(); i++) { |
37 | char c = s.charAt(i); |
38 | mangle(c, out); |
39 | } |
40 | } |
41 | |
42 | public static void mangle(int c, Writer out) throws IOException { |
43 | switch (c) { |
44 | case '_': |
45 | out.write("_1"); |
46 | break; |
47 | case ';': |
48 | out.write("_2"); |
49 | break; |
50 | case '[': |
51 | out.write("_3"); |
52 | break; |
53 | case '.': |
54 | case '/': |
55 | out.write('_'); |
56 | break; |
57 | default: |
58 | if (Character.isUnicodeIdentifierPart((char)c)) { |
59 | out.write(c); |
60 | } else { |
61 | String s = Integer.toHexString(c); |
62 | out.write('_'); |
63 | for (int i = 0; i + s.length() < 5; i++) { |
64 | out.write('0'); |
65 | } |
66 | out.write(s); |
67 | } |
68 | break; |
69 | } |
70 | } |
71 | } |