/**
* Copyright (C) 2001-2005 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.objectweb.speedo.generation.enhancer.pc;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.CodeVisitor;
import org.objectweb.asm.Constants;
import org.objectweb.speedo.api.SpeedoException;
import org.objectweb.speedo.genclass.api.SpeedoGenClassPO;
import org.objectweb.speedo.generation.api.SpeedoCompilerParameter;
import org.objectweb.speedo.generation.enhancer.common.LoggedClassAdapter;
import org.objectweb.speedo.generation.enhancer.common.Util;
import org.objectweb.speedo.generation.generator.lib.AbstractSpeedoGenerator;
import org.objectweb.speedo.generation.generator.lib.AbstractSpeedoGenerator.Field;
import org.objectweb.speedo.generation.lib.NamingRules;
import org.objectweb.speedo.lib.Personality;
import org.objectweb.speedo.metadata.SpeedoClass;
import org.objectweb.speedo.mim.api.HomeItf;
import org.objectweb.speedo.mim.api.LifeCycle;
import org.objectweb.speedo.mim.api.PersistentObjectItf;
import org.objectweb.speedo.mim.api.StateItf;
import org.objectweb.speedo.pm.api.POManagerFactoryItf;
import org.objectweb.speedo.pm.api.POManagerItf;
import org.objectweb.util.monolog.api.Logger;
import java.util.Map;
/**
* Defines an abstract modifier of the persistence class (PC).
*
*
* @author chassase
*/
public class AbstractPCModifier
extends LoggedClassAdapter
implements POVariableNames {
public final static String JCN_HOME = getJVMClassName(HomeItf.class);
public final static String JT_HOME = getJVMType(HomeItf.class);
public final static String JCN_PO = getJVMClassName(PersistentObjectItf.class);
public final static String JT_PO = getJVMType(PersistentObjectItf.class);
public final static String JCN_STATE = getJVMClassName(StateItf.class);
public final static String JT_STATE = getJVMType(StateItf.class);
public final static String JCN_POM = getJVMClassName(POManagerItf.class);
public final static String JT_POM = getJVMType(POManagerItf.class);
public final static String JT_POMF = getJVMType(POManagerFactoryItf.class);
public final static String JCN_POMF = getJVMClassName(POManagerFactoryItf.class);
public final static String JCN_LIFE_CYCLE = getJVMClassName(LifeCycle.class);
public final static String JCN_GCPO = getJVMClassName(SpeedoGenClassPO.class);
// Following constant cannot be static because dependent of personality!!
public String JCN_JDOPOM;
public String JT_JDOPOM;
public String JT_JDOPOMF;
public String JCN_JDOPOMF;
public final static String M_SPEEDO_GET_STATE = "speedoGetState";
public final static String M_SPEEDO_CREATE_STATE = "speedoCreateState";
public final static String M_SPEEDO_COPY_STATE = "speedoCopyState";
public final static String M_SPEEDO_GET_REF_STATE = "speedoGetReferenceState";
public final static String M_SPEEDO_SET_REF_STATE = "speedoSetReferenceState";
public final static String M_SPEEDO_GET_HOME = "speedoGetHome";
public final static String M_SPEEDO_GET_STATUS = "speedoGetStatus";
protected String classToWrite;
protected SpeedoClass speedoClass;
protected final String xfieldsJT;
protected final String xfieldsJCN;
protected final String xfieldsAncestorJCN;
protected final String xfieldsAncestorJT;
protected final String ancestorJCN;
protected final SpeedoCompilerParameter scp;
public AbstractPCModifier(ClassVisitor classVisitor,
Logger logger,
SpeedoClass sc,
SpeedoCompilerParameter cp,
Personality p) {
super(classVisitor, p, logger);
this.speedoClass = sc;
this.classToWrite = getJVMClassName(speedoClass.getFQName());
this.xfieldsJCN = NamingRules.fieldsName(classToWrite);
this.xfieldsJT = getJVMType(xfieldsJCN);
this.scp = cp;
SpeedoClass anc = sc.getAncestor();
if (anc == null) {
ancestorJCN = classToWrite;
xfieldsAncestorJCN = xfieldsJCN;
xfieldsAncestorJT = xfieldsJT;
} else {
ancestorJCN = getJVMClassName(anc.getFQName());;
xfieldsAncestorJCN = NamingRules.fieldsName(ancestorJCN);
xfieldsAncestorJT = getJVMType(xfieldsAncestorJCN);
}
JCN_JDOPOM = getJVMClassName(personality.getPOMClass());
JT_JDOPOM = getJVMType(personality.getPOMClass());
JT_JDOPOMF = getJVMType(personality.getPOMFClass());
JCN_JDOPOMF = getJVMClassName(personality.getPOMFClass());
}
protected void generateGetRefState(CodeVisitor mv, boolean needCurrentFields) {
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, classToWrite, REFSTATE_FIELD_NAME, xfieldsAncestorJT);
if (needCurrentFields && speedoClass.getSuperClassName() != null) {
mv.visitTypeInsn(CHECKCAST, xfieldsJCN);
}
}
protected Map getGenerationContext() {
SpeedoGeneratorImpl sgi = new SpeedoGeneratorImpl(personality);
sgi.setLogger(logger);
sgi.setSpeedoCompilerParameter(scp);
try {
return sgi.getContextAsMap(speedoClass);
} catch (SpeedoException e) {
throw personality.newRuntimeException(e.getMessage(), e);
}
}
public static void generateFieldIdAsLongArray(Field f, int nbField, CodeVisitor mv) {
final int arraySize = (nbField / 64) + ((nbField % 64) == 0 ? 0 : 1);
//array length
Util.visitIntConstant(mv, arraySize);
//declare the array
mv.visitIntInsn(NEWARRAY, Constants.T_LONG);
for (int i = 0; i <= (nbField / 64); i++) {
mv.visitInsn(Constants.DUP);
//index
Util.visitIntConstant(mv, i);
//value
if (f == null) {
Util.visitLongConstant(mv, 0xffffffffffffffffL);
} else if (i == f.jormFieldIdLongPos) {
Util.visitLongConstant(mv, f.jormFieldId);
} else {
Util.visitLongConstant(mv, 0);
}
mv.visitInsn(LASTORE);
}
}
}
class SpeedoGeneratorImpl extends AbstractSpeedoGenerator {
public SpeedoGeneratorImpl(Personality p) {
super(p);
}
public void generate(SpeedoClass sClass, String fileName) throws SpeedoException {
}
public boolean init() throws SpeedoException {
return true;
}
}