1 19 package gcc.util; 20 21 import java.util.*; 22 23 public abstract class JavaType 24 { 25 private static HashMap _wrapper = new HashMap(); 26 27 static 28 { 29 _wrapper.put("boolean", "java.lang.Boolean"); 30 _wrapper.put("byte", "java.lang.Byte"); 31 _wrapper.put("char", "java.lang.Character"); 32 _wrapper.put("double", "java.lang.Double"); 33 _wrapper.put("float", "java.lang.Float"); 34 _wrapper.put("int", "java.lang.Integer"); 35 _wrapper.put("long", "java.lang.Long"); 36 _wrapper.put("short", "java.lang.Short"); 37 } 38 39 43 public static String getName(Class t) 44 { 45 if (t.isArray()) 46 { 47 return getName(t.getComponentType()) + "[]"; 48 } 49 else 50 { 51 return t.getName().replace('$', '.'); 52 } 53 } 54 55 public static String wrapper(String type) 56 { 57 return (String)_wrapper.get(type); 58 } 59 60 public static String wrapper(Class type) 61 { 62 return wrapper(getName(type)); 63 } 64 65 public static String wrap(String type, String name) 66 { 67 String w = wrapper(type); 68 return w != null ? ("new " + w + "(" + name + ")") : name; 69 } 70 71 public static String wrap(Class type, String name) 72 { 73 return wrap(getName(type), name); 74 } 75 76 public static String unwrap(String type, String name) 77 { 78 String w = wrapper(type); 79 return w != null ? ("((" + w + ")" + name + ")." + type + "Value()") : name; 80 } 81 82 public static String unwrap(Class type, String name) 83 { 84 return unwrap(getName(type), name); 85 } 86 87 public static String unwrapObject(Class type, Object expression) 88 { 89 return unwrapObject(getName(type), expression); 90 } 91 92 public static String unwrapObject(String type, Object expression) 93 { 94 String w = wrapper(type); 95 if (w == null) 96 { 97 if (type.equals("java.lang.Object")) 98 { 99 return expression.toString(); 100 } 101 else 102 { 103 return "(" + type + ")" + expression; 104 } 105 } 106 else 107 { 108 return "((" + w + ")" + expression + ")." + type + "Value()"; 109 } 110 } 111 } 112 | Popular Tags |