KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > mivisitor > PrimaryKeyFieldAdder


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

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 JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.ArrayList JavaDoc;
40
41 /**
42  *
43  * @author S.Chassande-Barrioz
44  */

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             //nothing
68
}
69         public void setLogger(Logger logger) {
70             this.logger = logger;
71         }
72     }
73
74     protected String JavaDoc 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 JavaDoc fields = sc.jdoField.values();
96         List JavaDoc pkfields = new ArrayList JavaDoc();
97         Iterator JavaDoc 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 JavaDoc 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             //do nothing, the primary key field are inherited from the ancestor
119
} 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 JavaDoc) 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             //check that the lists are the same
146
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 JavaDoc fieldNames;
169         public List JavaDoc fieldTypes;
170
171         public UserIDClassAnalyzer(Logger logger) {
172             super(logger);
173             fieldNames = new ArrayList JavaDoc();
174             fieldTypes = new ArrayList JavaDoc();
175         }
176
177         public void visit(int version, int i, String JavaDoc s, String JavaDoc s1, String JavaDoc[] strings, String JavaDoc s2) {
178         }
179
180         public void visitInnerClass(String JavaDoc s, String JavaDoc s1, String JavaDoc s2, int i) {
181         }
182
183         public void visitField(final int access,
184                            final String JavaDoc name,
185                            final String JavaDoc desc,
186                            final Object JavaDoc 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 JavaDoc s, String JavaDoc s1, String JavaDoc[] 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