1 package alt.jiapi.reflect; 2 3 import java.util.ArrayList ; 4 9 class TypeHelper { 10 static String typeToDescriptor(String typeName) { 11 int idx = typeName.indexOf('['); 12 String type = null; 13 if (idx == -1) { 14 type = typeName; 15 } 16 else { 17 type = typeName.substring(0, idx).trim(); 18 } 19 20 StringBuffer dBuf = new StringBuffer (); 21 if (idx > 0) { 22 for (int i = idx; i < typeName.length(); i++) { 23 if (typeName.charAt(i) == '[') { 24 dBuf.append('['); 25 } 26 } 27 } 28 29 String desc = null; 30 if ("byte".equals(type)) { 32 dBuf.append("B"); 33 } 34 else if ("char".equals(type)) { 35 dBuf.append("C"); 36 } 37 else if ("double".equals(type)) { 38 dBuf.append("D"); 39 } 40 else if ("float".equals(type)) { 41 dBuf.append("F"); 42 } 43 else if ("int".equals(type)) { 44 dBuf.append("I"); 45 } 46 else if ("long".equals(type)) { 47 dBuf.append("J"); 48 } 49 else if ("short".equals(type)) { 50 dBuf.append("S"); 51 } 52 else if ("boolean".equals(type)) { 53 dBuf.append("Z"); 54 } 55 else if ("void".equals(type)) { 56 dBuf.append("V"); 57 } 58 else { 59 dBuf.append('L'); 61 dBuf.append(type.replace('.', '/')); 62 dBuf.append(';'); 63 } 65 66 return dBuf.toString(); 67 } 68 69 70 86 static String descriptorToType(String descriptor) { 87 StringBuffer typeBuf = new StringBuffer (); 88 int i = 0; 89 for(;descriptor.charAt(i) == '['; i++) { 90 typeBuf.append("[]"); 91 } 92 93 if (descriptor.charAt(i) == 'L') { 95 String type = descriptor.substring(i + 1, descriptor.length() - 1); 96 typeBuf.insert(0, type.replace('/', '.')); 97 } 98 else if ("B".equals(descriptor)) { 99 typeBuf.insert(0, "byte"); 100 } 101 else if ("C".equals(descriptor)) { 102 typeBuf.insert(0, "char"); 103 } 104 else if ("D".equals(descriptor)) { 105 typeBuf.insert(0, "double"); 106 } 107 else if ("F".equals(descriptor)) { 108 typeBuf.insert(0, "float"); 109 } 110 else if ("I".equals(descriptor)) { 111 typeBuf.insert(0, "int"); 112 } 113 else if ("J".equals(descriptor)) { 114 typeBuf.insert(0, "long"); 115 } 116 else if ("S".equals(descriptor)) { 117 typeBuf.insert(0, "short"); 118 } 119 else if ("Z".equals(descriptor)) { 120 typeBuf.insert(0, "boolean"); 121 } 122 else if ("V".equals(descriptor)) { 123 return "void"; 124 } 125 126 return typeBuf.toString(); 127 } 129 130 131 static String [] descriptorsToTypes(String pDesc) { 132 ArrayList al = new ArrayList (); 134 try { 135 for (int i = 0; i < pDesc.length(); i++) { 136 int dim = 0; 137 StringBuffer typeBuf = new StringBuffer (); 138 for(;pDesc.charAt(i) == '['; i++) { 139 typeBuf.append("[]"); 140 } 141 142 if (pDesc.charAt(i) != 'L') { 143 String type = descriptorToType("" + pDesc.charAt(i)); 144 typeBuf.insert(0, type); 145 al.add(typeBuf.toString()); 146 } 147 else { 148 int start = i; 149 for(;pDesc.charAt(i) != ';'; i++) { 150 ; 151 } 152 153 String desc = pDesc.substring(start, i + 1); 154 String typeName = descriptorToType(desc); 155 typeBuf.insert(0, typeName); 156 al.add(typeBuf.toString()); 157 } 158 } 159 } 160 catch(IllegalArgumentException iae) { 161 throw new IllegalArgumentException ("Illegal descriptor: " + pDesc); 162 } 163 164 return (String [])al.toArray(new String [0]); 165 } 166 167 168 static String signatureToDescriptor(Signature signature) { 169 StringBuffer desc = new StringBuffer ("("); 170 String typeNames[] = signature.getParameters(); 171 172 for (int i = 0; i < typeNames.length; i++) { 173 desc.append(typeToDescriptor(typeNames[i])); 174 } 175 desc.append(")"); 176 desc.append(typeToDescriptor(signature.getReturnType())); 177 178 return desc.toString(); 179 } 180 } 181 | Popular Tags |