Package org.objectweb.speedo.generation.enhancer.pc

Source Code of org.objectweb.speedo.generation.enhancer.pc.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.generation.enhancer.pc;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.CodeVisitor;
import org.objectweb.asm.Constants;
import org.objectweb.asm.Attribute;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.speedo.generation.enhancer.common.LoggedClassAdapter;
import org.objectweb.speedo.lib.Personality;
import org.objectweb.speedo.metadata.SpeedoClass;

/**
* It generates a no-arg constructor or changes the modifier of the no-arg
* constructor if required on persistent classes.
*
* @author S.Chassande-Barrioz
*/
public class NoArgConstructorAdder extends LoggedClassAdapter {
  /**
   * The status with regards to the no-arg constructor
   * possible value:
   * - SpeedoClass.NO_NO_ARG_CONSTRUCTOR
   * - SpeedoClass.NON_PUBLIC_NO_ARG_CONSTRUCTOR
   * - SpeedoClass.PUBLIC_NO_ARG_CONSTRUCTOR
   */
  byte status = 0;

  public NoArgConstructorAdder(ClassVisitor classVisitor, byte status, Personality p) {
    super(classVisitor, p);
    this.status = status;
  }

  public NoArgConstructorAdder(ClassVisitor classVisitor, Logger logger, byte status, Personality p) {
    super(classVisitor, p, logger);
    this.status = status;
  }

  public void visit(final int version, final int access,
            final String name,
            final String superName,
            final String[] interfaces,
            final String sourceFile) {
    cv.visit(version, access, name, superName, interfaces, sourceFile);
    //Generate a no-arg constructor if required
    if (status == SpeedoClass.NO_NO_ARG_CONSTRUCTOR) {
      CodeVisitor _cv = this.cv.visitMethod(
        Constants.ACC_PUBLIC, "<init>", "()V", null, null);
      _cv.visitVarInsn(Constants.ALOAD, 0);
      _cv.visitMethodInsn(Constants.INVOKESPECIAL, superName, "<init>", "()V");
      _cv.visitInsn(Constants.RETURN);
      _cv.visitMaxs(1, 1);
    }
  }

  public CodeVisitor visitMethod(final int access,
                   final String name,
                   final String desc,
                   final String[] exceptions,
                   final Attribute attrs) {
    int newaccess = access;
    //Change the modifier of the no-arg constructor if required
    if (status == SpeedoClass.NON_PUBLIC_NO_ARG_CONSTRUCTOR
      && name.equals("<init>")
      && desc.equals("()V")) {
      newaccess = Constants.ACC_PUBLIC;
    }
    return super.visitMethod(newaccess, name, desc, exceptions, attrs);
  }
}
TOP

Related Classes of org.objectweb.speedo.generation.enhancer.pc.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.