/*
 * Copyright 2014 NAVER 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.navercorp.pinpoint.plugin.spring.beans.interceptor;

import java.lang.reflect.Method;

import com.navercorp.pinpoint.bootstrap.instrument.Instrumentor;
import com.navercorp.pinpoint.bootstrap.instrument.transformer.TransformCallback;
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor1;
import com.navercorp.pinpoint.bootstrap.interceptor.annotation.IgnoreMethod;
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;

/**
 * 
 * @author Jongho Moon <jongho.moon@navercorp.com>
 *
 */
public class CreateBeanInstanceInterceptor extends AbstractSpringBeanCreationInterceptor implements AroundInterceptor1 {
    private final PLogger logger = PLoggerFactory.getLogger(getClass());
    
    public CreateBeanInstanceInterceptor(Instrumentor instrumentor, TransformCallback transformer, TargetBeanFilter filter) {
        super(instrumentor, transformer, filter);
    }

    @IgnoreMethod
    @Override
    public void before(Object target, Object arg0) {

    }

    @Override
    public void after(Object target, Object beanNameObject, Object result, Throwable throwable) {
        try {
            if (result == null) {
                return;
            }
            if (!(beanNameObject instanceof String)) {
                logger.warn("invalid type:{}", beanNameObject);
                return;
            }
            final String beanName = (String) beanNameObject;

            Object bean;
            try {
                Method getter = result.getClass().getMethod("getWrappedInstance"); 
                bean = getter.invoke(result);
            } catch (Exception e) {
                logger.warn("Fail to get create bean instance", e);
                return;
            }
            
            processBean(beanName, bean);
        } catch (Throwable t) {
            logger.warn("Unexpected exception", t);
        }
    }
}

@@ -658,42 +658,8 @@
 r;%0A%0A
-import java.lang.reflect.Method;%0A%0A
 impo
@@ -1076,15 +1076,45 @@
 y;%0A%0A
-/**%0A * 
+import java.lang.reflect.Method;%0A%0A/**
 %0A * 
@@ -1163,16 +1163,35 @@
 .com%3E%0A *
+ @Author Taejin Koo
 %0A */%0Apub
@@ -1381,20 +1381,71 @@
 ass());%0A
+%0A
     
+private volatile Method getWrappedInstanceMethod;%0A
 %0A    pub
@@ -2203,44 +2203,36 @@
  get
-ter = result.getClass().getMethod(%22g
+WrappedInstanceMethod = getG
 etWr
@@ -2248,12 +2248,23 @@
 ance
-%22
+Method(result
 );
- 
 %0A   
@@ -2286,19 +2286,37 @@
 an = get
-ter
+WrappedInstanceMethod
 .invoke(
@@ -2466,28 +2466,16 @@
       %7D%0A
-            
 %0A       
@@ -2608,10 +2608,637 @@
 %7D%0A    %7D%0A
+%0A    private Method getGetWrappedInstanceMethod(Object object) throws NoSuchMethodException %7B%0A        if (getWrappedInstanceMethod != null) %7B%0A            return getWrappedInstanceMethod;%0A        %7D%0A%0A        synchronized (this) %7B%0A            if (getWrappedInstanceMethod != null) %7B%0A                return getWrappedInstanceMethod;%0A            %7D%0A%0A            Method findedMethod = object.getClass().getMethod(%22getWrappedInstance%22);%0A            if (findedMethod != null) %7B%0A                getWrappedInstanceMethod = findedMethod;%0A                return getWrappedInstanceMethod;%0A            %7D%0A        %7D%0A        return null;%0A    %7D%0A%0A
 %7D%0A

Improve spring plugins performacne. #1292