Package org.objectweb.speedo.genclass.merger.jdo

Source Code of org.objectweb.speedo.genclass.merger.jdo.JDOGenClassMerger$NoArgConstructorAdder

/**
* 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.jdo;

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.jdo.JDOGenClass;
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.speedo.mim.jdo.api.JDOPersistentObjectItf;
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
*/
public class JDOGenClassMerger extends GenClassMerger {
  public final static String JDO_GEN_CLASS_NAME =
    JDOGenClass.class.getName().replace('.', '/');
  public final static String JDO_ITF =
    JDOPersistentObjectItf.class.getName().replace('.', '/');

  public JDOGenClassMerger() {
    super(Personality.JDO);
  }
 
    protected String getLoggerName() {
        return super.getLoggerName() + "." + Personality.JDO.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.JDO.getGenClassName(gcn));
  }
 
    /**
     * The second class is the defined by #EJB_GEN_CLASS_NAME
     */
  protected final String getSecondClass(String gcn) {
        return JDO_GEN_CLASS_NAME;
  }
 
  /**
     * return false if the class already implements JDOPersistentObject
     * interface, otherwise true.
   */
    protected boolean requireEnhancement(GCInfo gc) {
    if (gc.implement(JDO_ITF)) {
            //do nothing
            logger.log(BasicLevel.DEBUG, "The class " + gc.classToWrite + " is complete.");
      return false;
    }
        logger.log(BasicLevel.DEBUG, "The class " + gc.classToWrite + " is NOT a JDO persistent class.");
    return true;
  }

    /**
     * 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.JDO, 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);
        }
    }
}
TOP

Related Classes of org.objectweb.speedo.genclass.merger.jdo.JDOGenClassMerger$NoArgConstructorAdder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.