001: /**
002: * Copyright (C) 2001-2005 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.enhancer.pc;
018:
019: import org.objectweb.asm.ClassVisitor;
020: import org.objectweb.asm.CodeVisitor;
021: import org.objectweb.asm.Constants;
022: import org.objectweb.speedo.api.SpeedoException;
023: import org.objectweb.speedo.genclass.api.SpeedoGenClassPO;
024: import org.objectweb.speedo.generation.api.SpeedoCompilerParameter;
025: import org.objectweb.speedo.generation.enhancer.common.LoggedClassAdapter;
026: import org.objectweb.speedo.generation.enhancer.common.Util;
027: import org.objectweb.speedo.generation.generator.lib.AbstractSpeedoGenerator;
028: import org.objectweb.speedo.generation.generator.lib.AbstractSpeedoGenerator.Field;
029: import org.objectweb.speedo.generation.lib.NamingRules;
030: import org.objectweb.speedo.lib.Personality;
031: import org.objectweb.speedo.metadata.SpeedoClass;
032: import org.objectweb.speedo.mim.api.HomeItf;
033: import org.objectweb.speedo.mim.api.LifeCycle;
034: import org.objectweb.speedo.mim.api.PersistentObjectItf;
035: import org.objectweb.speedo.mim.api.StateItf;
036: import org.objectweb.speedo.pm.api.POManagerFactoryItf;
037: import org.objectweb.speedo.pm.api.POManagerItf;
038: import org.objectweb.util.monolog.api.Logger;
039:
040: import java.util.Map;
041:
042: /**
043: * Defines an abstract modifier of the persistence class (PC).
044: *
045: *
046: * @author chassase
047: */
048: public class AbstractPCModifier extends LoggedClassAdapter implements
049: POVariableNames {
050:
051: public final static String JCN_HOME = getJVMClassName(HomeItf.class);
052: public final static String JT_HOME = getJVMType(HomeItf.class);
053:
054: public final static String JCN_PO = getJVMClassName(PersistentObjectItf.class);
055: public final static String JT_PO = getJVMType(PersistentObjectItf.class);
056:
057: public final static String JCN_STATE = getJVMClassName(StateItf.class);
058: public final static String JT_STATE = getJVMType(StateItf.class);
059:
060: public final static String JCN_POM = getJVMClassName(POManagerItf.class);
061: public final static String JT_POM = getJVMType(POManagerItf.class);
062:
063: public final static String JT_POMF = getJVMType(POManagerFactoryItf.class);
064: public final static String JCN_POMF = getJVMClassName(POManagerFactoryItf.class);
065:
066: public final static String JCN_LIFE_CYCLE = getJVMClassName(LifeCycle.class);
067: public final static String JCN_GCPO = getJVMClassName(SpeedoGenClassPO.class);
068:
069: // Following constant cannot be static because dependent of personality!!
070: public String JCN_JDOPOM;
071: public String JT_JDOPOM;
072: public String JT_JDOPOMF;
073: public String JCN_JDOPOMF;
074:
075: public final static String M_SPEEDO_GET_STATE = "speedoGetState";
076: public final static String M_SPEEDO_CREATE_STATE = "speedoCreateState";
077: public final static String M_SPEEDO_COPY_STATE = "speedoCopyState";
078: public final static String M_SPEEDO_GET_REF_STATE = "speedoGetReferenceState";
079: public final static String M_SPEEDO_SET_REF_STATE = "speedoSetReferenceState";
080: public final static String M_SPEEDO_GET_HOME = "speedoGetHome";
081: public final static String M_SPEEDO_GET_STATUS = "speedoGetStatus";
082:
083: protected String classToWrite;
084: protected SpeedoClass speedoClass;
085: protected final String xfieldsJT;
086: protected final String xfieldsJCN;
087: protected final String xfieldsAncestorJCN;
088: protected final String xfieldsAncestorJT;
089: protected final String ancestorJCN;
090: protected final SpeedoCompilerParameter scp;
091:
092: public AbstractPCModifier(ClassVisitor classVisitor, Logger logger,
093: SpeedoClass sc, SpeedoCompilerParameter cp, Personality p) {
094: super (classVisitor, p, logger);
095: this .speedoClass = sc;
096: this .classToWrite = getJVMClassName(speedoClass.getFQName());
097: this .xfieldsJCN = NamingRules.fieldsName(classToWrite);
098: this .xfieldsJT = getJVMType(xfieldsJCN);
099: this .scp = cp;
100: SpeedoClass anc = sc.getAncestor();
101: if (anc == null) {
102: ancestorJCN = classToWrite;
103: xfieldsAncestorJCN = xfieldsJCN;
104: xfieldsAncestorJT = xfieldsJT;
105: } else {
106: ancestorJCN = getJVMClassName(anc.getFQName());
107: ;
108: xfieldsAncestorJCN = NamingRules.fieldsName(ancestorJCN);
109: xfieldsAncestorJT = getJVMType(xfieldsAncestorJCN);
110: }
111: JCN_JDOPOM = getJVMClassName(personality.getPOMClass());
112: JT_JDOPOM = getJVMType(personality.getPOMClass());
113: JT_JDOPOMF = getJVMType(personality.getPOMFClass());
114: JCN_JDOPOMF = getJVMClassName(personality.getPOMFClass());
115: }
116:
117: protected void generateGetRefState(CodeVisitor mv,
118: boolean needCurrentFields) {
119: mv.visitVarInsn(ALOAD, 0);
120: mv.visitFieldInsn(GETFIELD, classToWrite, REFSTATE_FIELD_NAME,
121: xfieldsAncestorJT);
122: if (needCurrentFields
123: && speedoClass.getSuperClassName() != null) {
124: mv.visitTypeInsn(CHECKCAST, xfieldsJCN);
125: }
126: }
127:
128: protected Map getGenerationContext() {
129: SpeedoGeneratorImpl sgi = new SpeedoGeneratorImpl(personality);
130: sgi.setLogger(logger);
131: sgi.setSpeedoCompilerParameter(scp);
132: try {
133: return sgi.getContextAsMap(speedoClass);
134: } catch (SpeedoException e) {
135: throw personality.newRuntimeException(e.getMessage(), e);
136: }
137: }
138:
139: public static void generateFieldIdAsLongArray(Field f, int nbField,
140: CodeVisitor mv) {
141: final int arraySize = (nbField / 64)
142: + ((nbField % 64) == 0 ? 0 : 1);
143: //array length
144: Util.visitIntConstant(mv, arraySize);
145: //declare the array
146: mv.visitIntInsn(NEWARRAY, Constants.T_LONG);
147: for (int i = 0; i <= (nbField / 64); i++) {
148: mv.visitInsn(Constants.DUP);
149: //index
150: Util.visitIntConstant(mv, i);
151: //value
152: if (f == null) {
153: Util.visitLongConstant(mv, 0xffffffffffffffffL);
154: } else if (i == f.jormFieldIdLongPos) {
155: Util.visitLongConstant(mv, f.jormFieldId);
156: } else {
157: Util.visitLongConstant(mv, 0);
158: }
159: mv.visitInsn(LASTORE);
160: }
161: }
162:
163: }
164:
165: class SpeedoGeneratorImpl extends AbstractSpeedoGenerator {
166:
167: public SpeedoGeneratorImpl(Personality p) {
168: super (p);
169: }
170:
171: public void generate(SpeedoClass sClass, String fileName)
172: throws SpeedoException {
173: }
174:
175: public boolean init() throws SpeedoException {
176: return true;
177: }
178: }
|