1    
2    /*
3    
4      The author of this software is Ian Kaplan
5      Bear Products International
6      www.bearcave.com
7      iank@bearcave.com
8    
9      Copyright (c) Ian Kaplan, 1999, 2000
10   
11     See copyright file for usage and licensing
12   
13   */
14   
15   package util;
16   
17   /**
18     Support methods for decoding and printing Java type
19     descriptors.
20   
21    */
22   public final class typeDesc {
23   
24     public static String charToType( char ch ) {
25       String typeName;
26   
27       typeName = "badType";
28   
29       switch (ch) {
30       case 'B':
31         typeName = "byte";
32         break;
33       case 'C':
34         typeName = "char";
35         break;
36       case 'D':
37         typeName = "double";
38         break;
39       case 'F':
40         typeName = "float";
41         break;
42       case 'I':
43         typeName = "int";
44         break;
45       case 'J':
46         typeName = "long";
47         break;
48       case 'S':
49         typeName = "short";
50         break;
51       case 'Z':
52         typeName = "boolean";
53         break;
54       } // switch
55   
56       return typeName;
57     } // charToType
58   
59   
60     public static boolean isTypeChar( char tChar ) {
61       return (tChar == 'B' ||
62   	    tChar == 'C' ||
63   	    tChar == 'D' ||
64   	    tChar == 'F' ||
65   	    tChar == 'I' ||
66   	    tChar == 'J' ||
67   	    tChar == 'S' ||
68    	    tChar == 'Z');
69     } // isTypeChar
70   
71   
72     /**
73        Parse a field descriptor and return a String describing the type.
74        For example, the descriptor [[I will result in the String
75        "int[][]".  The field descriptor defines the type of the field.
76        This method is also used to parse types for the declarations
77        of method local variables.
78   
79     */
80     public static String decodeFieldDesc( String descStr ) {
81       String typeStr = null;
82   
83       if (descStr != null) {
84         int dimCnt, charIx;
85         char typeChar;
86         
87         // count array dimensions
88         for (dimCnt = 0; descStr.charAt( dimCnt ) == '['; dimCnt++)
89   	/* nada */;
90   
91         charIx = dimCnt;
92         typeChar = descStr.charAt(charIx);
93         if (typeChar == 'L') {// the object is a class
94   	String objName;
95   
96   	charIx++;  // charIx is the start of the object name
97   	objName = descStr.substring( charIx );
98   	// convert from foo/bar/zorch form to foo.bar.zorch form
99   	typeStr = objNameFormat.toDotSeparator( objName );
100        }
101        else if (typeDesc.isTypeChar( typeChar )) {
102  	typeStr = typeDesc.charToType( typeChar );
103        }
104        // if it's an array, print the dimensions
105        for (int i = 0; i < dimCnt; i++) {
106  	typeStr = typeStr + "[]";
107        }
108      }
109  
110      return typeStr;
111    } // decodeFieldDesc
112  
113  
114  } // typeDesc
115