| 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: JniSignatureTypePrinter.java,v 1.2 2004/11/04 19:29:19 hastings Exp $ |
| 12 | */ |
| 13 | package com.moesol.generator.printer; |
| 14 | |
| 15 | import java.io.*; |
| 16 | |
| 17 | import com.moesol.generator.core.VisitClass; |
| 18 | import com.moesol.generator.visitor.*; |
| 19 | |
| 20 | public class JniSignatureTypePrinter extends TypePrinter { |
| 21 | private static JniSignatureTypePrinter instance = new JniSignatureTypePrinter(); |
| 22 | public static JniSignatureTypePrinter getInstance() { |
| 23 | return instance; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Wrap up an exception and throw it past the visitor framework. |
| 28 | */ |
| 29 | class VisitError extends Error { |
| 30 | VisitError(Throwable cause) { |
| 31 | super(cause); |
| 32 | } |
| 33 | } |
| 34 | public void print(Class aType, final Writer out) throws IOException { |
| 35 | TypeVisitor v = new TypeVisitor() { |
| 36 | private void print(char c) { |
| 37 | try { |
| 38 | out.write(c); |
| 39 | } catch (IOException e) { |
| 40 | throw new VisitError(e); |
| 41 | } |
| 42 | } |
| 43 | public void visitBoolean(Class type) { |
| 44 | print('Z'); |
| 45 | } |
| 46 | public void visitByte(Class type) { |
| 47 | print('B'); |
| 48 | } |
| 49 | public void visitCharacter(Class type) { |
| 50 | print('C'); |
| 51 | } |
| 52 | public void visitShort(Class type) { |
| 53 | print('S'); |
| 54 | } |
| 55 | public void visitInteger(Class type) { |
| 56 | print('I'); |
| 57 | } |
| 58 | public void visitLong(Class type) { |
| 59 | print('J'); |
| 60 | } |
| 61 | public void visitFloat(Class type) { |
| 62 | print('F'); |
| 63 | } |
| 64 | public void visitDouble(Class type) { |
| 65 | print('D'); |
| 66 | } |
| 67 | public void visitVoid(Class type) { |
| 68 | print('V'); |
| 69 | } |
| 70 | public void visitArray(Class type) { |
| 71 | print('['); |
| 72 | try { |
| 73 | JniSignatureTypePrinter.this.print(type.getComponentType(), out); |
| 74 | } catch (IOException e) { |
| 75 | throw new VisitError(e); |
| 76 | } |
| 77 | } |
| 78 | public void visitObject(Class type) { |
| 79 | print('L'); |
| 80 | printObjectSignature(type.getName()); |
| 81 | print(';'); |
| 82 | } |
| 83 | private void printObjectSignature(String typeName) { |
| 84 | for (int i = 0; i < typeName.length(); i++) { |
| 85 | if (typeName.charAt(i) == '.') { |
| 86 | print('/'); |
| 87 | } else { |
| 88 | print(typeName.charAt(i)); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | }; |
| 93 | try { |
| 94 | VisitClass.visitType(aType, v); |
| 95 | } catch (VisitError e) { |
| 96 | throw (IOException)e.getCause(); |
| 97 | } |
| 98 | } |
| 99 | } |