1 25 26 package org.objectweb.easybeans.enhancer.interceptors; 27 28 import java.util.List ; 29 30 import org.objectweb.asm.ClassWriter; 31 import org.objectweb.asm.MethodVisitor; 32 import org.objectweb.asm.Type; 33 import org.objectweb.easybeans.api.Factory; 34 import org.objectweb.easybeans.api.container.EZBEJBContext; 35 import org.objectweb.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata; 36 import org.objectweb.easybeans.enhancer.CommonClassGenerator; 37 38 44 public class InterceptorManagerGenerator extends CommonClassGenerator { 45 46 49 private EjbJarAnnotationMetadata ejbJarAnnotationMetadata = null; 50 51 54 private String generatedClassName = null; 55 56 59 private List <String > allInterceptors = null; 60 61 64 public static final String [] INTERFACES = new String [] {"org/objectweb/easybeans/api/injection/EasyBeansInjection"}; 65 66 69 private static final String EZB_EJBCONTEXT_DESC = Type.getDescriptor(EZBEJBContext.class); 70 71 77 public InterceptorManagerGenerator(final EjbJarAnnotationMetadata ejbJarAnnotationMetadata, 78 final String generatedClassName, final List <String > allInterceptors) { 79 super(new ClassWriter(true)); 80 this.ejbJarAnnotationMetadata = ejbJarAnnotationMetadata; 81 this.generatedClassName = generatedClassName; 82 this.allInterceptors = allInterceptors; 83 } 84 85 89 public void generate() { 90 addClassDeclaration(); 91 addAttributes(); 92 addConstructor(); 93 addMethods(); 94 endClass(); 95 } 96 97 100 private void addClassDeclaration() { 101 getCW().visit(GENERATED_CLASS_VERSION, ACC_PUBLIC + ACC_SUPER, generatedClassName, null, "java/lang/Object", 103 INTERFACES); 104 } 105 106 110 private void addAttributes() { 111 for (String interceptor : allInterceptors) { 112 String fieldName = getField(interceptor); 113 addAttribute(ACC_PRIVATE, fieldName, encodeClassDesc(interceptor)); 114 } 115 } 116 117 122 private static String getField(final String interceptorClass) { 123 return interceptorClass.replace("/", ""); 124 } 125 126 131 private static String getMethod(final String interceptorClass) { 132 return "get" + interceptorClass.replace("/", ""); 133 } 134 135 145 private void addConstructor() { 146 MethodVisitor mv = getCW().visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); 148 mv.visitCode(); 149 150 mv.visitVarInsn(ALOAD, 0); 152 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); 153 154 for (String interceptor : allInterceptors) { 156 mv.visitVarInsn(ALOAD, 0); 157 mv.visitTypeInsn(NEW, interceptor); 158 mv.visitInsn(DUP); 159 mv.visitMethodInsn(INVOKESPECIAL, interceptor, "<init>", "()V"); 160 mv.visitFieldInsn(PUTFIELD, generatedClassName, getField(interceptor), encodeClassDesc(interceptor)); 161 } 162 163 mv.visitInsn(RETURN); 165 166 mv.visitMaxs(0, 0); 168 mv.visitEnd(); 169 } 170 171 174 private void addMethods() { 175 addGetterMethods(); 176 addInjectedMethod(); 177 addDefaultMethods(); 178 } 179 180 184 private void addDefaultMethods() { 185 CommonClassGenerator.addFieldGettersSetters(getCW(), generatedClassName, "easyBeansFactory", Factory.class); 187 188 CommonClassGenerator.addFieldGettersSetters(getCW(), generatedClassName, "easyBeansContext", 190 EZBEJBContext.class); 191 } 192 193 194 203 private void addGetterMethods() { 204 for (String interceptor : allInterceptors) { 205 MethodVisitor mv = getCW().visitMethod(ACC_PUBLIC, getMethod(interceptor), 206 "()" + encodeClassDesc(interceptor), null, null); 207 mv.visitCode(); 208 mv.visitVarInsn(ALOAD, 0); 209 mv.visitFieldInsn(GETFIELD, generatedClassName, getField(interceptor), encodeClassDesc(interceptor)); 210 mv.visitInsn(ARETURN); 211 mv.visitMaxs(0, 0); 212 mv.visitEnd(); 213 } 214 } 215 216 228 private void addInjectedMethod() { 229 MethodVisitor mv = getCW().visitMethod(ACC_PUBLIC, "injectedByEasyBeans", "()V", null, null); 230 mv.visitCode(); 231 232 for (String interceptor : allInterceptors) { 233 if (ejbJarAnnotationMetadata.getClassAnnotationMetadata(interceptor) != null) { 236 String fieldName = getField(interceptor); 237 238 mv.visitVarInsn(ALOAD, 0); 240 mv.visitFieldInsn(GETFIELD, generatedClassName, fieldName, encodeClassDesc(interceptor)); 241 mv.visitVarInsn(ALOAD, 0); 242 mv.visitFieldInsn(GETFIELD, generatedClassName, "easyBeansContext", 243 EZB_EJBCONTEXT_DESC); 244 mv.visitMethodInsn(INVOKEVIRTUAL, interceptor, "setEasyBeansContext", 245 "(" + EZB_EJBCONTEXT_DESC + ")V"); 246 247 mv.visitVarInsn(ALOAD, 0); 249 mv.visitFieldInsn(GETFIELD, generatedClassName, fieldName, encodeClassDesc(interceptor)); 250 mv.visitMethodInsn(INVOKEVIRTUAL, interceptor, "injectedByEasyBeans", "()V"); 251 } 252 } 253 mv.visitInsn(RETURN); 254 mv.visitMaxs(0, 0); 255 mv.visitEnd(); 256 } 257 258 261 private void endClass() { 262 getCW().visitEnd(); 263 } 264 265 268 public byte[] getBytes() { 269 return getCW().toByteArray(); 270 } 271 272 } 273 | Popular Tags |