1 16 package com.google.gwt.dev.util; 17 18 import java.lang.reflect.Method ; 19 20 23 public class TypeInfo { 24 25 public static final int NOT_FOUND = 0; public static final int TYPE_ARRAY = 0x200000; 27 public static final int TYPE_COLLECTION = 0x180000; 28 public static final int TYPE_COLLECTION_LIST = 0x100000; 29 public static final int TYPE_COLLECTION_SET = 0x080000; 30 public static final int TYPE_PRIM = 0x07fc00; 31 public static final int TYPE_PRIM_BOOLEAN = 0x008000; 32 public static final int TYPE_PRIM_BYTE = 0x000800; 33 public static final int TYPE_PRIM_CHAR = 0x000400; 34 public static final int TYPE_PRIM_DOUBLE = 0x020000; 35 public static final int TYPE_PRIM_FLOAT = 0x010000; 36 public static final int TYPE_PRIM_INT = 0x002000; 37 public static final int TYPE_PRIM_LONG = 0x004000; 38 public static final int TYPE_PRIM_SHORT = 0x001000; 39 public static final int TYPE_PRIM_VOID = 0x040000; 40 public static final int TYPE_USER = 0x400000; 41 public static final int TYPE_WRAP = 0x0003ff; 42 public static final int TYPE_WRAP_BOOLEAN = 0x000040; 43 public static final int TYPE_WRAP_BYTE = 0x000004; 44 public static final int TYPE_WRAP_CHAR = 0x000002; 45 public static final int TYPE_WRAP_DATE = 0x000200; 46 public static final int TYPE_WRAP_DOUBLE = 0x000100; 47 public static final int TYPE_WRAP_FLOAT = 0x000080; 48 public static final int TYPE_WRAP_INT = 0x000010; 49 public static final int TYPE_WRAP_LONG = 0x000020; 50 public static final int TYPE_WRAP_SHORT = 0x000008; 51 public static final int TYPE_WRAP_STRING = 0x000001; 53 54 public static int classifyType(Class type) { 55 int ret = isPrimitiveType(type); 56 if (ret != 0) { 57 return ret; 58 } 59 60 ret = isPrimitiveWrapperType(type); 61 if (ret != 0) { 62 return ret; 63 } 64 65 if (type.isArray()) { 66 return TYPE_ARRAY; 67 } 68 69 return TYPE_USER; 70 } 71 72 public static Method getInterfaceMethod(Class intf, String methodName, 73 Class [] paramTypes, boolean includeInherited) { 74 try { 75 return intf.getDeclaredMethod(methodName, paramTypes); 76 } catch (NoSuchMethodException e) { 77 if (includeInherited) { 78 Class [] superintfs = intf.getInterfaces(); 79 for (int i = 0; i < superintfs.length; i++) { 80 Method method = getInterfaceMethod(superintfs[i], methodName, 81 paramTypes, true); 82 if (method != null) { 83 return method; 84 } 85 } 86 } 87 88 return null; 89 } 90 } 91 92 public static String getJavahMangledIdent(String before) { 93 StringBuffer after = new StringBuffer (); 94 char[] chars = before.toCharArray(); 95 for (int i = 0; i < chars.length; i++) { 96 char c = chars[i]; 97 if (c == '_') { 98 after.append("_1"); 99 } else if (c == ';') { 100 after.append("_2"); 101 } else if (c == '[') { 102 after.append("_3"); 103 } else if (c == '/') { 104 after.append('_'); 105 } else if (Character.isJavaIdentifierPart(c)) { 106 after.append(c); 107 } else { 108 throw new UnsupportedOperationException ("Cannot handle non-ascii yet"); 109 } 110 } 111 return after.toString(); 112 } 113 114 public static String getSourceRepresentation(Class type) { 115 if (type.equals(Integer.TYPE)) { 118 return "int"; 119 } else if (type.equals(Long.TYPE)) { 120 return "long"; 121 } else if (type.equals(Short.TYPE)) { 122 return "short"; 123 } else if (type.equals(Byte.TYPE)) { 124 return "byte"; 125 } else if (type.equals(Character.TYPE)) { 126 return "char"; 127 } else if (type.equals(Boolean.TYPE)) { 128 return "boolean"; 129 } else if (type.equals(Float.TYPE)) { 130 return "float"; 131 } else if (type.equals(Double.TYPE)) { 132 return "double"; 133 } 134 135 if (type.isArray()) { 138 Class componentType = type.getComponentType(); 139 return getSourceRepresentation(componentType) + "[]"; 140 } 141 142 return type.getName().replace('$', '.'); 145 } 146 147 public static int isPrimitiveType(Class type) { 148 if (type.equals(Integer.TYPE)) { 149 return TYPE_PRIM_INT; 150 } else if (type.equals(Long.TYPE)) { 151 return TYPE_PRIM_LONG; 152 } else if (type.equals(Character.TYPE)) { 153 return TYPE_PRIM_CHAR; 154 } else if (type.equals(Byte.TYPE)) { 155 return TYPE_PRIM_BYTE; 156 } else if (type.equals(Short.TYPE)) { 157 return TYPE_PRIM_SHORT; 158 } else if (type.equals(Boolean.TYPE)) { 159 return TYPE_PRIM_BOOLEAN; 160 } else if (type.equals(Float.TYPE)) { 161 return TYPE_PRIM_FLOAT; 162 } else if (type.equals(Double.TYPE)) { 163 return TYPE_PRIM_DOUBLE; 164 } else if (type.equals(Void.TYPE)) { 165 return TYPE_PRIM_VOID; 166 } else { 167 return NOT_FOUND; 168 } 169 } 170 171 public static int isPrimitiveWrapperType(Class type) { 172 if (type.equals(String .class)) { 173 return TYPE_WRAP_STRING; 174 } else if (type.equals(Integer .class)) { 175 return TYPE_WRAP_INT; 176 } else if (type.equals(Long .class)) { 177 return TYPE_WRAP_LONG; 178 } else if (type.equals(Character .class)) { 179 return TYPE_WRAP_CHAR; 180 } else if (type.equals(Byte .class)) { 181 return TYPE_WRAP_BYTE; 182 } else if (type.equals(Short .class)) { 183 return TYPE_WRAP_SHORT; 184 } else if (type.equals(Boolean .class)) { 185 return TYPE_WRAP_BOOLEAN; 186 } else if (type.equals(Float .class)) { 187 return TYPE_WRAP_FLOAT; 188 } else if (type.equals(Double .class)) { 189 return TYPE_WRAP_DOUBLE; 190 } else { 191 return NOT_FOUND; 192 } 193 } 194 } | Popular Tags |