1 27 28 package org.objectweb.speedo.generation.enhancer; 29 30 import org.objectweb.speedo.api.SpeedoProperties; 31 import org.objectweb.speedo.api.SpeedoException; 32 import org.objectweb.speedo.metadata.SpeedoXMLDescriptor; 33 import org.objectweb.speedo.metadata.SpeedoClass; 34 import org.objectweb.speedo.metadata.SpeedoField; 35 import org.objectweb.speedo.generation.lib.NamingRules; 36 import org.objectweb.asm.ClassWriter; 37 import org.objectweb.asm.ClassReader; 38 import org.objectweb.asm.ClassVisitor; 39 import org.objectweb.asm.CodeVisitor; 40 import org.objectweb.asm.Constants; 41 import org.objectweb.asm.Attribute; 42 import org.objectweb.util.monolog.api.Logger; 43 44 import java.util.Collection ; 45 import java.util.Iterator ; 46 import java.util.HashSet ; 47 import java.util.Set ; 48 49 public class DummyAccessorAdder extends EnhancerComponent { 50 51 public final static String LOGGER_NAME 52 = SpeedoProperties.LOGGER_NAME + ".generation.enhancer.fieldMdodifier"; 53 54 boolean add; 55 56 public DummyAccessorAdder(boolean add) { 57 this.add = add; 58 } 59 60 public boolean init() throws SpeedoException { 61 logger = scp.loggerFactory.getLogger(LOGGER_NAME); 62 return !scp.getXmldescriptor().isEmpty(); 63 } 64 65 public void process() throws SpeedoException { 66 if (scp.getXmldescriptor() == null || scp.getXmldescriptor().isEmpty()) { 67 return; 68 } 69 Collection xmls = scp.getXmldescriptor().values(); 70 for (Iterator itDesc = xmls.iterator(); itDesc.hasNext();) { 71 SpeedoXMLDescriptor desc = (SpeedoXMLDescriptor) itDesc.next(); 72 for (Iterator itclass = desc.getSpeedoClasses().iterator(); itclass.hasNext();) { 73 Set fields = new HashSet (); 74 SpeedoClass jdoclass = (SpeedoClass) itclass.next(); 75 Iterator it = jdoclass.jdoField.values().iterator(); 76 while (it.hasNext()) { 77 SpeedoField sf = (SpeedoField) it.next(); 78 if (sf.relationType != SpeedoField.NO_RELATION) { 79 fields.add(sf); 80 } 81 } 82 if (fields.isEmpty()) { 83 continue; 84 } 85 ClassWriter cw = new ClassWriter(false); 86 DummyAccessorModifier fam = new DummyAccessorModifier( 87 cw, logger, fields); 88 String name = jdoclass.getFQName(); 89 ClassReader cr = loadJavaClass(false, name, scp.output, false); 90 cr.accept(fam, false); 91 writeJavaClass(name, cw, scp.output); 92 } 93 } 94 } 95 96 class DummyAccessorModifier extends LoggedClassAdapter implements Constants { 97 98 private Set methods; 99 100 103 public DummyAccessorModifier(ClassVisitor classVisitor, 104 Logger logger, 105 Set fields) { 106 super(classVisitor, logger); 107 this.methods = new HashSet (); 108 Iterator i = fields.iterator(); 109 while (i.hasNext()) { 110 SpeedoField f = (SpeedoField)i.next(); 111 String getter = NamingRules.getterName(f.jdoClass, f.name); 112 String setter = NamingRules.setterName(f.jdoClass, f.name); 113 methods.add(getter); 114 methods.add(setter); 115 if (add) { 116 CodeVisitor _cv = this.cv.visitMethod(ACC_PUBLIC, getter, "()" + f.desc, null, null); 118 _cv.visitInsn(ACONST_NULL); 119 _cv.visitInsn(ARETURN); 120 _cv.visitMaxs(1, 1); 121 _cv = this.cv.visitMethod(ACC_PUBLIC, setter, "(" + f.desc + ")V", null, null); 123 _cv.visitInsn(RETURN); 124 _cv.visitMaxs(2, 2); 125 } 126 } 127 } 128 129 public CodeVisitor visitMethod(int i, String s, String s1, String [] strings, Attribute attrs) { 130 if (!add && methods.contains(s)) { 131 return null; 133 } else { 134 return super.visitMethod(i, s, s1, strings, attrs); 135 } 136 } 137 } 138 } 139 | Popular Tags |