/*
* ============================================================================
* 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 org.apache.bcel.Constants;
import org.apache.bcel.generic.ConstantPoolGen;
import org.apache.bcel.generic.IFEQ;
import org.apache.bcel.generic.INVOKEINTERFACE;
import org.apache.bcel.generic.InstructionHandle;
import org.apache.bcel.generic.InstructionList;
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 ApacheCommonTransform extends Transform {
private static final String INTERFACE = "org.apache.commons.logging.Log";
/* (non-Javadoc)
* @see net.sf.just4log.transform.Transform#getLogType()
*/
private static final ObjectType logType = new ObjectType(INTERFACE);
public ObjectType getLogType() {
return logType;
}
private static Log logger = LogFactory.getLog(ApacheCommonTransform.class);
private static String CLASSID =
"$Id: ApacheCommonTransform.java,v 1.6 2003/07/22 23:38:37 lbruand Exp $";
public static void register() {
Transform.register(new ApacheCommonTransform());
}
public String getClassname() {
return INTERFACE;
}
/**
*
*/
private ApacheCommonTransform() {
super();
}
public InstructionHandle insertFork(
InstructionList il,
InstructionHandle getStaticHandle,
InstructionHandle invokeInterfaceHandle,
ConstantPoolGen cp) {
logger.info("Inserting GETSTATIC");
InstructionHandle insertHandle =
il.insert(getStaticHandle, getStaticHandle.getInstruction().copy());
String isEnabled =
getIsEnabled(
(
(INVOKEINTERFACE) invokeInterfaceHandle
.getInstruction())
.getMethodName(
cp));
logger.info("Inserting INVOKEINTERFACE call to " + isEnabled);
il.insert(
getStaticHandle,
instFact.createInvoke(
INTERFACE,
isEnabled,
Type.BOOLEAN,
Type.NO_ARGS,
Constants.INVOKEINTERFACE));
logger.info("Inserting IFEQ");
il.insert(getStaticHandle, new IFEQ(invokeInterfaceHandle.getNext()));
return insertHandle;
}
/**
* Obtained the equivalent isXXXXEnabled method to the logmethod provided.
* @param originalMethod the methodname. (e.g. "debug", "info", "warn", "error", "fatal")
* @return the equivalent isXXXXEnabled method
*/
private static final String getIsEnabled(String originalMethod) {
return "is"
+ originalMethod.substring(0, 1).toUpperCase()
+ originalMethod.substring(1)
+ "Enabled";
}
/* (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, Transform.ENTER_STRING+ getMethodRepr(orig))
);
il.insert(
firstInstructionHandle,
instFact.createInvoke(
INTERFACE,
"trace",
Type.VOID,
new Type[] {Type.OBJECT},
Constants.INVOKEINTERFACE));
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, Transform.EXIT_STRING+ getMethodRepr(orig))
);
il.insert(
returnInstructionHandle,
instFact.createInvoke(
INTERFACE,
"trace",
Type.VOID,
new Type[] {Type.OBJECT},
Constants.INVOKEINTERFACE));
return backup;
}
}