1 25 26 package org.objectweb.easybeans.enhancer.bean; 27 28 import org.objectweb.asm.ClassAdapter; 29 import org.objectweb.asm.ClassVisitor; 30 import org.objectweb.asm.MethodAdapter; 31 import org.objectweb.asm.MethodVisitor; 32 import org.objectweb.asm.Opcodes; 33 import org.objectweb.asm.Type; 34 import org.objectweb.easybeans.api.Factory; 35 import org.objectweb.easybeans.api.bean.EasyBeansBean; 36 import org.objectweb.easybeans.api.bean.EasyBeansMDB; 37 import org.objectweb.easybeans.api.bean.EasyBeansSFSB; 38 import org.objectweb.easybeans.api.bean.EasyBeansSLSB; 39 import org.objectweb.easybeans.api.container.EZBEJBContext; 40 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata; 41 import org.objectweb.easybeans.enhancer.CommonClassGenerator; 42 import org.objectweb.easybeans.enhancer.interceptors.EasyBeansInvocationContextGenerator; 43 44 50 public class BeanClassAdapter extends ClassAdapter implements Opcodes { 51 52 55 private ClassAnnotationMetadata classAnnotationMetadata = null; 56 57 63 public BeanClassAdapter(final ClassAnnotationMetadata classAnnotationMetadata, final ClassVisitor cv) { 64 super(cv); 65 this.classAnnotationMetadata = classAnnotationMetadata; 66 } 67 68 87 @Override 88 public void visit(final int version, final int access, final String name, final String signature, 89 final String superName, final String [] interfaces) { 90 91 String [] newInterfaces = null; 92 93 if (classAnnotationMetadata.isBean()) { 95 newInterfaces = new String [interfaces.length + 1]; 97 System.arraycopy(interfaces, 0, newInterfaces, 0, interfaces.length); 98 99 int indexElement = newInterfaces.length - 1; 100 101 if (classAnnotationMetadata.isStateless()) { 103 newInterfaces[indexElement] = Type.getInternalName(EasyBeansSLSB.class); 104 } else if (classAnnotationMetadata.isStateful()) { 105 newInterfaces[indexElement] = Type.getInternalName(EasyBeansSFSB.class); 106 } else if (classAnnotationMetadata.isMdb()) { 107 newInterfaces[indexElement] = Type.getInternalName(EasyBeansMDB.class); 108 } else { 109 newInterfaces[indexElement] = Type.getInternalName(EasyBeansBean.class); 110 } 111 } else { 112 newInterfaces = interfaces; 113 } 114 115 super.visit(version, access, name, signature, superName, newInterfaces); 116 117 } 118 138 @Override 139 public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, 140 final String [] exceptions) { 141 142 if ("<init>".equals(name) && "()V".equals(desc)) { 144 MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); 145 return new AddMethodConstructorAdapter(mv); 146 } 147 return super.visitMethod(access, name, desc, signature, exceptions); 148 149 } 150 151 152 157 @Override 158 public void visitEnd() { 159 super.visitEnd(); 160 161 CommonClassGenerator.addFieldGettersSetters(cv, classAnnotationMetadata.getClassName(), 163 "easyBeansFactory", Factory.class); 164 165 166 if (classAnnotationMetadata.isBean()) { 167 CommonClassGenerator.addFieldGettersSetters(cv, classAnnotationMetadata.getClassName(), 169 "easyBeansInterceptorManager", "L" + classAnnotationMetadata.getClassName() 170 + EasyBeansInvocationContextGenerator.SUFFIX_INTERCEPTOR_MANAGER + ";"); 171 172 173 CommonClassGenerator.addFieldGettersSetters(cv, classAnnotationMetadata.getClassName(), 175 "easyBeansContext", EZBEJBContext.class); 176 177 if (classAnnotationMetadata.isSession()) { 179 CommonClassGenerator.addFieldGettersSetters(cv, classAnnotationMetadata.getClassName(), 180 "easyBeansRemoved", Boolean.TYPE); 181 } 182 183 if (classAnnotationMetadata.isStateful()) { 185 CommonClassGenerator.addFieldGettersSetters(cv, classAnnotationMetadata.getClassName(), 186 "easyBeansStatefulID", Long .class); 187 } 188 189 } 190 191 } 192 193 198 public class AddMethodConstructorAdapter extends MethodAdapter { 199 200 204 public AddMethodConstructorAdapter(final MethodVisitor mv) { 205 super(mv); 206 } 207 208 212 @Override 213 public void visitCode() { 214 super.visitCode(); 215 String clManager = classAnnotationMetadata.getClassName() 216 + EasyBeansInvocationContextGenerator.SUFFIX_INTERCEPTOR_MANAGER; 217 mv.visitVarInsn(ALOAD, 0); 218 mv.visitTypeInsn(NEW, clManager); 219 mv.visitInsn(DUP); 220 mv.visitMethodInsn(INVOKESPECIAL, clManager, "<init>", "()V"); 221 mv.visitFieldInsn(PUTFIELD, classAnnotationMetadata.getClassName(), "easyBeansInterceptorManager", "L" 222 + clManager + ";"); 223 224 } 225 226 } 227 228 } 229 230 | Popular Tags |