1 18 package org.objectweb.speedo.generation.enhancer; 19 20 import org.objectweb.asm.ClassVisitor; 21 import org.objectweb.asm.CodeVisitor; 22 import org.objectweb.asm.Constants; 23 import org.objectweb.speedo.metadata.SpeedoClass; 24 import org.objectweb.speedo.metadata.SpeedoField; 25 import org.objectweb.speedo.naming.api.UserId; 26 import org.objectweb.speedo.naming.api.UserIdFactory; 27 import org.objectweb.util.monolog.api.Logger; 28 import org.objectweb.util.monolog.api.BasicLevel; 29 30 import java.util.Iterator ; 31 32 37 public class PNameEnhancer extends LoggedClassAdapter { 38 39 public final static String USER_ID_DESC = 40 "L" + UserId.class.getName().replace('.', '/') + ";"; 41 public final static String PTYPE_CLASS_NAME = 42 "org/objectweb/jorm/type/api/PType"; 43 public final static String PTYPE_DESC = "L" + PTYPE_CLASS_NAME + ";"; 44 45 protected SpeedoClass jdoClass; 46 47 public PNameEnhancer(ClassVisitor classVisitor, 48 SpeedoClass jdoClass, 49 Logger logger) { 50 super(classVisitor, logger); 51 this.jdoClass = jdoClass; 52 } 53 54 public void visit(final int version, final int access, 55 final String className, 56 final String superName, 57 final String [] interfaces, 58 final String sourceFile) { 59 String [] itfs; 60 String uifcn = UserIdFactory.class.getName().replace('.', '/'); 61 boolean alreadyUIF = false; 62 if (interfaces == null || interfaces.length==0) { 63 itfs = new String []{uifcn}; 64 } else { 65 int i=0; 66 while(i<interfaces.length && !interfaces[i].equals(uifcn)) { 67 i++; 68 } 69 alreadyUIF = i<interfaces.length; 70 if (alreadyUIF) { 71 itfs = interfaces; 72 } else { 73 itfs = new String [interfaces.length + 1]; 74 System.arraycopy(interfaces, 0, itfs, 1, interfaces.length); 75 itfs[0] = uifcn; 76 } 77 } 78 if (alreadyUIF) { 79 logger.log(BasicLevel.DEBUG, "The class " + className 80 + " already contains the interface " + uifcn); 81 super.visit(version, access, className, superName, interfaces, sourceFile); 82 return; 83 } 84 logger.log(BasicLevel.DEBUG, "Add to the class " + className 85 + " the interface " + uifcn); 86 super.visit(version, access, className, superName, itfs, sourceFile); 87 88 String uicn = jdoClass.objectidClass.replace('.', '/'); 90 String methodName = "getNewUserId"; 91 if (debug) { 92 logger.log(BasicLevel.DEBUG, "Add the method '" + methodName 93 + "' in the class '" + className + "'"); 94 } 95 logger.log(BasicLevel.DEBUG, "Add to the class " + className 96 + " the method " + methodName); 97 CodeVisitor _cv = this.cv.visitMethod( 98 Constants.ACC_PUBLIC, 99 methodName, 100 "()" + USER_ID_DESC, 101 null, 102 null); 103 104 logger.log(BasicLevel.DEBUG, "add code: " + uicn + " t = new " + uicn + "();"); 107 _cv.visitTypeInsn(Constants.NEW, uicn); 108 _cv.visitInsn(Constants.DUP); 109 _cv.visitMethodInsn(Constants.INVOKESPECIAL, uicn, "<init>", "()V"); 110 _cv.visitVarInsn(Constants.ASTORE, 1); 111 112 boolean hasLongOrDouble = false; 114 SpeedoClass current = jdoClass; 115 while(current != null) { 116 for(Iterator it = current.jdoField.values().iterator(); it.hasNext();) { 117 SpeedoField sp = (SpeedoField) it.next(); 118 if (sp.primaryKey) { 119 hasLongOrDouble |= sp.desc.charAt(0) == 'D' 120 || sp.desc.charAt(0) == 'J'; 121 logger.log(BasicLevel.DEBUG, "add code: this." + sp.name + " = t." + sp.name); 122 _cv.visitVarInsn(Constants.ALOAD, 1); _cv.visitVarInsn(Constants.ALOAD, 0); 124 _cv.visitFieldInsn(Constants.GETFIELD, className, sp.name, sp.desc); 125 _cv.visitFieldInsn(Constants.PUTFIELD, uicn, sp.name, sp.desc); 126 } 127 } 128 SpeedoClass tmp = current.getSuper(); 129 current = (tmp == current ? null : tmp); 130 } 131 132 logger.log(BasicLevel.DEBUG, "add code: t.jdoSetPersistentClassName(this.getPType().getJormName());"); 134 _cv.visitVarInsn(Constants.ALOAD, 1); 135 _cv.visitVarInsn(Constants.ALOAD, 0); 136 _cv.visitMethodInsn(Constants.INVOKEVIRTUAL, 137 className, "getPType", "()" + PTYPE_DESC); 138 _cv.visitMethodInsn(Constants.INVOKEVIRTUAL, 139 PTYPE_CLASS_NAME, "getJormName", "()Ljava/lang/String;"); 140 _cv.visitMethodInsn(Constants.INVOKEVIRTUAL, 141 uicn, "jdoSetPersistentClassName", "(Ljava/lang/String;)V"); 142 logger.log(BasicLevel.DEBUG, "add code: return t;"); 143 _cv.visitVarInsn(Constants.ALOAD, 1); 144 _cv.visitInsn(Constants.ARETURN); 145 _cv.visitMaxs((hasLongOrDouble ? 3 : 2), 2); 146 } 147 } 148 | Popular Tags |