/*******************************************************************************
 * Copyright 2017 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 ******************************************************************************/
package com.ibm.javametrics.instrument;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.Method;

/**
 * MethodVisitor providing common servlet instrumentation
 *
 */
public class ServletCallBackAdapter extends BaseAdviceAdapter {

    private static final String SERVLET_CALLBACK_TYPE = "com/ibm/javametrics/instrument/ServletCallback";
    private static final String SERVLET_CALLBACK_BEFORE = "void before(java.lang.Object, java.lang.Object)";
    private static final String SERVLET_CALLBACK_AFTER = "void after(java.lang.Object, java.lang.Object)";

    protected ServletCallBackAdapter(String className, MethodVisitor mv, int access, String name, String desc) {
        super(className, mv, access, name, desc);
        if (Agent.debug) {
            System.err.println("Javametrics: Instrumenting: " + className + "." + name);
        }
    }

    @Override
    protected void onMethodEnter() {
        injectServletCallback(SERVLET_CALLBACK_BEFORE);
    }

    @Override
    protected void onMethodExit(int opcode) {
        injectServletCallback(SERVLET_CALLBACK_AFTER);
    }

    /**
     * Inject a callback to our servlet handler.
     * 
     * @param method
     */
    private void injectServletCallback(String method) {
        loadArgs();
        invokeStatic(Type.getType(SERVLET_CALLBACK_TYPE), Method.getMethod(method));
    }

}

@@ -778,16 +778,48 @@
 ument;%0A%0A
+import org.objectweb.asm.Label;%0A
 import o
@@ -1380,16 +1380,20 @@
 ect)%22;%0A%0A
+   %0A
     prot
@@ -2078,102 +2078,526 @@
     
-loadArgs();%0A        invokeStatic(Type.getType(SERVLET_CALLBACK_TYPE), Method.getMethod(method)
+Label tryStart = new Label();%0A        Label tryEnd = new Label();%0A        Label catchStart = new Label();%0A        Label catchEnd = new Label();%0A     %0A        visitTryCatchBlock(tryStart, tryEnd, catchStart, %22java/lang/NoClassDefFoundError%22);%0A        visitLabel(tryStart);%0A        loadArgs();%0A        invokeStatic(Type.getType(SERVLET_CALLBACK_TYPE), Method.getMethod(method));%0A        visitLabel(tryEnd);%0A        visitJumpInsn(GOTO, catchEnd);%0A        visitLabel(catchStart);%0A        pop();%0A        visitLabel(catchEnd
 );%0A 

Handle class not found error in injected callback