1 18 package org.objectweb.speedo.generation.mivisitor; 19 20 import org.objectweb.speedo.metadata.SpeedoClass; 21 import org.objectweb.speedo.metadata.SpeedoField; 22 import org.objectweb.speedo.api.SpeedoException; 23 import org.objectweb.speedo.api.SpeedoProperties; 24 import org.objectweb.speedo.generation.enhancer.EnhancerComponent; 25 import org.objectweb.speedo.generation.enhancer.LoggedClass; 26 import org.objectweb.speedo.generation.enhancer.Util; 27 import org.objectweb.speedo.generation.api.SpeedoCompilerParameter; 28 import org.objectweb.util.monolog.api.Logger; 29 import org.objectweb.util.monolog.api.BasicLevel; 30 import org.objectweb.asm.ClassVisitor; 31 import org.objectweb.asm.CodeVisitor; 32 import org.objectweb.asm.Constants; 33 import org.objectweb.asm.Type; 34 import org.objectweb.asm.Attribute; 35 36 import java.util.Collection ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 import java.util.ArrayList ; 40 41 45 public class PrimaryKeyFieldAdder extends AbstractMetaInfoVisitor { 46 47 private Loader loader; 48 49 public PrimaryKeyFieldAdder() { 50 super(); 51 loader = new Loader(); 52 } 53 54 public PrimaryKeyFieldAdder(MetaInfoVisitor mim) { 55 super(mim); 56 loader = new Loader(); 57 } 58 59 private class Loader extends EnhancerComponent { 60 61 public boolean init() throws SpeedoException { 62 isSrcJar = false; 63 return true; 64 } 65 66 public void process() throws SpeedoException { 67 } 69 public void setLogger(Logger logger) { 70 this.logger = logger; 71 } 72 } 73 74 protected String getLoggerName() { 75 return SpeedoProperties.LOGGER_NAME + ".generation.enhancer.analyzer"; 76 } 77 78 public void setLogger(Logger logger) { 79 super.setLogger(logger); 80 loader.setLogger(logger); 81 } 82 83 public void visitCompilerParameter(SpeedoCompilerParameter scp) throws SpeedoException { 84 loader.setSpeedoCompilerParameter(scp); 85 super.visitCompilerParameter(scp); 86 } 87 88 89 public void visitClass(SpeedoClass sc) throws SpeedoException { 90 super.visitClass(sc); 91 if (sc.objectidClass == null) { 92 return; 93 } 94 logger.log(BasicLevel.DEBUG, "PrimaryKeyFieldAdder.visitClass(" + sc.getFQName() + ")"); 95 Collection fields = sc.jdoField.values(); 96 List pkfields = new ArrayList (); 97 Iterator it = fields.iterator(); 98 while(it.hasNext()) { 99 SpeedoField sf = (SpeedoField) it.next(); 100 if (sf.primaryKey) { 101 pkfields.add(sf.name); 102 } 103 } 104 boolean isSrcJar = false; 105 SpeedoCompilerParameter _scp = loader.getSpeedoCompilerParameter(); 106 String className = sc.objectidClass; 107 if (className.indexOf('.') == -1) { 108 className = sc.jdoPackage.name + '.' + className; 109 } 110 UserIDClassAnalyzer uica = new UserIDClassAnalyzer(logger); 111 loader.loadJavaClass(isSrcJar, className, _scp.output,false) 112 .accept(uica, true); 113 if (uica.fieldNames.isEmpty()) { 114 throw new SpeedoException("The identifier class '" 115 + sc.objectidClass + "' has no public field field."); 116 } 117 if (sc.superClassName != null && sc.superClassName.length()>0) { 118 } else if (pkfields.isEmpty()) { 120 for(int i=0; i<uica.fieldNames.size(); i++) { 121 SpeedoField sf = (SpeedoField) sc.jdoField.get(uica.fieldNames.get(i)); 122 if (sf == null) { 123 logger.log(BasicLevel.WARN, "The public field '" 124 + uica.fieldNames.get(i) + "' of the identifier class " 125 + className + "' does not match to any field in the persistent class '" 126 + sc.getFQName() +"'."); 127 continue; 128 } 129 if (!sf.desc.equals(uica.fieldTypes.get(i))) { 130 throw new SpeedoException("The field '" + sf.name 131 + "' has not the same type in identifier class '" 132 + className + "' (" 133 + Util.type(Type.getType((String ) uica.fieldTypes.get(i))) 134 + ") and in the persistent class '" + sc.getFQName() 135 + "' (" + Util.type(Type.getType(sf.desc)) + ")."); 136 } 137 sf.primaryKey = true; 138 } 139 } else if (pkfields.size() != uica.fieldNames.size()) { 140 logger.log(BasicLevel.INFO, "The identifier field(s) " 141 + uica.fieldNames 142 + " does not match to persistent fields of the class " 143 + pkfields); 144 } else { 145 for(int i=0; i<pkfields.size(); i++) { 147 if (!uica.fieldNames.contains(pkfields.get(i))) { 148 logger.log(BasicLevel.WARN, "The field '" + pkfields.get(i) 149 + "' is marked primary-key=true whereas it is not " 150 + "defined in the identifier class '" + className 151 + "' associated to the persistent class '" 152 + sc.getFQName() + "'."); 153 } 154 } 155 for(int i=0; i<uica.fieldNames.size(); i++) { 156 if (!pkfields.contains(uica.fieldNames.get(i))) { 157 logger.log(BasicLevel.WARN, "The field '" + uica.fieldNames.get(i) 158 + "' is defined in the identifier class '" + className 159 + "' associated to the persistent class '" + sc.getFQName() 160 + "'. wheras it is not marked primary-key"); 161 } 162 } 163 } 164 } 165 166 private class UserIDClassAnalyzer extends LoggedClass implements ClassVisitor { 167 168 public List fieldNames; 169 public List fieldTypes; 170 171 public UserIDClassAnalyzer(Logger logger) { 172 super(logger); 173 fieldNames = new ArrayList (); 174 fieldTypes = new ArrayList (); 175 } 176 177 public void visit(int version, int i, String s, String s1, String [] strings, String s2) { 178 } 179 180 public void visitInnerClass(String s, String s1, String s2, int i) { 181 } 182 183 public void visitField(final int access, 184 final String name, 185 final String desc, 186 final Object value, 187 final Attribute attrs) { 188 if ((access & Constants.ACC_PUBLIC) != 0) { 189 fieldNames.add(name); 190 fieldTypes.add(desc); 191 } 192 } 193 194 public CodeVisitor visitMethod(int i, String s, String s1, String [] strings, Attribute attrs) { 195 return null; 196 } 197 198 public void visitAttribute(Attribute attribute) { 199 } 200 201 public void visitEnd() { 202 } 203 } 204 } 205 | Popular Tags |