package com.profiler.modifier.method.interceptors;

import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.profiler.common.ServiceType;
import com.profiler.context.Trace;
import com.profiler.context.TraceContext;
import com.profiler.interceptor.ApiIdSupport;
import com.profiler.interceptor.ByteCodeMethodDescriptorSupport;
import com.profiler.interceptor.MethodDescriptor;
import com.profiler.interceptor.StaticAroundInterceptor;
import com.profiler.interceptor.TraceContextSupport;
import com.profiler.util.StringUtils;

/**
 * 
 * @author netspider
 * 
 */
public class MethodInterceptor implements StaticAroundInterceptor, ByteCodeMethodDescriptorSupport, ApiIdSupport, TraceContextSupport {

	private final Logger logger = Logger.getLogger(MethodInterceptor.class.getName());
	private MethodDescriptor descriptor;
	private int apiId;
	private TraceContext traceContext;

	@Override
	public void before(Object target, String className, String methodName, String parameterDescription, Object[] args) {
		if (logger.isLoggable(Level.INFO)) {
			logger.info("before " + StringUtils.toString(target) + " " + className + "." + methodName + parameterDescription + " args:" + Arrays.toString(args));
		}

		Trace trace = TraceContext.getTraceContext().currentTraceObject();
		if (trace == null) {
			return;
		}

		try {
			trace.traceBlockBegin();
		} catch (Exception e) {
			System.out.println("@@@@@@@@@@@@" + e.getMessage());
			throw new RuntimeException(e);
		}
		
		trace.recordRpcName(ServiceType.INTERNAL_METHOD, null, null);
		trace.markBeforeTime();
	}

	@Override
	public void after(Object target, String className, String methodName, String parameterDescription, Object[] args, Object result) {
		if (logger.isLoggable(Level.INFO)) {
			logger.info("after " + StringUtils.toString(target) + " " + className + "." + methodName + parameterDescription + " args:" + Arrays.toString(args));
		}

		Trace trace = TraceContext.getTraceContext().currentTraceObject();
		if (trace == null) {
			return;
		}
        
		trace.recordApi(descriptor);
		trace.recordException(result);
		trace.markAfterTime();
		trace.traceBlockEnd();
	}

	@Override
	public void setMethodDescriptor(MethodDescriptor descriptor) {
		this.descriptor = descriptor;
	}

	@Override
	public void setApiId(int apiId) {
		this.apiId = apiId;
	}

	@Override
	public void setTraceContext(TraceContext traceContext) {
		this.traceContext = traceContext;
	}
}

@@ -1354,17 +1354,8 @@
 %09%7D%0A%0A
-%09%09try %7B%0A%09
 %09%09tr
@@ -1381,131 +1381,8 @@
 ();%0A
-%09%09%7D catch (Exception e) %7B%0A%09%09%09System.out.println(%22@@@@@@@@@@@@%22 + e.getMessage());%0A%09%09%09throw new RuntimeException(e);%0A%09%09%7D%0A%09%09%0A
 %09%09tr

[NOBTS] represent depth of each methods