1 28 29 package org.jibx.binding.model; 30 31 import java.util.HashMap ; 32 33 40 41 public class ClassUtils 42 { 43 44 private static HashMap s_variantMap = new HashMap (); 45 46 47 private static HashMap s_signatureMap = new HashMap (); 48 49 static { 50 51 s_variantMap.put("boolean", new String [] { "Z" }); 53 s_variantMap.put("byte", new String [] { "B", "S", "I" }); 54 s_variantMap.put("char", new String [] { "C", "I" }); 55 s_variantMap.put("double", new String [] { "D" }); 56 s_variantMap.put("float", new String [] { "F" }); 57 s_variantMap.put("int", new String [] { "I" }); 58 s_variantMap.put("long", new String [] { "J" }); 59 s_variantMap.put("short", new String [] { "S", "I" }); 60 s_variantMap.put("void", new String [] { "V" }); 61 62 s_signatureMap.put("boolean", "Z"); 64 s_signatureMap.put("byte", "B"); 65 s_signatureMap.put("char", "C"); 66 s_signatureMap.put("double", "D"); 67 s_signatureMap.put("float", "F"); 68 s_signatureMap.put("int", "I"); 69 s_signatureMap.put("long", "J"); 70 s_signatureMap.put("short", "S"); 71 s_signatureMap.put("void", "V"); 72 } 73 74 79 public static boolean isPrimitive(String type) { 80 return s_variantMap.get(type) != null; 81 } 82 83 93 public static IClassItem findVirtualMethod(String name, String [] sigs, 94 ValidationContext vctx) { 95 96 int split = name.lastIndexOf('.'); 98 String cname = name.substring(0, split); 99 String mname = name.substring(split+1); 100 IClass iclas = vctx.getRequiredClassInfo(cname); 101 102 for (int i = 0; i < sigs.length; i++) { 104 IClassItem method = iclas.getMethod(mname, sigs[i]); 105 if (method != null) { 106 return method; 107 } 108 } 109 return null; 110 } 111 112 122 public static IClassItem findStaticMethod(String name, String [] sigs, 123 ValidationContext vctx) { 124 125 int split = name.lastIndexOf('.'); 127 String cname = name.substring(0, split); 128 String mname = name.substring(split+1); 129 IClass iclas = vctx.getRequiredClassInfo(cname); 130 131 for (int i = 0; i < sigs.length; i++) { 133 IClassItem method = iclas.getStaticMethod(mname, sigs[i]); 134 if (method != null) { 135 return method; 136 } 137 } 138 return null; 139 } 140 141 150 public static String [] getSignatureVariants(String name, 151 ValidationContext vctx) { 152 Object obj = s_variantMap.get(name); 153 if (obj == null) { 154 IClass iclas = vctx.getRequiredClassInfo(name); 155 return iclas.getInstanceSigs(); 156 } else { 157 return (String [])obj; 158 } 159 } 160 161 169 public static String getSignature(String type) { 170 171 String sig = (String )s_signatureMap.get(type); 173 if (sig == null) { 174 175 int dim = 0; 177 int split = type.indexOf('['); 178 if (split >= 0) { 179 180 int mark = split; 182 while ((type.length()-mark) >= 2) { 183 if (type.charAt(mark) == '[' || 184 type.charAt(mark+1) == ']') { 185 dim++; 186 mark += 2; 187 } else { 188 throw new IllegalArgumentException 189 ("Invalid type name " + type); 190 } 191 } 192 193 if (mark < type.length()) { 195 throw new IllegalArgumentException ("Invalid type name " + 196 type); 197 } 198 199 String cname = type.substring(0, split); 201 String base = (String )s_signatureMap.get(cname); 202 if (base == null) { 203 204 base = "L" + cname.replace('.', '/') + ';'; 206 s_signatureMap.put(cname, base); 207 208 } 209 210 StringBuffer buff = new StringBuffer (dim + base.length()); 212 for (int i = 0; i < dim; i++) { 213 buff.append('['); 214 } 215 buff.append(base); 216 sig = buff.toString(); 217 218 } else { 219 220 sig = "L" + type.replace('.', '/') + ';'; 222 223 } 224 225 s_signatureMap.put(type, sig); 227 } 228 229 return sig; 231 } 232 233 243 public static boolean isAssignable(String from, String to, 244 ValidationContext vctx) { 245 246 if (from.equals(to)) { 248 return true; 249 } else { 250 251 Object fobj = s_variantMap.get(from); 253 Object tobj = s_variantMap.get(to); 254 if (fobj == null && tobj == null) { 255 256 IClass fclas = vctx.getRequiredClassInfo(from); 258 IClass tclas = vctx.getRequiredClassInfo(to); 259 260 String [] sigs = fclas.getInstanceSigs(); 262 String match = getSignature(to); 263 for (int i = 0; i < sigs.length; i++) { 264 if (match.equals(sigs[i])) { 265 return true; 266 } 267 } 268 return false; 269 270 } else if (fobj != null && tobj != null) { 271 272 String [] fsigs = (String [])fobj; 274 String [] tsigs = (String [])tobj; 275 if (tsigs.length == 1) { 276 for (int i = 0; i < fsigs.length; i++) { 277 if (fsigs[i] == tsigs[0]) { 278 return true; 279 } 280 } 281 } 282 return false; 283 284 } else { 285 286 return false; 288 289 } 290 } 291 } 292 293 299 public static Class loadClass(String name) { 300 try { 301 return ClassUtils.class.getClassLoader().loadClass(name); 302 } catch (ClassNotFoundException ex) { 303 return null; 304 } 305 } 306 } | Popular Tags |