Package net.sf.just4log.transform

Source Code of net.sf.just4log.transform.LogJDK14Transform

/*
* ============================================================================
*                   The Apache Software License, Version 1.1
* ============================================================================
*
*    Copyright (C) 2000-2003 Lucas Bruand. All
*    rights reserved.
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
* 1. Redistributions of  source code must  retain the above copyright  notice,
*    this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
*
* 3. The end-user documentation included with the redistribution, if any, must
*    include  the following  acknowledgment:  "This product includes  software
*    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
*    Alternately, this  acknowledgment may  appear in the software itself,  if
*    and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Just4Log" and  "Apache Software Foundation"  must not be used to
*    endorse  or promote  products derived  from this  software without  prior
*    written permission. For written permission, please contact
*    apache@apache.org.
*
* 5. Products  derived from this software may not  be called "Apache", nor may
*    "Apache" appear  in their name,  without prior written permission  of the
*    Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
* APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
* DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
* ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
* (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software  consists of voluntary contributions made  by many individuals
* on behalf of the  Apache Software Foundation.  For more  information  on the
* Apache Software Foundation, please see <http://www.apache.org/>.
*
*/
package net.sf.just4log.transform;

import java.util.HashMap;

import org.apache.bcel.Constants;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.IFEQ;
import org.apache.bcel.generic.InstructionHandle;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.InvokeInstruction;
import org.apache.bcel.generic.MethodGen;
import org.apache.bcel.generic.ObjectType;
import org.apache.bcel.generic.PUSH;
import org.apache.bcel.generic.Type;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* @author Lucas Bruand
*/

public class LogJDK14Transform extends Transform {

  private static final String INTERFACE = "java.util.logging.Logger";
  /* (non-Javadoc)
     * @see net.sf.just4log.transform.Transform#getLogType()
     */
  private static final ObjectType logType = new ObjectType(INTERFACE);
  public ObjectType getLogType() {
    return logType;
  }
  private static final String LEVEL = "java.util.logging.Level";

  private static final ObjectType LevelType = new ObjectType(LEVEL);
  private static final ObjectType[] ARGUMENTS_ISENABLEDFOR =
    new ObjectType[] { new ObjectType(LEVEL)};

  private static Log logger = LogFactory.getLog(LogJDK14Transform.class);
  private static String CLASSID =
    "$Id: LogJDK14Transform.java,v 1.6 2003/07/23 21:19:12 lbruand Exp $";

  public static void register() {
    Transform.register(new LogJDK14Transform());
  }

  /**
   *
   */
  public LogJDK14Transform() {
    super();
  }

  /* (non-Javadoc)
   * @see net.sf.just4log.transform.Transform#insertFork(org.apache.bcel.generic.InstructionList, org.apache.bcel.generic.InstructionHandle, org.apache.bcel.generic.InstructionHandle, org.apache.bcel.generic.ConstantPoolGen)
   */
  public InstructionHandle insertFork(
    InstructionList il,
    InstructionHandle getStaticHandle,
    InstructionHandle invokeHandle,
    ConstantPoolGen cp) {
    String level =
      getIsEnabled(
        ((InvokeInstruction) invokeHandle.getInstruction()),
        cp);
    if (level == null) {
      // this is not a correct methodname.
      return null;
    }
    logger.info("the level of enabling is: " + level);
    logger.info("Inserting GETSTATIC");
    InstructionHandle insertHandle =
      il.insert(getStaticHandle, getStaticHandle.getInstruction().copy());
    il.insert(
      getStaticHandle,
      instFact.createGetStatic(LEVEL, level, LevelType));

    logger.info("Inserting INVOKE call to isLoggable");

    il.insert(
      getStaticHandle,
      instFact.createInvoke(
        INTERFACE,
        "isLoggable",
        Type.BOOLEAN,
        ARGUMENTS_ISENABLEDFOR,
        Constants.INVOKEVIRTUAL));
    logger.info("Inserting IFEQ");
    il.insert(getStaticHandle, new IFEQ(invokeHandle.getNext()));
    return insertHandle;
  }
  private static HashMap mapping = new HashMap();
  static {
    mapping.put("finest", "FINEST");
    mapping.put("finer", "FINER");
    mapping.put("fine", "FINE");
    mapping.put("config", "CONFIG");
    mapping.put("info", "INFO");
    mapping.put("warning", "WARNING");
    mapping.put("severe", "SEVERE");
    mapping.put("entering", "FINER");
    mapping.put("exiting", "FINER");
    mapping.put("throwing", "FINER");
  }
  public String getIsEnabled(
    InvokeInstruction invokeInstruction,
    ConstantPoolGen cp) {
    return (String) mapping.get(invokeInstruction.getMethodName(cp));
  }

  public String getClassname() {
    return INTERFACE;
  }
  /* (non-Javadoc)
   * @see net.sf.just4log.transform.Transform#insertEnter(org.apache.bcel.generic.InstructionList, org.apache.bcel.generic.InstructionHandle, org.apache.bcel.generic.ConstantPoolGen)
   */
  public InstructionHandle insertEnter(
    MethodGen orig,
    InstructionList il,
    InstructionHandle firstInstructionHandle,
    ConstantPoolGen cp) {
      logger.info("Inserting Enter code.");
      InstructionHandle backup = il.insert(
        firstInstructionHandle,
        instFact.createGetStatic(
          clazz.getClassName(),
          loggerAttribute.getName(),
          loggerAttribute.getType()));
       
     
      il.insert(firstInstructionHandle,
        new PUSH(cp, clazz.getClassName())
      );
      il.insert(firstInstructionHandle,
        new PUSH(cp, getMethodRepr(orig))
      );
     
   
      il.insert(
        firstInstructionHandle,
            instFact.createInvoke(
              INTERFACE,
              "entering",
              Type.VOID,
              new Type[] {Type.STRING, Type.STRING},
              Constants.INVOKEVIRTUAL));

      return backup;
  }

  /* (non-Javadoc)
   * @see net.sf.just4log.transform.Transform#insertExit(org.apache.bcel.generic.InstructionList, org.apache.bcel.generic.InstructionHandle, org.apache.bcel.generic.ConstantPoolGen)
   */
  public InstructionHandle insertExit(
    MethodGen orig,
    InstructionList il,
    InstructionHandle returnInstructionHandle,
    ConstantPoolGen cp) {
      logger.info("Inserting Exit code.");
      InstructionHandle backup = il.insert(
      returnInstructionHandle,
        instFact.createGetStatic(
          clazz.getClassName(),
          loggerAttribute.getName(),
          loggerAttribute.getType()));
       
     
     
      il.insert(returnInstructionHandle,
        new PUSH(cp, clazz.getClassName())
      );
      il.insert(returnInstructionHandle,
        new PUSH(cp, getMethodRepr(orig))
      );
   
      il.insert(
      returnInstructionHandle,
            instFact.createInvoke(
              INTERFACE,
              "exiting",
              Type.VOID,
              new Type[] {Type.STRING, Type.STRING},
              Constants.INVOKEVIRTUAL));

      return backup;
  }

}
TOP

Related Classes of net.sf.just4log.transform.LogJDK14Transform

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.