1 18 package org.objectweb.speedo.generation.enhancer; 19 20 import org.objectweb.asm.Attribute; 21 import org.objectweb.asm.ClassVisitor; 22 import org.objectweb.asm.CodeAdapter; 23 import org.objectweb.asm.CodeVisitor; 24 import org.objectweb.asm.Constants; 25 import org.objectweb.speedo.generation.lib.NamingRules; 26 import org.objectweb.speedo.metadata.SpeedoClass; 27 import org.objectweb.speedo.metadata.SpeedoField; 28 import org.objectweb.util.monolog.api.Logger; 29 30 import java.util.Collection ; 31 32 37 public class PersistenceAwareClassModifier extends LoggedClassAdapter { 38 39 private final Collection xmlDescriptors; 40 41 public PersistenceAwareClassModifier(ClassVisitor cv, 42 Collection xmlDescriptors, 43 Logger logger) { 44 super(cv, logger); 45 this.xmlDescriptors = xmlDescriptors; 46 } 47 48 public CodeVisitor visitMethod(final int access, 49 final String name, 50 final String desc, 51 final String [] exceptions, 52 final Attribute attrs) { 53 CodeVisitor _cv = this.cv.visitMethod(access, name, desc, exceptions, attrs); 54 return new PersistenceAwareCodeModifier(_cv); 55 } 56 57 64 SpeedoField fetchJDOField(final String name, final String className) { 65 SpeedoClass c = Util.isPersistentCapable(className, xmlDescriptors); 66 if (c != null) { 67 return (SpeedoField) c.jdoField.get(name); 68 } else { 69 return null; 70 } 71 } 72 73 class PersistenceAwareCodeModifier extends CodeAdapter { 74 75 public PersistenceAwareCodeModifier(CodeVisitor cv) { 76 super(cv); 77 } 78 79 public void visitFieldInsn(final int opcode, 80 final String owner, 81 final String name, 82 final String desc) { 83 if (opcode == Constants.PUTFIELD) { 84 SpeedoField jdofield = fetchJDOField(name, owner.replace('/', '.')); 85 if (jdofield != null) { 86 String setterName = NamingRules.setterName(jdofield); 87 String setterDesc = "(" + desc + ")V"; 88 cv.visitMethodInsn( 89 Constants.INVOKEVIRTUAL, 90 owner, 91 setterName, 92 setterDesc); 93 return; 94 } 95 } else if (opcode == Constants.GETFIELD) { 96 SpeedoField jdofield = fetchJDOField(name, owner.replace('/', '.')); 97 if (jdofield != null) { 98 String getterName = NamingRules.getterName( 99 jdofield.jdoClass, 100 jdofield.name); 101 String getterDesc = "()" + desc; 102 cv.visitMethodInsn( 103 Constants.INVOKEVIRTUAL, 104 owner, 105 getterName, 106 getterDesc); 107 return; 108 } 109 } 110 cv.visitFieldInsn(opcode, owner, name, desc); 111 } 112 } 113 } 114 | Popular Tags |