1 18 package org.objectweb.speedo.generation.enhancer; 19 20 import org.objectweb.speedo.metadata.SpeedoXMLDescriptor; 21 import org.objectweb.speedo.metadata.SpeedoPackage; 22 import org.objectweb.speedo.metadata.SpeedoClass; 23 import org.objectweb.speedo.generation.lib.NamingRules; 24 import org.objectweb.asm.Type; 25 import org.objectweb.asm.Constants; 26 import org.objectweb.asm.CodeVisitor; 27 28 import java.util.Set ; 29 import java.util.HashSet ; 30 import java.util.Collection ; 31 import java.util.Iterator ; 32 33 38 public class Util { 39 40 private static Set PERSISTENT_CLASSES; 41 private static Set AUTO_PERSISTENT_CLASSES; 42 43 static { 44 PERSISTENT_CLASSES = new HashSet (); 45 AUTO_PERSISTENT_CLASSES = new HashSet (); 46 47 AUTO_PERSISTENT_CLASSES.add("java.lang.String"); 48 AUTO_PERSISTENT_CLASSES.add("java.lang.Character"); 49 AUTO_PERSISTENT_CLASSES.add("java.lang.Boolean"); 50 AUTO_PERSISTENT_CLASSES.add("java.lang.Number"); 51 AUTO_PERSISTENT_CLASSES.add("java.lang.Byte"); 52 AUTO_PERSISTENT_CLASSES.add("java.lang.Integer"); 53 AUTO_PERSISTENT_CLASSES.add("java.lang.Long"); 54 AUTO_PERSISTENT_CLASSES.add("java.lang.Double"); 55 AUTO_PERSISTENT_CLASSES.add("java.lang.Float"); 56 AUTO_PERSISTENT_CLASSES.add("java.lang.Short"); 57 58 AUTO_PERSISTENT_CLASSES.add("java.math.BigInteger"); 59 AUTO_PERSISTENT_CLASSES.add("java.math.BigDecimal"); 60 61 PERSISTENT_CLASSES.add("java.util.Collection"); 62 PERSISTENT_CLASSES.add("java.util.Map"); 63 PERSISTENT_CLASSES.add("java.util.Set"); 64 PERSISTENT_CLASSES.add("java.util.List"); 65 PERSISTENT_CLASSES.add("java.util.LinkedList"); 66 PERSISTENT_CLASSES.add("java.util.ArrayList"); 67 PERSISTENT_CLASSES.add("java.util.HashMap"); 68 PERSISTENT_CLASSES.add("java.util.TreeMap"); 69 PERSISTENT_CLASSES.add("java.util.WeakHashMap"); 70 PERSISTENT_CLASSES.add("java.util.Properties"); 71 72 PERSISTENT_CLASSES.add("java.util.Vector"); 73 PERSISTENT_CLASSES.add("java.util.HashTable"); 74 PERSISTENT_CLASSES.add("java.util.Stack"); 75 76 PERSISTENT_CLASSES.add("java.util.Date"); 77 PERSISTENT_CLASSES.add("java.util.Locale"); 78 79 AUTO_PERSISTENT_CLASSES.add("java.sql.Date"); 80 AUTO_PERSISTENT_CLASSES.add("java.sql.Time"); 81 AUTO_PERSISTENT_CLASSES.add("java.sql.Timestamp"); 82 83 PERSISTENT_CLASSES.addAll(AUTO_PERSISTENT_CLASSES); 84 } 85 86 95 public static boolean isPersistentType(final String desc, 96 final Collection xml) { 97 Type type = Type.getType(desc); 98 if (type.getSort() == Type.ARRAY) { 99 return isPersistentType(type.getElementType().getDescriptor(), xml); 101 } else if (type.getSort() == Type.OBJECT) { 102 String className = type.getClassName(); 104 return PERSISTENT_CLASSES.contains(className) 105 || isPersistentCapable(className, xml) != null; 106 } else { 107 return true; 109 } 110 } 111 public static boolean isAutomaticPersistentType(final String desc, 112 final Collection xml) { 113 Type type = Type.getType(desc); 114 if (type.getSort() == Type.ARRAY) { 115 return false; 116 } else if (type.getSort() == Type.OBJECT) { 117 String className = type.getClassName(); 119 return AUTO_PERSISTENT_CLASSES.contains(className) 120 || isPersistentCapable(className, xml) != null; 121 } else { 122 return true; 124 } 125 } 126 127 134 public static SpeedoClass isPersistentCapable(final String className, 135 final Collection xml) { 136 Iterator i = xml.iterator(); 137 while (i.hasNext()) { 138 SpeedoXMLDescriptor xmlDesc = (SpeedoXMLDescriptor) i.next(); 139 String fieldPackage = NamingRules.packageName(className); 140 String fieldClass = NamingRules.className(className); 141 SpeedoPackage pkg = (SpeedoPackage) xmlDesc.jdoPackage.get(fieldPackage); 142 if (pkg != null) { 143 SpeedoClass c = (SpeedoClass) pkg.jdoClass.get(fieldClass); 144 if (c != null) { 145 return c; 146 } 147 } 148 } 149 return null; 150 } 151 152 155 public static void visitIntConstant(CodeVisitor cv, int value) { 156 if (value == 0) { 157 cv.visitInsn(Constants.ICONST_0); 158 } else if (value == 1) { 159 cv.visitInsn(Constants.ICONST_1); 160 } else if (value == 2) { 161 cv.visitInsn(Constants.ICONST_2); 162 } else if (value == 3) { 163 cv.visitInsn(Constants.ICONST_3); 164 } else if (value == 4) { 165 cv.visitInsn(Constants.ICONST_4); 166 } else if (value == 5) { 167 cv.visitInsn(Constants.ICONST_5); 168 } else if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) { 169 cv.visitIntInsn(Constants.BIPUSH, value); 170 } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) { 171 cv.visitIntInsn(Constants.SIPUSH, value); 172 } else { 173 cv.visitLdcInsn(new Integer (value)); 174 } 175 } 176 177 public static void visitLongConstant(CodeVisitor cv, long value) { 178 if (value == 0) { 179 cv.visitInsn(Constants.LCONST_0); 180 } else if (value == 1) { 181 cv.visitInsn(Constants.LCONST_1); 182 } else { 183 cv.visitLdcInsn(new Long (value)); 184 } 185 } 186 187 190 public static String modifier(int access) { 191 StringBuffer buf = new StringBuffer (); 192 if ((access & Constants.ACC_PUBLIC) != 0) { 193 buf.append("public "); 194 } 195 if ((access & Constants.ACC_PRIVATE) != 0) { 196 buf.append("private "); 197 } 198 if ((access & Constants.ACC_PROTECTED) != 0) { 199 buf.append("protected "); 200 } 201 if ((access & Constants.ACC_FINAL) != 0) { 202 buf.append("final "); 203 } 204 if ((access & Constants.ACC_STATIC) != 0) { 205 buf.append("static "); 206 } 207 if ((access & Constants.ACC_SYNCHRONIZED) != 0) { 208 buf.append("synchronized "); 209 } 210 if ((access & Constants.ACC_VOLATILE) != 0) { 211 buf.append("volatile "); 212 } 213 if ((access & Constants.ACC_TRANSIENT) != 0) { 214 buf.append("transient "); 215 } 216 if ((access & Constants.ACC_NATIVE) != 0) { 217 buf.append("native "); 218 } 219 if ((access & Constants.ACC_ABSTRACT) != 0) { 220 buf.append("abstract "); 221 } 222 if ((access & Constants.ACC_STRICT) != 0) { 223 buf.append("strictfp "); 224 } 225 return buf.toString(); 226 } 227 228 public static String type(Type type) { 229 switch (type.getSort()) { 230 case Type.BOOLEAN: 231 return "boolean"; 232 case Type.CHAR: 233 return "char"; 234 case Type.BYTE: 235 return "byte"; 236 case Type.SHORT: 237 return "short"; 238 case Type.INT: 239 return "int"; 240 case Type.FLOAT: 241 return "float"; 242 case Type.LONG: 243 return "long"; 244 case Type.DOUBLE: 245 return "double"; 246 case Type.ARRAY: 247 String result = type(type.getElementType()); 248 for (int i = 0; i < type.getDimensions(); ++i) { 249 result += "[]"; 250 } 251 return result; 252 default: 253 result = type.getClassName(); 254 if (result.startsWith("java.lang.")) { 255 return result.substring("java.lang.".length()); 256 } 257 return result; 258 } 259 } 260 261 public static Class getClass(Type type, ClassLoader cl) throws Exception { 262 switch (type.getSort()) { 263 case Type.BOOLEAN: 264 return Boolean.TYPE; 265 case Type.CHAR: 266 return Character.TYPE; 267 case Type.BYTE: 268 return Byte.TYPE; 269 case Type.SHORT: 270 return Short.TYPE; 271 case Type.INT: 272 return Integer.TYPE; 273 case Type.FLOAT: 274 return Float.TYPE; 275 case Type.LONG: 276 return Long.TYPE; 277 case Type.DOUBLE: 278 return Double.TYPE; 279 default: 280 return Class.forName(type.getClassName(), false, cl); 281 } 282 } 283 public static String getClassName(Type type) { 284 switch (type.getSort()) { 285 case Type.BOOLEAN: 286 return "Boolean.TYPE"; 287 case Type.CHAR: 288 return "Character.TYPE"; 289 case Type.BYTE: 290 return "Byte.TYPE"; 291 case Type.SHORT: 292 return "Short.TYPE"; 293 case Type.INT: 294 return "Integer.TYPE"; 295 case Type.FLOAT: 296 return "Float.TYPE"; 297 case Type.LONG: 298 return "Long.TYPE"; 299 case Type.DOUBLE: 300 return "Double.TYPE"; 301 case Type.ARRAY: 302 StringBuffer result = new StringBuffer (type(type.getElementType())); 303 for (int i = 0; i < type.getDimensions(); ++i) { 304 result.append("[]"); 305 } 306 return result.append(".class").toString(); 307 default: 308 return type.getClassName() + ".class"; 309 } 310 } 311 } 312 | Popular Tags |