1 27 28 package org.objectweb.speedo.generation.enhancer; 29 30 import org.objectweb.jorm.lib.PBindingImpl; 31 import org.objectweb.speedo.api.SpeedoException; 32 import org.objectweb.speedo.api.SpeedoProperties; 33 import org.objectweb.speedo.metadata.SpeedoClass; 34 import org.objectweb.speedo.metadata.SpeedoPackage; 35 import org.objectweb.speedo.metadata.SpeedoXMLDescriptor; 36 import org.objectweb.speedo.metadata.SpeedoIdentity; 37 import org.objectweb.speedo.generation.lib.NamingRules; 38 import org.objectweb.util.monolog.api.BasicLevel; 39 import org.objectweb.util.monolog.api.Logger; 40 import org.objectweb.asm.ClassReader; 41 import org.objectweb.asm.ClassWriter; 42 import org.objectweb.asm.attrs.Attributes; 43 44 import java.util.ArrayList ; 45 import java.util.Iterator ; 46 import java.util.Collection ; 47 import java.util.List ; 48 import java.util.TreeSet ; 49 import java.util.Set ; 50 51 58 public class PersistentClassEnhancer extends EnhancerComponent { 59 60 public final static String LOGGER_NAME 61 = SpeedoProperties.LOGGER_NAME + ".generation.enhancer"; 62 63 66 public boolean init() { 67 logger = scp.loggerFactory.getLogger(LOGGER_NAME); 68 isSrcJar = false; 70 return !scp.getXmldescriptor().isEmpty(); 71 } 72 73 79 public void process() throws SpeedoException { 80 if (scp.getXmldescriptor().isEmpty()) 81 return; 82 83 ArrayList except = new ArrayList (); 84 Collection xmls = scp.getXmldescriptor().values(); 85 Logger log = null; 86 for (Iterator itDesc = xmls.iterator(); itDesc.hasNext();) { 87 SpeedoXMLDescriptor desc = (SpeedoXMLDescriptor) itDesc.next(); 88 for (Iterator itPack = desc.jdoPackage.values().iterator(); itPack.hasNext();) { 89 SpeedoPackage sp = (SpeedoPackage) itPack.next(); 90 for (Iterator itclass = sp.jdoClass.values().iterator(); itclass.hasNext();) { 91 SpeedoClass jdoclass = (SpeedoClass) itclass.next(); 92 93 String name = sp.name + '.' + jdoclass.name; 94 String proxyName = NamingRules.proxyName(name); 95 96 ClassWriter cw = new ClassWriter(true); 97 98 RedundencyRemover rr = new RedundencyRemover(cw, logger); 99 100 List itfs = new ArrayList (); 101 102 Set oldClassNames = new TreeSet (); 104 oldClassNames.add(name.replace('.', '/')); 105 ClassReader cr; 106 MergedClassModifier mmodifier; 107 ClassRenamer crenamer; 108 if (jdoclass.superClassName == null) { 109 String bindingName = PBindingImpl.class.getName(); 110 log = scp.loggerFactory.getLogger(LOGGER_NAME + ".merge.binding." + name); 111 log.log(BasicLevel.DEBUG, "Merging the Binding of class: " + name); 112 cr = loadJavaClass(false, bindingName, scp.output, true); 113 mmodifier = new MergedClassModifier(rr, itfs, log); 114 oldClassNames.add(bindingName.replace('.', '/')); 115 crenamer = new ClassRenamer(mmodifier, 116 oldClassNames, name.replace('.', '/'), log); 117 cr.accept(crenamer, Attributes.getDefaultAttributes(), false); 118 } 119 log = scp.loggerFactory.getLogger(LOGGER_NAME + ".merge.proxy." + name); 121 log.log(BasicLevel.DEBUG, "Merging the Proxy of class: " + name); 122 String superClass = jdoclass.superClassName; 123 cr = loadJavaClass(false, proxyName, scp.output, true); 124 if (superClass == null || superClass.length() == 0) { 125 mmodifier = new MergedClassModifier(rr, itfs, log); 126 } else { 127 mmodifier = new MergedClassModifier(rr, itfs, 128 superClass.replace('.', '/'), log); 129 } 130 crenamer = new ClassRenamer(mmodifier, oldClassNames, 131 name.replace('.', '/'), logger); 132 cr.accept(crenamer, Attributes.getDefaultAttributes(), false); 133 134 log = scp.loggerFactory.getLogger(LOGGER_NAME + ".fieldaccess." + name); 136 log.log(BasicLevel.DEBUG, "Modifying the field access of class: " + name); 137 cr = loadJavaClass(false, name, scp.output, false); 138 ClassAccessorModifier caccessmodifier = 139 new ClassAccessorModifier(rr, xmls, log); 140 ClassInterfaceAdder citfadder = new ClassInterfaceAdder( 141 caccessmodifier, itfs, log); 142 JDOImplRegistrationAdder reg = new JDOImplRegistrationAdder(citfadder, jdoclass, logger); 143 cr.accept(reg, Attributes.getDefaultAttributes(), false); 144 writeJavaClass(name, cw, scp.output); 145 146 if (jdoclass.identityType == SpeedoIdentity.USER_ID 147 && jdoclass.objectidClass != null 148 && (jdoclass.superClassName == null || !jdoclass.objectidClass.equals(jdoclass.getSuper().objectidClass)) 150 ) { 151 log = scp.loggerFactory.getLogger(LOGGER_NAME + ".userid." + name); 152 cw = new ClassWriter(true); 153 String cn = jdoclass.objectidClass; 154 cr = loadJavaClass(false, cn, scp.output, true); 155 156 cr.accept(new UserIdEnhancer(cw, jdoclass, log), 157 Attributes.getDefaultAttributes(), false); 158 writeJavaClass(cn, cw, scp.output); 159 160 cw = new ClassWriter(false); 161 cn = NamingRules.pnameName(jdoclass.objectidClass); 162 cr = loadJavaClass(false, cn, scp.output, true); 163 cr.accept(new PNameEnhancer(cw, jdoclass, log), 164 Attributes.getDefaultAttributes(), false); 165 writeJavaClass(cn, cw, scp.output); 166 } 167 } 168 } 169 } 170 171 if (!except.isEmpty() && logger.isLoggable(BasicLevel.WARN)) { 173 for (Iterator it = except.iterator(); it.hasNext();) { 174 Exception e = (Exception ) it.next(); 175 logger.log(BasicLevel.WARN, e.getLocalizedMessage()); 176 } 177 } 178 } 179 } 180 | Popular Tags |