/**
* 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.genclass.merger.ejb;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.CodeVisitor;
import org.objectweb.asm.Constants;
import org.objectweb.jorm.util.lib.StringReplace;
import org.objectweb.speedo.api.SpeedoException;
import org.objectweb.speedo.genclass.ejb.EJBGenClass;
import org.objectweb.speedo.genclass.merger.GCInfo;
import org.objectweb.speedo.genclass.merger.GenClassMerger;
import org.objectweb.speedo.generation.enhancer.common.LoggedClassAdapter;
import org.objectweb.speedo.lib.Personality;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;
import java.io.File;
/**
* Generate the sub class of the generic class dedicated to JDO.
*
* @author S.Chassande-Barrioz, P. Dechamboux
*/
public class EJBGenClassMerger extends GenClassMerger {
public final static String EJB_GEN_CLASS_NAME =
EJBGenClass.class.getName().replace('.', '/');
public EJBGenClassMerger() {
super(Personality.EJB);
}
protected String getLoggerName() {
return super.getLoggerName() + "." + Personality.EJB.getName();
}
/**
* The class to write is into the sub package 'jdo' and the name is prefixed
* by JDO.
*/
protected String getClassToWrite(String gcn) {
return StringReplace.replaceChar('\\', '/',
Personality.EJB.getGenClassName(gcn));
}
/**
* The second class is the defined by #EJB_GEN_CLASS_NAME
*/
protected final String getSecondClass(String gcn) {
return EJB_GEN_CLASS_NAME;
}
/**
* return false if the class already implement a mandatory interface
* for persistent object, else true.
*/
protected boolean requireEnhancement(GCInfo gc) {
return false;
}
/**
* There is no first class, because the class is created.
*/
protected void writeFirstClass(final GCInfo gc, ClassVisitor current)
throws SpeedoException {
//create the sub class for JDO
gc.setSuperName(StringReplace.replaceChar(File.separatorChar, '/',
gc.gcn.substring(0, gc.gcn.length()-6)));
}
/**
* Add a no arg constructor in addition to the normal behavior
*/
protected void writeSecondClass(final GCInfo gc, ClassVisitor current)
throws SpeedoException {
super.writeSecondClass(gc,
new NoArgConstructorAdder(current, gc, logger));
}
/**
* ASM class adapter for adding no arg constructor
*
* @author S.Chassande-Barrioz
*/
private static class NoArgConstructorAdder extends LoggedClassAdapter {
GCInfo gc;
public NoArgConstructorAdder(ClassVisitor current, GCInfo gc, Logger l) {
super(current, Personality.EJB, l);
this.gc = gc;
}
public void visit(final int version,
final int access,
final String name,
final String supername,
final String[] interfaces,
final String sourceFile) {
super.cv.visit(version, access, name, supername, interfaces, sourceFile);
CodeVisitor mv = cv.visitMethod(Constants.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitVarInsn(Constants.ALOAD, 0);
mv.visitMethodInsn(Constants.INVOKESPECIAL, gc.getSuperName(), "<init>", "()V");
mv.visitInsn(Constants.RETURN);
mv.visitMaxs(1, 1);
}
}
}