1 15 package org.apache.hivemind.service; 16 17 import java.lang.reflect.Method ; 18 import java.lang.reflect.Modifier ; 19 import java.lang.reflect.Proxy ; 20 21 26 public class ClassFabUtils 27 { 28 private static long _uid = System.currentTimeMillis(); 29 30 private static final char QUOTE = '"'; 31 32 private ClassFabUtils() 33 { 34 } 35 36 39 40 public static synchronized String generateClassName(String baseName) 41 { 42 return "$" + baseName + "_" + Long.toHexString(_uid++); 43 } 44 45 51 52 public static synchronized String generateClassName(Class interfaceClass) 53 { 54 String name = interfaceClass.getName(); 55 56 int dotx = name.lastIndexOf('.'); 57 58 return generateClassName(name.substring(dotx + 1)); 59 } 60 61 66 public static String getJavaClassName(Class inputClass) 67 { 68 if (inputClass.isArray()) 69 return getJavaClassName(inputClass.getComponentType()) + "[]"; 70 71 return inputClass.getName(); 72 } 73 74 78 public static boolean isToString(Method method) 79 { 80 if (!method.getName().equals("toString")) 81 return false; 82 83 if (method.getParameterTypes().length > 0) 84 return false; 85 86 return method.getReturnType().equals(String .class); 87 } 88 89 97 public static void addToStringMethod(ClassFab classFab, String toStringResult) 98 { 99 StringBuffer buffer = new StringBuffer ("return "); 100 buffer.append(QUOTE); 101 buffer.append(toStringResult); 102 buffer.append(QUOTE); 103 buffer.append(";"); 104 105 classFab.addMethod(Modifier.PUBLIC, new MethodSignature(String .class, "toString", null, 106 null), buffer.toString()); 107 } 108 109 119 public static Class getInstanceClass(ClassFab classFab, Object instance, Class interfaceClass) 120 { 121 Class instanceClass = instance.getClass(); 122 123 if (!classFab.canConvert(instanceClass)) 124 return interfaceClass; 125 126 return instanceClass; 127 } 128 129 139 public static Class getInstanceClass(Object instance, Class interfaceClass) 140 { 141 Class instanceClass = instance.getClass(); 142 143 if (Proxy.isProxyClass(instanceClass)) 144 return interfaceClass; 145 146 return instanceClass; 147 } 148 149 155 156 public static void addNoOpMethod(ClassFab cf, MethodSignature m) 157 { 158 StringBuffer body = new StringBuffer ("{ "); 159 160 Class returnType = m.getReturnType(); 161 162 if (returnType != void.class) 163 { 164 body.append("return"); 165 166 if (returnType.isPrimitive()) 167 { 168 if (returnType == boolean.class) 169 body.append(" false"); 170 else if (returnType == long.class) 171 body.append(" 0L"); 172 else if (returnType == float.class) 173 body.append(" 0.0f"); 174 else if (returnType == double.class) 175 body.append(" 0.0d"); 176 else 177 body.append(" 0"); 178 } 179 else 180 { 181 body.append(" null"); 182 } 183 184 body.append(";"); 185 } 186 187 body.append(" }"); 188 189 cf.addMethod(Modifier.PUBLIC, m, body.toString()); 190 } 191 } | Popular Tags |