001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.generation.generator.lib;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.Collections;
022: import java.util.Comparator;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027:
028: import org.apache.velocity.VelocityContext;
029: import org.apache.velocity.context.Context;
030: import org.objectweb.asm.Constants;
031: import org.objectweb.asm.Type;
032: import org.objectweb.jorm.generator.lib.CommonHelper;
033: import org.objectweb.jorm.lib.JormPathHelper;
034: import org.objectweb.jorm.metainfo.api.Class;
035: import org.objectweb.jorm.metainfo.api.ClassProject;
036: import org.objectweb.jorm.metainfo.api.ClassRef;
037: import org.objectweb.jorm.metainfo.api.GenClassRef;
038: import org.objectweb.jorm.metainfo.api.Mapping;
039: import org.objectweb.jorm.metainfo.api.NameDef;
040: import org.objectweb.jorm.metainfo.api.Reference;
041: import org.objectweb.jorm.metainfo.api.TypedElement;
042: import org.objectweb.jorm.type.api.PType;
043: import org.objectweb.speedo.api.SpeedoException;
044: import org.objectweb.speedo.api.SpeedoProperties;
045: import org.objectweb.speedo.api.UserFieldMapping;
046: import org.objectweb.speedo.genclass.SupportedGenClass;
047: import org.objectweb.speedo.generation.api.SpeedoCompilerParameter;
048: import org.objectweb.speedo.generation.lib.NamingRules;
049: import org.objectweb.speedo.lib.Personality;
050: import org.objectweb.speedo.metadata.SpeedoArray;
051: import org.objectweb.speedo.metadata.SpeedoClass;
052: import org.objectweb.speedo.metadata.SpeedoCollection;
053: import org.objectweb.speedo.metadata.SpeedoExtension;
054: import org.objectweb.speedo.metadata.SpeedoFetchGroup;
055: import org.objectweb.speedo.metadata.SpeedoField;
056: import org.objectweb.speedo.metadata.SpeedoMap;
057: import org.objectweb.speedo.metadata.SpeedoNullValue;
058: import org.objectweb.speedo.metadata.SpeedoVersion;
059: import org.objectweb.speedo.mim.api.FetchPlanItf;
060: import org.objectweb.util.monolog.api.BasicLevel;
061:
062: /**
063: *
064: * @author S.Chassande-Barrioz
065: */
066: public abstract class AbstractSpeedoGenerator extends
067: AbstractVelocityGenerator implements SupportedGenClass {
068:
069: protected static CommonHelper jormTools = new CommonHelper();
070:
071: public AbstractSpeedoGenerator(Personality p) {
072: super (p);
073: }
074:
075: public Map getContextAsMap(SpeedoClass moClass)
076: throws SpeedoException {
077: int nbField = moClass.computeFieldNumbers();
078: Map ctx = new HashMap();
079: //creation of the Velocity context
080: ctx.put("helper", this );
081: ctx.put("isNotAbstract", new Boolean(!moClass.isAbstract));
082: ctx.put("isAbstract", new Boolean(moClass.isAbstract));
083: ctx.put("withLog", new Boolean(!moClass.isAbstract));
084: ctx.put("package", moClass.moPackage.name);
085: ctx.put("mapper", scp.mapperName);
086: ctx.put("projectName", scp.projectName);
087: ctx.put("isSerializable", new Boolean(moClass.isSerializable));
088: ctx.put("detachable", new Boolean(moClass.isDetachable));
089:
090: //class name
091: ctx.put("baseClassName", moClass.name);
092: ctx.put("classNameMapping", NamingRules
093: .mappingName(moClass.name));
094: ctx
095: .put("classNameFields", NamingRules
096: .fieldsName(moClass.name));
097: ctx.put("classNameHome", NamingRules.homeName(moClass.name));
098: ctx.put("classNameKey", NamingRules.keyName(moClass.name));
099: ctx.put("classNameAccessor", NamingRules
100: .accessorName(moClass.name));
101:
102: //inheritance
103: ctx.put("hasSuperclass", Boolean.valueOf(moClass
104: .getSuperClassName() != null));
105: ctx.put("superClassName", moClass.getSuperClassName());
106: ctx.put("hasSubClasses", Boolean.valueOf(!moClass.jormclass
107: .getSubClasses().isEmpty()));
108:
109: //Identity type
110: if (moClass.identity.isDataStore()) {
111: ctx.put("isContainerId", Boolean.TRUE);
112: ctx.put("isContainerIdValue", "true");
113: } else {
114: ctx.put("isContainerId", Boolean.FALSE);
115: ctx.put("isContainerIdValue", "false");
116: }
117: String gcpnamehints = scp.nmf.getNamingManager(moClass)
118: .getGCPNameHints(moClass,
119: getClassNameDef(moClass.jormclass));
120: ctx.put("gcPNameHints", gcpnamehints);
121:
122: //version strategy
123: if (moClass.version != null) {
124: ctx.put("versioningStrategy", new Byte(
125: moClass.version.strategy));
126: ctx
127: .put(
128: "isDatetime",
129: (moClass.version.strategy == SpeedoVersion.DATE_TIME) ? Boolean.TRUE
130: : Boolean.FALSE);
131: ctx.put("isVersioned", Boolean.TRUE);
132: ctx.put("versionString", "Speedo"
133: + SpeedoVersion.toString(moClass.version.strategy));
134: } else {
135: ctx.put("versioningStrategy", new Byte(
136: SpeedoVersion.NO_VERSION));
137: ctx.put("isVersioned", Boolean.FALSE);
138: }
139:
140: // persistent fields
141: List fields = new ArrayList(moClass.fields.size());
142: ctx.put("fields", fields);
143:
144: List referenceFields = new ArrayList(moClass.fields.size());
145: ctx.put("referenceFields", referenceFields);
146:
147: List primitiveFields = new ArrayList(moClass.fields.size());
148: ctx.put("primitiveFields", primitiveFields);
149:
150: List fieldsToSerialize = new ArrayList(moClass.fields.size());
151: ctx.put("fieldsToSerialize", fieldsToSerialize);
152:
153: StringBuffer maxLongVals = new StringBuffer("new long[]{");
154: String sep = "";
155: int ndlong = (moClass.computeFieldNumbers() / 64) + 1;
156: for (int i = 0; i < ndlong; i++) {
157: maxLongVals.append(sep);
158: maxLongVals.append("0xffffffffffffffffL");
159: sep = ", ";
160: }
161: maxLongVals.append("}");
162: ctx.put("serializedFieldsId", maxLongVals);
163: ctx.put("declLong", "long[]");
164: ctx.put("useofLong", "fields");
165:
166: boolean hasRef = false;
167: ctx.put("needSpeedoGenClassListener", new Boolean(false));
168:
169: for (Iterator it = moClass.fields.values().iterator(); it
170: .hasNext();) {
171: SpeedoField sp = (SpeedoField) it.next();
172: Field f = new Field();
173: fields.add(f);
174: if ((sp.visibility & Constants.ACC_TRANSIENT) == 0
175: && (sp.visibility & Constants.ACC_STATIC) == 0) {
176: fieldsToSerialize.add(f);
177: }
178: fillFieldInfo(f, sp, nbField, ctx);
179: hasRef = hasRef || !f.isClassical;
180: if (f.isReference) {
181: referenceFields.add(f);
182: } else {
183: primitiveFields.add(f);
184: }
185: }
186:
187: // add the fetchgroups defined for this class into the context
188: List fetchGroups = new ArrayList(moClass.fetchGroups.size());
189: boolean defaultRedefined = false;
190: boolean valuesRedefined = false;
191: ctx.put("fetchGroups", fetchGroups);
192: for (Iterator it = moClass.fetchGroups.values().iterator(); it
193: .hasNext();) {
194: SpeedoFetchGroup sfg = (SpeedoFetchGroup) it.next();
195: if (sfg.name.equals(FetchPlanItf.DEFAULT))
196: defaultRedefined = true;
197: else if (sfg.name.equals(FetchPlanItf.VALUES))
198: valuesRedefined = true;
199: FetchGroup fg = new FetchGroup();
200: fetchGroups.add(fg);
201: fillFetchGroupInfo(fg, sfg);
202: }
203: ctx.put("defaultRedefined", new Boolean(defaultRedefined));
204: ctx.put("valuesRedefined", new Boolean(valuesRedefined));
205: ctx.put("hasRef", new Boolean(hasRef));
206: return ctx;
207: }
208:
209: protected final Context getContext(SpeedoClass jdoClass)
210: throws SpeedoException {
211: Map m = getContextAsMap(jdoClass);
212: Context vctx = new VelocityContext();
213: for (Iterator it = m.entrySet().iterator(); it.hasNext();) {
214: Map.Entry me = (Map.Entry) it.next();
215: vctx.put((String) me.getKey(), me.getValue());
216: }
217: return vctx;
218: }
219:
220: public void fillFetchGroupInfo(FetchGroup fg, SpeedoFetchGroup sfg)
221: throws SpeedoException {
222: fg.name = sfg.name;
223: fg.postload = sfg.postLoad;
224: //add the list of Field to load with this fetchgroup
225: fg.fields = new ArrayList(sfg.getFields().size());
226: Iterator it = sfg.getFields().values().iterator();
227: while (it.hasNext()) {
228: SpeedoField sf = (SpeedoField) it.next();
229: Field f = new Field();
230: fg.fields.add(f);
231: fillFieldInfo4FetchGroup(f, sf);
232: }
233: //add the names of the nested fetchgroups
234: fg.nestedFetchGroups = new ArrayList(sfg.getNestedFetchGroups()
235: .size());
236: Iterator itFG = sfg.getNestedFetchGroups().keySet().iterator();
237: while (itFG.hasNext()) {
238: fg.nestedFetchGroups.add(itFG.next());
239: }
240: }
241:
242: public void fillFieldInfo4FetchGroup(Field f, SpeedoField sf) {
243: f.name = sf.name;
244: f.depth = sf.depth;
245: f.fetchGroup = sf.fetchGroup;
246: if (f.fetchGroup != null)
247: f.name += SpeedoFetchGroup.FG_SLASH + f.fetchGroup;
248: if (f.depth > 0)
249: f.name += SpeedoFetchGroup.FG_AT + f.depth;
250: }
251:
252: public void fillFieldInfo(Field f, SpeedoField sp, int nbField,
253: Map ctx) throws SpeedoException {
254: SpeedoClass moClass = sp.moClass;
255: f.name = sp.name;
256: TypedElement te = moClass.jormclass.getTypedElement(f.name);
257: f.type = sp.type();
258: f.jvmType = sp.type;
259: f.nameUpperFirst = Character.toUpperCase(f.name.charAt(0))
260: + f.name.substring(1);
261: f.number = sp.number;
262: f.getter = NamingRules.getterName(sp.moClass, sp.name);
263: f.setter = NamingRules.setterName(sp.moClass, sp.name);
264: f.isClassical = isClassicalType(sp.type());
265: f.modifier = sp.modifier();
266: f.isKey = sp.primaryKey;
267: f.jormmeth = jormTools.upperFL(te.getName());
268: f.jormfield = te.getName();
269: f.isNotauthorizedToBeNull = (sp.nullValue == SpeedoNullValue.EXCEPTION);
270: f.declaration = sp.publicSignature();
271: f.defaultFetchGroup = sp.defaultFetchGroup;
272: String cacheName = sp
273: .getExtensionValueByKey(SpeedoProperties.USER_CACHE);
274: if (cacheName != null) {
275: f.userCacheNames = Collections.singleton(cacheName);
276: } else {
277: f.userCacheNames = Collections.EMPTY_SET;
278: }
279:
280: // looks for identity type of the field and tests if it is an array
281: Type fieldType = Type.getReturnType(sp.type);
282: if (!f.isClassical && fieldType.getSort() == Type.OBJECT) {
283: SpeedoClass jdoclass = scp.smi
284: .getSpeedoClass(fieldType.getClassName(),
285: sp.moClass.moPackage.xmlDescriptor);
286: if (jdoclass != null) {
287: f.isContainerId = jdoclass.identity.isDataStore();
288: } else {
289: f.isContainerId = true;
290: }
291: f.isArray = false;
292: } else {
293: f.isArray = sp.jdoTuple instanceof SpeedoArray;
294: }
295:
296: f.jormMeth = jormTools.upperFL(f.name);
297:
298: //Field Id
299: f.jormFieldIdLongPos = sp.number / 64;
300: f.jormFieldId = 1L << (sp.number % 64);
301: f.jormFielIdDecl = "new long[]{";
302: String sep = "";
303: for (int i = 0; i <= (nbField / 64); i++) {
304: f.jormFielIdDecl += sep
305: + (i == f.jormFieldIdLongPos ? f.jormFieldId + "L"
306: : "0L");
307: sep = ", ";
308: }
309: f.jormFielIdDecl += "}";
310:
311: f.isReference = jormTools.isReference(te);
312: if (f.isReference) { //reference field
313: fillReferenceInfo(f, sp, te, ctx, fieldType);
314: } else { //primitive field
315: f.jormType = te.getType().getJavaName();
316: f.memoryType = sp.type();
317: f.jormcast = sp.type();
318: f.toMemory = "val";
319: if (te.getType().getTypeCode() == PType.TYPECODE_SERIALIZED) {
320: f.toMemory = "(" + f.memoryType + ")" + f.toMemory;
321: }
322: f.toStorage = f.name;
323: fillUserFieldMappingInfo(f, sp);
324: }
325:
326: }
327:
328: public void fillReferenceInfo(Field f, SpeedoField sp,
329: TypedElement te, Map ctx, Type asmType)
330: throws SpeedoException {
331: f.jormType = "Object";
332: f.isMultiValued = jormTools.isGenClassRef(te);
333: f.depth = sp.depth;
334: //Jorm Cast
335: if (f.isMultiValued) {
336: if (sp.jdoTuple instanceof SpeedoCollection) {
337: f.jormcast = "java.util.Collection";
338: } else if (sp.jdoTuple instanceof SpeedoMap) {
339: f.jormcast = "java.util.Map";
340: f.isMap = true;
341: } else if (sp.jdoTuple instanceof SpeedoArray) {
342: f.jormcast = te.getType().getJormName() + "[]";
343: } else {
344: f.jormcast = "Object";
345: }
346:
347: String[] s = getCollectionClass(asmType, scp.personality);
348: f.memoryType = s[0];
349: f.gcproxy = s[1];
350: f.gcJDKImpl = s[2];
351: GenClassRef gcr = (GenClassRef) te;
352: if (gcr.isClassRef()) {
353: f.accessorClassName = NamingRules.fieldsName(gcr
354: .getClassRef().getMOClass().getFQName());
355: }
356: } else {
357: f.jormcast = te.getType().getJormName();
358:
359: f.memoryType = te.getType().getJormName();
360: f.accessorClassName = NamingRules.fieldsName(f.memoryType);
361: }
362:
363: f.linkedField = getLinkedField((Reference) te);
364: //Cascade delete
365: f.isCascadeDelete = sp.isCascadeDelete;
366:
367: fillRelationInfo(f, sp, te, ctx, asmType);
368: }
369:
370: public void fillRelationInfo(Field f, SpeedoField sp,
371: TypedElement te, Map ctx, Type asmType)
372: throws SpeedoException {
373: if (sp.relationType == SpeedoField.NO_BI_RELATION) {
374: return;
375: }
376: SpeedoClass jdoClass = sp.moClass;
377: f.reverse = sp.reverseField;
378: f.reverseGetter = NamingRules.getterName(null, sp.reverseField);
379: f.reverseSetter = NamingRules.setterName(null, sp.reverseField);
380: f.coherentSetter = NamingRules.coherentSetterName(sp.moClass,
381: sp.name);
382: SpeedoClass otherClass = null;
383: switch (sp.relationType) {
384: case SpeedoField.ONE_ONE_BI_RELATION:
385: f.is11Relation = true;
386: otherClass = jdoClass.moPackage.xmlDescriptor
387: .getSpeedoClass(sp.type(), true);
388: break;
389:
390: case SpeedoField.ONE_MANY_BI_RELATION:
391: f.is1MRelation = true;
392: if (sp.jdoTuple instanceof SpeedoCollection) {
393: f.elemType = (String) ((SpeedoCollection) sp.jdoTuple).elementType;
394: } else if (sp.jdoTuple instanceof SpeedoMap) {
395: f.elemType = (String) ((SpeedoMap) sp.jdoTuple).valueType;
396: f.keyField = NamingRules
397: .getterName(
398: null,
399: sp
400: .getExtensionValueByKey(SpeedoProperties.KEY_FIELD))
401: + "()";
402: } else {
403: throw new SpeedoException(
404: "Unmanaged relation with this multivalued field: "
405: + sp.jdoTuple);
406: }
407: if (f.elemType.indexOf('.') == -1) {
408: f.elemType = jdoClass.moPackage.name + '.' + f.elemType;
409: }
410: otherClass = jdoClass.moPackage.xmlDescriptor
411: .getSpeedoClass(f.elemType, true);
412: ctx.put("needSpeedoGenClassListener", new Boolean(true));
413: break;
414:
415: case SpeedoField.MANY_ONE_BI_RELATION:
416: f.isM1Relation = true;
417: // speedoClass corresponding to sp.type()
418: otherClass = jdoClass.moPackage.xmlDescriptor
419: .getSpeedoClass(sp.type(), true);
420: f.reverseField = otherClass.getField(f.reverse);
421: // number of the reverse field
422: f.reverseNumber = f.reverseField.number;
423: if (f.reverseField.jdoTuple instanceof SpeedoMap) {
424: f.reverseIsMap = true;
425: String reverseKeyFieldName = f.reverseField
426: .getExtensionValueByKey(SpeedoProperties.KEY_FIELD);
427: f.reverseKField = jdoClass
428: .getField(reverseKeyFieldName);
429: f.reverseKeyField = NamingRules.getterName(null,
430: reverseKeyFieldName)
431: + "()";
432: }
433: break;
434:
435: case SpeedoField.MANY_MANY_BI_RELATION:
436: f.isMMRelation = true;
437: f.reverseIsMap = sp.jdoTuple instanceof SpeedoMap;
438: f.elemType = (String) ((SpeedoCollection) sp.jdoTuple).elementType;
439: if (f.elemType.indexOf('.') == -1) {
440: f.elemType = jdoClass.moPackage.name + '.' + f.elemType;
441: }
442: // speedoClass corresponding to f.elemType
443: otherClass = jdoClass.moPackage.xmlDescriptor
444: .getSpeedoClass(f.elemType, true);
445: // number of the reverse field
446: ctx.put("needSpeedoGenClassListener", new Boolean(true));
447: break;
448: }
449: f.reverseField = otherClass.getField(f.reverse);
450: f.reverseNumber = f.reverseField.number;
451: }
452:
453: public void fillUserFieldMappingInfo(Field f, SpeedoField sp)
454: throws SpeedoException {
455: SpeedoExtension se = sp
456: .getExtensionByKey(SpeedoProperties.FIELD_CONVERTER);
457: if (se == null) {
458: return;
459: }
460: f.userFieldMapping = se.value;
461: UserFieldMapping ufm = null;
462: try {
463: ufm = (UserFieldMapping) java.lang.Class.forName(se.value)
464: .newInstance();
465: } catch (Exception e) {
466: throw new SpeedoException(
467: "Impossible to instanciate the UserFieldMapping class '"
468: + se.value + "' for the field '" + sp.name
469: + "' of the class '"
470: + sp.moClass.getFQName() + "':", e);
471: }
472: if (ufm.getStorageType().isPrimitive()) {
473: f.toMemory = "new " + getJavaLangType(ufm.getStorageType())
474: + "(" + f.toMemory + ")";
475: }
476:
477: if (ufm.getMemoryType().isPrimitive()) {
478: f.toStorage = "new " + getJavaLangType(ufm.getMemoryType())
479: + "(" + f.toStorage + ")";
480: }
481:
482: f.toMemory = f.name + "UFM.toMemory(" + f.toMemory + ")";
483: f.toStorage = f.name + "UFM.toStorage(" + f.toStorage + ")";
484:
485: if (ufm.getStorageType().isPrimitive()) {
486: f.toStorage = "((" + getJavaLangType(ufm.getStorageType())
487: + ") " + f.toStorage + ")."
488: + getValueGetter(ufm.getStorageType()) + "()";
489: } else {
490: f.toStorage = "(" + ufm.getStorageType().getName() + ") "
491: + f.toStorage;
492: }
493:
494: if (ufm.getMemoryType().isPrimitive()) {
495: f.toMemory = "((" + getJavaLangType(ufm.getMemoryType())
496: + ") " + f.toMemory + ")."
497: + getValueGetter(ufm.getMemoryType()) + "()";
498: } else {
499: f.toMemory = "(" + ufm.getMemoryType().getName() + ") "
500: + f.toMemory;
501: }
502:
503: }
504:
505: private String getLinkedField(Reference r) {
506: String res = null;
507: if (r instanceof GenClassRef) {
508: res = JormPathHelper.getPath((GenClassRef) r, false);
509: } else if (r instanceof ClassRef) {
510: res = JormPathHelper.getPath((ClassRef) r);
511: }
512: return res;
513: }
514:
515: protected String getValueGetter(java.lang.Class c) {
516: return c.getName() + "Value";
517: }
518:
519: public final java.lang.Class[][] PRIMITIVE_TYPES = {
520: { Boolean.TYPE, Boolean.class }, { Byte.TYPE, Byte.class },
521: { Character.TYPE, Character.class },
522: { Short.TYPE, Short.class },
523: { Integer.TYPE, Integer.class }, { Long.TYPE, Long.class },
524: { Float.TYPE, Float.class }, { Double.TYPE, Double.class } };
525:
526: protected String getJavaLangType(java.lang.Class c) {
527: for (int i = 0; i < PRIMITIVE_TYPES.length; i++) {
528: if (c == PRIMITIVE_TYPES[i][0]) {
529: return PRIMITIVE_TYPES[i][1].getName();
530: }
531: }
532: return null;
533: }
534:
535: protected String[] getCollectionClass(Type fieldtype,
536: Personality pers) {
537: String[] res = new String[3];
538: res[0] = fieldtype.getClassName();
539: for (int i = 0; i < GC_IMPL.length && res[1] == null; i++) {
540: if (GC_IMPL[i][0].equals(res[0])) {
541: res[1] = pers.getGenClassName(GC_IMPL[i][1]);
542: res[2] = GC_IMPL[i][2];
543: }
544: }
545: if (res[1] == null) {
546: logger.log(BasicLevel.ERROR, "Type " + res[0]
547: + " is not supported for a persistent field");
548: }
549: return res;
550: }
551:
552: public String getPNameGetter(PType type) {
553: switch (type.getTypeCode()) {
554: case PType.TYPECODE_CHAR:
555: return "pngetCharField";
556: case PType.TYPECODE_OBJCHAR:
557: return "pngetOcharField";
558: case PType.TYPECODE_BYTE:
559: return "pngetByteField";
560: case PType.TYPECODE_OBJBYTE:
561: return "pngetObyteField";
562: case PType.TYPECODE_SHORT:
563: return "pngetShortField";
564: case PType.TYPECODE_OBJSHORT:
565: return "pngetOshortField";
566: case PType.TYPECODE_INT:
567: return "pngetIntField";
568: case PType.TYPECODE_OBJINT:
569: return "pngetOintField";
570: case PType.TYPECODE_LONG:
571: return "pngetLongField";
572: case PType.TYPECODE_OBJLONG:
573: return "pngetOlongField";
574: case PType.TYPECODE_STRING:
575: return "pngetStringField";
576: case PType.TYPECODE_DATE:
577: return "pngetDateField";
578: case PType.TYPECODE_CHARARRAY:
579: return "pngetCharArrayField";
580: case PType.TYPECODE_BYTEARRAY:
581: return "pngetByteArrayField";
582: default:
583: return null;
584: }
585: }
586:
587: protected Mapping getMapping(Class clazz) throws SpeedoException {
588: return getMapping(clazz, scp);
589: }
590:
591: public static Mapping getMapping(Class clazz,
592: SpeedoCompilerParameter scp) throws SpeedoException {
593: ClassProject cp = clazz.getClassProject(scp.projectName);
594: if (cp == null) {
595: throw new SpeedoException(
596: "No classproject found for the class "
597: + clazz.getFQName() + " and the project "
598: + scp.projectName);
599: }
600: int idx = scp.mapperName.indexOf('.');
601: Mapping m = cp.getMapping(idx == -1 ? scp.mapperName
602: : scp.mapperName.substring(0, idx));
603: if (m == null) {
604: throw new SpeedoException("No mapping found for the class "
605: + clazz.getFQName() + ", the project "
606: + scp.projectName + " and the mapper "
607: + scp.mapperName);
608: }
609: return m;
610: }
611:
612: protected NameDef getClassNameDef(Class clazz)
613: throws SpeedoException {
614: return getClassNameDef(clazz, scp);
615: }
616:
617: public static NameDef getClassNameDef(Class clazz,
618: SpeedoCompilerParameter scp) throws SpeedoException {
619: return getMapping(clazz, scp).getClassMapping()
620: .getIdentifierMapping().getNameDef();
621: }
622:
623: protected Map computeUserCaches(SpeedoClass sc) {
624: Map userCaches = new HashMap();
625: computeUserCaches(sc, userCaches, true);
626: for (Iterator iter = userCaches.values().iterator(); iter
627: .hasNext();) {
628: List element = (List) iter.next();
629: Collections.sort(element, new Comparator() {
630: public int compare(Object o1, Object o2) {
631: return ((SpeedoField) o1).name
632: .compareTo(((SpeedoField) o2).name);
633: }
634: });
635: }
636: return userCaches;
637: }
638:
639: protected void computeUserCaches(SpeedoClass sc, Map userCaches,
640: boolean addNew) {
641: for (Iterator it = sc.fields.values().iterator(); it.hasNext();) {
642: SpeedoField sf = (SpeedoField) it.next();
643: String cacheName = sf
644: .getExtensionValueByKey(SpeedoProperties.USER_CACHE);
645: if (cacheName == null) {
646: continue;
647: }
648: List sfs = (List) userCaches.get(cacheName);
649: if (sfs == null) {
650: if (!addNew) {
651: continue;
652: }
653: sfs = new ArrayList();
654: userCaches.put(cacheName, sfs);
655: }
656: sfs.add(sf);
657: }
658: if (sc.getSuperClassName() != null) {
659: computeUserCaches(sc.getSuper(), userCaches, true);
660: }
661: }
662:
663: public class Field {
664: protected String name;
665: public String nameUpperFirst;
666: public long number;
667: public String getter;
668: public String setter;
669: public String coherentSetter;
670: public String reverse;
671: public int reverseNumber;
672: public SpeedoField reverseField;
673: public String reverseGetter;
674: public String reverseSetter;
675: public String modifier;
676: public boolean isClassical;
677: public boolean isContainerId;
678: public boolean isArray;
679: public String declaration;
680: public boolean isKey;
681: public String jormMeth;
682: public int jormFieldIdLongPos;
683: public long jormFieldId;
684: public String jormFielIdDecl;
685: public String jormType;
686: public String linkedField;
687: public String memoryType;
688: public String elemType;
689: public boolean isMultiValued = false;
690: public boolean isReference = false;
691: public boolean is11Relation = false;
692: public boolean is1MRelation = false;
693: public boolean isM1Relation = false;
694: public boolean isMMRelation = false;
695: public boolean isMap;
696: public boolean reverseIsMap;
697: public String reverseKeyField;
698: public SpeedoField reverseKField;
699: public String keyField;
700: public String toMemory = null;
701: public String toStorage = null;
702: public String userFieldMapping = null;
703: public boolean isCascadeDelete = false;
704: public int depth;
705: public String fetchGroup;
706: public boolean defaultFetchGroup;
707:
708: protected String jormfield;
709: protected String jormcast;
710: protected String jormmeth;
711: protected String type;
712: protected boolean isNotauthorizedToBeNull = false;
713: /**
714: * The name of the SpeedoGenClassPO class to use
715: */
716: public String gcproxy;
717: public String gcJDKImpl;
718:
719: public String accessorClassName;
720:
721: /**
722: * The name of the user cache which the index contains this field
723: */
724: public Collection userCacheNames;
725: public String jvmType;
726:
727: public String getNameUpperFirst() {
728: return nameUpperFirst;
729: }
730:
731: public void setNameUpperFirst(String nameUpperFirst) {
732: this .nameUpperFirst = nameUpperFirst;
733: }
734:
735: public boolean getIsReference() {
736: return isReference;
737: }
738:
739: public int getJormFieldIdLongPos() {
740: return jormFieldIdLongPos;
741: }
742:
743: public String getGcproxy() {
744: return gcproxy;
745: }
746:
747: public String getGcJDKImpl() {
748: return gcJDKImpl;
749: }
750:
751: public String getName() {
752: return name;
753: }
754:
755: public long getNumber() {
756: return number;
757: }
758:
759: public String getGetter() {
760: return getter;
761: }
762:
763: public String getSetter() {
764: return setter;
765: }
766:
767: public String getModifier() {
768: return modifier;
769: }
770:
771: public boolean isClassical() {
772: return isClassical;
773: }
774:
775: public boolean isContainerId() {
776: return isContainerId;
777: }
778:
779: public boolean isArray() {
780: return isArray;
781: }
782:
783: public boolean getIsRelation() {
784: return is11Relation || is1MRelation || isM1Relation
785: || isMMRelation;
786: }
787:
788: public boolean getIs11Relation() {
789: return is11Relation;
790: }
791:
792: public boolean getIs1MRelation() {
793: return is1MRelation;
794: }
795:
796: public boolean getIsM1Relation() {
797: return isM1Relation;
798: }
799:
800: public boolean getIsMMRelation() {
801: return isMMRelation;
802: }
803:
804: public boolean getIsxMRelation() {
805: return is1MRelation || isMMRelation;
806: }
807:
808: public boolean getIsMap() {
809: return isMap;
810: }
811:
812: public boolean getReverseIsMap() {
813: return reverseIsMap;
814: }
815:
816: public boolean getDefaultFetchGroup() {
817: return defaultFetchGroup;
818: }
819:
820: public String getReverseKeyField() {
821: return reverseKeyField;
822: }
823:
824: public String getReverse() {
825: return reverse;
826: }
827:
828: public boolean getIsCascadeDelete() {
829: return isCascadeDelete;
830: }
831:
832: public int getReverseNumber() {
833: return reverseNumber;
834: }
835:
836: public String getReverseGetter() {
837: return reverseGetter;
838: }
839:
840: public String getReverseSetter() {
841: return reverseSetter;
842: }
843:
844: public String getCoherentSetter() {
845: return coherentSetter;
846: }
847:
848: public String getDeclaration() {
849: return declaration;
850: }
851:
852: public boolean isKey() {
853: return isKey;
854: }
855:
856: public String getJormMeth() {
857: return jormMeth;
858: }
859:
860: public long getJormFieldId() {
861: return jormFieldId;
862: }
863:
864: public String getJormType() {
865: return jormType;
866: }
867:
868: public String getLinkedField() {
869: return linkedField;
870: }
871:
872: public String getMemoryType() {
873: return memoryType;
874: }
875:
876: public String getElemType() {
877: return elemType;
878: }
879:
880: public boolean getIsMultiValued() {
881: return isMultiValued;
882: }
883:
884: public String getJormFielIdDecl() {
885: return jormFielIdDecl;
886: }
887:
888: public String getToMemory() {
889: return toMemory;
890: }
891:
892: public String getToStorage() {
893: return toStorage;
894: }
895:
896: public String getUserFieldMapping() {
897: return userFieldMapping;
898: }
899:
900: public String getJormfield() {
901: return jormfield;
902: }
903:
904: public String getJormcast() {
905: return jormcast;
906: }
907:
908: public String getJormmeth() {
909: return jormmeth;
910: }
911:
912: public String getType() {
913: return type;
914: }
915:
916: public boolean getIsNotauthorizedToBeNull() {
917: return isNotauthorizedToBeNull;
918: }
919:
920: public String getKeyField() {
921: return keyField;
922: }
923:
924: public int getDepth() {
925: return depth;
926: }
927:
928: public String getFetchGroup() {
929: return fetchGroup;
930: }
931:
932: public String getAccessorClassName() {
933: return accessorClassName;
934: }
935:
936: public Collection getUserCacheNames() {
937: return userCacheNames;
938: }
939: }
940:
941: public class FetchGroup {
942: public String name;
943: public boolean postload;
944:
945: /**
946: * The list of names of the nested fetchgroups
947: */
948: public List nestedFetchGroups;
949:
950: public List fields;
951:
952: public List getFields() {
953: return fields;
954: }
955:
956: public String getName() {
957: return name;
958: }
959:
960: public List getNestedFetchGroups() {
961: return nestedFetchGroups;
962: }
963:
964: public boolean isPostload() {
965: return postload;
966: }
967:
968: }
969:
970: }
|